]> git.lizzy.rs Git - rust.git/blob - crates/rust-analyzer/src/semantic_tokens.rs
3117b757285c330fdc0e99dc18abf7b1989ca56c
[rust.git] / crates / rust-analyzer / src / semantic_tokens.rs
1 //! Semantic Tokens helpers
2
3 use std::ops;
4
5 use lsp_types::{
6     Range, SemanticToken, SemanticTokenModifier, SemanticTokenType, SemanticTokens,
7     SemanticTokensEdit,
8 };
9
10 macro_rules! define_semantic_token_types {
11     ($(($ident:ident, $string:literal)),*$(,)?) => {
12         $(pub(crate) const $ident: SemanticTokenType = SemanticTokenType::new($string);)*
13
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,
35             $($ident),*
36         ];
37     };
38 }
39
40 define_semantic_token_types![
41     (ANGLE, "angle"),
42     (ARITHMETIC, "arithmetic"),
43     (ATTRIBUTE, "attribute"),
44     (BITWISE, "bitwise"),
45     (BOOLEAN, "boolean"),
46     (BRACE, "brace"),
47     (BRACKET, "bracket"),
48     (BUILTIN_ATTRIBUTE, "builtinAttribute"),
49     (BUILTIN_TYPE, "builtinType"),
50     (CHAR, "character"),
51     (COLON, "colon"),
52     (COMMA, "comma"),
53     (COMPARISON, "comparison"),
54     (CONST_PARAMETER, "constParameter"),
55     (DOT, "dot"),
56     (ESCAPE_SEQUENCE, "escapeSequence"),
57     (FORMAT_SPECIFIER, "formatSpecifier"),
58     (GENERIC, "generic"),
59     (LABEL, "label"),
60     (LIFETIME, "lifetime"),
61     (LOGICAL, "logical"),
62     (OPERATOR, "operator"),
63     (PARENTHESIS, "parenthesis"),
64     (PUNCTUATION, "punctuation"),
65     (SELF_KEYWORD, "selfKeyword"),
66     (SEMICOLON, "semicolon"),
67     (TYPE_ALIAS, "typeAlias"),
68     (UNION, "union"),
69     (UNRESOLVED_REFERENCE, "unresolvedReference"),
70 ];
71
72 macro_rules! define_semantic_token_modifiers {
73     ($(($ident:ident, $string:literal)),*$(,)?) => {
74         $(pub(crate) const $ident: SemanticTokenModifier = SemanticTokenModifier::new($string);)*
75
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,
85             $($ident),*
86         ];
87     };
88 }
89
90 define_semantic_token_modifiers![
91     (ASYNC, "async"),
92     (ATTRIBUTE_MODIFIER, "attribute"),
93     (CALLABLE, "callable"),
94     (CONSTANT, "constant"),
95     (CONSUMING, "consuming"),
96     (CONTROL_FLOW, "controlFlow"),
97     (CRATE_ROOT, "crateRoot"),
98     (INJECTED, "injected"),
99     (INTRA_DOC_LINK, "intraDocLink"),
100     (LIBRARY, "library"),
101     (MUTABLE, "mutable"),
102     (PUBLIC, "public"),
103     (REFERENCE, "reference"),
104     (TRAIT_MODIFIER, "trait"),
105     (UNSAFE, "unsafe"),
106 ];
107
108 #[derive(Default)]
109 pub(crate) struct ModifierSet(pub(crate) u32);
110
111 impl ops::BitOrAssign<SemanticTokenModifier> for ModifierSet {
112     fn bitor_assign(&mut self, rhs: SemanticTokenModifier) {
113         let idx = SUPPORTED_MODIFIERS.iter().position(|it| it == &rhs).unwrap();
114         self.0 |= 1 << idx;
115     }
116 }
117
118 /// Tokens are encoded relative to each other.
119 ///
120 /// This is a direct port of <https://github.com/microsoft/vscode-languageserver-node/blob/f425af9de46a0187adb78ec8a46b9b2ce80c5412/server/src/sematicTokens.proposed.ts#L45>
121 pub(crate) struct SemanticTokensBuilder {
122     id: String,
123     prev_line: u32,
124     prev_char: u32,
125     data: Vec<SemanticToken>,
126 }
127
128 impl SemanticTokensBuilder {
129     pub(crate) fn new(id: String) -> Self {
130         SemanticTokensBuilder { id, prev_line: 0, prev_char: 0, data: Default::default() }
131     }
132
133     /// Push a new token onto the builder
134     pub(crate) fn push(&mut self, range: Range, token_index: u32, modifier_bitset: u32) {
135         let mut push_line = range.start.line as u32;
136         let mut push_char = range.start.character as u32;
137
138         if !self.data.is_empty() {
139             push_line -= self.prev_line;
140             if push_line == 0 {
141                 push_char -= self.prev_char;
142             }
143         }
144
145         // A token cannot be multiline
146         let token_len = range.end.character - range.start.character;
147
148         let token = SemanticToken {
149             delta_line: push_line,
150             delta_start: push_char,
151             length: token_len as u32,
152             token_type: token_index,
153             token_modifiers_bitset: modifier_bitset,
154         };
155
156         self.data.push(token);
157
158         self.prev_line = range.start.line as u32;
159         self.prev_char = range.start.character as u32;
160     }
161
162     pub(crate) fn build(self) -> SemanticTokens {
163         SemanticTokens { result_id: Some(self.id), data: self.data }
164     }
165 }
166
167 pub(crate) fn diff_tokens(old: &[SemanticToken], new: &[SemanticToken]) -> Vec<SemanticTokensEdit> {
168     let offset = new.iter().zip(old.iter()).take_while(|&(n, p)| n == p).count();
169
170     let (_, old) = old.split_at(offset);
171     let (_, new) = new.split_at(offset);
172
173     let offset_from_end =
174         new.iter().rev().zip(old.iter().rev()).take_while(|&(n, p)| n == p).count();
175
176     let (old, _) = old.split_at(old.len() - offset_from_end);
177     let (new, _) = new.split_at(new.len() - offset_from_end);
178
179     if old.is_empty() && new.is_empty() {
180         vec![]
181     } else {
182         // The lsp data field is actually a byte-diff but we
183         // travel in tokens so `start` and `delete_count` are in multiples of the
184         // serialized size of `SemanticToken`.
185         vec![SemanticTokensEdit {
186             start: 5 * offset as u32,
187             delete_count: 5 * old.len() as u32,
188             data: Some(new.into()),
189         }]
190     }
191 }
192
193 pub(crate) fn type_index(ty: SemanticTokenType) -> u32 {
194     SUPPORTED_TYPES.iter().position(|it| *it == ty).unwrap() as u32
195 }
196
197 #[cfg(test)]
198 mod tests {
199     use super::*;
200
201     fn from(t: (u32, u32, u32, u32, u32)) -> SemanticToken {
202         SemanticToken {
203             delta_line: t.0,
204             delta_start: t.1,
205             length: t.2,
206             token_type: t.3,
207             token_modifiers_bitset: t.4,
208         }
209     }
210
211     #[test]
212     fn test_diff_insert_at_end() {
213         let before = [from((1, 2, 3, 4, 5)), from((6, 7, 8, 9, 10))];
214         let after = [from((1, 2, 3, 4, 5)), from((6, 7, 8, 9, 10)), from((11, 12, 13, 14, 15))];
215
216         let edits = diff_tokens(&before, &after);
217         assert_eq!(
218             edits[0],
219             SemanticTokensEdit {
220                 start: 10,
221                 delete_count: 0,
222                 data: Some(vec![from((11, 12, 13, 14, 15))])
223             }
224         );
225     }
226
227     #[test]
228     fn test_diff_insert_at_beginning() {
229         let before = [from((1, 2, 3, 4, 5)), from((6, 7, 8, 9, 10))];
230         let after = [from((11, 12, 13, 14, 15)), from((1, 2, 3, 4, 5)), from((6, 7, 8, 9, 10))];
231
232         let edits = diff_tokens(&before, &after);
233         assert_eq!(
234             edits[0],
235             SemanticTokensEdit {
236                 start: 0,
237                 delete_count: 0,
238                 data: Some(vec![from((11, 12, 13, 14, 15))])
239             }
240         );
241     }
242
243     #[test]
244     fn test_diff_insert_in_middle() {
245         let before = [from((1, 2, 3, 4, 5)), from((6, 7, 8, 9, 10))];
246         let after = [
247             from((1, 2, 3, 4, 5)),
248             from((10, 20, 30, 40, 50)),
249             from((60, 70, 80, 90, 100)),
250             from((6, 7, 8, 9, 10)),
251         ];
252
253         let edits = diff_tokens(&before, &after);
254         assert_eq!(
255             edits[0],
256             SemanticTokensEdit {
257                 start: 5,
258                 delete_count: 0,
259                 data: Some(vec![from((10, 20, 30, 40, 50)), from((60, 70, 80, 90, 100))])
260             }
261         );
262     }
263
264     #[test]
265     fn test_diff_remove_from_end() {
266         let before = [from((1, 2, 3, 4, 5)), from((6, 7, 8, 9, 10)), from((11, 12, 13, 14, 15))];
267         let after = [from((1, 2, 3, 4, 5)), from((6, 7, 8, 9, 10))];
268
269         let edits = diff_tokens(&before, &after);
270         assert_eq!(edits[0], SemanticTokensEdit { start: 10, delete_count: 5, data: Some(vec![]) });
271     }
272
273     #[test]
274     fn test_diff_remove_from_beginning() {
275         let before = [from((11, 12, 13, 14, 15)), from((1, 2, 3, 4, 5)), from((6, 7, 8, 9, 10))];
276         let after = [from((1, 2, 3, 4, 5)), from((6, 7, 8, 9, 10))];
277
278         let edits = diff_tokens(&before, &after);
279         assert_eq!(edits[0], SemanticTokensEdit { start: 0, delete_count: 5, data: Some(vec![]) });
280     }
281
282     #[test]
283     fn test_diff_remove_from_middle() {
284         let before = [
285             from((1, 2, 3, 4, 5)),
286             from((10, 20, 30, 40, 50)),
287             from((60, 70, 80, 90, 100)),
288             from((6, 7, 8, 9, 10)),
289         ];
290         let after = [from((1, 2, 3, 4, 5)), from((6, 7, 8, 9, 10))];
291
292         let edits = diff_tokens(&before, &after);
293         assert_eq!(edits[0], SemanticTokensEdit { start: 5, delete_count: 10, data: Some(vec![]) });
294     }
295 }