From 3aae223d938e5a36d997c45a0f86cfcabf83b570 Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Sat, 13 Apr 2019 09:31:03 +0300 Subject: hide some scopes --- crates/ra_assists/src/inline_local_variable.rs | 11 +++-------- crates/ra_hir/src/expr/scope.rs | 7 +++++-- crates/ra_hir/src/source_binder.rs | 11 +++++++++++ crates/ra_ide_api/src/references.rs | 22 +++++++++------------- 4 files changed, 28 insertions(+), 23 deletions(-) (limited to 'crates') diff --git a/crates/ra_assists/src/inline_local_variable.rs b/crates/ra_assists/src/inline_local_variable.rs index 950c2910b..9493acec9 100644 --- a/crates/ra_assists/src/inline_local_variable.rs +++ b/crates/ra_assists/src/inline_local_variable.rs @@ -1,7 +1,4 @@ -use hir::{ - db::HirDatabase, - source_binder::function_from_child_node, -}; +use hir::db::HirDatabase; use ra_syntax::{ ast::{self, AstNode, AstToken, PatKind, ExprKind}, TextRange, @@ -29,10 +26,8 @@ pub(crate) fn inline_local_varialbe(mut ctx: AssistCtx) -> Opt } else { let_stmt.syntax().range() }; - - let function = function_from_child_node(ctx.db, ctx.frange.file_id, bind_pat.syntax())?; - let scope = function.scopes(ctx.db); - let refs = scope.find_all_refs(bind_pat); + let analyzer = hir::SourceAnalyzer::new(ctx.db, ctx.frange.file_id, bind_pat.syntax(), None); + let refs = analyzer.find_all_refs(bind_pat)?; let mut wrap_in_parens = vec![true; refs.len()]; diff --git a/crates/ra_hir/src/expr/scope.rs b/crates/ra_hir/src/expr/scope.rs index 404c979eb..a9be9fbdb 100644 --- a/crates/ra_hir/src/expr/scope.rs +++ b/crates/ra_hir/src/expr/scope.rs @@ -173,7 +173,10 @@ impl ScopesWithSourceMap { .unwrap_or(original_scope) } - pub fn resolve_local_name(&self, name_ref: &ast::NameRef) -> Option { + pub(crate) fn resolve_local_name( + &self, + name_ref: &ast::NameRef, + ) -> Option { let mut shadowed = FxHashSet::default(); let name = name_ref.as_name(); let ret = self @@ -190,7 +193,7 @@ impl ScopesWithSourceMap { }) } - pub fn find_all_refs(&self, pat: &ast::BindPat) -> Vec { + pub(crate) fn find_all_refs(&self, pat: &ast::BindPat) -> Vec { let fn_def = pat.syntax().ancestors().find_map(ast::FnDef::cast).unwrap(); let ptr = Either::A(AstPtr::new(pat.into())); fn_def diff --git a/crates/ra_hir/src/source_binder.rs b/crates/ra_hir/src/source_binder.rs index 0d2746ac0..bdb300311 100644 --- a/crates/ra_hir/src/source_binder.rs +++ b/crates/ra_hir/src/source_binder.rs @@ -17,6 +17,7 @@ use ra_syntax::{ use crate::{ HirDatabase, Function, Struct, Enum, Const, Static, Either, DefWithBody, AsName, Module, HirFileId, Crate, Trait, Resolver, + expr::scope::{ReferenceDescriptor, ScopeEntryWithSyntax}, ids::LocationCtx, expr, AstId }; @@ -222,6 +223,7 @@ pub struct SourceAnalyzer { resolver: Resolver, body_source_map: Option>, infer: Option>, + scopes: Option, } #[derive(Debug, Clone, PartialEq, Eq)] @@ -248,6 +250,7 @@ impl SourceAnalyzer { resolver: resolver_for_node(db, file_id, node, offset), body_source_map: def_with_body.map(|it| it.body_source_map(db)), infer: def_with_body.map(|it| it.infer(db)), + scopes: def_with_body.map(|it| it.scopes(db)), } } @@ -302,6 +305,14 @@ impl SourceAnalyzer { Some(res) } + pub fn find_all_refs(&self, pat: &ast::BindPat) -> Option> { + self.scopes.as_ref().map(|it| it.find_all_refs(pat)) + } + + pub fn resolve_local_name(&self, name_ref: &ast::NameRef) -> Option { + self.scopes.as_ref()?.resolve_local_name(name_ref) + } + #[cfg(test)] pub(crate) fn body_source_map(&self) -> Arc { self.body_source_map.clone().unwrap() diff --git a/crates/ra_ide_api/src/references.rs b/crates/ra_ide_api/src/references.rs index 3e30e047c..ee2c1d0f0 100644 --- a/crates/ra_ide_api/src/references.rs +++ b/crates/ra_ide_api/src/references.rs @@ -61,12 +61,11 @@ pub(crate) fn find_all_refs( position: FilePosition, ) -> Option { let file = db.parse(position.file_id); - let (binding, descr) = find_binding(db, &file, position)?; + let (binding, analyzer) = find_binding(db, &file, position)?; let declaration = NavigationTarget::from_bind_pat(position.file_id, binding); - let references = descr - .scopes(db) - .find_all_refs(binding) + let references = analyzer + .find_all_refs(binding)? .into_iter() .map(move |ref_desc| FileRange { file_id: position.file_id, range: ref_desc.range }) .collect::>(); @@ -77,21 +76,18 @@ pub(crate) fn find_all_refs( db: &RootDatabase, source_file: &'a SourceFile, position: FilePosition, - ) -> Option<(&'a ast::BindPat, hir::Function)> { + ) -> Option<(&'a ast::BindPat, hir::SourceAnalyzer)> { let syntax = source_file.syntax(); if let Some(binding) = find_node_at_offset::(syntax, position.offset) { - let descr = - source_binder::function_from_child_node(db, position.file_id, binding.syntax())?; - return Some((binding, descr)); + let analyzer = hir::SourceAnalyzer::new(db, position.file_id, binding.syntax(), None); + return Some((binding, analyzer)); }; let name_ref = find_node_at_offset::(syntax, position.offset)?; - let descr = - source_binder::function_from_child_node(db, position.file_id, name_ref.syntax())?; - let scope = descr.scopes(db); - let resolved = scope.resolve_local_name(name_ref)?; + let analyzer = hir::SourceAnalyzer::new(db, position.file_id, name_ref.syntax(), None); + let resolved = analyzer.resolve_local_name(name_ref)?; if let Either::A(ptr) = resolved.ptr() { if let ast::PatKind::BindPat(binding) = ptr.to_node(source_file).kind() { - return Some((binding, descr)); + return Some((binding, analyzer)); } } None -- cgit v1.2.3