diff --git a/crates/ra_analysis/src/imp.rs b/crates/ra_analysis/src/imp.rs index 90184a4b95c7..517867e86708 100644 --- a/crates/ra_analysis/src/imp.rs +++ b/crates/ra_analysis/src/imp.rs @@ -9,7 +9,7 @@ use std::{ }; use relative_path::RelativePath; -use ra_editor::{self, FileSymbol, LineIndex, find_node_at_offset, LocalEdit}; +use ra_editor::{self, FileSymbol, LineIndex, find_node_at_offset, LocalEdit, resolve_local_name}; use ra_syntax::{ TextUnit, TextRange, SmolStr, File, AstNode, SyntaxKind::*, @@ -197,7 +197,21 @@ impl AnalysisImpl { let file = root.syntax(file_id); let syntax = file.syntax(); if let Some(name_ref) = find_node_at_offset::(syntax, offset) { - return self.index_resolve(name_ref, token); + + // First try to resolve the symbol locally + if let Some((name, range)) = resolve_local_name(&file, offset, name_ref) { + let mut vec = vec![]; + vec.push((file_id, FileSymbol { + name, + node_range: range, + kind : NAME + })); + + return vec; + } else { + // If that fails try the index based approach. + return self.index_resolve(name_ref, token); + } } if let Some(name) = find_node_at_offset::(syntax, offset) { if let Some(module) = name.syntax().parent().and_then(ast::Module::cast) { diff --git a/crates/ra_editor/src/lib.rs b/crates/ra_editor/src/lib.rs index a93924e0051e..2a801f7da1ec 100644 --- a/crates/ra_editor/src/lib.rs +++ b/crates/ra_editor/src/lib.rs @@ -19,7 +19,7 @@ mod scope; mod test_utils; use ra_syntax::{ - File, TextUnit, TextRange, SyntaxNodeRef, + File, TextUnit, TextRange, SmolStr, SyntaxNodeRef, ast::{self, AstNode, NameOwner}, algo::find_leaf_at_offset, SyntaxKind::{self, *}, @@ -164,6 +164,14 @@ pub fn find_node_at_offset<'a, N: AstNode<'a>>( .next() } +pub fn resolve_local_name(file: &File, offset: TextUnit, name_ref: ast::NameRef) -> Option<(SmolStr, TextRange)> { + let fn_def = find_node_at_offset::(file.syntax(), offset)?; + let scopes = scope::FnScopes::new(fn_def); + let scope_entry = scope::resolve_local_name(name_ref, &scopes)?; + let name = scope_entry.ast().name()?; + Some((scope_entry.name(), name.syntax().range())) +} + #[cfg(test)] mod tests { use super::*; diff --git a/crates/ra_editor/src/scope/fn_scope.rs b/crates/ra_editor/src/scope/fn_scope.rs index eddd874956fe..a99bd182265d 100644 --- a/crates/ra_editor/src/scope/fn_scope.rs +++ b/crates/ra_editor/src/scope/fn_scope.rs @@ -89,7 +89,7 @@ impl ScopeEntry { .unwrap() .text() } - fn ast(&self) -> ast::BindPat { + pub fn ast(&self) -> ast::BindPat { ast::BindPat::cast(self.syntax.borrowed()) .unwrap() } @@ -241,6 +241,17 @@ struct ScopeData { entries: Vec } +pub fn resolve_local_name<'a>(name_ref: ast::NameRef, scopes: &'a FnScopes) -> Option<&'a ScopeEntry> { + use std::collections::HashSet; + + let mut shadowed = HashSet::new(); + scopes.scope_chain(name_ref.syntax()) + .flat_map(|scope| scopes.entries(scope).iter()) + .filter(|entry| shadowed.insert(entry.name())) + .filter(|entry| entry.name() == name_ref.text()) + .nth(0) +} + #[cfg(test)] mod tests { use super::*; @@ -265,7 +276,7 @@ mod tests { .flat_map(|scope| scopes.entries(scope)) .map(|it| it.name()) .collect::>(); - assert_eq!(expected, actual.as_slice()); + assert_eq!(actual.as_slice(), expected); } #[test] @@ -326,4 +337,61 @@ mod tests { &["x"], ); } -} + + #[test] + fn test_shadow_variable() { + do_check(r" + fn foo(x: String) { + let x : &str = &x<|>; + }", + &["x"], + ); + } + + fn do_check_local_name(code: &str, expected_offset: u32) { + let (off, code) = extract_offset(code); + let file = File::parse(&code); + let fn_def: ast::FnDef = find_node_at_offset(file.syntax(), off).unwrap(); + let name_ref: ast::NameRef = find_node_at_offset(file.syntax(), off).unwrap(); + + let scopes = FnScopes::new(fn_def); + + let local_name = resolve_local_name(name_ref, &scopes).unwrap().ast().name().unwrap(); + + let expected_name = find_node_at_offset::(file.syntax(), expected_offset.into()).unwrap(); + assert_eq!(local_name.syntax().range(), expected_name.syntax().range()); + } + + #[test] + fn test_resolve_local_name() { + do_check_local_name(r#" + fn foo(x: i32, y: u32) { + { + let z = x * 2; + } + { + let t = x<|> * 3; + } + }"#, + 21); + } + + #[test] + fn test_resolve_local_name_declaration() { + do_check_local_name(r#" + fn foo(x: String) { + let x : &str = &x<|>; + }"#, + 21); + } + + #[test] + fn test_resolve_local_name_shadow() { + do_check_local_name(r" + fn foo(x: String) { + let x : &str = &x; + x<|> + }", + 46); + } +} \ No newline at end of file diff --git a/crates/ra_editor/src/scope/mod.rs b/crates/ra_editor/src/scope/mod.rs index 2f25230f8f32..7d6d530f787f 100644 --- a/crates/ra_editor/src/scope/mod.rs +++ b/crates/ra_editor/src/scope/mod.rs @@ -2,7 +2,7 @@ mod fn_scope; mod mod_scope; pub use self::{ - fn_scope::FnScopes, + fn_scope::{FnScopes, resolve_local_name}, mod_scope::ModuleScope, };