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