Skip to content

Commit

Permalink
feat: use nerd-icons-corfu instead of kind-icon.
Browse files Browse the repository at this point in the history
  • Loading branch information
seagle0128 committed Oct 16, 2023
1 parent 186f5e2 commit 87ae312
Showing 1 changed file with 2 additions and 69 deletions.
71 changes: 2 additions & 69 deletions lisp/init-corfu.el
Original file line number Diff line number Diff line change
Expand Up @@ -47,76 +47,9 @@
(use-package corfu-terminal
:hook (global-corfu-mode . corfu-terminal-mode)))

(use-package kind-icon
:when (icons-displayable-p)
(use-package nerd-icons-corfu
:after corfu
:functions nerd-icons-codicon
:init
(defconst corfu-kind-icon-mapping
`((array . ,(nerd-icons-codicon "nf-cod-symbol_array" :face 'font-lock-type-face))
(boolean . ,(nerd-icons-codicon "nf-cod-symbol_boolean" :face 'font-lock-builtin-face))
(class . ,(nerd-icons-codicon "nf-cod-symbol_class" :face 'font-lock-type-face))
(color . ,(nerd-icons-codicon "nf-cod-symbol_color" :face 'success) )
(command . ,(nerd-icons-codicon "nf-cod-terminal" :face 'default) )
(constant . ,(nerd-icons-codicon "nf-cod-symbol_constant" :face 'font-lock-constant-face) )
(constructor . ,(nerd-icons-codicon "nf-cod-triangle_right" :face 'font-lock-function-name-face) )
(enummember . ,(nerd-icons-codicon "nf-cod-symbol_enum_member" :face 'font-lock-builtin-face) )
(enum-member . ,(nerd-icons-codicon "nf-cod-symbol_enum_member" :face 'font-lock-builtin-face) )
(enum . ,(nerd-icons-codicon "nf-cod-symbol_enum" :face 'font-lock-builtin-face) )
(event . ,(nerd-icons-codicon "nf-cod-symbol_event" :face 'font-lock-warning-face) )
(field . ,(nerd-icons-codicon "nf-cod-symbol_field" :face 'font-lock-variable-name-face) )
(file . ,(nerd-icons-codicon "nf-cod-symbol_file" :face 'font-lock-string-face) )
(folder . ,(nerd-icons-codicon "nf-cod-folder" :face 'font-lock-doc-face) )
(interface . ,(nerd-icons-codicon "nf-cod-symbol_interface" :face 'font-lock-type-face) )
(keyword . ,(nerd-icons-codicon "nf-cod-symbol_keyword" :face 'font-lock-keyword-face) )
(macro . ,(nerd-icons-codicon "nf-cod-symbol_misc" :face 'font-lock-keyword-face) )
(magic . ,(nerd-icons-codicon "nf-cod-wand" :face 'font-lock-builtin-face) )
(method . ,(nerd-icons-codicon "nf-cod-symbol_method" :face 'font-lock-function-name-face) )
(function . ,(nerd-icons-codicon "nf-cod-symbol_method" :face 'font-lock-function-name-face) )
(module . ,(nerd-icons-codicon "nf-cod-file_submodule" :face 'font-lock-preprocessor-face) )
(numeric . ,(nerd-icons-codicon "nf-cod-symbol_numeric" :face 'font-lock-builtin-face) )
(operator . ,(nerd-icons-codicon "nf-cod-symbol_operator" :face 'font-lock-comment-delimiter-face) )
(param . ,(nerd-icons-codicon "nf-cod-symbol_parameter" :face 'default) )
(property . ,(nerd-icons-codicon "nf-cod-symbol_property" :face 'font-lock-variable-name-face) )
(reference . ,(nerd-icons-codicon "nf-cod-references" :face 'font-lock-variable-name-face) )
(snippet . ,(nerd-icons-codicon "nf-cod-symbol_snippet" :face 'font-lock-string-face) )
(string . ,(nerd-icons-codicon "nf-cod-symbol_string" :face 'font-lock-string-face) )
(struct . ,(nerd-icons-codicon "nf-cod-symbol_structure" :face 'font-lock-variable-name-face) )
(text . ,(nerd-icons-codicon "nf-cod-text_size" :face 'font-lock-doc-face) )
(typeparameter . ,(nerd-icons-codicon "nf-cod-list_unordered" :face 'font-lock-type-face) )
(type-parameter . ,(nerd-icons-codicon "nf-cod-list_unordered" :face 'font-lock-type-face) )
(unit . ,(nerd-icons-codicon "nf-cod-symbol_ruler" :face 'font-lock-constant-face) )
(value . ,(nerd-icons-codicon "nf-cod-symbol_field" :face 'font-lock-builtin-face) )
(variable . ,(nerd-icons-codicon "nf-cod-symbol_variable" :face 'font-lock-variable-name-face) )
(t . ,(nerd-icons-codicon "nf-cod-code" :face 'font-lock-warning-face))))

(defsubst nerd-icon--metadata-get (metadata type-name)
"Get METADATA for keyword TYPE-NAME from the completion properties."
(or
(plist-get completion-extra-properties (intern (format ":%s" type-name)))
(cdr (assq (intern type-name) metadata))))

(defsubst nerd-icon-formatted (kind)
"Get icon for KIND."
(let* ((icon (alist-get kind corfu-kind-icon-mapping))
(icon-face (get-text-property 0 'face icon))
(icon-bg (plist-get icon-face :inherit))
(icon-pad (propertize " " 'face (append '(:height 0.5) icon-bg)))
(item-pad (propertize " " 'face '(:height 0.5))))
(concat icon-pad icon icon-pad item-pad)))

(defun nerd-icon-margin-formatter (metadata)
"Return a margin-formatter function which produces kind icons.
METADATA is the completion metadata supplied by the caller (see
info node `(elisp)Programmed Completion'). To use, add this
function to the relevant margin-formatters list."
(if-let ((kind-func (nerd-icon--metadata-get metadata "company-kind")))
(lambda (cand)
(if-let ((kind (funcall kind-func cand)))
(nerd-icon-formatted kind)
(nerd-icon-formatted t)))))

(add-to-list 'corfu-margin-formatters #'nerd-icon-margin-formatter))
:init (add-to-list 'corfu-margin-formatters #'nerd-icons-corfu-formatter))

;; Add extensions
(use-package cape
Expand Down

0 comments on commit 87ae312

Please sign in to comment.