rainstormstudio / nerd-icons.el

GNU General Public License v3.0
206 stars 22 forks source link

can't mapping nerd icon to kind icon in mac #33

Closed C1oud555 closed 1 year ago

C1oud555 commented 1 year ago

emacs-version: 29.0.91 -- emacs-plus

use nf-cod-symbol_keyword as example:

result of (insert (nerd-icons-codicon "nf-cod-symbol_keyword")) is fine.

tttt

but the result of kind-icon-mapping is totally messed up.

rainstormstudio commented 1 year ago

maybe the faces provided are not using the nerd font family.

what is your default font family? Could you please try removing the face properties for the mapping?

C1oud555 commented 1 year ago

my font settings

  (set-face-attribute 'default nil :family "SF Mono" :height 160)
(if (display-graphic-p)
      (dolist (charset '(kana han symbol cjk-misc bopomofo))
        (set-fontset-font (frame-parameter nil 'font) charset
                          (font-spec :family "Hiragino Sans GB"))))

and I tried removing the face properties for the mapping, It makes the icon become ???

rainstormstudio commented 1 year ago

I think it's because Nerd Font has some code points having conflicts with CJK code points.

maybe you can also try to manually set the faces for the icons to use nerd font family

jilen commented 1 year ago

@C1oud555 The kind-icon package may override family of nerd icon face. I've made a customized margin formatter, you may take a look at https://github.com/jilen/.emacs.d/blob/main/lisp/init-corfu.el#L74

Note, this file use lexical scope.

C1oud555 commented 1 year ago

@jilen Thanks a lot. This works well!

Aaronzinhoo commented 10 months ago

I think it's because Nerd Font has some code points having conflicts with CJK code points.

maybe you can also try to manually set the faces for the icons to use nerd font family

@rainstormstudio Can you provide an example of how you would do this? I am facing a similar issue actually.

rainstormstudio commented 10 months ago

@Aaronzinhoo I personally do not have this issue on my own machine, but I later adopted @jilen 's solution for fixing it on another machine.

the following is the config (requires lexical-binding):

(require 'color)

(use-package corfu
  :elpaca (:files (:defaults "extensions/*"))
  :custom
  (corfu-cycle t)
  (corfu-auto t)
  (corfu-popupinfo-delay 0)
  :hook
  (eshell-mode . (lambda()
                   (setq-local corfu-auto nil)
                   (corfu-mode)))
  :init
  (global-corfu-mode)
  (corfu-history-mode)
  (corfu-popupinfo-mode))

(use-package kind-icon
  :after corfu
  :custom
  (kind-icon-default-face 'corfu-default)
  :init
  (setq kind-icon-use-icons nil)

  (defsubst rgb-blend (rgb1 rgb2 frac)
    (apply #'color-rgb-to-hex
           (cl-mapcar (lambda (a b)
                        (+ (* a frac) (* b (- 1.0 frac))))
                      rgb1 rgb2)))

  (defsubst icon-face-spec (face)
    (let* ((default-bg (frame-parameter nil 'background-color))
           (fg (or (face-foreground face) (frame-parameter nil 'foreground-color)))
           (bg (rgb-blend (color-name-to-rgb fg) (color-name-to-rgb default-bg) 0.12)))
      `(:foreground ,fg :background ,bg)))

  (defun codicon (name face)
    (nerd-icons-codicon name :face (icon-face-spec face)))

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

  (defsubst nerd-icon--metadata-get (metadata type-name)
    (or
     (plist-get completion-extra-properties (intern (format ":%s" type-name)))
     (cdr (assq (intern type-name) metadata))))

  (defsubst nerd-icon-formatted (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)
    (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)))))
  :config
  (add-to-list 'corfu-margin-formatters #'nerd-icon-margin-formatter))
minad commented 9 months ago

It would be nice if someone creates a separate nerd-icons-corfu or nerd-icons-kind package and publishes it on MELPA, such that the indirection via kind-icon is avoided. It boils down to something like the following:

;; -*- lexical-binding: t -*-

(defun nerd-icons-corfu-kind-to-icon (kind)
  (substring (symbol-name kind) 0 1)) ;; TODO Add the mapping here

(defun nerd-icons-corfu-margin-formatter (_metadata)
  (when-let (kind (plist-get completion-extra-properties :company-kind))
    (lambda (cand)
      (propertize
       (concat
        (propertize " " 'display '(space :width 0.5))
        (nerd-icons-corfu-kind-to-icon (or (funcall kind cand) t))
        (propertize " " 'display '(space :width 0.5)))
       'face '(:inherit error))))) ;; TODO Adapt styling

(setq corfu-margin-formatters #'nerd-icons-corfu-margin-formatter)

@rainstormstudio In your code above you pull in kind-icon which should not be needed.

minad commented 9 months ago

Thankfully @LuigiPiucco packaged up nerd-icons-corfu: https://github.com/LuigiPiucco/nerd-icons-corfu