From fa12ed2b8f3466af88644e59127cd169549f8899 Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Sun, 21 Apr 2019 12:13:48 +0300 Subject: switch to official extend selection API --- crates/ra_lsp_server/src/main_loop/handlers.rs | 47 +++++++++++++++++++++++++- 1 file changed, 46 insertions(+), 1 deletion(-) (limited to 'crates/ra_lsp_server/src/main_loop') diff --git a/crates/ra_lsp_server/src/main_loop/handlers.rs b/crates/ra_lsp_server/src/main_loop/handlers.rs index eb8a53545..530081494 100644 --- a/crates/ra_lsp_server/src/main_loop/handlers.rs +++ b/crates/ra_lsp_server/src/main_loop/handlers.rs @@ -11,7 +11,7 @@ use ra_ide_api::{ FileId, FilePosition, FileRange, FoldKind, Query, RangeInfo, RunnableKind, Severity, Cancelable, AssistId, }; -use ra_syntax::{AstNode, SyntaxKind, TextUnit}; +use ra_syntax::{AstNode, SyntaxKind, TextUnit, TextRange}; use ra_prof::profile; use rustc_hash::FxHashMap; use serde::{Serialize, Deserialize}; @@ -39,10 +39,15 @@ pub fn handle_syntax_tree(world: ServerWorld, params: req::SyntaxTreeParams) -> Ok(res) } +// FIXME: drop this API pub fn handle_extend_selection( world: ServerWorld, params: req::ExtendSelectionParams, ) -> Result { + log::error!( + "extend selection is deprecated and will be removed soon, + use the new selection range API in LSP", + ); let file_id = params.text_document.try_conv_with(&world)?; let line_index = world.analysis().file_line_index(file_id); let selections = params @@ -55,6 +60,46 @@ pub fn handle_extend_selection( Ok(req::ExtendSelectionResult { selections }) } +pub fn handle_selection_range( + world: ServerWorld, + params: req::SelectionRangeParams, +) -> Result> { + let file_id = params.text_document.try_conv_with(&world)?; + let line_index = world.analysis().file_line_index(file_id); + params + .positions + .into_iter() + .map_conv_with(&line_index) + .map(|position| { + let mut ranges = Vec::new(); + { + let mut range = TextRange::from_to(position, position); + loop { + ranges.push(range); + let frange = FileRange { file_id, range }; + let next = world.analysis().extend_selection(frange)?; + if next == range { + break; + } else { + range = next + } + } + } + let mut range = req::SelectionRange { + range: ranges.last().unwrap().conv_with(&line_index), + parent: None, + }; + for r in ranges.iter().rev().skip(1) { + range = req::SelectionRange { + range: r.conv_with(&line_index), + parent: Some(Box::new(range)), + } + } + Ok(range) + }) + .collect() +} + pub fn handle_find_matching_brace( world: ServerWorld, params: req::FindMatchingBraceParams, -- cgit v1.2.3