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