Skip to content

Commit 2b12abd

Browse files
committed
Add semantic highlighting documentation for themes
1 parent 966ea33 commit 2b12abd

File tree

2 files changed

+40
-0
lines changed

2 files changed

+40
-0
lines changed

README.md

+3
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,9 @@ end
3939
See the [documentation](https://shopify.github.io/ruby-lsp) for more in-depth details about the
4040
[supported features](https://shopify.github.io/ruby-lsp/RubyLsp/Requests.html).
4141

42+
For creating rich themes for Ruby using the semantic highlighting information, see the [semantic highlighting
43+
documentation](SEMANTIC_HIGHLIGHTING.md).
44+
4245
## Learn More
4346

4447
* [RubyConf 2022: Improving the development experience with language servers](https://www.youtube.com/watch?v=kEfXPTm1aCI) ([Vinicius Stock](https://github.com/vinistock))

SEMANTIC_HIGHLIGHTING.md

+37
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
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

Comments
 (0)