]> git.lizzy.rs Git - rust.git/blob - crates/rust-analyzer/src/semantic_tokens.rs
576bd8adcce3404ca12cec0ecad09a5f1f061eb8
[rust.git] / crates / rust-analyzer / src / semantic_tokens.rs
1 //! Semantic Tokens helpers
2
3 use std::ops;
4
5 use lsp_types::{Range, SemanticToken, SemanticTokenModifier, SemanticTokenType, SemanticTokens};
6
7 macro_rules! define_semantic_token_types {
8     ($(($ident:ident, $string:literal)),*$(,)?) => {
9         $(pub(crate) const $ident: SemanticTokenType = SemanticTokenType::new($string);)*
10
11         pub(crate) const SUPPORTED_TYPES: &[SemanticTokenType] = &[
12             SemanticTokenType::COMMENT,
13             SemanticTokenType::KEYWORD,
14             SemanticTokenType::STRING,
15             SemanticTokenType::NUMBER,
16             SemanticTokenType::REGEXP,
17             SemanticTokenType::OPERATOR,
18             SemanticTokenType::NAMESPACE,
19             SemanticTokenType::TYPE,
20             SemanticTokenType::STRUCT,
21             SemanticTokenType::CLASS,
22             SemanticTokenType::INTERFACE,
23             SemanticTokenType::ENUM,
24             SemanticTokenType::TYPE_PARAMETER,
25             SemanticTokenType::FUNCTION,
26             SemanticTokenType::MEMBER,
27             SemanticTokenType::PROPERTY,
28             SemanticTokenType::MACRO,
29             SemanticTokenType::VARIABLE,
30             SemanticTokenType::PARAMETER,
31             SemanticTokenType::LABEL,
32             $($ident),*
33         ];
34     };
35 }
36
37 define_semantic_token_types![
38     (ATTRIBUTE, "attribute"),
39     (BOOLEAN, "boolean"),
40     (BUILTIN_TYPE, "builtinType"),
41     (ENUM_MEMBER, "enumMember"),
42     (ESCAPE_SEQUENCE, "escapeSequence"),
43     (FORMAT_SPECIFIER, "formatSpecifier"),
44     (GENERIC, "generic"),
45     (LIFETIME, "lifetime"),
46     (PUNCTUATION, "punctuation"),
47     (SELF_KEYWORD, "selfKeyword"),
48     (TYPE_ALIAS, "typeAlias"),
49     (UNION, "union"),
50     (UNRESOLVED_REFERENCE, "unresolvedReference"),
51 ];
52
53 macro_rules! define_semantic_token_modifiers {
54     ($(($ident:ident, $string:literal)),*$(,)?) => {
55         $(pub(crate) const $ident: SemanticTokenModifier = SemanticTokenModifier::new($string);)*
56
57         pub(crate) const SUPPORTED_MODIFIERS: &[SemanticTokenModifier] = &[
58             SemanticTokenModifier::DOCUMENTATION,
59             SemanticTokenModifier::DECLARATION,
60             SemanticTokenModifier::DEFINITION,
61             SemanticTokenModifier::STATIC,
62             SemanticTokenModifier::ABSTRACT,
63             SemanticTokenModifier::DEPRECATED,
64             SemanticTokenModifier::READONLY,
65             $($ident),*
66         ];
67     };
68 }
69
70 define_semantic_token_modifiers![
71     (CONSTANT, "constant"),
72     (CONTROL_FLOW, "controlFlow"),
73     (INJECTED, "injected"),
74     (MUTABLE, "mutable"),
75     (UNSAFE, "unsafe"),
76     (ATTRIBUTE_MODIFIER, "attribute"),
77 ];
78
79 #[derive(Default)]
80 pub(crate) struct ModifierSet(pub(crate) u32);
81
82 impl ops::BitOrAssign<SemanticTokenModifier> for ModifierSet {
83     fn bitor_assign(&mut self, rhs: SemanticTokenModifier) {
84         let idx = SUPPORTED_MODIFIERS.iter().position(|it| it == &rhs).unwrap();
85         self.0 |= 1 << idx;
86     }
87 }
88
89 /// Tokens are encoded relative to each other.
90 ///
91 /// This is a direct port of https://github.com/microsoft/vscode-languageserver-node/blob/f425af9de46a0187adb78ec8a46b9b2ce80c5412/server/src/sematicTokens.proposed.ts#L45
92 #[derive(Default)]
93 pub(crate) struct SemanticTokensBuilder {
94     prev_line: u32,
95     prev_char: u32,
96     data: Vec<SemanticToken>,
97 }
98
99 impl SemanticTokensBuilder {
100     /// Push a new token onto the builder
101     pub fn push(&mut self, range: Range, token_index: u32, modifier_bitset: u32) {
102         let mut push_line = range.start.line as u32;
103         let mut push_char = range.start.character as u32;
104
105         if !self.data.is_empty() {
106             push_line -= self.prev_line;
107             if push_line == 0 {
108                 push_char -= self.prev_char;
109             }
110         }
111
112         // A token cannot be multiline
113         let token_len = range.end.character - range.start.character;
114
115         let token = SemanticToken {
116             delta_line: push_line,
117             delta_start: push_char,
118             length: token_len as u32,
119             token_type: token_index,
120             token_modifiers_bitset: modifier_bitset,
121         };
122
123         self.data.push(token);
124
125         self.prev_line = range.start.line as u32;
126         self.prev_char = range.start.character as u32;
127     }
128
129     pub fn build(self) -> SemanticTokens {
130         SemanticTokens { result_id: None, data: self.data }
131     }
132 }
133
134 pub fn type_index(type_: SemanticTokenType) -> u32 {
135     SUPPORTED_TYPES.iter().position(|it| *it == type_).unwrap() as u32
136 }