vim icon indicating copy to clipboard operation
vim copied to clipboard

[Feature Request]: lsp semantic highlight

Open I-Want-ToBelieve opened this issue 1 year ago β€’ 1 comments

https://github.com/dracula/vim/issues/271

Machine Info

  • Vim type (vim/gvim/neovim): neovim
  • Vim version:0.7.2
  • OS: archlinux
  • Terminal/Terminal Emulator/VTE: kitty
  • TERM environment variable: xterm-kitty

Additional Info

https://github.com/theHamsta/nvim-semantic-tokens

I-Want-ToBelieve avatar Sep 21 '22 03:09 I-Want-ToBelieve

What exactly is the request here?

  • We have some Treesitter support
  • #271 is closed with possible ways for users to resolve the discrepancy
  • the 'Additional Info' link doesn't explain the request

benknoble avatar Sep 21 '22 13:09 benknoble

Using LSP's semantic highlighting as a complement to Treesitter highlighting eliminates differences with VSCode highlighting.

When neovim is ready to provide LSP semantic tokens, the only thing the theme plugin needs to do is map LSP highlight groups to colors or highlight groups you like to actually see semantic highlights.

I-Want-ToBelieve avatar Sep 22 '22 05:09 I-Want-ToBelieve

Is that somehow different from https://github.com/dracula/vim/blob/master/colors/dracula.vim#L263-L275 ?

Is this something for which we need to monitor NeoVim releases to know when this feature stabilizes?

benknoble avatar Sep 22 '22 12:09 benknoble

(I think the answer to my first question is yes.)

benknoble avatar Sep 22 '22 12:09 benknoble

(I think the answer to my first question is yes.)

You are right, I quoted the previous question to illustrate what can be done next

I-Want-ToBelieve avatar Sep 23 '22 05:09 I-Want-ToBelieve

https://github.com/dracula/vim/blob/master/colors/dracula.vim#L263-L275 defines some mappings for LSP diagnostic highlight groups

The LSP semantic highlighting group is about the highlighting group of syntax tokens with different semantics

For example a mutable variable and a read-only constant variable can have different LSP highlight groups: LspVariable and LspVariableReadOnly. Thanks to this, we can map them with different colors, the final rendering effect of the theme is the same as VSCode

I-Want-ToBelieve avatar Sep 23 '22 05:09 I-Want-ToBelieve

Is this something for which we need to monitor NeoVim releases to know when this feature stabilizes?

This still needs addressed.

The LSP semantic highlighting group is about the highlighting group of syntax tokens with different semantics

For example a mutable variable and a read-only constant variable can have different LSP highlight groups: LspVariable and LspVariableReadOnly. Thanks to this, we can map them with different colors, the final rendering effect of the theme is the same as VSCode

Contributions welcome :)

benknoble avatar Sep 23 '22 12:09 benknoble

Is this something for which we need to monitor NeoVim releases to know when this feature stabilizes?

https://github.com/neovim/neovim/pull/15723 Now there is a neovim plugin that implements LSP semantic highlighting, which can be used but may not be merged into NeoVim for a while

I-Want-ToBelieve avatar Sep 24 '22 09:09 I-Want-ToBelieve

I tried this plugin, and the effect is very good. ~~It can be said that it is very close to the rendering effect of vscode, except for a few small flaws caused by the completely different syntax highlighting mechanism of treesitter and textmate~~

By customizing highlights.scm The final rendering effect is exactly the same as VSCode!

image

image

image

Highlight group mapping is easy:

use('dracula/vim', {
    disable: is_vscode,
    cond: can_load,
    as: 'dracula',
    config () {
      vim.cmd('colorscheme dracula')
      vim.cmd('hi! link LspParameter DraculaOrangeItalic')
      vim.cmd('hi! link LspVariableReadOnly DraculaPurple')
      vim.cmd('hi! link LspFunction DraculaGreen')
      vim.cmd('hi! link LspMember DraculaGreen')
      // vim.cmd('hi! link LspNamespace DraculaCyan')
      // vim.cmd('hi! link LspNamespaceDeclaration DraculaCyan')
      vim.cmd('hi! link LspProperty  Identifier')
      vim.cmd('hi! link LspEnumMember Identifier')
      // vim.cmd('hi! link LspClass DraculaCyan')
      vim.cmd('hi! link LspOperator DraculaPink')
      vim.cmd('hi! link @constructor.imported Identifier')
      vim.cmd('hi! link @namespace.imported Identifier')
      vim.cmd('hi! link @namespace.exported Identifier')
      vim.cmd('hi! link @variable.imported.specifier Identifier')
      vim.cmd('hi! link @operator.module DraculaPurple')
      // vim.cmd('hi! link @constructor.jsx DraculaCyanItalic')
      vim.cmd('hi! link @field Identifier')
    },
  })

But haven't tested other languages, there might be something to break

I-Want-ToBelieve avatar Sep 24 '22 09:09 I-Want-ToBelieve

I don't know what you mean by "customizing highlights.scm."

My major concern is that this appears to be an experimental plugin and not NeoVim coreβ€”I would be more comfortable waiting until the features land in NeoVim core.

What do you think @dsifford? I don't use NeoVim, so I don't know if my understanding of the situation is correct or not.

benknoble avatar Sep 26 '22 14:09 benknoble

I think it's fine to accept PRs for this so long as it's wrapped in a has() condition (like other plugins we support)..

But for very specific customizations, I think it would be safer to customize down to the language level and not broadly...

So, for example.. Instead of @constructor.imported, maybe do @constructor.imported.javascript

dsifford avatar Sep 26 '22 14:09 dsifford

customizing highlights.scm refers to https://github.com/nvim-treesitter/nvim-treesitter#adding-queries

I-Want-ToBelieve avatar Sep 27 '22 05:09 I-Want-ToBelieve

Given that this experimental plugin seems to be different from the LSP semantic highlighting specification, I can submit a PR about the LSP semantic highlighting group mapping after the plugin's author has dealt with it, and about the custom highlights.scm section, There is no definite specification, just record it here for the time being.

I-Want-ToBelieve avatar Sep 27 '22 05:09 I-Want-ToBelieve

Since semantic tokens support was merged (https://github.com/neovim/neovim/pull/21100) dracula themes highlights break when the client attaches.

A 5 sec example using nvim nighlty, dracula and sumneko_lua as lsp: After the client is loaded treesitter highlighting stops.

https://user-images.githubusercontent.com/34311583/216818870-a330f292-3c0c-4eb0-a78d-ff4a7eed0aae.mov

For now the way I found to go about is to nil the semanticTokensProvider when dracula is used or call vim.lsp.semantic_tokens.stop() for language server or to manually extend the highlight groups like @I-Want-ToBelieve describes above.

As it becomes harder to write good code without a dracula theme πŸ§›πŸ»β€β™‚οΈ, do you have the chance to extend the themes highlighting to that @dsifford, @benknoble? 😊


Edit: just found we are already doing quite well, when just disabling semantic.keyword and semantic.variable

--- lsp/settings/sumneko_lua.lua
return {
	settings = {
		Lua = {
			-- ...
			semantic = {
				keyword = false,
				variable = false,
			},
			-- ...
		},
	},
}

ttytm avatar Feb 05 '23 12:02 ttytm

As I have said before, contributions are welcome in the form of PRs. This isn't something I use or even would be in a position to test (except that it doesn't break plain old Dracula on Vim).

Last I heard Derek was pretty busy, though he might be in a better position with respect to this feature now.

benknoble avatar Feb 05 '23 13:02 benknoble

try https://github.com/xiantang/darcula-dark.nvim

xiantang avatar Jul 30 '23 04:07 xiantang