]> git.lizzy.rs Git - rust.git/blob - crates/rust-analyzer/src/semantic_tokens.rs
Merge #4161
[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     (BUILTIN_TYPE, "builtinType"),
40     (ENUM_MEMBER, "enumMember"),
41     (LIFETIME, "lifetime"),
42     (TYPE_ALIAS, "typeAlias"),
43     (UNION, "union"),
44     (UNRESOLVED_REFERENCE, "unresolvedReference"),
45     (FORMAT_SPECIFIER, "formatSpecifier"),
46 ];
47
48 macro_rules! define_semantic_token_modifiers {
49     ($(($ident:ident, $string:literal)),*$(,)?) => {
50         $(pub(crate) const $ident: SemanticTokenModifier = SemanticTokenModifier::new($string);)*
51
52         pub(crate) const SUPPORTED_MODIFIERS: &[SemanticTokenModifier] = &[
53             SemanticTokenModifier::DOCUMENTATION,
54             SemanticTokenModifier::DECLARATION,
55             SemanticTokenModifier::DEFINITION,
56             SemanticTokenModifier::STATIC,
57             SemanticTokenModifier::ABSTRACT,
58             SemanticTokenModifier::DEPRECATED,
59             SemanticTokenModifier::READONLY,
60             $($ident),*
61         ];
62     };
63 }
64
65 define_semantic_token_modifiers![
66     (CONSTANT, "constant"),
67     (CONTROL_FLOW, "controlFlow"),
68     (MUTABLE, "mutable"),
69     (UNSAFE, "unsafe"),
70 ];
71
72 #[derive(Default)]
73 pub(crate) struct ModifierSet(pub(crate) u32);
74
75 impl ops::BitOrAssign<SemanticTokenModifier> for ModifierSet {
76     fn bitor_assign(&mut self, rhs: SemanticTokenModifier) {
77         let idx = SUPPORTED_MODIFIERS.iter().position(|it| it == &rhs).unwrap();
78         self.0 |= 1 << idx;
79     }
80 }
81
82 /// Tokens are encoded relative to each other.
83 ///
84 /// This is a direct port of https://github.com/microsoft/vscode-languageserver-node/blob/f425af9de46a0187adb78ec8a46b9b2ce80c5412/server/src/sematicTokens.proposed.ts#L45
85 #[derive(Default)]
86 pub(crate) struct SemanticTokensBuilder {
87     prev_line: u32,
88     prev_char: u32,
89     data: Vec<SemanticToken>,
90 }
91
92 impl SemanticTokensBuilder {
93     /// Push a new token onto the builder
94     pub fn push(&mut self, range: Range, token_index: u32, modifier_bitset: u32) {
95         let mut push_line = range.start.line as u32;
96         let mut push_char = range.start.character as u32;
97
98         if !self.data.is_empty() {
99             push_line -= self.prev_line;
100             if push_line == 0 {
101                 push_char -= self.prev_char;
102             }
103         }
104
105         // A token cannot be multiline
106         let token_len = range.end.character - range.start.character;
107
108         let token = SemanticToken {
109             delta_line: push_line,
110             delta_start: push_char,
111             length: token_len as u32,
112             token_type: token_index,
113             token_modifiers_bitset: modifier_bitset,
114         };
115
116         self.data.push(token);
117
118         self.prev_line = range.start.line as u32;
119         self.prev_char = range.start.character as u32;
120     }
121
122     pub fn build(self) -> SemanticTokens {
123         SemanticTokens { result_id: None, data: self.data }
124     }
125 }
126
127 pub fn type_index(type_: SemanticTokenType) -> u32 {
128     SUPPORTED_TYPES.iter().position(|it| *it == type_).unwrap() as u32
129 }