LSP: Add support for prepareRename

This commit is contained in:
Jeremy A. Kolb 2018-10-19 15:25:10 -04:00
parent 2844c8fdfa
commit 9f9e41885c
2 changed files with 24 additions and 1 deletions

View file

@ -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<Option<PrepareRenameResponse>> {
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,

View file

@ -248,6 +248,7 @@ fn on_request(
.on::<req::CodeActionRequest>(handlers::handle_code_action)?
.on::<req::FoldingRangeRequest>(handlers::handle_folding_range)?
.on::<req::SignatureHelpRequest>(handlers::handle_signature_help)?
.on::<req::PrepareRenameRequest>(handlers::handle_prepare_rename)?
.on::<req::Rename>(handlers::handle_rename)?
.on::<req::References>(handlers::handle_references)?
.finish();