From 828bd73195a43dfba2837812c070880914001e8f Mon Sep 17 00:00:00 2001 From: "Jeremy A. Kolb" Date: Sat, 6 Oct 2018 12:02:15 -0400 Subject: Resolve local names first --- crates/ra_editor/src/lib.rs | 10 +++++----- crates/ra_editor/src/scope/fn_scope.rs | 11 +++++------ 2 files changed, 10 insertions(+), 11 deletions(-) (limited to 'crates/ra_editor/src') diff --git a/crates/ra_editor/src/lib.rs b/crates/ra_editor/src/lib.rs index fcb3e12e6..2a801f7da 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,12 +164,12 @@ pub fn find_node_at_offset<'a, N: AstNode<'a>>( .next() } -pub fn resolve_local_name<'a>(file: &'a File, offset: TextUnit, name_ref: ast::NameRef) -> Option> { +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); - - // TODO: This doesn't work because of scopes lifetime - scope::resolve_local_name(name_ref, &scopes) + 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)] diff --git a/crates/ra_editor/src/scope/fn_scope.rs b/crates/ra_editor/src/scope/fn_scope.rs index 03f9df094..67eb8e2ab 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,16 +241,15 @@ struct ScopeData { entries: Vec } -pub fn resolve_local_name<'a>(name_ref: ast::NameRef, scopes: &'a FnScopes) -> Option> { +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(); - let names = scopes.scope_chain(name_ref.syntax()) + 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)?; - names.ast().name() + .nth(0) } #[cfg(test)] @@ -365,7 +364,7 @@ mod tests { let scopes = FnScopes::new(fn_def); - let local_name = resolve_local_name(name_ref, &scopes).unwrap(); + 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()); -- cgit v1.2.3