Skip to content

Commit

Permalink
switch to official extend selection API
Browse files Browse the repository at this point in the history
  • Loading branch information
matklad committed Apr 21, 2019
1 parent 31b7697 commit fa12ed2
Show file tree
Hide file tree
Showing 13 changed files with 89 additions and 63 deletions.
28 changes: 14 additions & 14 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,4 @@ incremental = true
debug = true

[patch.'crates-io']
lsp-types = { git = "https://github.com/matklad/lsp-types", branch = "selection-range" }
2 changes: 1 addition & 1 deletion crates/gen_lsp_server/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ license = "MIT OR Apache-2.0"
description = "Generic LSP server scaffold."

[dependencies]
lsp-types = "0.56.0"
lsp-types = "0.57.0"
log = "0.4.3"
failure = "0.1.4"
serde_json = "1.0.34"
Expand Down
2 changes: 1 addition & 1 deletion crates/ra_lsp_server/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"

Expand Down
3 changes: 2 additions & 1 deletion crates/ra_lsp_server/src/caps.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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),
Expand Down
1 change: 1 addition & 0 deletions crates/ra_lsp_server/src/main_loop.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)?
Expand Down
47 changes: 46 additions & 1 deletion crates/ra_lsp_server/src/main_loop/handlers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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};
Expand Down Expand Up @@ -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
Expand All @@ -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,
Expand Down
22 changes: 22 additions & 0 deletions crates/ra_lsp_server/src/req.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
5 changes: 0 additions & 5 deletions editors/code/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -80,11 +80,6 @@
"title": "Show Syntax Tree",
"category": "Rust Analyzer"
},
{
"command": "rust-analyzer.extendSelection",
"title": "Extend selection",
"category": "Rust Analyzer"
},
{
"command": "rust-analyzer.matchingBrace",
"title": "Find matching brace",
Expand Down
34 changes: 0 additions & 34 deletions editors/code/src/commands/extend_selection.ts

This file was deleted.

2 changes: 0 additions & 2 deletions editors/code/src/commands/index.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import * as analyzerStatus from './analyzer_status';
import * as applySourceChange from './apply_source_change';
import * as extendSelection from './extend_selection';
import * as joinLines from './join_lines';
import * as matchingBrace from './matching_brace';
import * as onEnter from './on_enter';
Expand All @@ -11,7 +10,6 @@ import * as syntaxTree from './syntaxTree';
export {
analyzerStatus,
applySourceChange,
extendSelection,
joinLines,
matchingBrace,
parentModule,
Expand Down
4 changes: 0 additions & 4 deletions editors/code/src/extension.ts
Original file line number Diff line number Diff line change
Expand Up @@ -57,10 +57,6 @@ export function activate(context: vscode.ExtensionContext) {
registerCommand('rust-analyzer.collectGarbage', () =>
Server.client.sendRequest<null>('rust-analyzer/collectGarbage', null)
);
registerCommand(
'rust-analyzer.extendSelection',
commands.extendSelection.handle
);
registerCommand(
'rust-analyzer.matchingBrace',
commands.matchingBrace.handle
Expand Down
1 change: 1 addition & 0 deletions editors/code/src/server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,7 @@ export class Server {
}
}
};
Server.client.registerProposedFeatures();
Server.client.onReady().then(() => {
for (const [type, handler] of notificationHandlers) {
Server.client.onNotification(type, handler);
Expand Down

0 comments on commit fa12ed2

Please sign in to comment.