From 16f67ee384d5b49358de167069535af727b87dba Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Wed, 28 Nov 2018 01:11:29 +0300 Subject: move resolve_local to Scopes --- crates/ra_analysis/src/hir/function/mod.rs | 28 ++++++++++++++++++++++++++- crates/ra_analysis/src/hir/function/scope.rs | 29 +++++++++++++++++++++++++++- 2 files changed, 55 insertions(+), 2 deletions(-) (limited to 'crates/ra_analysis/src/hir/function') diff --git a/crates/ra_analysis/src/hir/function/mod.rs b/crates/ra_analysis/src/hir/function/mod.rs index 5e44a88a7..d171b6a8d 100644 --- a/crates/ra_analysis/src/hir/function/mod.rs +++ b/crates/ra_analysis/src/hir/function/mod.rs @@ -6,8 +6,8 @@ use std::{ }; use ra_syntax::{ + TextRange, TextUnit, SyntaxNodeRef, ast::{self, AstNode, DocCommentsOwner, NameOwner}, - TextRange, TextUnit, }; use crate::{ @@ -39,6 +39,32 @@ impl FunctionDescriptor { FunctionDescriptor { fn_id } } + pub(crate) fn guess_for_name_ref( + db: &impl HirDatabase, + file_id: FileId, + name_ref: ast::NameRef, + ) -> Option { + FunctionDescriptor::guess_for_node(db, file_id, name_ref.syntax()) + } + + pub(crate) fn guess_for_bind_pat( + db: &impl HirDatabase, + file_id: FileId, + bind_pat: ast::BindPat, + ) -> Option { + FunctionDescriptor::guess_for_node(db, file_id, bind_pat.syntax()) + } + + fn guess_for_node( + db: &impl HirDatabase, + file_id: FileId, + node: SyntaxNodeRef, + ) -> Option { + let fn_def = node.ancestors().find_map(ast::FnDef::cast)?; + let res = FunctionDescriptor::guess_from_source(db, file_id, fn_def); + Some(res) + } + pub(crate) fn scope(&self, db: &impl HirDatabase) -> Arc { db.fn_scopes(self.fn_id) } diff --git a/crates/ra_analysis/src/hir/function/scope.rs b/crates/ra_analysis/src/hir/function/scope.rs index b8bdebe47..76b2fea68 100644 --- a/crates/ra_analysis/src/hir/function/scope.rs +++ b/crates/ra_analysis/src/hir/function/scope.rs @@ -1,9 +1,9 @@ use rustc_hash::{FxHashMap, FxHashSet}; use ra_syntax::{ + AstNode, SmolStr, SyntaxNodeRef, TextRange, algo::generate, ast::{self, ArgListOwner, LoopBodyOwner, NameOwner}, - AstNode, SmolStr, SyntaxNodeRef, }; use crate::{ @@ -70,6 +70,27 @@ impl FnScopes { .nth(0); ret } + + pub fn find_all_refs(&self, pat: ast::BindPat) -> Vec { + let fn_def = pat.syntax().ancestors().find_map(ast::FnDef::cast).unwrap(); + let name_ptr = LocalSyntaxPtr::new(pat.syntax()); + let refs: Vec<_> = fn_def + .syntax() + .descendants() + .filter_map(ast::NameRef::cast) + .filter(|name_ref| match self.resolve_local_name(*name_ref) { + None => false, + Some(entry) => entry.ptr() == name_ptr, + }) + .map(|name_ref| ReferenceDescriptor { + name: name_ref.syntax().text().to_string(), + range: name_ref.syntax().range(), + }) + .collect(); + + refs + } + fn root_scope(&mut self) -> ScopeId { self.scopes.alloc(ScopeData { parent: None, @@ -262,6 +283,12 @@ fn compute_expr_scopes(expr: ast::Expr, scopes: &mut FnScopes, scope: ScopeId) { } } +#[derive(Debug)] +pub struct ReferenceDescriptor { + pub range: TextRange, + pub name: String, +} + #[cfg(test)] mod tests { use ra_editor::find_node_at_offset; -- cgit v1.2.3