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/Cargo.toml | 2 +- crates/ra_lsp_server/src/caps.rs | 3 +- crates/ra_lsp_server/src/main_loop.rs | 1 + crates/ra_lsp_server/src/main_loop/handlers.rs | 47 +++++++++++++++++++++++++- crates/ra_lsp_server/src/req.rs | 22 ++++++++++++ 5 files changed, 72 insertions(+), 3 deletions(-) (limited to 'crates/ra_lsp_server') diff --git a/crates/ra_lsp_server/Cargo.toml b/crates/ra_lsp_server/Cargo.toml index bc181e4eb..c855d6f68 100644 --- a/crates/ra_lsp_server/Cargo.toml +++ b/crates/ra_lsp_server/Cargo.toml @@ -15,7 +15,7 @@ crossbeam-channel = "0.3.5" flexi_logger = "0.11.0" log = "0.4.3" url_serde = "0.2.0" -lsp-types = "0.56.0" +lsp-types = "0.57.0" rustc-hash = "1.0" parking_lot = "0.7.0" diff --git a/crates/ra_lsp_server/src/caps.rs b/crates/ra_lsp_server/src/caps.rs index 2af2b89fe..f6d2b75e7 100644 --- a/crates/ra_lsp_server/src/caps.rs +++ b/crates/ra_lsp_server/src/caps.rs @@ -2,7 +2,7 @@ use lsp_types::{ CodeActionProviderCapability, CodeLensOptions, CompletionOptions, DocumentOnTypeFormattingOptions, ExecuteCommandOptions, FoldingRangeProviderCapability, RenameOptions, RenameProviderCapability, ServerCapabilities, SignatureHelpOptions, TextDocumentSyncCapability, TextDocumentSyncKind, - TextDocumentSyncOptions, ImplementationProviderCapability, + TextDocumentSyncOptions, ImplementationProviderCapability, GenericCapability, }; pub fn server_capabilities() -> ServerCapabilities { @@ -37,6 +37,7 @@ pub fn server_capabilities() -> ServerCapabilities { first_trigger_character: "=".to_string(), more_trigger_character: Some(vec![".".to_string()]), }), + selection_range_provider: Some(GenericCapability::default()), folding_range_provider: Some(FoldingRangeProviderCapability::Simple(true)), rename_provider: Some(RenameProviderCapability::Options(RenameOptions { prepare_provider: Some(true), diff --git a/crates/ra_lsp_server/src/main_loop.rs b/crates/ra_lsp_server/src/main_loop.rs index 07ac4917a..dc1f8f3f7 100644 --- a/crates/ra_lsp_server/src/main_loop.rs +++ b/crates/ra_lsp_server/src/main_loop.rs @@ -297,6 +297,7 @@ fn on_request( .on::(handlers::handle_analyzer_status)? .on::(handlers::handle_syntax_tree)? .on::(handlers::handle_extend_selection)? + .on::(handlers::handle_selection_range)? .on::(handlers::handle_find_matching_brace)? .on::(handlers::handle_join_lines)? .on::(handlers::handle_on_enter)? 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, diff --git a/crates/ra_lsp_server/src/req.rs b/crates/ra_lsp_server/src/req.rs index 4f35ab9b5..6090eb7b9 100644 --- a/crates/ra_lsp_server/src/req.rs +++ b/crates/ra_lsp_server/src/req.rs @@ -64,6 +64,28 @@ pub struct ExtendSelectionResult { pub selections: Vec, } +pub enum SelectionRangeRequest {} + +impl Request for SelectionRangeRequest { + type Params = SelectionRangeParams; + type Result = Vec; + const METHOD: &'static str = "textDocument/selectionRange"; +} + +#[derive(Deserialize, Debug)] +#[serde(rename_all = "camelCase")] +pub struct SelectionRangeParams { + pub text_document: TextDocumentIdentifier, + pub positions: Vec, +} + +#[derive(Serialize, Debug)] +#[serde(rename_all = "camelCase")] +pub struct SelectionRange { + pub range: Range, + pub parent: Option>, +} + pub enum FindMatchingBrace {} impl Request for FindMatchingBrace { -- cgit v1.2.3