ccls and LSP Semantic Tokens

ccls 的一个feature branch (Support LSP semantic tokens by MaskRay · Pull Request #972 · MaskRay/ccls · GitHub) 支持 LSP semantic tokens。我打算废弃掉现有的 emacs-ccls/ccls-semantic-highlight.el ,得用 semantic tokens 模拟 rainbow semantic highlighting。

ccls and LSP Semantic Tokens | MaskRay 提到了一个解决方案,但我现在不怎么用Emacs了没有力气找到正确的loop + defface 方式:

(setq lsp-semantic-tokens-enable t)
(defface lsp-face-semhl-namespace-scope '((t :weight bold)) "highlight for namespace scope symbols" :group 'lsp-semantic-tokens)
(custom-set-faces
  '(lsp-face-semhl-definition ((t :inherit font-lock-function-name-face)))
  '(lsp-face-semhl-static ((t :weight bold)))
  '(lsp-face-semhl-member ((t :slant italic)))
  )

(defface lsp-face-semhl-id0 '((t :foreground "#429921")) "" :group 'lsp-semantic-tokens)
(defface lsp-face-semhl-id1 '((t :foreground "#58c1a4")) "" :group 'lsp-semantic-tokens)
(defface lsp-face-semhl-id2 '((t :foreground "#98c1a4")) "" :group 'lsp-semantic-tokens)
(defface lsp-face-semhl-id3 '((t :foreground "#6851a4")) "" :group 'lsp-semantic-tokens)
(defface lsp-face-semhl-id4 '((t :foreground "#3b51c4")) "" :group 'lsp-semantic-tokens)
... needs 10 IDs
(setq lsp-semantic-token-modifier-faces
      '(("declaration" . lsp-face-semhl-interface)
        ("definition" . lsp-face-semhl-definition)
        ("implementation" . lsp-face-semhl-implementation)
        ("readonly" . lsp-face-semhl-constant)
        ("static" . lsp-face-semhl-static)
        ("deprecated" . lsp-face-semhl-deprecated)
        ("abstract" . lsp-face-semhl-keyword)
        ("async" . lsp-face-semhl-macro)
        ("modification" . lsp-face-semhl-operator)
        ("documentation" . lsp-face-semhl-comment)
        ("defaultLibrary" . lsp-face-semhl-default-library)
        ("classScope" . lsp-face-semhl-member)
        ("namespaceScope" . lsp-face-semhl-namespace-scope)
        ("id0" . lsp-face-semhl-id0)
        ("id1" . lsp-face-semhl-id1)
        ("id2" . lsp-face-semhl-id2)
        ("id3" . lsp-face-semhl-id3)
        ("id4" . lsp-face-semhl-id4)
.... needs 10 IDs
        ))
1 个赞
(macroexpand '(defface lsp-face-semhl-id0 '((t :foreground "#429921")) "" :group 'lsp-semantic-tokens))
;; (custom-declare-face 'lsp-face-semhl-id0
;;     '((t :foreground "#429921")) "" :group 'lsp-semantic-tokens)

custom-declare-face 就行了。

还要再多提示?

(cl-loop for color in '("#429921" "#58c1a4")
         for i = 0 then (1+ i)
         do (custom-declare-face (intern (format "lsp-face-semhl-id%d" i))
                                 `((t :foreground ,color))
                                 "" :group 'lsp-semantic-tokens))


(setq lsp-semantic-token-modifier-faces
      `(("declaration" . lsp-face-semhl-interface)
        ("definition" . lsp-face-semhl-definition)
        ("implementation" . lsp-face-semhl-implementation)
        ("readonly" . lsp-face-semhl-constant)
        ("static" . lsp-face-semhl-static)
        ...
        ,@(cl-loop for i from 0 to 10
                   collect (cons (format "id%d" i)
                                 (intern (format "lsp-face-semhl-id%d" i))))
        ))

谢谢您。

看上去还需要一个feature request: semantic tokens: face name derived from both token type and modifier · Issue #4590 · emacs-lsp/lsp-mode · GitHub semantic tokens: face name derived from both token type and modifier