Skip to content
xhcoding edited this page Nov 5, 2023 · 2 revisions

Semantic Tokens Highlight

The symbol highlighting in Emacs is based on treesit, but treesit cannot highlight some semantic symbols, such as macro definitions in C or C++:

image

Therefore, lsp-bridge provides lsp-bridge-semantic-tokens-mode, which instructs Emacs to highlight some special semantic symbols. The image below shows the effect after enabling it:

image

Custom Highlight Tokens

In the LSP protocol, semantic symbols consist of types and modifiers, such as global variable, which has a type of variable and a modifier of global scope. Some semantic symbols do not have modifiers, such as comment types.

The lsp-bridge-semantic-tokens-mode provides three variables to control which semantic symbols are highlighted:

  1. lsp-bridge-semantic-tokens-type-faces: faces corresponding to symbol types
  2. lsp-bridge-semantic-tokens-type-modifier-faces: faces corresponding to symbol modifiers
  3. lsp-bridge-semantic-tokens-ignore-modifier-limit-types: only symbols with defined type faces and modifier faces will be highlighted. Adding types to this variable allows ignoring the check for symbol modifiers.

For example, to highlight global variables, the following settings need to be made before starting lsp-bridge-mode:

(defface lsp-bridge-semantic-tokens-variable-face
  '((t (:inherit font-lock-variable-name-face)))
  "Face used for variable name."
  :group 'lsp-bridge-semantic-tokens)

(defface lsp-bridge-semantic-tokens-global-scope-face
  '((t :weight extra-bold))
  "Face used for globalScope token."
  :group 'lsp-bridge-semantic-tokens)

(setq-default lsp-bridge-semantic-tokens-type-faces
    [("variable" . lsp-bridge-
semantic-tokens-variable-face)])

(setq-default lsp-bridge-semantic-tokens-type-modifier-faces
    [("globalScope" . lsp-bridge-semantic-tokens-global-scope-face)])

(setq-default lsp-bridge-semantic-tokens-ignore-modifier-limit-types [])

If you want to highlight all variables, regardless of whether they are global or local variables, you can add the "variable" type to the lsp-bridge-semantic-tokens-ignore-modifier-limit-types and ignore the check for modifiers:

(setq-default lsp-bridge-semantic-tokens-ignore-modifier-limit-types ["variable"])