aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAleksey Kladov <[email protected]>2020-05-10 18:09:22 +0100
committerAleksey Kladov <[email protected]>2020-05-10 18:09:22 +0100
commitbd8422643ad381fa603d569d0553723c1aa4cd1e (patch)
tree64b2f5004cbcc7b2233b364fae0694ffc7de9ce1
parent1586bab0b97bef411e6187dfc389557edbc5a16e (diff)
to_proto::semantic_tokens
-rw-r--r--crates/rust-analyzer/src/main_loop/handlers.rs37
-rw-r--r--crates/rust-analyzer/src/to_proto.rs30
2 files changed, 33 insertions, 34 deletions
diff --git a/crates/rust-analyzer/src/main_loop/handlers.rs b/crates/rust-analyzer/src/main_loop/handlers.rs
index daa5b4411..4f619654f 100644
--- a/crates/rust-analyzer/src/main_loop/handlers.rs
+++ b/crates/rust-analyzer/src/main_loop/handlers.rs
@@ -35,7 +35,6 @@ use crate::{
35 diagnostics::DiagnosticTask, 35 diagnostics::DiagnosticTask,
36 from_json, from_proto, 36 from_json, from_proto,
37 req::{self, InlayHint, InlayHintsParams}, 37 req::{self, InlayHint, InlayHintsParams},
38 semantic_tokens::SemanticTokensBuilder,
39 to_proto, 38 to_proto,
40 world::WorldSnapshot, 39 world::WorldSnapshot,
41 LspError, Result, 40 LspError, Result,
@@ -1147,23 +1146,9 @@ pub fn handle_semantic_tokens(
1147 let text = world.analysis().file_text(file_id)?; 1146 let text = world.analysis().file_text(file_id)?;
1148 let line_index = world.analysis().file_line_index(file_id)?; 1147 let line_index = world.analysis().file_line_index(file_id)?;
1149 1148
1150 let mut builder = SemanticTokensBuilder::default(); 1149 let highlights = world.analysis().highlight(file_id)?;
1151 1150 let semantic_tokens = to_proto::semantic_tokens(&text, &line_index, highlights);
1152 for highlight_range in world.analysis().highlight(file_id)?.into_iter() { 1151 Ok(Some(semantic_tokens.into()))
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()))
1167} 1152}
1168 1153
1169pub fn handle_semantic_tokens_range( 1154pub fn handle_semantic_tokens_range(
@@ -1173,18 +1158,10 @@ pub fn handle_semantic_tokens_range(
1173 let _p = profile("handle_semantic_tokens_range"); 1158 let _p = profile("handle_semantic_tokens_range");
1174 1159
1175 let frange = from_proto::file_range(&world, params.text_document, params.range)?; 1160 let frange = from_proto::file_range(&world, params.text_document, params.range)?;
1161 let text = world.analysis().file_text(frange.file_id)?;
1176 let line_index = world.analysis().file_line_index(frange.file_id)?; 1162 let line_index = world.analysis().file_line_index(frange.file_id)?;
1177 1163
1178 let mut builder = SemanticTokensBuilder::default(); 1164 let highlights = world.analysis().highlight_range(frange)?;
1179 1165 let semantic_tokens = to_proto::semantic_tokens(&text, &line_index, highlights);
1180 for highlight_range in world.analysis().highlight_range(frange)?.into_iter() { 1166 Ok(Some(semantic_tokens.into()))
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()))
1190} 1167}
diff --git a/crates/rust-analyzer/src/to_proto.rs b/crates/rust-analyzer/src/to_proto.rs
index 39f2e6d4d..29b47608e 100644
--- a/crates/rust-analyzer/src/to_proto.rs
+++ b/crates/rust-analyzer/src/to_proto.rs
@@ -3,8 +3,8 @@ use ra_db::{FileId, FileRange};
3use ra_ide::{ 3use ra_ide::{
4 translate_offset_with_edit, Assist, CompletionItem, CompletionItemKind, Documentation, 4 translate_offset_with_edit, Assist, CompletionItem, CompletionItemKind, Documentation,
5 FileSystemEdit, Fold, FoldKind, FunctionSignature, Highlight, HighlightModifier, HighlightTag, 5 FileSystemEdit, Fold, FoldKind, FunctionSignature, Highlight, HighlightModifier, HighlightTag,
6 InlayHint, InlayKind, InsertTextFormat, LineIndex, NavigationTarget, ReferenceAccess, Severity, 6 HighlightedRange, InlayHint, InlayKind, InsertTextFormat, LineIndex, NavigationTarget,
7 SourceChange, SourceFileEdit, 7 ReferenceAccess, Severity, SourceChange, SourceFileEdit,
8}; 8};
9use ra_syntax::{SyntaxKind, TextRange, TextSize}; 9use ra_syntax::{SyntaxKind, TextRange, TextSize};
10use ra_text_edit::{Indel, TextEdit}; 10use ra_text_edit::{Indel, TextEdit};
@@ -227,8 +227,30 @@ pub(crate) fn inlay_int(line_index: &LineIndex, inlay_hint: InlayHint) -> req::I
227 } 227 }
228} 228}
229 229
230// TODO: this is wrong 230pub(crate) fn semantic_tokens(
231pub(crate) fn token_type_index_modifiers_bitself(highlight: Highlight) -> (u32, u32) { 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
253fn token_type_index_modifiers_bitself(highlight: Highlight) -> (u32, u32) {
232 let mut mods = semantic_tokens::ModifierSet::default(); 254 let mut mods = semantic_tokens::ModifierSet::default();
233 let type_ = match highlight.tag { 255 let type_ = match highlight.tag {
234 HighlightTag::Struct => lsp_types::SemanticTokenType::STRUCT, 256 HighlightTag::Struct => lsp_types::SemanticTokenType::STRUCT,