From 2844c8fdfa1889e74ce26c71371f8af43f0f6f4e Mon Sep 17 00:00:00 2001 From: "Jeremy A. Kolb" Date: Thu, 18 Oct 2018 17:56:22 -0400 Subject: [PATCH] Handle renaming of local variables --- crates/ra_lsp_server/src/caps.rs | 6 ++- .../ra_lsp_server/src/main_loop/handlers.rs | 40 +++++++++++++++++++ crates/ra_lsp_server/src/main_loop/mod.rs | 1 + 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 84c43bbec2c..b6436b646c4 100644 --- a/crates/ra_lsp_server/src/caps.rs +++ b/crates/ra_lsp_server/src/caps.rs @@ -2,7 +2,7 @@ use languageserver_types::{ CodeActionProviderCapability, CompletionOptions, DocumentOnTypeFormattingOptions, ExecuteCommandOptions, FoldingRangeProviderCapability, ServerCapabilities, SignatureHelpOptions, TextDocumentSyncCapability, TextDocumentSyncKind, - TextDocumentSyncOptions, + TextDocumentSyncOptions, RenameProviderCapability, RenameOptions }; pub fn server_capabilities() -> ServerCapabilities { @@ -40,7 +40,9 @@ pub fn server_capabilities() -> ServerCapabilities { more_trigger_character: None, }), folding_range_provider: Some(FoldingRangeProviderCapability::Simple(true)), - rename_provider: None, + rename_provider: Some(RenameProviderCapability::Options(RenameOptions{ + prepare_provider: Some(true) + })), color_provider: None, execute_command_provider: Some(ExecuteCommandOptions { 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 9b8d40eaa07..3de440e1fdc 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::{ CodeActionResponse, Command, CompletionItem, CompletionItemKind, Diagnostic, DiagnosticSeverity, DocumentSymbol, FoldingRange, FoldingRangeKind, FoldingRangeParams, InsertTextFormat, Location, Position, SymbolInformation, TextDocumentIdentifier, TextEdit, + RenameParams, WorkspaceEdit }; use ra_analysis::{FileId, FoldKind, JobToken, Query, RunnableKind}; use ra_syntax::text_utils::contains_offset_nonstrict; @@ -17,6 +18,8 @@ use crate::{ Result, }; +use std::collections::HashMap; + pub fn handle_syntax_tree( world: ServerWorld, params: req::SyntaxTreeParams, @@ -460,6 +463,43 @@ pub fn handle_signature_help( } } +pub fn handle_rename( + world: ServerWorld, + params: RenameParams, + token: JobToken, +) -> Result> { + let file_id = params.text_document.try_conv_with(&world)?; + let line_index = world.analysis().file_line_index(file_id); + let offset = params.position.conv_with(&line_index); + + if params.new_name.is_empty() { + return Ok(None); + } + + let refs = world.analysis().find_all_refs(file_id, offset, &token); + if refs.is_empty() { + return Ok(None); + } + + let mut changes = HashMap::new(); + for r in refs { + if let Ok(loc) = to_location(r.0, r.1, &world, &line_index) { + changes.entry(loc.uri).or_insert(Vec::new()).push( + TextEdit { + range: loc.range, + new_text: params.new_name.clone() + }); + } + } + + Ok(Some(WorkspaceEdit { + changes: Some(changes), + + // TODO: return this instead if client/server support it. See #144 + document_changes : None, + })) +} + pub fn handle_references( world: ServerWorld, 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 7efec8a7ae9..23674644710 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( .on::(handlers::handle_code_action)? .on::(handlers::handle_folding_range)? .on::(handlers::handle_signature_help)? + .on::(handlers::handle_rename)? .on::(handlers::handle_references)? .finish(); match req {