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_TYPE, "builtinType"),
52 (COMPARISON, "comparison"),
53 (CONST_PARAMETER, "constParameter"),
55 (ESCAPE_SEQUENCE, "escapeSequence"),
56 (FORMAT_SPECIFIER, "formatSpecifier"),
59 (LIFETIME, "lifetime"),
61 (OPERATOR, "operator"),
62 (PARENTHESIS, "parenthesis"),
63 (PUNCTUATION, "punctuation"),
64 (SELF_KEYWORD, "selfKeyword"),
65 (SEMICOLON, "semicolon"),
66 (TYPE_ALIAS, "typeAlias"),
68 (UNRESOLVED_REFERENCE, "unresolvedReference"),
71 macro_rules! define_semantic_token_modifiers {
72 ($(($ident:ident, $string:literal)),*$(,)?) => {
73 $(pub(crate) const $ident: SemanticTokenModifier = SemanticTokenModifier::new($string);)*
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,
88 define_semantic_token_modifiers![
89 (CONSTANT, "constant"),
90 (CONTROL_FLOW, "controlFlow"),
91 (INJECTED, "injected"),
93 (CONSUMING, "consuming"),
97 (ATTRIBUTE_MODIFIER, "attribute"),
98 (TRAIT_MODIFIER, "trait"),
99 (CALLABLE, "callable"),
100 (INTRA_DOC_LINK, "intraDocLink"),
104 pub(crate) struct ModifierSet(pub(crate) u32);
106 impl ops::BitOrAssign<SemanticTokenModifier> for ModifierSet {
107 fn bitor_assign(&mut self, rhs: SemanticTokenModifier) {
108 let idx = SUPPORTED_MODIFIERS.iter().position(|it| it == &rhs).unwrap();
113 /// Tokens are encoded relative to each other.
115 /// This is a direct port of <https://github.com/microsoft/vscode-languageserver-node/blob/f425af9de46a0187adb78ec8a46b9b2ce80c5412/server/src/sematicTokens.proposed.ts#L45>
116 pub(crate) struct SemanticTokensBuilder {
120 data: Vec<SemanticToken>,
123 impl SemanticTokensBuilder {
124 pub(crate) fn new(id: String) -> Self {
125 SemanticTokensBuilder { id, prev_line: 0, prev_char: 0, data: Default::default() }
128 /// Push a new token onto the builder
129 pub(crate) fn push(&mut self, range: Range, token_index: u32, modifier_bitset: u32) {
130 let mut push_line = range.start.line as u32;
131 let mut push_char = range.start.character as u32;
133 if !self.data.is_empty() {
134 push_line -= self.prev_line;
136 push_char -= self.prev_char;
140 // A token cannot be multiline
141 let token_len = range.end.character - range.start.character;
143 let token = SemanticToken {
144 delta_line: push_line,
145 delta_start: push_char,
146 length: token_len as u32,
147 token_type: token_index,
148 token_modifiers_bitset: modifier_bitset,
151 self.data.push(token);
153 self.prev_line = range.start.line as u32;
154 self.prev_char = range.start.character as u32;
157 pub(crate) fn build(self) -> SemanticTokens {
158 SemanticTokens { result_id: Some(self.id), data: self.data }
162 pub(crate) fn diff_tokens(old: &[SemanticToken], new: &[SemanticToken]) -> Vec<SemanticTokensEdit> {
163 let offset = new.iter().zip(old.iter()).take_while(|&(n, p)| n == p).count();
165 let (_, old) = old.split_at(offset);
166 let (_, new) = new.split_at(offset);
168 let offset_from_end =
169 new.iter().rev().zip(old.iter().rev()).take_while(|&(n, p)| n == p).count();
171 let (old, _) = old.split_at(old.len() - offset_from_end);
172 let (new, _) = new.split_at(new.len() - offset_from_end);
174 if old.is_empty() && new.is_empty() {
177 // The lsp data field is actually a byte-diff but we
178 // travel in tokens so `start` and `delete_count` are in multiples of the
179 // serialized size of `SemanticToken`.
180 vec![SemanticTokensEdit {
181 start: 5 * offset as u32,
182 delete_count: 5 * old.len() as u32,
183 data: Some(new.into()),
188 pub(crate) fn type_index(ty: SemanticTokenType) -> u32 {
189 SUPPORTED_TYPES.iter().position(|it| *it == ty).unwrap() as u32
196 fn from(t: (u32, u32, u32, u32, u32)) -> SemanticToken {
202 token_modifiers_bitset: t.4,
207 fn test_diff_insert_at_end() {
208 let before = [from((1, 2, 3, 4, 5)), from((6, 7, 8, 9, 10))];
209 let after = [from((1, 2, 3, 4, 5)), from((6, 7, 8, 9, 10)), from((11, 12, 13, 14, 15))];
211 let edits = diff_tokens(&before, &after);
217 data: Some(vec![from((11, 12, 13, 14, 15))])
223 fn test_diff_insert_at_beginning() {
224 let before = [from((1, 2, 3, 4, 5)), from((6, 7, 8, 9, 10))];
225 let after = [from((11, 12, 13, 14, 15)), from((1, 2, 3, 4, 5)), from((6, 7, 8, 9, 10))];
227 let edits = diff_tokens(&before, &after);
233 data: Some(vec![from((11, 12, 13, 14, 15))])
239 fn test_diff_insert_in_middle() {
240 let before = [from((1, 2, 3, 4, 5)), from((6, 7, 8, 9, 10))];
242 from((1, 2, 3, 4, 5)),
243 from((10, 20, 30, 40, 50)),
244 from((60, 70, 80, 90, 100)),
245 from((6, 7, 8, 9, 10)),
248 let edits = diff_tokens(&before, &after);
254 data: Some(vec![from((10, 20, 30, 40, 50)), from((60, 70, 80, 90, 100))])
260 fn test_diff_remove_from_end() {
261 let before = [from((1, 2, 3, 4, 5)), from((6, 7, 8, 9, 10)), from((11, 12, 13, 14, 15))];
262 let after = [from((1, 2, 3, 4, 5)), from((6, 7, 8, 9, 10))];
264 let edits = diff_tokens(&before, &after);
265 assert_eq!(edits[0], SemanticTokensEdit { start: 10, delete_count: 5, data: Some(vec![]) });
269 fn test_diff_remove_from_beginning() {
270 let before = [from((11, 12, 13, 14, 15)), from((1, 2, 3, 4, 5)), from((6, 7, 8, 9, 10))];
271 let after = [from((1, 2, 3, 4, 5)), from((6, 7, 8, 9, 10))];
273 let edits = diff_tokens(&before, &after);
274 assert_eq!(edits[0], SemanticTokensEdit { start: 0, delete_count: 5, data: Some(vec![]) });
278 fn test_diff_remove_from_middle() {
280 from((1, 2, 3, 4, 5)),
281 from((10, 20, 30, 40, 50)),
282 from((60, 70, 80, 90, 100)),
283 from((6, 7, 8, 9, 10)),
285 let after = [from((1, 2, 3, 4, 5)), from((6, 7, 8, 9, 10))];
287 let edits = diff_tokens(&before, &after);
288 assert_eq!(edits[0], SemanticTokensEdit { start: 5, delete_count: 10, data: Some(vec![]) });