Skip to content

Commit 7ebfc3d

Browse files
committed
Document semantic token tags
1 parent 5a1fd05 commit 7ebfc3d

File tree

3 files changed

+76
-6
lines changed

3 files changed

+76
-6
lines changed

crates/ide/src/syntax_highlighting.rs

Lines changed: 74 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -42,24 +42,94 @@ pub struct HlRange {
4242
// Feature: Semantic Syntax Highlighting
4343
//
4444
// rust-analyzer highlights the code semantically.
45-
// For example, `bar` in `foo::Bar` might be colored differently depending on whether `Bar` is an enum or a trait.
46-
// rust-analyzer does not specify colors directly, instead it assigns tag (like `struct`) and a set of modifiers (like `declaration`) to each token.
45+
// For example, `Bar` in `foo::Bar` might be colored differently depending on whether `Bar` is an enum or a trait.
46+
// rust-analyzer does not specify colors directly, instead it assigns a tag (like `struct`) and a set of modifiers (like `declaration`) to each token.
4747
// It's up to the client to map those to specific colors.
4848
//
4949
// The general rule is that a reference to an entity gets colored the same way as the entity itself.
5050
// We also give special modifier for `mut` and `&mut` local variables.
5151
//
52+
//
53+
// .Token Tags
54+
//
55+
// Rust-analyzer currently emits the following token tags:
56+
//
57+
// - For items:
58+
// +
59+
// [horizontal]
60+
// enum:: Emitted for enums.
61+
// function:: Emitted for free-standing functions.
62+
// macro:: Emitted for macros.
63+
// method:: Emitted for associated functions, also knowns as methods.
64+
// namespace:: Emitted for modules.
65+
// struct:: Emitted for structs.
66+
// trait:: Emitted for traits.
67+
// typeAlias:: Emitted for type aliases and `Self` in `impl`s.
68+
// union:: Emitted for unions.
69+
//
70+
// - For literals:
71+
// +
72+
// [horizontal]
73+
// boolean:: Emitted for the boolean literals `true` and `false`.
74+
// character:: Emitted for character literals.
75+
// number:: Emitted for numeric literals.
76+
// string:: Emitted for string literals.
77+
// escapeSequence:: Emitted for escaped sequences inside strings like `\n`.
78+
// formatSpecifier:: Emitted for format specifiers `{:?}` in `format!`-like macros.
79+
//
80+
// - For operators:
81+
// +
82+
// [horizontal]
83+
// operator:: Emitted for general operators.
84+
// arithmetic:: Emitted for the arithmetic operators `+`, `-`, `*`, `/`, `+=`, `-=`, `*=`, `/=`.
85+
// bitwise:: Emitted for the bitwise operators `|`, `&`, `!`, `^`, `|=`, `&=`, `^=`.
86+
// comparison:: Emitted for the comparison operators `>`, `<`, `==`, `>=`, `<=`, `!=`.
87+
// logical:: Emitted for the logical operators `||`, `&&`, `!`.
88+
//
89+
// - For punctuation:
90+
// +
91+
// [horizontal]
92+
// punctuation:: Emitted for general punctuation.
93+
// angle:: Emitted for `<>` angle brackets.
94+
// brace:: Emitted for `{}` braces.
95+
// bracket:: Emitted for `[]` brackets.
96+
// parenthesis:: Emitted for `()` parentheses.
97+
// colon:: Emitted for the `:` token.
98+
// comma:: Emitted for the `,` token.
99+
// dot:: Emitted for the `.` token.
100+
// Semi:: Emitted for the `;` token.
101+
//
102+
// //-
103+
//
104+
// [horizontal]
105+
// attribute:: Emitted for attributes.
106+
// builtinType:: Emitted for builtin types like `u32`, `str` and `f32`.
107+
// comment:: Emitted for comments.
108+
// constParameter:: Emitted for const parameters.
109+
// enumMember:: Emitted for enum variants.
110+
// generic:: Emitted for generic tokens that have no mapping.
111+
// keyword:: Emitted for keywords.
112+
// label:: Emitted for labels.
113+
// lifetime:: Emitted for lifetimes.
114+
// parameter:: Emitted for non-self function parameters.
115+
// property:: Emitted for struct and union fields.
116+
// selfKeyword:: Emitted for the self function parameter and self path-specifier.
117+
// typeParameter:: Emitted for type parameters.
118+
// unresolvedReference:: Emitted for unresolved references, names that rust-analyzer can't find the definition of.
119+
// variable:: Emitted for locals, constants and statics.
120+
//
121+
//
52122
// .Token Modifiers
53123
//
54124
// Token modifiers allow to style some elements in the source code more precisely.
55125
//
56126
// Rust-analyzer currently emits the following token modifiers:
57127
//
58128
// [horizontal]
59-
// associated:: Emitted for associated items.
60129
// async:: Emitted for async functions and the `async` and `await` keywords.
61130
// attribute:: Emitted for tokens inside attributes.
62131
// callable:: Emitted for locals whose types implements one of the `Fn*` traits.
132+
// constant:: Emitted for consts.
63133
// consuming:: Emitted for locals that are being consumed when use in a function call.
64134
// controlFlow:: Emitted for control-flow related tokens, this includes the `?` operator.
65135
// declaration:: Emitted for names of definitions, like `foo` in `fn foo() {}`.
@@ -68,7 +138,7 @@ pub struct HlRange {
68138
// intraDocLink:: Emitted for intra doc links in doc-strings.
69139
// library:: Emitted for items that are defined outside of the current crate.
70140
// mutable:: Emitted for mutable locals and statics.
71-
// static:: Emitted for "static" functions, also known as functions that do not take a `self` param.
141+
// static:: Emitted for "static" functions, also known as functions that do not take a `self` param, as well as statics and consts.
72142
// trait:: Emitted for associated trait items.
73143
// unsafe:: Emitted for unsafe operations, like unsafe function calls, as well as the `unsafe` token.
74144
//

crates/rust-analyzer/src/semantic_tokens.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ define_semantic_token_types![
4646
(BRACE, "brace"),
4747
(BRACKET, "bracket"),
4848
(BUILTIN_TYPE, "builtinType"),
49-
(CHAR_LITERAL, "characterLiteral"),
49+
(CHAR, "character"),
5050
(COLON, "colon"),
5151
(COMMA, "comma"),
5252
(COMPARISON, "comparison"),

crates/rust-analyzer/src/to_proto.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -466,7 +466,7 @@ fn semantic_token_type_and_modifiers(
466466
HlTag::BoolLiteral => semantic_tokens::BOOLEAN,
467467
HlTag::BuiltinType => semantic_tokens::BUILTIN_TYPE,
468468
HlTag::ByteLiteral | HlTag::NumericLiteral => lsp_types::SemanticTokenType::NUMBER,
469-
HlTag::CharLiteral => semantic_tokens::CHAR_LITERAL,
469+
HlTag::CharLiteral => semantic_tokens::CHAR,
470470
HlTag::Comment => lsp_types::SemanticTokenType::COMMENT,
471471
HlTag::EscapeSequence => semantic_tokens::ESCAPE_SEQUENCE,
472472
HlTag::FormatSpecifier => semantic_tokens::FORMAT_SPECIFIER,

0 commit comments

Comments
 (0)