switch to official extend selection API

This commit is contained in:
Aleksey Kladov 2019-04-21 12:13:48 +03:00
parent 31b7697cf6
commit fa12ed2b8f
13 changed files with 89 additions and 63 deletions

View file

@ -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),

View file

@ -297,6 +297,7 @@ fn on_request(
.on::<req::AnalyzerStatus>(handlers::handle_analyzer_status)?
.on::<req::SyntaxTree>(handlers::handle_syntax_tree)?
.on::<req::ExtendSelection>(handlers::handle_extend_selection)?
.on::<req::SelectionRangeRequest>(handlers::handle_selection_range)?
.on::<req::FindMatchingBrace>(handlers::handle_find_matching_brace)?
.on::<req::JoinLines>(handlers::handle_join_lines)?
.on::<req::OnEnter>(handlers::handle_on_enter)?

View file

@ -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<req::ExtendSelectionResult> {
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<Vec<req::SelectionRange>> {
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,

View file

@ -64,6 +64,28 @@ pub struct ExtendSelectionResult {
pub selections: Vec<Range>,
}
pub enum SelectionRangeRequest {}
impl Request for SelectionRangeRequest {
type Params = SelectionRangeParams;
type Result = Vec<SelectionRange>;
const METHOD: &'static str = "textDocument/selectionRange";
}
#[derive(Deserialize, Debug)]
#[serde(rename_all = "camelCase")]
pub struct SelectionRangeParams {
pub text_document: TextDocumentIdentifier,
pub positions: Vec<Position>,
}
#[derive(Serialize, Debug)]
#[serde(rename_all = "camelCase")]
pub struct SelectionRange {
pub range: Range,
pub parent: Option<Box<SelectionRange>>,
}
pub enum FindMatchingBrace {}
impl Request for FindMatchingBrace {