//! Semantic Tokens helpers use std::ops; use lsp_types::{Range, SemanticToken, SemanticTokenModifier, SemanticTokenType, SemanticTokens}; macro_rules! define_semantic_token_types { ($(($ident:ident, $string:literal)),*$(,)?) => { $(pub(crate) const $ident: SemanticTokenType = SemanticTokenType::new($string);)* pub(crate) const SUPPORTED_TYPES: &[SemanticTokenType] = &[ SemanticTokenType::COMMENT, SemanticTokenType::KEYWORD, SemanticTokenType::STRING, SemanticTokenType::NUMBER, SemanticTokenType::REGEXP, SemanticTokenType::OPERATOR, SemanticTokenType::NAMESPACE, SemanticTokenType::TYPE, SemanticTokenType::STRUCT, SemanticTokenType::CLASS, SemanticTokenType::INTERFACE, SemanticTokenType::ENUM, SemanticTokenType::TYPE_PARAMETER, SemanticTokenType::FUNCTION, SemanticTokenType::MEMBER, SemanticTokenType::PROPERTY, SemanticTokenType::MACRO, SemanticTokenType::VARIABLE, SemanticTokenType::PARAMETER, SemanticTokenType::LABEL, $($ident),* ]; }; } define_semantic_token_types![ (ATTRIBUTE, "attribute"), (BUILTIN_TYPE, "builtinType"), (ENUM_MEMBER, "enumMember"), (LIFETIME, "lifetime"), (TYPE_ALIAS, "typeAlias"), (UNION, "union"), (UNRESOLVED_REFERENCE, "unresolvedReference"), (FORMAT_SPECIFIER, "formatSpecifier"), ]; macro_rules! define_semantic_token_modifiers { ($(($ident:ident, $string:literal)),*$(,)?) => { $(pub(crate) const $ident: SemanticTokenModifier = SemanticTokenModifier::new($string);)* pub(crate) const SUPPORTED_MODIFIERS: &[SemanticTokenModifier] = &[ SemanticTokenModifier::DOCUMENTATION, SemanticTokenModifier::DECLARATION, SemanticTokenModifier::DEFINITION, SemanticTokenModifier::STATIC, SemanticTokenModifier::ABSTRACT, SemanticTokenModifier::DEPRECATED, SemanticTokenModifier::READONLY, $($ident),* ]; }; } define_semantic_token_modifiers![ (CONSTANT, "constant"), (CONTROL_FLOW, "controlFlow"), (MUTABLE, "mutable"), (UNSAFE, "unsafe"), ]; #[derive(Default)] pub(crate) struct ModifierSet(pub(crate) u32); impl ops::BitOrAssign for ModifierSet { fn bitor_assign(&mut self, rhs: SemanticTokenModifier) { let idx = SUPPORTED_MODIFIERS.iter().position(|it| it == &rhs).unwrap(); self.0 |= 1 << idx; } } /// Tokens are encoded relative to each other. /// /// This is a direct port of https://github.com/microsoft/vscode-languageserver-node/blob/f425af9de46a0187adb78ec8a46b9b2ce80c5412/server/src/sematicTokens.proposed.ts#L45 #[derive(Default)] pub(crate) struct SemanticTokensBuilder { prev_line: u32, prev_char: u32, data: Vec, } impl SemanticTokensBuilder { /// Push a new token onto the builder pub fn push(&mut self, range: Range, token_index: u32, modifier_bitset: u32) { let mut push_line = range.start.line as u32; let mut push_char = range.start.character as u32; if !self.data.is_empty() { push_line -= self.prev_line; if push_line == 0 { push_char -= self.prev_char; } } // A token cannot be multiline let token_len = range.end.character - range.start.character; let token = SemanticToken { delta_line: push_line, delta_start: push_char, length: token_len as u32, token_type: token_index, token_modifiers_bitset: modifier_bitset, }; self.data.push(token); self.prev_line = range.start.line as u32; self.prev_char = range.start.character as u32; } pub fn build(self) -> SemanticTokens { SemanticTokens { result_id: None, data: self.data } } } pub fn type_index(type_: SemanticTokenType) -> u32 { SUPPORTED_TYPES.iter().position(|it| *it == type_).unwrap() as u32 }