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::TYPE_PARAMETER,
28 SemanticTokenType::FUNCTION,
29 SemanticTokenType::MEMBER,
30 SemanticTokenType::PROPERTY,
31 SemanticTokenType::MACRO,
32 SemanticTokenType::VARIABLE,
33 SemanticTokenType::PARAMETER,
34 SemanticTokenType::LABEL,
40 define_semantic_token_types![
41 (ATTRIBUTE, "attribute"),
43 (BUILTIN_TYPE, "builtinType"),
44 (ENUM_MEMBER, "enumMember"),
45 (ESCAPE_SEQUENCE, "escapeSequence"),
46 (FORMAT_SPECIFIER, "formatSpecifier"),
48 (LIFETIME, "lifetime"),
49 (PUNCTUATION, "punctuation"),
50 (SELF_KEYWORD, "selfKeyword"),
51 (TYPE_ALIAS, "typeAlias"),
53 (UNRESOLVED_REFERENCE, "unresolvedReference"),
56 macro_rules! define_semantic_token_modifiers {
57 ($(($ident:ident, $string:literal)),*$(,)?) => {
58 $(pub(crate) const $ident: SemanticTokenModifier = SemanticTokenModifier::new($string);)*
60 pub(crate) const SUPPORTED_MODIFIERS: &[SemanticTokenModifier] = &[
61 SemanticTokenModifier::DOCUMENTATION,
62 SemanticTokenModifier::DECLARATION,
63 SemanticTokenModifier::DEFINITION,
64 SemanticTokenModifier::STATIC,
65 SemanticTokenModifier::ABSTRACT,
66 SemanticTokenModifier::DEPRECATED,
67 SemanticTokenModifier::READONLY,
73 define_semantic_token_modifiers![
74 (CONSTANT, "constant"),
75 (CONTROL_FLOW, "controlFlow"),
76 (INJECTED, "injected"),
79 (ATTRIBUTE_MODIFIER, "attribute"),
83 pub(crate) struct ModifierSet(pub(crate) u32);
85 impl ops::BitOrAssign<SemanticTokenModifier> for ModifierSet {
86 fn bitor_assign(&mut self, rhs: SemanticTokenModifier) {
87 let idx = SUPPORTED_MODIFIERS.iter().position(|it| it == &rhs).unwrap();
92 /// Tokens are encoded relative to each other.
94 /// This is a direct port of https://github.com/microsoft/vscode-languageserver-node/blob/f425af9de46a0187adb78ec8a46b9b2ce80c5412/server/src/sematicTokens.proposed.ts#L45
95 pub(crate) struct SemanticTokensBuilder {
99 data: Vec<SemanticToken>,
102 impl SemanticTokensBuilder {
103 pub fn new(id: String) -> Self {
104 SemanticTokensBuilder { id, prev_line: 0, prev_char: 0, data: Default::default() }
107 /// Push a new token onto the builder
108 pub fn push(&mut self, range: Range, token_index: u32, modifier_bitset: u32) {
109 let mut push_line = range.start.line as u32;
110 let mut push_char = range.start.character as u32;
112 if !self.data.is_empty() {
113 push_line -= self.prev_line;
115 push_char -= self.prev_char;
119 // A token cannot be multiline
120 let token_len = range.end.character - range.start.character;
122 let token = SemanticToken {
123 delta_line: push_line,
124 delta_start: push_char,
125 length: token_len as u32,
126 token_type: token_index,
127 token_modifiers_bitset: modifier_bitset,
130 self.data.push(token);
132 self.prev_line = range.start.line as u32;
133 self.prev_char = range.start.character as u32;
136 pub fn build(self) -> SemanticTokens {
137 SemanticTokens { result_id: Some(self.id), data: self.data }
141 pub fn diff_tokens(old: &[SemanticToken], new: &[SemanticToken]) -> Vec<SemanticTokensEdit> {
142 let offset = new.iter().zip(old.iter()).take_while(|&(n, p)| n == p).count();
144 let (_, old) = old.split_at(offset);
145 let (_, new) = new.split_at(offset);
147 let offset_from_end =
148 new.iter().rev().zip(old.iter().rev()).take_while(|&(n, p)| n == p).count();
150 let (old, _) = old.split_at(old.len() - offset_from_end);
151 let (new, _) = new.split_at(new.len() - offset_from_end);
153 if old.is_empty() && new.is_empty() {
156 // The lsp data field is actually a byte-diff but we
157 // travel in tokens so `start` and `delete_count` are in multiples of the
158 // serialized size of `SemanticToken`.
159 vec![SemanticTokensEdit {
160 start: 5 * offset as u32,
161 delete_count: 5 * old.len() as u32,
162 data: Some(new.into()),
167 pub fn type_index(type_: SemanticTokenType) -> u32 {
168 SUPPORTED_TYPES.iter().position(|it| *it == type_).unwrap() as u32
175 fn from(t: (u32, u32, u32, u32, u32)) -> SemanticToken {
181 token_modifiers_bitset: t.4,
186 fn test_diff_insert_at_end() {
187 let before = [from((1, 2, 3, 4, 5)), from((6, 7, 8, 9, 10))];
188 let after = [from((1, 2, 3, 4, 5)), from((6, 7, 8, 9, 10)), from((11, 12, 13, 14, 15))];
190 let edits = diff_tokens(&before, &after);
196 data: Some(vec![from((11, 12, 13, 14, 15))])
202 fn test_diff_insert_at_beginning() {
203 let before = [from((1, 2, 3, 4, 5)), from((6, 7, 8, 9, 10))];
204 let after = [from((11, 12, 13, 14, 15)), from((1, 2, 3, 4, 5)), from((6, 7, 8, 9, 10))];
206 let edits = diff_tokens(&before, &after);
212 data: Some(vec![from((11, 12, 13, 14, 15))])
218 fn test_diff_insert_in_middle() {
219 let before = [from((1, 2, 3, 4, 5)), from((6, 7, 8, 9, 10))];
221 from((1, 2, 3, 4, 5)),
222 from((10, 20, 30, 40, 50)),
223 from((60, 70, 80, 90, 100)),
224 from((6, 7, 8, 9, 10)),
227 let edits = diff_tokens(&before, &after);
233 data: Some(vec![from((10, 20, 30, 40, 50)), from((60, 70, 80, 90, 100))])
239 fn test_diff_remove_from_end() {
240 let before = [from((1, 2, 3, 4, 5)), from((6, 7, 8, 9, 10)), from((11, 12, 13, 14, 15))];
241 let after = [from((1, 2, 3, 4, 5)), from((6, 7, 8, 9, 10))];
243 let edits = diff_tokens(&before, &after);
244 assert_eq!(edits[0], SemanticTokensEdit { start: 10, delete_count: 5, data: Some(vec![]) });
248 fn test_diff_remove_from_beginning() {
249 let before = [from((11, 12, 13, 14, 15)), from((1, 2, 3, 4, 5)), from((6, 7, 8, 9, 10))];
250 let after = [from((1, 2, 3, 4, 5)), from((6, 7, 8, 9, 10))];
252 let edits = diff_tokens(&before, &after);
253 assert_eq!(edits[0], SemanticTokensEdit { start: 0, delete_count: 5, data: Some(vec![]) });
257 fn test_diff_remove_from_middle() {
259 from((1, 2, 3, 4, 5)),
260 from((10, 20, 30, 40, 50)),
261 from((60, 70, 80, 90, 100)),
262 from((6, 7, 8, 9, 10)),
264 let after = [from((1, 2, 3, 4, 5)), from((6, 7, 8, 9, 10))];
266 let edits = diff_tokens(&before, &after);
267 assert_eq!(edits[0], SemanticTokensEdit { start: 5, delete_count: 10, data: Some(vec![]) });