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