Skip to content

Commit fa12ed2

Browse files
committed
switch to official extend selection API
1 parent 31b7697 commit fa12ed2

File tree

13 files changed

+89
-63
lines changed

13 files changed

+89
-63
lines changed

Cargo.lock

+14-14
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

+1
Original file line numberDiff line numberDiff line change
@@ -6,3 +6,4 @@ incremental = true
66
debug = true
77

88
[patch.'crates-io']
9+
lsp-types = { git = "https://github.com/matklad/lsp-types", branch = "selection-range" }

crates/gen_lsp_server/Cargo.toml

+1-1
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ license = "MIT OR Apache-2.0"
88
description = "Generic LSP server scaffold."
99

1010
[dependencies]
11-
lsp-types = "0.56.0"
11+
lsp-types = "0.57.0"
1212
log = "0.4.3"
1313
failure = "0.1.4"
1414
serde_json = "1.0.34"

crates/ra_lsp_server/Cargo.toml

+1-1
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ crossbeam-channel = "0.3.5"
1515
flexi_logger = "0.11.0"
1616
log = "0.4.3"
1717
url_serde = "0.2.0"
18-
lsp-types = "0.56.0"
18+
lsp-types = "0.57.0"
1919
rustc-hash = "1.0"
2020
parking_lot = "0.7.0"
2121

crates/ra_lsp_server/src/caps.rs

+2-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ use lsp_types::{
22
CodeActionProviderCapability, CodeLensOptions, CompletionOptions, DocumentOnTypeFormattingOptions,
33
ExecuteCommandOptions, FoldingRangeProviderCapability, RenameOptions, RenameProviderCapability,
44
ServerCapabilities, SignatureHelpOptions, TextDocumentSyncCapability, TextDocumentSyncKind,
5-
TextDocumentSyncOptions, ImplementationProviderCapability,
5+
TextDocumentSyncOptions, ImplementationProviderCapability, GenericCapability,
66
};
77

88
pub fn server_capabilities() -> ServerCapabilities {
@@ -37,6 +37,7 @@ pub fn server_capabilities() -> ServerCapabilities {
3737
first_trigger_character: "=".to_string(),
3838
more_trigger_character: Some(vec![".".to_string()]),
3939
}),
40+
selection_range_provider: Some(GenericCapability::default()),
4041
folding_range_provider: Some(FoldingRangeProviderCapability::Simple(true)),
4142
rename_provider: Some(RenameProviderCapability::Options(RenameOptions {
4243
prepare_provider: Some(true),

crates/ra_lsp_server/src/main_loop.rs

+1
Original file line numberDiff line numberDiff line change
@@ -297,6 +297,7 @@ fn on_request(
297297
.on::<req::AnalyzerStatus>(handlers::handle_analyzer_status)?
298298
.on::<req::SyntaxTree>(handlers::handle_syntax_tree)?
299299
.on::<req::ExtendSelection>(handlers::handle_extend_selection)?
300+
.on::<req::SelectionRangeRequest>(handlers::handle_selection_range)?
300301
.on::<req::FindMatchingBrace>(handlers::handle_find_matching_brace)?
301302
.on::<req::JoinLines>(handlers::handle_join_lines)?
302303
.on::<req::OnEnter>(handlers::handle_on_enter)?

crates/ra_lsp_server/src/main_loop/handlers.rs

+46-1
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ use ra_ide_api::{
1111
FileId, FilePosition, FileRange, FoldKind, Query, RangeInfo, RunnableKind, Severity, Cancelable,
1212
AssistId,
1313
};
14-
use ra_syntax::{AstNode, SyntaxKind, TextUnit};
14+
use ra_syntax::{AstNode, SyntaxKind, TextUnit, TextRange};
1515
use ra_prof::profile;
1616
use rustc_hash::FxHashMap;
1717
use serde::{Serialize, Deserialize};
@@ -39,10 +39,15 @@ pub fn handle_syntax_tree(world: ServerWorld, params: req::SyntaxTreeParams) ->
3939
Ok(res)
4040
}
4141

42+
// FIXME: drop this API
4243
pub fn handle_extend_selection(
4344
world: ServerWorld,
4445
params: req::ExtendSelectionParams,
4546
) -> Result<req::ExtendSelectionResult> {
47+
log::error!(
48+
"extend selection is deprecated and will be removed soon,
49+
use the new selection range API in LSP",
50+
);
4651
let file_id = params.text_document.try_conv_with(&world)?;
4752
let line_index = world.analysis().file_line_index(file_id);
4853
let selections = params
@@ -55,6 +60,46 @@ pub fn handle_extend_selection(
5560
Ok(req::ExtendSelectionResult { selections })
5661
}
5762

63+
pub fn handle_selection_range(
64+
world: ServerWorld,
65+
params: req::SelectionRangeParams,
66+
) -> Result<Vec<req::SelectionRange>> {
67+
let file_id = params.text_document.try_conv_with(&world)?;
68+
let line_index = world.analysis().file_line_index(file_id);
69+
params
70+
.positions
71+
.into_iter()
72+
.map_conv_with(&line_index)
73+
.map(|position| {
74+
let mut ranges = Vec::new();
75+
{
76+
let mut range = TextRange::from_to(position, position);
77+
loop {
78+
ranges.push(range);
79+
let frange = FileRange { file_id, range };
80+
let next = world.analysis().extend_selection(frange)?;
81+
if next == range {
82+
break;
83+
} else {
84+
range = next
85+
}
86+
}
87+
}
88+
let mut range = req::SelectionRange {
89+
range: ranges.last().unwrap().conv_with(&line_index),
90+
parent: None,
91+
};
92+
for r in ranges.iter().rev().skip(1) {
93+
range = req::SelectionRange {
94+
range: r.conv_with(&line_index),
95+
parent: Some(Box::new(range)),
96+
}
97+
}
98+
Ok(range)
99+
})
100+
.collect()
101+
}
102+
58103
pub fn handle_find_matching_brace(
59104
world: ServerWorld,
60105
params: req::FindMatchingBraceParams,

crates/ra_lsp_server/src/req.rs

+22
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,28 @@ pub struct ExtendSelectionResult {
6464
pub selections: Vec<Range>,
6565
}
6666

67+
pub enum SelectionRangeRequest {}
68+
69+
impl Request for SelectionRangeRequest {
70+
type Params = SelectionRangeParams;
71+
type Result = Vec<SelectionRange>;
72+
const METHOD: &'static str = "textDocument/selectionRange";
73+
}
74+
75+
#[derive(Deserialize, Debug)]
76+
#[serde(rename_all = "camelCase")]
77+
pub struct SelectionRangeParams {
78+
pub text_document: TextDocumentIdentifier,
79+
pub positions: Vec<Position>,
80+
}
81+
82+
#[derive(Serialize, Debug)]
83+
#[serde(rename_all = "camelCase")]
84+
pub struct SelectionRange {
85+
pub range: Range,
86+
pub parent: Option<Box<SelectionRange>>,
87+
}
88+
6789
pub enum FindMatchingBrace {}
6890

6991
impl Request for FindMatchingBrace {

editors/code/package.json

-5
Original file line numberDiff line numberDiff line change
@@ -80,11 +80,6 @@
8080
"title": "Show Syntax Tree",
8181
"category": "Rust Analyzer"
8282
},
83-
{
84-
"command": "rust-analyzer.extendSelection",
85-
"title": "Extend selection",
86-
"category": "Rust Analyzer"
87-
},
8883
{
8984
"command": "rust-analyzer.matchingBrace",
9085
"title": "Find matching brace",

editors/code/src/commands/extend_selection.ts

-34
This file was deleted.

editors/code/src/commands/index.ts

-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
import * as analyzerStatus from './analyzer_status';
22
import * as applySourceChange from './apply_source_change';
3-
import * as extendSelection from './extend_selection';
43
import * as joinLines from './join_lines';
54
import * as matchingBrace from './matching_brace';
65
import * as onEnter from './on_enter';
@@ -11,7 +10,6 @@ import * as syntaxTree from './syntaxTree';
1110
export {
1211
analyzerStatus,
1312
applySourceChange,
14-
extendSelection,
1513
joinLines,
1614
matchingBrace,
1715
parentModule,

editors/code/src/extension.ts

-4
Original file line numberDiff line numberDiff line change
@@ -57,10 +57,6 @@ export function activate(context: vscode.ExtensionContext) {
5757
registerCommand('rust-analyzer.collectGarbage', () =>
5858
Server.client.sendRequest<null>('rust-analyzer/collectGarbage', null)
5959
);
60-
registerCommand(
61-
'rust-analyzer.extendSelection',
62-
commands.extendSelection.handle
63-
);
6460
registerCommand(
6561
'rust-analyzer.matchingBrace',
6662
commands.matchingBrace.handle

editors/code/src/server.ts

+1
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,7 @@ export class Server {
7474
}
7575
}
7676
};
77+
Server.client.registerProposedFeatures();
7778
Server.client.onReady().then(() => {
7879
for (const [type, handler] of notificationHandlers) {
7980
Server.client.onNotification(type, handler);

0 commit comments

Comments
 (0)