Handle renaming of local variables

This commit is contained in:
Jeremy A. Kolb 2018-10-18 17:56:22 -04:00
parent 3746689e9d
commit 2844c8fdfa
3 changed files with 45 additions and 2 deletions

View File

@ -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()],

View File

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

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::Rename>(handlers::handle_rename)?
.on::<req::References>(handlers::handle_references)?
.finish();
match req {