From 3746689e9ddea455d10a41d9fc3af33b22a3707d Mon Sep 17 00:00:00 2001 From: "Jeremy A. Kolb" Date: Thu, 18 Oct 2018 13:40:12 -0400 Subject: Implement Find All References for local variables --- crates/ra_lsp_server/src/caps.rs | 2 +- crates/ra_lsp_server/src/main_loop/handlers.rs | 16 ++++++++++++++++ crates/ra_lsp_server/src/main_loop/mod.rs | 1 + crates/ra_lsp_server/src/req.rs | 2 +- 4 files changed, 19 insertions(+), 2 deletions(-) (limited to 'crates/ra_lsp_server/src') diff --git a/crates/ra_lsp_server/src/caps.rs b/crates/ra_lsp_server/src/caps.rs index 1dd495791..84c43bbec 100644 --- a/crates/ra_lsp_server/src/caps.rs +++ b/crates/ra_lsp_server/src/caps.rs @@ -27,7 +27,7 @@ pub fn server_capabilities() -> ServerCapabilities { definition_provider: Some(true), type_definition_provider: None, implementation_provider: None, - references_provider: None, + references_provider: Some(true), document_highlight_provider: None, document_symbol_provider: Some(true), workspace_symbol_provider: Some(true), diff --git a/crates/ra_lsp_server/src/main_loop/handlers.rs b/crates/ra_lsp_server/src/main_loop/handlers.rs index c25b63852..9b8d40eaa 100644 --- a/crates/ra_lsp_server/src/main_loop/handlers.rs +++ b/crates/ra_lsp_server/src/main_loop/handlers.rs @@ -460,6 +460,22 @@ pub fn handle_signature_help( } } +pub fn handle_references( + world: ServerWorld, + params: req::ReferenceParams, + 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); + + let refs = world.analysis().find_all_refs(file_id, offset, &token); + + Ok(Some(refs.into_iter() + .filter_map(|r| to_location(r.0, r.1, &world, &line_index).ok()) + .collect())) +} + pub fn handle_code_action( world: ServerWorld, params: req::CodeActionParams, diff --git a/crates/ra_lsp_server/src/main_loop/mod.rs b/crates/ra_lsp_server/src/main_loop/mod.rs index a11baf4aa..7efec8a7a 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_references)? .finish(); match req { Ok((id, handle)) => { diff --git a/crates/ra_lsp_server/src/req.rs b/crates/ra_lsp_server/src/req.rs index b76bfbcbc..6cd04d84c 100644 --- a/crates/ra_lsp_server/src/req.rs +++ b/crates/ra_lsp_server/src/req.rs @@ -7,7 +7,7 @@ pub use languageserver_types::{ CompletionResponse, DocumentOnTypeFormattingParams, DocumentSymbolParams, DocumentSymbolResponse, ExecuteCommandParams, Hover, InitializeResult, PublishDiagnosticsParams, SignatureHelp, TextDocumentEdit, TextDocumentPositionParams, - TextEdit, WorkspaceSymbolParams, + TextEdit, WorkspaceSymbolParams, ReferenceParams, }; pub enum SyntaxTree {} -- cgit v1.2.3 From 2844c8fdfa1889e74ce26c71371f8af43f0f6f4e Mon Sep 17 00:00:00 2001 From: "Jeremy A. Kolb" Date: Thu, 18 Oct 2018 17:56:22 -0400 Subject: Handle renaming of local variables --- crates/ra_lsp_server/src/caps.rs | 6 ++-- crates/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(-) (limited to 'crates/ra_lsp_server/src') 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::{ 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 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::{ 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 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( .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 { -- cgit v1.2.3 From 9f9e41885cc3b5133138ba751e0cac31f1b8f223 Mon Sep 17 00:00:00 2001 From: "Jeremy A. Kolb" Date: Fri, 19 Oct 2018 15:25:10 -0400 Subject: LSP: Add support for prepareRename --- crates/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(-) (limited to 'crates/ra_lsp_server/src') diff --git a/crates/ra_lsp_server/src/main_loop/handlers.rs b/crates/ra_lsp_server/src/main_loop/handlers.rs index 3de440e1f..639fe4553 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 236746447..165f2e78f 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(); -- cgit v1.2.3