1 //! Semantic Tokens helpers
6 Range, SemanticToken, SemanticTokenModifier, SemanticTokenType, SemanticTokens,
10 macro_rules! define_semantic_token_types {
11 ($(($ident:ident, $string:literal)),*$(,)?) => {
12 $(pub(crate) const $ident: SemanticTokenType = SemanticTokenType::new($string);)*
14 pub(crate) const SUPPORTED_TYPES: &[SemanticTokenType] = &[
15 SemanticTokenType::COMMENT,
16 SemanticTokenType::KEYWORD,
17 SemanticTokenType::STRING,
18 SemanticTokenType::NUMBER,
19 SemanticTokenType::REGEXP,
20 SemanticTokenType::OPERATOR,
21 SemanticTokenType::NAMESPACE,
22 SemanticTokenType::TYPE,
23 SemanticTokenType::STRUCT,
24 SemanticTokenType::CLASS,
25 SemanticTokenType::INTERFACE,
26 SemanticTokenType::ENUM,
27 SemanticTokenType::ENUM_MEMBER,
28 SemanticTokenType::TYPE_PARAMETER,
29 SemanticTokenType::FUNCTION,
30 SemanticTokenType::METHOD,
31 SemanticTokenType::PROPERTY,
32 SemanticTokenType::MACRO,
33 SemanticTokenType::VARIABLE,
34 SemanticTokenType::PARAMETER,
40 define_semantic_token_types![
42 (ARITHMETIC, "arithmetic"),
43 (ATTRIBUTE, "attribute"),
48 (BUILTIN_ATTRIBUTE, "builtinAttribute"),
49 (BUILTIN_TYPE, "builtinType"),
53 (COMPARISON, "comparison"),
54 (CONST_PARAMETER, "constParameter"),
56 (ESCAPE_SEQUENCE, "escapeSequence"),
57 (FORMAT_SPECIFIER, "formatSpecifier"),
60 (LIFETIME, "lifetime"),
62 (OPERATOR, "operator"),
63 (PARENTHESIS, "parenthesis"),
64 (PUNCTUATION, "punctuation"),
65 (SELF_KEYWORD, "selfKeyword"),
66 (SEMICOLON, "semicolon"),
67 (TYPE_ALIAS, "typeAlias"),
69 (UNRESOLVED_REFERENCE, "unresolvedReference"),
72 macro_rules! define_semantic_token_modifiers {
73 ($(($ident:ident, $string:literal)),*$(,)?) => {
74 $(pub(crate) const $ident: SemanticTokenModifier = SemanticTokenModifier::new($string);)*
76 pub(crate) const SUPPORTED_MODIFIERS: &[SemanticTokenModifier] = &[
77 SemanticTokenModifier::DOCUMENTATION,
78 SemanticTokenModifier::DECLARATION,
79 SemanticTokenModifier::DEFINITION,
80 SemanticTokenModifier::STATIC,
81 SemanticTokenModifier::ABSTRACT,
82 SemanticTokenModifier::DEPRECATED,
83 SemanticTokenModifier::READONLY,
84 SemanticTokenModifier::DEFAULT_LIBRARY,
90 define_semantic_token_modifiers![
92 (ATTRIBUTE_MODIFIER, "attribute"),
93 (CALLABLE, "callable"),
94 (CONSTANT, "constant"),
95 (CONSUMING, "consuming"),
96 (CONTROL_FLOW, "controlFlow"),
97 (INJECTED, "injected"),
98 (INTRA_DOC_LINK, "intraDocLink"),
100 (MUTABLE, "mutable"),
102 (REFERENCE, "reference"),
103 (TRAIT_MODIFIER, "trait"),
108 pub(crate) struct ModifierSet(pub(crate) u32);
110 impl ops::BitOrAssign<SemanticTokenModifier> for ModifierSet {
111 fn bitor_assign(&mut self, rhs: SemanticTokenModifier) {
112 let idx = SUPPORTED_MODIFIERS.iter().position(|it| it == &rhs).unwrap();
117 /// Tokens are encoded relative to each other.
119 /// This is a direct port of <https://github.com/microsoft/vscode-languageserver-node/blob/f425af9de46a0187adb78ec8a46b9b2ce80c5412/server/src/sematicTokens.proposed.ts#L45>
120 pub(crate) struct SemanticTokensBuilder {
124 data: Vec<SemanticToken>,
127 impl SemanticTokensBuilder {
128 pub(crate) fn new(id: String) -> Self {
129 SemanticTokensBuilder { id, prev_line: 0, prev_char: 0, data: Default::default() }
132 /// Push a new token onto the builder
133 pub(crate) fn push(&mut self, range: Range, token_index: u32, modifier_bitset: u32) {
134 let mut push_line = range.start.line as u32;
135 let mut push_char = range.start.character as u32;
137 if !self.data.is_empty() {
138 push_line -= self.prev_line;
140 push_char -= self.prev_char;
144 // A token cannot be multiline
145 let token_len = range.end.character - range.start.character;
147 let token = SemanticToken {
148 delta_line: push_line,
149 delta_start: push_char,
150 length: token_len as u32,
151 token_type: token_index,
152 token_modifiers_bitset: modifier_bitset,
155 self.data.push(token);
157 self.prev_line = range.start.line as u32;
158 self.prev_char = range.start.character as u32;
161 pub(crate) fn build(self) -> SemanticTokens {
162 SemanticTokens { result_id: Some(self.id), data: self.data }
166 pub(crate) fn diff_tokens(old: &[SemanticToken], new: &[SemanticToken]) -> Vec<SemanticTokensEdit> {
167 let offset = new.iter().zip(old.iter()).take_while(|&(n, p)| n == p).count();
169 let (_, old) = old.split_at(offset);
170 let (_, new) = new.split_at(offset);
172 let offset_from_end =
173 new.iter().rev().zip(old.iter().rev()).take_while(|&(n, p)| n == p).count();
175 let (old, _) = old.split_at(old.len() - offset_from_end);
176 let (new, _) = new.split_at(new.len() - offset_from_end);
178 if old.is_empty() && new.is_empty() {
181 // The lsp data field is actually a byte-diff but we
182 // travel in tokens so `start` and `delete_count` are in multiples of the
183 // serialized size of `SemanticToken`.
184 vec![SemanticTokensEdit {
185 start: 5 * offset as u32,
186 delete_count: 5 * old.len() as u32,
187 data: Some(new.into()),
192 pub(crate) fn type_index(ty: SemanticTokenType) -> u32 {
193 SUPPORTED_TYPES.iter().position(|it| *it == ty).unwrap() as u32
200 fn from(t: (u32, u32, u32, u32, u32)) -> SemanticToken {
206 token_modifiers_bitset: t.4,
211 fn test_diff_insert_at_end() {
212 let before = [from((1, 2, 3, 4, 5)), from((6, 7, 8, 9, 10))];
213 let after = [from((1, 2, 3, 4, 5)), from((6, 7, 8, 9, 10)), from((11, 12, 13, 14, 15))];
215 let edits = diff_tokens(&before, &after);
221 data: Some(vec![from((11, 12, 13, 14, 15))])
227 fn test_diff_insert_at_beginning() {
228 let before = [from((1, 2, 3, 4, 5)), from((6, 7, 8, 9, 10))];
229 let after = [from((11, 12, 13, 14, 15)), from((1, 2, 3, 4, 5)), from((6, 7, 8, 9, 10))];
231 let edits = diff_tokens(&before, &after);
237 data: Some(vec![from((11, 12, 13, 14, 15))])
243 fn test_diff_insert_in_middle() {
244 let before = [from((1, 2, 3, 4, 5)), from((6, 7, 8, 9, 10))];
246 from((1, 2, 3, 4, 5)),
247 from((10, 20, 30, 40, 50)),
248 from((60, 70, 80, 90, 100)),
249 from((6, 7, 8, 9, 10)),
252 let edits = diff_tokens(&before, &after);
258 data: Some(vec![from((10, 20, 30, 40, 50)), from((60, 70, 80, 90, 100))])
264 fn test_diff_remove_from_end() {
265 let before = [from((1, 2, 3, 4, 5)), from((6, 7, 8, 9, 10)), from((11, 12, 13, 14, 15))];
266 let after = [from((1, 2, 3, 4, 5)), from((6, 7, 8, 9, 10))];
268 let edits = diff_tokens(&before, &after);
269 assert_eq!(edits[0], SemanticTokensEdit { start: 10, delete_count: 5, data: Some(vec![]) });
273 fn test_diff_remove_from_beginning() {
274 let before = [from((11, 12, 13, 14, 15)), from((1, 2, 3, 4, 5)), from((6, 7, 8, 9, 10))];
275 let after = [from((1, 2, 3, 4, 5)), from((6, 7, 8, 9, 10))];
277 let edits = diff_tokens(&before, &after);
278 assert_eq!(edits[0], SemanticTokensEdit { start: 0, delete_count: 5, data: Some(vec![]) });
282 fn test_diff_remove_from_middle() {
284 from((1, 2, 3, 4, 5)),
285 from((10, 20, 30, 40, 50)),
286 from((60, 70, 80, 90, 100)),
287 from((6, 7, 8, 9, 10)),
289 let after = [from((1, 2, 3, 4, 5)), from((6, 7, 8, 9, 10))];
291 let edits = diff_tokens(&before, &after);
292 assert_eq!(edits[0], SemanticTokensEdit { start: 5, delete_count: 10, data: Some(vec![]) });