From 9f9e41885cc3b5133138ba751e0cac31f1b8f223 Mon Sep 17 00:00:00 2001 From: "Jeremy A. Kolb" Date: Fri, 19 Oct 2018 15:25:10 -0400 Subject: [PATCH] LSP: Add support for prepareRename --- .../ra_lsp_server/src/main_loop/handlers.rs | 24 ++++++++++++++++++- crates/ra_lsp_server/src/main_loop/mod.rs | 1 + 2 files changed, 24 insertions(+), 1 deletion(-) diff --git a/crates/ra_lsp_server/src/main_loop/handlers.rs b/crates/ra_lsp_server/src/main_loop/handlers.rs index 3de440e1fd..639fe45537 100644 --- a/crates/ra_lsp_server/src/main_loop/handlers.rs +++ b/crates/ra_lsp_server/src/main_loop/handlers.rs @@ -4,7 +4,7 @@ use languageserver_types::{ CodeActionResponse, Command, CompletionItem, CompletionItemKind, Diagnostic, DiagnosticSeverity, DocumentSymbol, FoldingRange, FoldingRangeKind, FoldingRangeParams, InsertTextFormat, Location, Position, SymbolInformation, TextDocumentIdentifier, TextEdit, - RenameParams, WorkspaceEdit + RenameParams, WorkspaceEdit, PrepareRenameResponse }; use ra_analysis::{FileId, FoldKind, JobToken, Query, RunnableKind}; use ra_syntax::text_utils::contains_offset_nonstrict; @@ -463,6 +463,28 @@ pub fn handle_signature_help( } } +pub fn handle_prepare_rename( + world: ServerWorld, + params: req::TextDocumentPositionParams, + 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); + + // We support renaming references like handle_rename does. + // In the future we may want to reject the renaming of things like keywords here too. + let refs = world.analysis().find_all_refs(file_id, offset, &token); + if refs.is_empty() { + return Ok(None); + } + + let r = refs.first().unwrap(); + let loc = to_location(r.0, r.1, &world, &line_index)?; + + Ok(Some(PrepareRenameResponse::Range(loc.range))) +} + pub fn handle_rename( world: ServerWorld, params: RenameParams, diff --git a/crates/ra_lsp_server/src/main_loop/mod.rs b/crates/ra_lsp_server/src/main_loop/mod.rs index 2367464471..165f2e78f0 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_prepare_rename)? .on::(handlers::handle_rename)? .on::(handlers::handle_references)? .finish();