1 //! Semantic Tokens helpers
5 use lsp_types::{Range, SemanticToken, SemanticTokenModifier, SemanticTokenType, SemanticTokens};
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");
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");
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,
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,
68 pub(crate) struct ModifierSet(pub(crate) u32);
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();
77 /// Tokens are encoded relative to each other.
79 /// This is a direct port of https://github.com/microsoft/vscode-languageserver-node/blob/f425af9de46a0187adb78ec8a46b9b2ce80c5412/server/src/sematicTokens.proposed.ts#L45
81 pub(crate) struct SemanticTokensBuilder {
84 data: Vec<SemanticToken>,
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;
93 if !self.data.is_empty() {
94 push_line -= self.prev_line;
96 push_char -= self.prev_char;
100 // A token cannot be multiline
101 let token_len = range.end.character - range.start.character;
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,
111 self.data.push(token);
113 self.prev_line = range.start.line as u32;
114 self.prev_char = range.start.character as u32;
117 pub fn build(self) -> SemanticTokens {
118 SemanticTokens { result_id: None, data: self.data }
122 pub fn type_index(type_: SemanticTokenType) -> u32 {
123 SUPPORTED_TYPES.iter().position(|it| *it == type_).unwrap() as u32