hide some scopes

This commit is contained in:
Aleksey Kladov 2019-04-13 09:31:03 +03:00
parent 58fe5598e7
commit 3aae223d93
4 changed files with 28 additions and 23 deletions

View File

@ -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<impl HirDatabase>) -> 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()];

View File

@ -173,7 +173,10 @@ impl ScopesWithSourceMap {
.unwrap_or(original_scope)
}
pub fn resolve_local_name(&self, name_ref: &ast::NameRef) -> Option<ScopeEntryWithSyntax> {
pub(crate) fn resolve_local_name(
&self,
name_ref: &ast::NameRef,
) -> Option<ScopeEntryWithSyntax> {
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<ReferenceDescriptor> {
pub(crate) fn find_all_refs(&self, pat: &ast::BindPat) -> Vec<ReferenceDescriptor> {
let fn_def = pat.syntax().ancestors().find_map(ast::FnDef::cast).unwrap();
let ptr = Either::A(AstPtr::new(pat.into()));
fn_def

View File

@ -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<Arc<crate::expr::BodySourceMap>>,
infer: Option<Arc<crate::ty::InferenceResult>>,
scopes: Option<crate::expr::ScopesWithSourceMap>,
}
#[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<Vec<ReferenceDescriptor>> {
self.scopes.as_ref().map(|it| it.find_all_refs(pat))
}
pub fn resolve_local_name(&self, name_ref: &ast::NameRef) -> Option<ScopeEntryWithSyntax> {
self.scopes.as_ref()?.resolve_local_name(name_ref)
}
#[cfg(test)]
pub(crate) fn body_source_map(&self) -> Arc<crate::expr::BodySourceMap> {
self.body_source_map.clone().unwrap()

View File

@ -61,12 +61,11 @@ pub(crate) fn find_all_refs(
position: FilePosition,
) -> Option<ReferenceSearchResult> {
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::<Vec<_>>();
@ -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::<ast::BindPat>(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::<ast::NameRef>(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