|
4 | 4 | use lsp_types::{
|
5 | 5 | self, CreateFile, DiagnosticSeverity, DocumentChangeOperation, DocumentChanges, Documentation,
|
6 | 6 | Location, LocationLink, MarkupContent, MarkupKind, Position, Range, RenameFile, ResourceOp,
|
7 |
| - SymbolKind, TextDocumentEdit, TextDocumentIdentifier, TextDocumentItem, |
8 |
| - TextDocumentPositionParams, Url, VersionedTextDocumentIdentifier, WorkspaceEdit, |
| 7 | + SemanticTokenModifier, SemanticTokenType, SymbolKind, TextDocumentEdit, TextDocumentIdentifier, |
| 8 | + TextDocumentItem, TextDocumentPositionParams, Url, VersionedTextDocumentIdentifier, |
| 9 | + WorkspaceEdit, |
9 | 10 | };
|
10 | 11 | use ra_ide::{
|
11 |
| - translate_offset_with_edit, CompletionItem, CompletionItemKind, FileId, FilePosition, |
| 12 | + tags, translate_offset_with_edit, CompletionItem, CompletionItemKind, FileId, FilePosition, |
12 | 13 | FileRange, FileSystemEdit, Fold, FoldKind, InsertTextFormat, LineCol, LineIndex,
|
13 | 14 | NavigationTarget, RangeInfo, ReferenceAccess, Severity, SourceChange, SourceFileEdit,
|
14 | 15 | };
|
15 | 16 | use ra_syntax::{SyntaxKind, TextRange, TextUnit};
|
16 | 17 | use ra_text_edit::{AtomTextEdit, TextEdit};
|
17 | 18 | use ra_vfs::LineEndings;
|
18 | 19 |
|
19 |
| -use crate::{req, world::WorldSnapshot, Result}; |
| 20 | +use crate::{req, semantic_tokens, world::WorldSnapshot, Result}; |
20 | 21 |
|
21 | 22 | pub trait Conv {
|
22 | 23 | type Output;
|
@@ -302,6 +303,76 @@ impl ConvWith<&FoldConvCtx<'_>> for Fold {
|
302 | 303 | }
|
303 | 304 | }
|
304 | 305 |
|
| 306 | +impl Conv for &'static str { |
| 307 | + type Output = (SemanticTokenType, Vec<SemanticTokenModifier>); |
| 308 | + |
| 309 | + fn conv(self) -> (SemanticTokenType, Vec<SemanticTokenModifier>) { |
| 310 | + let token_type: SemanticTokenType = match self { |
| 311 | + tags::FIELD => SemanticTokenType::MEMBER, |
| 312 | + tags::FUNCTION => SemanticTokenType::FUNCTION, |
| 313 | + tags::MODULE => SemanticTokenType::NAMESPACE, |
| 314 | + tags::CONSTANT => { |
| 315 | + return ( |
| 316 | + SemanticTokenType::VARIABLE, |
| 317 | + vec![SemanticTokenModifier::STATIC, SemanticTokenModifier::READONLY], |
| 318 | + ) |
| 319 | + } |
| 320 | + tags::MACRO => SemanticTokenType::MACRO, |
| 321 | + |
| 322 | + tags::VARIABLE => { |
| 323 | + return (SemanticTokenType::VARIABLE, vec![SemanticTokenModifier::READONLY]) |
| 324 | + } |
| 325 | + tags::VARIABLE_MUT => SemanticTokenType::VARIABLE, |
| 326 | + |
| 327 | + tags::TYPE => SemanticTokenType::TYPE, |
| 328 | + tags::TYPE_BUILTIN => SemanticTokenType::TYPE, |
| 329 | + tags::TYPE_SELF => { |
| 330 | + return (SemanticTokenType::TYPE, vec![SemanticTokenModifier::REFERENCE]) |
| 331 | + } |
| 332 | + tags::TYPE_PARAM => SemanticTokenType::TYPE_PARAMETER, |
| 333 | + tags::TYPE_LIFETIME => { |
| 334 | + return (SemanticTokenType::LABEL, vec![SemanticTokenModifier::REFERENCE]) |
| 335 | + } |
| 336 | + |
| 337 | + tags::LITERAL_BYTE => SemanticTokenType::NUMBER, |
| 338 | + tags::LITERAL_NUMERIC => SemanticTokenType::NUMBER, |
| 339 | + tags::LITERAL_CHAR => SemanticTokenType::NUMBER, |
| 340 | + |
| 341 | + tags::LITERAL_COMMENT => { |
| 342 | + return (SemanticTokenType::COMMENT, vec![SemanticTokenModifier::DOCUMENTATION]) |
| 343 | + } |
| 344 | + |
| 345 | + tags::LITERAL_STRING => SemanticTokenType::STRING, |
| 346 | + tags::LITERAL_ATTRIBUTE => SemanticTokenType::KEYWORD, |
| 347 | + |
| 348 | + tags::KEYWORD => SemanticTokenType::KEYWORD, |
| 349 | + tags::KEYWORD_UNSAFE => SemanticTokenType::KEYWORD, |
| 350 | + tags::KEYWORD_CONTROL => SemanticTokenType::KEYWORD, |
| 351 | + unknown => panic!("Unknown semantic token: {}", unknown), |
| 352 | + }; |
| 353 | + |
| 354 | + (token_type, vec![]) |
| 355 | + } |
| 356 | +} |
| 357 | + |
| 358 | +impl Conv for (SemanticTokenType, Vec<SemanticTokenModifier>) { |
| 359 | + type Output = (u32, u32); |
| 360 | + |
| 361 | + fn conv(self) -> Self::Output { |
| 362 | + let token_index = |
| 363 | + semantic_tokens::supported_token_types().iter().position(|it| *it == self.0).unwrap(); |
| 364 | + let mut token_modifier_bitset = 0; |
| 365 | + for modifier in self.1.iter() { |
| 366 | + token_modifier_bitset |= semantic_tokens::supported_token_modifiers() |
| 367 | + .iter() |
| 368 | + .position(|it| it == modifier) |
| 369 | + .unwrap(); |
| 370 | + } |
| 371 | + |
| 372 | + (token_index as u32, token_modifier_bitset as u32) |
| 373 | + } |
| 374 | +} |
| 375 | + |
305 | 376 | impl<T: ConvWith<CTX>, CTX> ConvWith<CTX> for Option<T> {
|
306 | 377 | type Output = Option<T::Output>;
|
307 | 378 |
|
|
0 commit comments