-
Notifications
You must be signed in to change notification settings - Fork 154
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #701 from Shopify/vs/add_semantic_highlighting_doc…
…umentation Add semantic highlighting documentation
- Loading branch information
Showing
4 changed files
with
60 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
# Semantic highlighting | ||
|
||
The Ruby LSP supports semantic highlighting. This feature informs editors about the right token types for each part of | ||
the code to allow for rich and accurate highlighting. The strategy taken by the Ruby LSP is to only return tokens for | ||
syntax that is ambiguous in Ruby (as opposed to all existing tokens) to optimize for performance. | ||
|
||
An example of ambiguous syntax in Ruby are local variables and method calls. If you look at this line in isolation: | ||
```ruby | ||
foo | ||
``` | ||
it is not possible to tell if `foo` is a local variable or a method call. It depends on whether `foo` was assigned to | ||
something before or not. This is one scenario where semantic highlighting removes the ambiguity for themes, returning | ||
the correct token type by statically analyzing the code. | ||
|
||
To enhance a theme's Ruby syntax highlighting using the Ruby LSP, check the information below. You may also want to | ||
check out the [Spinel theme](https://github.com/Shopify/vscode-shopify-ruby/blob/main/themes/dark_spinel.json) as an | ||
example, which uses all of the Ruby LSP's semantic highlighting information. | ||
|
||
## Token types | ||
|
||
According to the LSP specification, language servers can either use token types and modifiers from the [default | ||
list](https://microsoft.github.io/language-server-protocol/specification#semanticTokenTypes) or contribute new semantic | ||
tokens of their own. Currently, the Ruby LSP does not contribute any new semantic tokens and only uses the ones | ||
contained in the default list. | ||
|
||
## Token list | ||
|
||
| Syntax | Type.Modifier | Note | | ||
| ------------- | ------------- | ------------- | | ||
| Sorbet annotation methods such as `let` or `cast` | type | Not every annotation is handled | | ||
| Method calls with any syntax | method | | | ||
| Constant references (including classes and modules) | namespace | We don't yet differentiate between module and class references | | ||
| Method definition | method.declaration | | | ||
| self | variable.default_library | | | ||
| Method, block and lambda arguments | parameter | | | ||
| Class declaration | class.declaration | | | ||
| Module declaration | class.declaration | | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
19 changes: 19 additions & 0 deletions
19
test/expectations/semantic_highlighting/module_and_reference.exp.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
{ | ||
"params": [], | ||
"result": [ | ||
{ | ||
"delta_line": 0, | ||
"delta_start_char": 7, | ||
"length": 3, | ||
"token_type": 0, | ||
"token_modifiers": 1 | ||
}, | ||
{ | ||
"delta_line": 3, | ||
"delta_start_char": 0, | ||
"length": 3, | ||
"token_type": 0, | ||
"token_modifiers": 0 | ||
} | ||
] | ||
} |