Skip to content

Commit

Permalink
implement proposed selectionRange API
Browse files Browse the repository at this point in the history
  • Loading branch information
matklad committed Dec 21, 2018
1 parent f1fafee commit fffa069
Show file tree
Hide file tree
Showing 9 changed files with 1,667 additions and 25 deletions.
3 changes: 3 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,6 @@ members = [ "crates/*" ]
[profile.release]
incremental = true
debug = true

[patch.crates-io]
languageserver-types = {path="../languageserver-types"}
10 changes: 10 additions & 0 deletions crates/ra_analysis/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -268,6 +268,16 @@ impl Analysis {
pub fn extend_selection(&self, file: &SourceFileNode, range: TextRange) -> TextRange {
ra_editor::extend_selection(file, range).unwrap_or(range)
}
pub fn selection_ranges(&self, position: FilePosition) -> Vec<TextRange> {
let file = self.file_syntax(position.file_id);
let mut res = Vec::new();
let mut range = TextRange::offset_len(position.offset, 0.into());
while let Some(r) = ra_editor::extend_selection(&file, range) {
res.push(r);
range = r;
}
res
}
pub fn matching_brace(&self, file: &SourceFileNode, offset: TextUnit) -> Option<TextUnit> {
ra_editor::matching_brace(file, offset)
}
Expand Down
1 change: 1 addition & 0 deletions crates/ra_lsp_server/src/caps.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ pub fn server_capabilities() -> ServerCapabilities {
more_trigger_character: None,
}),
folding_range_provider: Some(FoldingRangeProviderCapability::Simple(true)),
selection_range_provider: Some(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 @@ -250,6 +250,7 @@ fn on_request(
.on::<req::Completion>(handlers::handle_completion)?
.on::<req::CodeActionRequest>(handlers::handle_code_action)?
.on::<req::FoldingRangeRequest>(handlers::handle_folding_range)?
.on::<req::SelectionRangeRequest>(handlers::handle_selection_range)?
.on::<req::SignatureHelpRequest>(handlers::handle_signature_help)?
.on::<req::HoverRequest>(handlers::handle_hover)?
.on::<req::PrepareRenameRequest>(handlers::handle_prepare_rename)?
Expand Down
16 changes: 16 additions & 0 deletions crates/ra_lsp_server/src/main_loop/handlers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,22 @@ pub fn handle_extend_selection(
Ok(req::ExtendSelectionResult { selections })
}

pub fn handle_selection_range(
world: ServerWorld,
params: req::TextDocumentPositionParams,
) -> Result<Vec<req::SelectionRange>> {
let position = params.try_conv_with(&world)?;
let line_index = world.analysis().file_line_index(position.file_id);
let ranges = world
.analysis()
.selection_ranges(position)
.into_iter()
.map_conv_with(&line_index)
.map(|range| req::SelectionRange { range })
.collect();
Ok(ranges)
}

pub fn handle_find_matching_brace(
world: ServerWorld,
params: req::FindMatchingBraceParams,
Expand Down
14 changes: 14 additions & 0 deletions crates/ra_lsp_server/src/req.rs
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,20 @@ pub struct ExtendSelectionResult {
pub selections: Vec<Range>,
}

pub enum SelectionRangeRequest {}

impl Request for SelectionRangeRequest {
type Params = TextDocumentPositionParams;
type Result = Vec<SelectionRange>;
const METHOD: &'static str = "textDocument/selectionRange";
}

#[derive(Serialize, Debug)]
#[serde(rename_all = "camelCase")]
pub struct SelectionRange {
pub range: Range,
}

pub enum FindMatchingBrace {}

impl Request for FindMatchingBrace {
Expand Down
Loading

0 comments on commit fffa069

Please sign in to comment.