aboutsummaryrefslogtreecommitdiff
path: root/crates/ra_lsp_server
diff options
context:
space:
mode:
authorJeremy A. Kolb <[email protected]>2018-10-18 22:56:22 +0100
committerJeremy A. Kolb <[email protected]>2018-10-18 22:56:22 +0100
commit2844c8fdfa1889e74ce26c71371f8af43f0f6f4e (patch)
tree3e38e04f74f574b9637ed72e13f4fb01c2f22aa7 /crates/ra_lsp_server
parent3746689e9ddea455d10a41d9fc3af33b22a3707d (diff)
Handle renaming of local variables
Diffstat (limited to 'crates/ra_lsp_server')
-rw-r--r--crates/ra_lsp_server/src/caps.rs6
-rw-r--r--crates/ra_lsp_server/src/main_loop/handlers.rs40
-rw-r--r--crates/ra_lsp_server/src/main_loop/mod.rs1
3 files changed, 45 insertions, 2 deletions
diff --git a/crates/ra_lsp_server/src/caps.rs b/crates/ra_lsp_server/src/caps.rs
index 84c43bbec..b6436b646 100644
--- a/crates/ra_lsp_server/src/caps.rs
+++ b/crates/ra_lsp_server/src/caps.rs
@@ -2,7 +2,7 @@ use languageserver_types::{
2 CodeActionProviderCapability, CompletionOptions, DocumentOnTypeFormattingOptions, 2 CodeActionProviderCapability, CompletionOptions, DocumentOnTypeFormattingOptions,
3 ExecuteCommandOptions, FoldingRangeProviderCapability, ServerCapabilities, 3 ExecuteCommandOptions, FoldingRangeProviderCapability, ServerCapabilities,
4 SignatureHelpOptions, TextDocumentSyncCapability, TextDocumentSyncKind, 4 SignatureHelpOptions, TextDocumentSyncCapability, TextDocumentSyncKind,
5 TextDocumentSyncOptions, 5 TextDocumentSyncOptions, RenameProviderCapability, RenameOptions
6}; 6};
7 7
8pub fn server_capabilities() -> ServerCapabilities { 8pub fn server_capabilities() -> ServerCapabilities {
@@ -40,7 +40,9 @@ pub fn server_capabilities() -> ServerCapabilities {
40 more_trigger_character: None, 40 more_trigger_character: None,
41 }), 41 }),
42 folding_range_provider: Some(FoldingRangeProviderCapability::Simple(true)), 42 folding_range_provider: Some(FoldingRangeProviderCapability::Simple(true)),
43 rename_provider: None, 43 rename_provider: Some(RenameProviderCapability::Options(RenameOptions{
44 prepare_provider: Some(true)
45 })),
44 color_provider: None, 46 color_provider: None,
45 execute_command_provider: Some(ExecuteCommandOptions { 47 execute_command_provider: Some(ExecuteCommandOptions {
46 commands: vec!["apply_code_action".to_string()], 48 commands: vec!["apply_code_action".to_string()],
diff --git a/crates/ra_lsp_server/src/main_loop/handlers.rs b/crates/ra_lsp_server/src/main_loop/handlers.rs
index 9b8d40eaa..3de440e1f 100644
--- a/crates/ra_lsp_server/src/main_loop/handlers.rs
+++ b/crates/ra_lsp_server/src/main_loop/handlers.rs
@@ -4,6 +4,7 @@ use languageserver_types::{
4 CodeActionResponse, Command, CompletionItem, CompletionItemKind, Diagnostic, 4 CodeActionResponse, Command, CompletionItem, CompletionItemKind, Diagnostic,
5 DiagnosticSeverity, DocumentSymbol, FoldingRange, FoldingRangeKind, FoldingRangeParams, 5 DiagnosticSeverity, DocumentSymbol, FoldingRange, FoldingRangeKind, FoldingRangeParams,
6 InsertTextFormat, Location, Position, SymbolInformation, TextDocumentIdentifier, TextEdit, 6 InsertTextFormat, Location, Position, SymbolInformation, TextDocumentIdentifier, TextEdit,
7 RenameParams, WorkspaceEdit
7}; 8};
8use ra_analysis::{FileId, FoldKind, JobToken, Query, RunnableKind}; 9use ra_analysis::{FileId, FoldKind, JobToken, Query, RunnableKind};
9use ra_syntax::text_utils::contains_offset_nonstrict; 10use ra_syntax::text_utils::contains_offset_nonstrict;
@@ -17,6 +18,8 @@ use crate::{
17 Result, 18 Result,
18}; 19};
19 20
21use std::collections::HashMap;
22
20pub fn handle_syntax_tree( 23pub fn handle_syntax_tree(
21 world: ServerWorld, 24 world: ServerWorld,
22 params: req::SyntaxTreeParams, 25 params: req::SyntaxTreeParams,
@@ -460,6 +463,43 @@ pub fn handle_signature_help(
460 } 463 }
461} 464}
462 465
466pub fn handle_rename(
467 world: ServerWorld,
468 params: RenameParams,
469 token: JobToken,
470) -> Result<Option<WorkspaceEdit>> {
471 let file_id = params.text_document.try_conv_with(&world)?;
472 let line_index = world.analysis().file_line_index(file_id);
473 let offset = params.position.conv_with(&line_index);
474
475 if params.new_name.is_empty() {
476 return Ok(None);
477 }
478
479 let refs = world.analysis().find_all_refs(file_id, offset, &token);
480 if refs.is_empty() {
481 return Ok(None);
482 }
483
484 let mut changes = HashMap::new();
485 for r in refs {
486 if let Ok(loc) = to_location(r.0, r.1, &world, &line_index) {
487 changes.entry(loc.uri).or_insert(Vec::new()).push(
488 TextEdit {
489 range: loc.range,
490 new_text: params.new_name.clone()
491 });
492 }
493 }
494
495 Ok(Some(WorkspaceEdit {
496 changes: Some(changes),
497
498 // TODO: return this instead if client/server support it. See #144
499 document_changes : None,
500 }))
501}
502
463pub fn handle_references( 503pub fn handle_references(
464 world: ServerWorld, 504 world: ServerWorld,
465 params: req::ReferenceParams, 505 params: req::ReferenceParams,
diff --git a/crates/ra_lsp_server/src/main_loop/mod.rs b/crates/ra_lsp_server/src/main_loop/mod.rs
index 7efec8a7a..236746447 100644
--- a/crates/ra_lsp_server/src/main_loop/mod.rs
+++ b/crates/ra_lsp_server/src/main_loop/mod.rs
@@ -248,6 +248,7 @@ fn on_request(
248 .on::<req::CodeActionRequest>(handlers::handle_code_action)? 248 .on::<req::CodeActionRequest>(handlers::handle_code_action)?
249 .on::<req::FoldingRangeRequest>(handlers::handle_folding_range)? 249 .on::<req::FoldingRangeRequest>(handlers::handle_folding_range)?
250 .on::<req::SignatureHelpRequest>(handlers::handle_signature_help)? 250 .on::<req::SignatureHelpRequest>(handlers::handle_signature_help)?
251 .on::<req::Rename>(handlers::handle_rename)?
251 .on::<req::References>(handlers::handle_references)? 252 .on::<req::References>(handlers::handle_references)?
252 .finish(); 253 .finish();
253 match req { 254 match req {