From 9b9fc135d6763b403dcf2455c35251d5c36d83e5 Mon Sep 17 00:00:00 2001 From: "Jeremy A. Kolb" Date: Wed, 24 Oct 2018 15:54:58 -0400 Subject: Simplify find_all_refs by always resolving a ast::BindPat --- crates/ra_analysis/src/imp.rs | 48 +++++++++++++++---------------------------- 1 file changed, 16 insertions(+), 32 deletions(-) diff --git a/crates/ra_analysis/src/imp.rs b/crates/ra_analysis/src/imp.rs index f950a7995..1eb8cb912 100644 --- a/crates/ra_analysis/src/imp.rs +++ b/crates/ra_analysis/src/imp.rs @@ -325,40 +325,24 @@ impl AnalysisImpl { let file = self.db.file_syntax(file_id); let syntax = file.syntax(); - let mut ret = vec![]; - - if let Some(binding) = find_node_at_offset::(syntax, offset) { - let decl = DeclarationDescriptor::new(binding); - - ret.push((file_id, decl.range)); - - ret.extend(decl.find_all_refs().into_iter() - .map(|ref_desc| (file_id, ref_desc.range ))); - - return ret; - } - - // 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(&self.db, file_id, name_ref) { - - ret.push((file_id, resolved.1)); - - if let Some(fn_def) = find_node_at_offset::(syntax, offset) { + // Find the binding associated with the offset + let maybe_binding = find_node_at_offset::(syntax, offset) + .or_else(|| { + let name_ref = find_node_at_offset::(syntax, offset)?; + let resolved = resolve_local_name(&self.db, file_id, name_ref)?; + find_node_at_offset::(syntax, resolved.1.end()) + }); + + let binding = match maybe_binding { + None => return Vec::new(), + Some(it) => it, + }; - let refs : Vec<_> = fn_def.syntax().descendants() - .filter_map(ast::NameRef::cast) - .filter(|&n: &ast::NameRef| resolve_local_name(&self.db, file_id, n) == Some(resolved.clone())) - .collect(); + let decl = DeclarationDescriptor::new(binding); - for r in refs { - ret.push((file_id, r.syntax().range())); - } - } - } - } + let mut ret = vec![(file_id, decl.range)]; + ret.extend(decl.find_all_refs().into_iter() + .map(|ref_desc| (file_id, ref_desc.range ))); ret } -- cgit v1.2.3