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