My C++ language server ccls implements a semantic highlighting
features through the non-standard messages
$ccls/publishSemanticHighlight
and
$ccls/publishSkippedRanges
, supported by both emacs-ccls
and vscode-ccls.
For years, my primary editor was Emacs. In 2018, I created emacs-ccls (a fork of emacs-cquery) and enjoyed its rainbow semantic highlighting. My setup also relied heavily on two features:
My elisp skills have gotten a bit rusty since I haven't been coding in it for a while. Switching to Neovim presented a challenge: it lacked the rainbow highlighting I loved.
Thankfully, Neovim supports "semantic tokens" from LSP 3.16, a standardized approach adopted by many editors.
I've made changes to ccls (available on a
branch) to support semantic tokens. This basically adapts the
$ccls/publishSemanticHighlight
code to support
textDocument/semanticTokens/full
and
textDocument/semanticTokens/range
.
While semantic tokens do not differentiate symbols, we can define custom modifiers to achieve similar results.
ccls assigns the same modifier ID to tokens belonging to the same symbol. For different symbols, it attempts to assign unique IDs. While we have only 10 predefined IDs (each linked to a specific color), there's a chance that two symbols might share the same ID. However, this is uncommon and generally acceptable.
For a token with type variable
, Neovim's built-in LSP
plugin assigns it a highlight group
@lsp.typemod.variable.id$i.cpp
where $i
is an
integer between 0 and 9. We can customize a foreground color for each
modifier ID. The complete Lua code follows:
1 | local func_colors = { |
This is quite appealing, but I need assistance to get code lens working.
When this feature branch is merged, Emacs users can just remove the following two lines:
TODO: How to change lsp-semantic-token-modifier-faces
to
support rainbow semantic tokens in lsp-mode and emacs-ccls?
Help is needed to remove $ccls/publishSemanticHighlight
to use the built-in semantic tokens support. Unfortunately, vscode-ccls
is not actively maintained, and I lack the expertise to maintain the
plugin of an editor I do not commonly use.