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_analysis/src/imp.rs | 32 ++++++++++++++++++++ crates/ra_analysis/src/lib.rs | 3 ++ crates/ra_analysis/tests/tests.rs | 42 ++++++++++++++++++++++++++ crates/ra_editor/src/scope/fn_scope.rs | 1 - 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 +- 8 files changed, 96 insertions(+), 3 deletions(-) diff --git a/crates/ra_analysis/src/imp.rs b/crates/ra_analysis/src/imp.rs index f1403cb5d..a67b1717a 100644 --- a/crates/ra_analysis/src/imp.rs +++ b/crates/ra_analysis/src/imp.rs @@ -257,6 +257,38 @@ impl AnalysisImpl { vec![] } + pub fn find_all_refs(&self, file_id: FileId, offset: TextUnit, _token: &JobToken) -> Vec<(FileId, TextRange)> { + let root = self.root(file_id); + let file = root.syntax(file_id); + let syntax = file.syntax(); + + let mut ret = vec![]; + + // Find the symbol we are looking for + if let Some(name_ref) = find_node_at_offset::(syntax, offset) { + + // We are only handing local references for now + if let Some(resolved) = resolve_local_name(&file, offset, name_ref) { + + ret.push((file_id, resolved.1)); + + if let Some(fn_def) = find_node_at_offset::(syntax, offset) { + + let refs : Vec<_> = fn_def.syntax().descendants() + .filter_map(ast::NameRef::cast) + .filter(|n: &ast::NameRef| resolve_local_name(&file, n.syntax().range().start(), *n) == Some(resolved.clone())) + .collect(); + + for r in refs { + ret.push((file_id, r.syntax().range())); + } + } + } + } + + ret + } + pub fn diagnostics(&self, file_id: FileId) -> Vec { let root = self.root(file_id); let module_tree = root.module_tree(); diff --git a/crates/ra_analysis/src/lib.rs b/crates/ra_analysis/src/lib.rs index 2eeacaabe..46cc0722b 100644 --- a/crates/ra_analysis/src/lib.rs +++ b/crates/ra_analysis/src/lib.rs @@ -217,6 +217,9 @@ impl Analysis { self.imp .approximately_resolve_symbol(file_id, offset, token) } + pub fn find_all_refs(&self, file_id: FileId, offset: TextUnit, token: &JobToken) -> Vec<(FileId, TextRange)> { + self.imp.find_all_refs(file_id, offset, token) + } pub fn parent_module(&self, file_id: FileId) -> Vec<(FileId, FileSymbol)> { self.imp.parent_module(file_id) } diff --git a/crates/ra_analysis/tests/tests.rs b/crates/ra_analysis/tests/tests.rs index e0c637d65..0c2c69ea0 100644 --- a/crates/ra_analysis/tests/tests.rs +++ b/crates/ra_analysis/tests/tests.rs @@ -10,6 +10,8 @@ use std::sync::Arc; use ra_analysis::{ Analysis, AnalysisHost, CrateGraph, CrateId, FileId, FileResolver, FnDescriptor, JobHandle, }; +use ra_syntax::TextRange; + use relative_path::{RelativePath, RelativePathBuf}; use rustc_hash::FxHashMap; use test_utils::{assert_eq_dbg, extract_offset}; @@ -225,3 +227,43 @@ fn bar() { assert_eq!(desc.ret_type, None); assert_eq!(param, Some(1)); } + +fn get_all_refs(text: &str) -> Vec<(FileId, TextRange)> { + let (offset, code) = extract_offset(text); + let code = code.as_str(); + + let (_handle, token) = JobHandle::new(); + let snap = analysis(&[("/lib.rs", code)]); + + snap.find_all_refs(FileId(1), offset, &token) +} + +#[test] +fn test_find_all_refs_for_local() { + let code = r#" + fn main() { + let mut i = 1; + let j = 1; + i = i<|> + j; + + { + i = 0; + } + + i = 5; + }"#; + + let refs = get_all_refs(code); + assert_eq!(refs.len(), 5); +} + +#[test] +fn test_find_all_refs_for_param_inside() { + let code = r#" + fn foo(i : u32) -> u32 { + i<|> + }"#; + + let refs = get_all_refs(code); + assert_eq!(refs.len(), 2); +} \ No newline at end of file diff --git a/crates/ra_editor/src/scope/fn_scope.rs b/crates/ra_editor/src/scope/fn_scope.rs index 9088e5a60..f10bdf657 100644 --- a/crates/ra_editor/src/scope/fn_scope.rs +++ b/crates/ra_editor/src/scope/fn_scope.rs @@ -270,7 +270,6 @@ pub fn resolve_local_name<'a>( .filter(|entry| shadowed.insert(entry.name())) .filter(|entry| entry.name() == name_ref.text()) .nth(0); - eprintln!("ret = {:?}", ret); ret } 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