to_proto::semantic_tokens

This commit is contained in:
Aleksey Kladov
2020-05-10 19:09:22 +02:00
parent 1586bab0b9
commit bd8422643a
2 changed files with 33 additions and 34 deletions

View File

@@ -35,7 +35,6 @@ use crate::{
diagnostics::DiagnosticTask, diagnostics::DiagnosticTask,
from_json, from_proto, from_json, from_proto,
req::{self, InlayHint, InlayHintsParams}, req::{self, InlayHint, InlayHintsParams},
semantic_tokens::SemanticTokensBuilder,
to_proto, to_proto,
world::WorldSnapshot, world::WorldSnapshot,
LspError, Result, LspError, Result,
@@ -1147,23 +1146,9 @@ pub fn handle_semantic_tokens(
let text = world.analysis().file_text(file_id)?; let text = world.analysis().file_text(file_id)?;
let line_index = world.analysis().file_line_index(file_id)?; let line_index = world.analysis().file_line_index(file_id)?;
let mut builder = SemanticTokensBuilder::default(); let highlights = world.analysis().highlight(file_id)?;
let semantic_tokens = to_proto::semantic_tokens(&text, &line_index, highlights);
for highlight_range in world.analysis().highlight(file_id)?.into_iter() { Ok(Some(semantic_tokens.into()))
let (token_index, modifier_bitset) =
to_proto::token_type_index_modifiers_bitself(highlight_range.highlight);
for mut range in line_index.lines(highlight_range.range) {
if text[range].ends_with('\n') {
range = TextRange::new(range.start(), range.end() - TextSize::of('\n'));
}
let range = to_proto::range(&line_index, range);
builder.push(range, token_index, modifier_bitset);
}
}
let tokens = builder.build();
Ok(Some(tokens.into()))
} }
pub fn handle_semantic_tokens_range( pub fn handle_semantic_tokens_range(
@@ -1173,18 +1158,10 @@ pub fn handle_semantic_tokens_range(
let _p = profile("handle_semantic_tokens_range"); let _p = profile("handle_semantic_tokens_range");
let frange = from_proto::file_range(&world, params.text_document, params.range)?; let frange = from_proto::file_range(&world, params.text_document, params.range)?;
let text = world.analysis().file_text(frange.file_id)?;
let line_index = world.analysis().file_line_index(frange.file_id)?; let line_index = world.analysis().file_line_index(frange.file_id)?;
let mut builder = SemanticTokensBuilder::default(); let highlights = world.analysis().highlight_range(frange)?;
let semantic_tokens = to_proto::semantic_tokens(&text, &line_index, highlights);
for highlight_range in world.analysis().highlight_range(frange)?.into_iter() { Ok(Some(semantic_tokens.into()))
let (token_type, token_modifiers) =
to_proto::token_type_index_modifiers_bitself(highlight_range.highlight);
let range = to_proto::range(&line_index, highlight_range.range);
builder.push(range, token_type, token_modifiers);
}
let tokens = builder.build();
Ok(Some(tokens.into()))
} }

View File

@@ -3,8 +3,8 @@ use ra_db::{FileId, FileRange};
use ra_ide::{ use ra_ide::{
translate_offset_with_edit, Assist, CompletionItem, CompletionItemKind, Documentation, translate_offset_with_edit, Assist, CompletionItem, CompletionItemKind, Documentation,
FileSystemEdit, Fold, FoldKind, FunctionSignature, Highlight, HighlightModifier, HighlightTag, FileSystemEdit, Fold, FoldKind, FunctionSignature, Highlight, HighlightModifier, HighlightTag,
InlayHint, InlayKind, InsertTextFormat, LineIndex, NavigationTarget, ReferenceAccess, Severity, HighlightedRange, InlayHint, InlayKind, InsertTextFormat, LineIndex, NavigationTarget,
SourceChange, SourceFileEdit, ReferenceAccess, Severity, SourceChange, SourceFileEdit,
}; };
use ra_syntax::{SyntaxKind, TextRange, TextSize}; use ra_syntax::{SyntaxKind, TextRange, TextSize};
use ra_text_edit::{Indel, TextEdit}; use ra_text_edit::{Indel, TextEdit};
@@ -227,8 +227,30 @@ pub(crate) fn inlay_int(line_index: &LineIndex, inlay_hint: InlayHint) -> req::I
} }
} }
// TODO: this is wrong pub(crate) fn semantic_tokens(
pub(crate) fn token_type_index_modifiers_bitself(highlight: Highlight) -> (u32, u32) { text: &str,
line_index: &LineIndex,
highlights: Vec<HighlightedRange>,
) -> lsp_types::SemanticTokens {
let mut builder = semantic_tokens::SemanticTokensBuilder::default();
for highlight_range in highlights {
let (token_index, modifier_bitset) =
token_type_index_modifiers_bitself(highlight_range.highlight);
for mut text_range in line_index.lines(highlight_range.range) {
if text[text_range].ends_with('\n') {
text_range =
TextRange::new(text_range.start(), text_range.end() - TextSize::of('\n'));
}
let range = range(&line_index, text_range);
builder.push(range, token_index, modifier_bitset);
}
}
builder.build()
}
fn token_type_index_modifiers_bitself(highlight: Highlight) -> (u32, u32) {
let mut mods = semantic_tokens::ModifierSet::default(); let mut mods = semantic_tokens::ModifierSet::default();
let type_ = match highlight.tag { let type_ = match highlight.tag {
HighlightTag::Struct => lsp_types::SemanticTokenType::STRUCT, HighlightTag::Struct => lsp_types::SemanticTokenType::STRUCT,