Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

cmd/govim: add support for semantic token highlighting #1093

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

leitzler
Copy link
Member

In LSP there is a concept of "semantic tokens" that gopls can provide
us. It allows us to do a more granular syntax highlighting than vim
provides default for Go.

This is a first take of how it might work, used to try out to what
extent it makes sense to govim to provide syntax highlighting.

The highlight groups are currently defined as a part of the syntax
highlighting so to try it out you currently have to have syntax
enabled/on when loading vim. Semantic tokens will replace the native
syntax. Turn off syntax (":syntax off") after startup to see semantic
tokens only.

Enable by adding to your .vimrc:

call govim#config#Set("ExperimentalSemanticTokens", 1)

This change aims to keep track of all visible lines in vim, and only
request semantic tokens for those ranges. Each time one of the
autocommands CursorHold, CursorHoldI or CursorMoved fires visible
lines are re-evaluated. Since highlighting is very sensitive to
latency (and CursorMoved triggers A LOT) the delta calculation is
done on the vim side. That way govim only sees changes.

In govim we also debounce multiple updates, and keep track of
which lines we placed tokens on, to be able to remove non-visible
tokens. Since a buffer might be visible in several windows we also
ensure that we do not remove tokens visible in other windows.

@leitzler
Copy link
Member Author

Example of how it looks like:

asciicast

@leitzler
Copy link
Member Author

asciicast

Demo with a larger file (~100k, the limit in gopls).

@leitzler leitzler force-pushed the cmd_govim_semtok branch 4 times, most recently from c3979fe to 04461cd Compare October 28, 2021 14:31
@leitzler leitzler force-pushed the cmd_govim_semtok branch 13 times, most recently from 4437665 to af447f4 Compare May 16, 2022 08:22
In LSP there is a concept of "semantic tokens" that gopls can provide
us. It allows us to do a more granular syntax highlighting than vim
provides default for Go.

This is a first take of how it might work, used to try out to what
extent it makes sense to govim to provide syntax highlighting.

The highlight groups are currently defined as a part of the syntax
highlighting so to try it out you currently have to have syntax
enabled/on when loading vim. Semantic tokens will replace the native
syntax. Turn off syntax (":syntax off") after startup to see semantic
tokens only.

Enable by adding to your .vimrc:

  call govim#config#Set("ExperimentalSemanticTokens", 1)

This change aims to keep track of all visible lines in vim, and only
request semantic tokens for those ranges. Each time one of the
autocommands CursorHold, CursorHoldI or CursorMoved fires visible
lines are re-evaluated. Since highlighting is very sensitive to
latency (and CursorMoved triggers A LOT) the delta calculation is
done on the vim side. That way govim only sees changes.

In govim we also debounce multiple updates, and keep track of
which lines we placed tokens on, to be able to remove non-visible
tokens. Since a buffer might be visible in several windows we also
ensure that we do not remove tokens visible in other windows.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

1 participant