|
| 1 | +# Semantic highlighting |
| 2 | + |
| 3 | +The Ruby LSP supports semantic highlighting. This feature informs editors about the right token types for each part of |
| 4 | +the code to allow for richer and accurate highlighting. The strategy taken by the Ruby LSP is to only return tokens for |
| 5 | +syntax that is ambiguous in Ruby (as opposed to all existing tokens) to optimize for performance. |
| 6 | + |
| 7 | +An example of ambiguous syntax in Ruby are local variables and method calls. If you look at this line in isolation: |
| 8 | +```ruby |
| 9 | +foo |
| 10 | +``` |
| 11 | +it is not possible to tell if `foo` is a local variable or a method call. It depends on whether `foo` was assigned to |
| 12 | +something before or not. This is one scenario where semantic highlighting removes the ambiguity for themes, returning |
| 13 | +the correct token type by statically analyzing the code. |
| 14 | + |
| 15 | +To enhance a theme's Ruby syntax highlighting using the Ruby LSP, check the inform below. You may also want to check out |
| 16 | +the [Spinel theme](https://github.com/Shopify/vscode-shopify-ruby/blob/main/themes/dark_spinel.json) as an example, |
| 17 | +which uses all of the Ruby LSP's semantic highlighting information. |
| 18 | + |
| 19 | +## Token types |
| 20 | + |
| 21 | +According to the LSP specification, language servers can either use token types and modifiers from the [default |
| 22 | +list](https://microsoft.github.io/language-server-protocol/specification#semanticTokenTypes) or contribute new semantic |
| 23 | +tokens of their own. Currently, the Ruby LSP does not contribute any new semantic tokens and only uses the ones |
| 24 | +contained in the default list. |
| 25 | + |
| 26 | +## Token list |
| 27 | + |
| 28 | +| Syntax | Type.Modifier | Note | |
| 29 | +| ------------- | ------------- | ------------- | |
| 30 | +| Sorbet annotation methods such as `let` or `cast` | type | Not every annotation is handled | |
| 31 | +| Method calls with any syntax | method | | |
| 32 | +| Constant references (including classes and modules) | namespace | We don't yet differentiate between module and class references | |
| 33 | +| Method definition | method.declaration | | |
| 34 | +| self | variable.default_library | | |
| 35 | +| Method, block and lambda arguments | parameter | | |
| 36 | +| Class declaration | class.declaration | | |
| 37 | +| Module declaration | class.declaration | | |
0 commit comments