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 (CRATE_ROOT, "crateRoot"),
98 (INJECTED, "injected"),
99 (INTRA_DOC_LINK, "intraDocLink"),
100 (LIBRARY, "library"),
101 (MUTABLE, "mutable"),
103 (REFERENCE, "reference"),
104 (TRAIT_MODIFIER, "trait"),
109 pub(crate) struct ModifierSet(pub(crate) u32);
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();
118 /// Tokens are encoded relative to each other.
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 {
125 data: Vec<SemanticToken>,
128 impl SemanticTokensBuilder {
129 pub(crate) fn new(id: String) -> Self {
130 SemanticTokensBuilder { id, prev_line: 0, prev_char: 0, data: Default::default() }
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;
138 if !self.data.is_empty() {
139 push_line -= self.prev_line;
141 push_char -= self.prev_char;
145 // A token cannot be multiline
146 let token_len = range.end.character - range.start.character;
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,
156 self.data.push(token);
158 self.prev_line = range.start.line as u32;
159 self.prev_char = range.start.character as u32;
162 pub(crate) fn build(self) -> SemanticTokens {
163 SemanticTokens { result_id: Some(self.id), data: self.data }
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();
170 let (_, old) = old.split_at(offset);
171 let (_, new) = new.split_at(offset);
173 let offset_from_end =
174 new.iter().rev().zip(old.iter().rev()).take_while(|&(n, p)| n == p).count();
176 let (old, _) = old.split_at(old.len() - offset_from_end);
177 let (new, _) = new.split_at(new.len() - offset_from_end);
179 if old.is_empty() && new.is_empty() {
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()),
193 pub(crate) fn type_index(ty: SemanticTokenType) -> u32 {
194 SUPPORTED_TYPES.iter().position(|it| *it == ty).unwrap() as u32
201 fn from(t: (u32, u32, u32, u32, u32)) -> SemanticToken {
207 token_modifiers_bitset: t.4,
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))];
216 let edits = diff_tokens(&before, &after);
222 data: Some(vec![from((11, 12, 13, 14, 15))])
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))];
232 let edits = diff_tokens(&before, &after);
238 data: Some(vec![from((11, 12, 13, 14, 15))])
244 fn test_diff_insert_in_middle() {
245 let before = [from((1, 2, 3, 4, 5)), from((6, 7, 8, 9, 10))];
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)),
253 let edits = diff_tokens(&before, &after);
259 data: Some(vec![from((10, 20, 30, 40, 50)), from((60, 70, 80, 90, 100))])
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))];
269 let edits = diff_tokens(&before, &after);
270 assert_eq!(edits[0], SemanticTokensEdit { start: 10, delete_count: 5, data: Some(vec![]) });
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))];
278 let edits = diff_tokens(&before, &after);
279 assert_eq!(edits[0], SemanticTokensEdit { start: 0, delete_count: 5, data: Some(vec![]) });
283 fn test_diff_remove_from_middle() {
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)),
290 let after = [from((1, 2, 3, 4, 5)), from((6, 7, 8, 9, 10))];
292 let edits = diff_tokens(&before, &after);
293 assert_eq!(edits[0], SemanticTokensEdit { start: 5, delete_count: 10, data: Some(vec![]) });