branch: externals/kind-icon commit 3b0c9c317e64a6ac71510b78f9b2790e57478eea Author: JD Smith <93749+jdtsm...@users.noreply.github.com> Commit: JD Smith <93749+jdtsm...@users.noreply.github.com>
Renamed kind-icon --- README.md | 18 +++---- kind-prefix.el => kind-icon.el | 118 ++++++++++++++++++++--------------------- 2 files changed, 67 insertions(+), 69 deletions(-) diff --git a/README.md b/README.md index 249303c..07447f3 100644 --- a/README.md +++ b/README.md @@ -1,29 +1,29 @@ -# kind-prefix -_Kind_ icon or short-text prefix badge labels for emacs in-region completion. +# kind-icon +Supplies a _kind_ icon or short-text prefix for emacs completion in the buffer. This emacs package adds icon or text-based completion prefixes based on the `:company-kind` property many completion backends (such as lsp-mode) provide. It works by creating a custom `affixation-function` for in-buffer completion, if the backend does not already provide one. An affixation function specifies a prefix and suffix to go along with completion candidate text. ## Installation -Get it from melpa (TBD). Enable in any buffer with completion enabled using `kind-prefix-mode`. E.g., to enable for the completion UI [corfu](https://github.com/minad/corfu): +Get it from melpa (TBD). Enable in any buffer with completion enabled using `kind-icon-mode`. E.g., to enable for the completion UI [corfu](https://github.com/minad/corfu): ```elisp -(use-package kind-prefix ;package availability TBD +(use-package kind-icon ;package availability TBD :ensure t - :hook (corfu-mode . kind-prefix-mode)) + :hook (corfu-mode . kind-icon-mode)) ``` ## Configuration Defaults should normally work fine, but some of the important configuration variables include: -- `kind-prefix-use-icons`: If non-nil (the default), prefer icons for prefix badges. +- `kind-icon-use-icons`: If non-nil (the default), prefer icons for prefix badges. -- `kind-prefix-mapping`: This is the top level configuration mapping `:company-kind` "types" like `'variable` and `'function`. Each item in this list has the format `(sym short-text :keyword value ...)` where `sym` is the kind (a symbol), and `short-text` is the abbreviated text to display (if icons are not used). The rest of the list is a property list with optional keys `:icon` and `:face`. The latter will be used to set the text foreground and background colors on the badge. The f [...] +- `kind-icon-mapping`: This is the top level configuration mapping `:company-kind` "types" like `'variable` and `'function`. Each item in this list has the format `(sym short-text :keyword value ...)` where `sym` is the kind (a symbol), and `short-text` is the abbreviated text to display (if icons are not used). The rest of the list is a property list with optional keys `:icon` and `:face`. The latter will be used to set the text foreground and background colors on the badge. The for [...] -- `kind-prefix-default-face`: A face from which background color will be taken and blended with the `:face` foreground color in the mapping table to create a custom background color. If not set, the frame default background color will be used for this purpose. Similarly, the foreground color for this face, if set, will be used if a `:face` foreground is missing from the mapping. +- `kind-icon-default-face`: A face from which background color will be taken and blended with the `:face` foreground color in the mapping table to create a custom background color. If not set, the frame default background color will be used for this purpose. Similarly, the foreground color for this face, if set, will be used if a `:face` foreground is missing from the mapping. -- `kind-prefix-blend-frac`: The fractional blend between custom badge +- `kind-icon-blend-frac`: The fractional blend between custom badge `:face` foreground and background (see above) color to use as a custom background for each badge. A value of 0.0 simply replicates the background color. Values should likely stay below 0.3 or so. diff --git a/kind-prefix.el b/kind-icon.el similarity index 70% rename from kind-prefix.el rename to kind-icon.el index 0c44a32..ba7d6c8 100644 --- a/kind-prefix.el +++ b/kind-icon.el @@ -1,17 +1,17 @@ -;;; kind-prefix.el --- Completion kind prefixes -*- lexical-binding: t -*- +;;; kind-icon.el --- Completion kind icons -*- lexical-binding: t -*- ;; Copyright (C) 2021 J.D. Smith ;; Author: J.D. Smith -;; Homepage: https://github.com/jdtsmith/kind-prefix +;; Homepage: https://github.com/jdtsmith/kind-icon ;; Package-Requires: ((emacs "27.1")) ;; Package-Version: 0.0.1 ;; Keywords: completion ;;; Commentary: -;; kind-prefix-mode adds a prefix badge based on :company-kind for -;; compatible completion UI's utilizing completion-in-region. The +;; kind-icon-mode adds an icon or text prefix based on :company-kind +;; for compatible completion UI's utilizing completion-in-region. The ;; "kind" prefix is typically used for differentiating variables, ;; functions, etc. among completion results. It works by creating and ;; setting into `completion-extra-properties' a custom @@ -19,9 +19,9 @@ ;; short-text or icon-based "badge" representing the kind of the ;; candidate. Icons are by default loaded from the "material" library ;; provided by svg-lib, which is required (unless only short-text -;; badges are desired, see `kind-prefix-use-icons'). +;; badges are desired, see `kind-icon-use-icons'). -;; kind-prefix is free software: you can redistribute it +;; kind-icon is free software: you can redistribute it ;; and/or modify it under the terms of the GNU General Public License ;; as published by the Free Software Foundation, either version 3 of ;; the License, or (at your option) any later version. @@ -36,19 +36,19 @@ ;;; Code: -(defgroup kind-prefix nil +(defgroup kind-icon nil "Completion prefixes from :company-kind." :group 'convenience - :prefix "kind-prefix") + :prefix "kind-icon") -(defcustom kind-prefix-use-icons t +(defcustom kind-icon-use-icons t "Whether to use icons for prefix display." :type 'boolean) (unless (require 'svg-lib nil 'noerror) - (setq kind-prefix-use-icons nil)) + (setq kind-icon-use-icons nil)) -(defcustom kind-prefix-mapping ;; adapted from company +(defcustom kind-icon-mapping ;; adapted from company '((array "a" :icon "code-brackets" :face font-lock-type-face) (boolean "b" :icon "circle-half-full" :face font-lock-builtin-face) (class "c" :icon "view-grid-plus-outline" :face font-lock-type-face) @@ -84,23 +84,23 @@ The format should be an alist of type: This information is used to build a prefix for kind KIND. A prefix is a propertized string of either the-short TEXT or ICON (from LIBRARY; see `svg-icon'), depending on the value of -variable `kind-prefix-use-icons' . FACE-OR-COLOR can either be a +variable `kind-icon-use-icons' . FACE-OR-COLOR can either be a color string or a face from which we the :foreground face-property is taken. The background is automatically computed to lie between the background color and foreground (see -`kind-prefix-blend-frac')." +`kind-icon-blend-frac')." :type 'list) -(defcustom kind-prefix-blend-frac 0.12 +(defcustom kind-icon-blend-frac 0.12 "Fractional blend between foreground and background colors. This is used for the background for the short-text kind prefixes." :type 'float) -(defcustom kind-prefix-default-face nil +(defcustom kind-icon-default-face nil "The default face to use for coloring. Normally foreground colors are supplied by the face matching in -`kind-prefix-mapping', but if no face is supplied in the mapping, +`kind-icon-mapping', but if no face is supplied in the mapping, the foreground color is taken from the foreground of this face, or (if nil) to the default frame foreground color. The background color for blending the foreground into the background is also @@ -108,15 +108,13 @@ taken from this face, if provided, defaulting to the frame background color." :type 'face) -(defcustom kind-prefix-icon-style +(defcustom kind-icon-default-style '(:padding 0 :stroke 0 :margin 0 :radius 0 :height 1.0 :scale 1.0) "Default style parameters for building SVG icons. See `svg-lib-style-compute-default'." :type 'plist) -;;(defvar kind-prefix--cached nil) - -(defsubst kind-prefix--rgb-blend (rgb1 rgb2 frac) +(defsubst kind-icon--rgb-blend (rgb1 rgb2 frac) "Return a fractional blend between two colors RGB1 and RGB2. Each is a 3 element list. The fractional blend point is the float FRAC." @@ -125,61 +123,61 @@ float FRAC." (+ (* a frac) (* b (- 1.0 frac)))) rgb1 rgb2))) -(defconst kind-prefix--unknown +(defconst kind-icon--unknown (propertize "??" 'face '(:weight bold :foreground "Red"))) -(defsubst kind-prefix--metdata-get (metadata type-name) +(defsubst kind-icon--metdata-get (metadata type-name) (or (cdr (assq (intern type-name) metadata)) (plist-get completion-extra-properties (intern (format ":%s" type-name))))) -(defun kind-prefix-badge (kind) +(defun kind-icon-badge (kind) "Return a kind badge, either an SVG icon or short-text abbreviation. -Caches as :display-icon in `kind-prefix-mapping', and returns the +Caches as :display-icon in `kind-icon-mapping', and returns the cached value, if set. For the background color, computes a blend between a nominal background color (from either the frame background color, or the :background property -`kind-prefix-default-face', if set). See -`kind-prefix-blend-frac'. For the foreground color, uses the -:face mapping's :foreground color, the `kind-prefix-default-face' +`kind-icon-default-face', if set). See +`kind-icon-blend-frac'. For the foreground color, uses the +:face mapping's :foreground color, the `kind-icon-default-face' foreground, or the default frame foreground, in that order of priority." - (when-let ((map (assq kind kind-prefix-mapping)) + (when-let ((map (assq kind kind-icon-mapping)) (plist (cddr map))) (or (plist-get plist :display-icon) (let* ((bg-rgb (color-name-to-rgb - (if kind-prefix-default-face - (face-attribute kind-prefix-default-face :background nil t) + (if kind-icon-default-face + (face-attribute kind-icon-default-face :background nil t) (frame-parameter nil 'background-color)))) (col-face (plist-get plist :face)) (col (if col-face (face-attribute col-face :foreground) - (if kind-prefix-default-face - (face-attribute kind-prefix-default-face :foreground nil t) + (if kind-icon-default-face + (face-attribute kind-icon-default-face :foreground nil t) (frame-parameter nil 'foreground-color)))) - (bg-col (kind-prefix--rgb-blend + (bg-col (kind-icon--rgb-blend (color-name-to-rgb col) bg-rgb - kind-prefix-blend-frac)) - (disp (if-let ((kind-prefix-use-icons) + kind-icon-blend-frac)) + (disp (if-let ((kind-icon-use-icons) (icon (plist-get plist :icon))) (propertize "**" 'face `(:background ,bg-col) 'display (apply #'svg-lib-icon icon nil :foreground col :background bg-col - kind-prefix-icon-style)) + kind-icon-default-style)) (propertize (cadr map) 'face `(:weight bold :foreground ,col :background ,bg-col))))) (plist-put plist :display-icon disp) disp)))) -(defun kind-prefix-reset-cache () - "Remove all cached icons from `kind-prefix-mapping'." +(defun kind-icon-reset-cache () + "Remove all cached icons from `kind-icon-mapping'." (interactive) - (cl-loop for item in kind-prefix-mapping + (cl-loop for item in kind-icon-mapping do (plist-put (cddr item) :display-icon nil))) -(defun kind-prefix--affixation-function (kind-func &optional ann-func) - "Create and return a custom kind-prefix affixation function. +(defun kind-icon--affixation-function (kind-func &optional ann-func) + "Create and return a custom kind-icon affixation function. The company-kind function should be passed in as KIND-FUNC and any annotation-function as ANN-FUNC. The returned function supplies a candiate kind badge -- abbreviated text key or icon -- @@ -192,41 +190,41 @@ and its result used as the affixation suffix, first setting the (add-face-text-property 0 (length suffix) 'completions-annotations 'append suffix) (if-let ((kind (funcall kind-func cand)) - (badge (kind-prefix-badge kind))) + (badge (kind-icon-badge kind))) (list cand badge suffix) - (list cand kind-prefix--unknown suffix)))) + (list cand kind-icon--unknown suffix)))) candidates))) -(defvar-local kind-prefix--orig-completion-function nil +(defvar-local kind-icon--orig-completion-function nil "The prior completion-in-region-function we are wrapping.") -(defun kind-prefix--completion-in-region-function (start end table &optional pred) - "Set a custom affixation function for kind-prefix. +(defun kind-icon--completion-in-region-function (start end table &optional pred) + "Set a custom affixation function for kind-icon. Only operates if no affixation function is already set." (let* ((str (buffer-substring start (point))) (metadata (completion-metadata str table pred)) - (kind-func (kind-prefix--metdata-get metadata "company-kind")) - (ann-func (kind-prefix--metdata-get metadata "annotation-function")) - (aff-func (kind-prefix--metdata-get metadata "affixation-function"))) + (kind-func (kind-icon--metdata-get metadata "company-kind")) + (ann-func (kind-icon--metdata-get metadata "annotation-function")) + (aff-func (kind-icon--metdata-get metadata "affixation-function"))) (if (and kind-func (not aff-func)) ;; add a custom affixation function (setq completion-extra-properties (plist-put completion-extra-properties :affixation-function - (kind-prefix--affixation-function kind-func ann-func))))) - (funcall kind-prefix--orig-completion-function start end table pred)) + (kind-icon--affixation-function kind-func ann-func))))) + (funcall kind-icon--orig-completion-function start end table pred)) -(define-minor-mode kind-prefix-mode +(define-minor-mode kind-icon-mode "Minor mode enabling kind prefix by wrapping the completion-in-region-function." :init-value nil (if completion-in-region-function - (if kind-prefix-mode + (if kind-icon-mode (progn - (kind-prefix-reset-cache) + (kind-icon-reset-cache) (setq-local - kind-prefix--orig-completion-function completion-in-region-function - completion-in-region-function #'kind-prefix--completion-in-region-function)) + kind-icon--orig-completion-function completion-in-region-function + completion-in-region-function #'kind-icon--completion-in-region-function)) (setq-local - completion-in-region-function kind-prefix--orig-completion-function - kind-prefix--orig-completion-function nil)) - (error "Cannot enable kind-prefix: no completion-in-region-function found"))) + completion-in-region-function kind-icon--orig-completion-function + kind-icon--orig-completion-function nil)) + (error "Cannot enable kind-icon: no completion-in-region-function found"))) -(provide 'kind-prefix) +(provide 'kind-icon)