Skip to content

Commit bd84226

Browse files
committed
to_proto::semantic_tokens
1 parent 1586bab commit bd84226

File tree

2 files changed

+33
-34
lines changed

2 files changed

+33
-34
lines changed

crates/rust-analyzer/src/main_loop/handlers.rs

Lines changed: 7 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,6 @@ use crate::{
3535
diagnostics::DiagnosticTask,
3636
from_json, from_proto,
3737
req::{self, InlayHint, InlayHintsParams},
38-
semantic_tokens::SemanticTokensBuilder,
3938
to_proto,
4039
world::WorldSnapshot,
4140
LspError, Result,
@@ -1147,23 +1146,9 @@ pub fn handle_semantic_tokens(
11471146
let text = world.analysis().file_text(file_id)?;
11481147
let line_index = world.analysis().file_line_index(file_id)?;
11491148

1150-
let mut builder = SemanticTokensBuilder::default();
1151-
1152-
for highlight_range in world.analysis().highlight(file_id)?.into_iter() {
1153-
let (token_index, modifier_bitset) =
1154-
to_proto::token_type_index_modifiers_bitself(highlight_range.highlight);
1155-
for mut range in line_index.lines(highlight_range.range) {
1156-
if text[range].ends_with('\n') {
1157-
range = TextRange::new(range.start(), range.end() - TextSize::of('\n'));
1158-
}
1159-
let range = to_proto::range(&line_index, range);
1160-
builder.push(range, token_index, modifier_bitset);
1161-
}
1162-
}
1163-
1164-
let tokens = builder.build();
1165-
1166-
Ok(Some(tokens.into()))
1149+
let highlights = world.analysis().highlight(file_id)?;
1150+
let semantic_tokens = to_proto::semantic_tokens(&text, &line_index, highlights);
1151+
Ok(Some(semantic_tokens.into()))
11671152
}
11681153

11691154
pub fn handle_semantic_tokens_range(
@@ -1173,18 +1158,10 @@ pub fn handle_semantic_tokens_range(
11731158
let _p = profile("handle_semantic_tokens_range");
11741159

11751160
let frange = from_proto::file_range(&world, params.text_document, params.range)?;
1161+
let text = world.analysis().file_text(frange.file_id)?;
11761162
let line_index = world.analysis().file_line_index(frange.file_id)?;
11771163

1178-
let mut builder = SemanticTokensBuilder::default();
1179-
1180-
for highlight_range in world.analysis().highlight_range(frange)?.into_iter() {
1181-
let (token_type, token_modifiers) =
1182-
to_proto::token_type_index_modifiers_bitself(highlight_range.highlight);
1183-
let range = to_proto::range(&line_index, highlight_range.range);
1184-
builder.push(range, token_type, token_modifiers);
1185-
}
1186-
1187-
let tokens = builder.build();
1188-
1189-
Ok(Some(tokens.into()))
1164+
let highlights = world.analysis().highlight_range(frange)?;
1165+
let semantic_tokens = to_proto::semantic_tokens(&text, &line_index, highlights);
1166+
Ok(Some(semantic_tokens.into()))
11901167
}

crates/rust-analyzer/src/to_proto.rs

Lines changed: 26 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,8 @@ use ra_db::{FileId, FileRange};
33
use ra_ide::{
44
translate_offset_with_edit, Assist, CompletionItem, CompletionItemKind, Documentation,
55
FileSystemEdit, Fold, FoldKind, FunctionSignature, Highlight, HighlightModifier, HighlightTag,
6-
InlayHint, InlayKind, InsertTextFormat, LineIndex, NavigationTarget, ReferenceAccess, Severity,
7-
SourceChange, SourceFileEdit,
6+
HighlightedRange, InlayHint, InlayKind, InsertTextFormat, LineIndex, NavigationTarget,
7+
ReferenceAccess, Severity, SourceChange, SourceFileEdit,
88
};
99
use ra_syntax::{SyntaxKind, TextRange, TextSize};
1010
use ra_text_edit::{Indel, TextEdit};
@@ -227,8 +227,30 @@ pub(crate) fn inlay_int(line_index: &LineIndex, inlay_hint: InlayHint) -> req::I
227227
}
228228
}
229229

230-
// TODO: this is wrong
231-
pub(crate) fn token_type_index_modifiers_bitself(highlight: Highlight) -> (u32, u32) {
230+
pub(crate) fn semantic_tokens(
231+
text: &str,
232+
line_index: &LineIndex,
233+
highlights: Vec<HighlightedRange>,
234+
) -> lsp_types::SemanticTokens {
235+
let mut builder = semantic_tokens::SemanticTokensBuilder::default();
236+
237+
for highlight_range in highlights {
238+
let (token_index, modifier_bitset) =
239+
token_type_index_modifiers_bitself(highlight_range.highlight);
240+
for mut text_range in line_index.lines(highlight_range.range) {
241+
if text[text_range].ends_with('\n') {
242+
text_range =
243+
TextRange::new(text_range.start(), text_range.end() - TextSize::of('\n'));
244+
}
245+
let range = range(&line_index, text_range);
246+
builder.push(range, token_index, modifier_bitset);
247+
}
248+
}
249+
250+
builder.build()
251+
}
252+
253+
fn token_type_index_modifiers_bitself(highlight: Highlight) -> (u32, u32) {
232254
let mut mods = semantic_tokens::ModifierSet::default();
233255
let type_ = match highlight.tag {
234256
HighlightTag::Struct => lsp_types::SemanticTokenType::STRUCT,

0 commit comments

Comments
 (0)