mirror of
https://github.com/rust-lang/rust-analyzer.git
synced 2025-10-02 06:41:48 +00:00
hide some scopes
This commit is contained in:
parent
58fe5598e7
commit
3aae223d93
4 changed files with 28 additions and 23 deletions
|
@ -1,7 +1,4 @@
|
||||||
use hir::{
|
use hir::db::HirDatabase;
|
||||||
db::HirDatabase,
|
|
||||||
source_binder::function_from_child_node,
|
|
||||||
};
|
|
||||||
use ra_syntax::{
|
use ra_syntax::{
|
||||||
ast::{self, AstNode, AstToken, PatKind, ExprKind},
|
ast::{self, AstNode, AstToken, PatKind, ExprKind},
|
||||||
TextRange,
|
TextRange,
|
||||||
|
@ -29,10 +26,8 @@ pub(crate) fn inline_local_varialbe(mut ctx: AssistCtx<impl HirDatabase>) -> Opt
|
||||||
} else {
|
} else {
|
||||||
let_stmt.syntax().range()
|
let_stmt.syntax().range()
|
||||||
};
|
};
|
||||||
|
let analyzer = hir::SourceAnalyzer::new(ctx.db, ctx.frange.file_id, bind_pat.syntax(), None);
|
||||||
let function = function_from_child_node(ctx.db, ctx.frange.file_id, bind_pat.syntax())?;
|
let refs = analyzer.find_all_refs(bind_pat)?;
|
||||||
let scope = function.scopes(ctx.db);
|
|
||||||
let refs = scope.find_all_refs(bind_pat);
|
|
||||||
|
|
||||||
let mut wrap_in_parens = vec![true; refs.len()];
|
let mut wrap_in_parens = vec![true; refs.len()];
|
||||||
|
|
||||||
|
|
|
@ -173,7 +173,10 @@ impl ScopesWithSourceMap {
|
||||||
.unwrap_or(original_scope)
|
.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 mut shadowed = FxHashSet::default();
|
||||||
let name = name_ref.as_name();
|
let name = name_ref.as_name();
|
||||||
let ret = self
|
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 fn_def = pat.syntax().ancestors().find_map(ast::FnDef::cast).unwrap();
|
||||||
let ptr = Either::A(AstPtr::new(pat.into()));
|
let ptr = Either::A(AstPtr::new(pat.into()));
|
||||||
fn_def
|
fn_def
|
||||||
|
|
|
@ -17,6 +17,7 @@ use ra_syntax::{
|
||||||
use crate::{
|
use crate::{
|
||||||
HirDatabase, Function, Struct, Enum, Const, Static, Either, DefWithBody,
|
HirDatabase, Function, Struct, Enum, Const, Static, Either, DefWithBody,
|
||||||
AsName, Module, HirFileId, Crate, Trait, Resolver,
|
AsName, Module, HirFileId, Crate, Trait, Resolver,
|
||||||
|
expr::scope::{ReferenceDescriptor, ScopeEntryWithSyntax},
|
||||||
ids::LocationCtx,
|
ids::LocationCtx,
|
||||||
expr, AstId
|
expr, AstId
|
||||||
};
|
};
|
||||||
|
@ -222,6 +223,7 @@ pub struct SourceAnalyzer {
|
||||||
resolver: Resolver,
|
resolver: Resolver,
|
||||||
body_source_map: Option<Arc<crate::expr::BodySourceMap>>,
|
body_source_map: Option<Arc<crate::expr::BodySourceMap>>,
|
||||||
infer: Option<Arc<crate::ty::InferenceResult>>,
|
infer: Option<Arc<crate::ty::InferenceResult>>,
|
||||||
|
scopes: Option<crate::expr::ScopesWithSourceMap>,
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Debug, Clone, PartialEq, Eq)]
|
#[derive(Debug, Clone, PartialEq, Eq)]
|
||||||
|
@ -248,6 +250,7 @@ impl SourceAnalyzer {
|
||||||
resolver: resolver_for_node(db, file_id, node, offset),
|
resolver: resolver_for_node(db, file_id, node, offset),
|
||||||
body_source_map: def_with_body.map(|it| it.body_source_map(db)),
|
body_source_map: def_with_body.map(|it| it.body_source_map(db)),
|
||||||
infer: def_with_body.map(|it| it.infer(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)
|
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)]
|
#[cfg(test)]
|
||||||
pub(crate) fn body_source_map(&self) -> Arc<crate::expr::BodySourceMap> {
|
pub(crate) fn body_source_map(&self) -> Arc<crate::expr::BodySourceMap> {
|
||||||
self.body_source_map.clone().unwrap()
|
self.body_source_map.clone().unwrap()
|
||||||
|
|
|
@ -61,12 +61,11 @@ pub(crate) fn find_all_refs(
|
||||||
position: FilePosition,
|
position: FilePosition,
|
||||||
) -> Option<ReferenceSearchResult> {
|
) -> Option<ReferenceSearchResult> {
|
||||||
let file = db.parse(position.file_id);
|
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 declaration = NavigationTarget::from_bind_pat(position.file_id, binding);
|
||||||
|
|
||||||
let references = descr
|
let references = analyzer
|
||||||
.scopes(db)
|
.find_all_refs(binding)?
|
||||||
.find_all_refs(binding)
|
|
||||||
.into_iter()
|
.into_iter()
|
||||||
.map(move |ref_desc| FileRange { file_id: position.file_id, range: ref_desc.range })
|
.map(move |ref_desc| FileRange { file_id: position.file_id, range: ref_desc.range })
|
||||||
.collect::<Vec<_>>();
|
.collect::<Vec<_>>();
|
||||||
|
@ -77,21 +76,18 @@ pub(crate) fn find_all_refs(
|
||||||
db: &RootDatabase,
|
db: &RootDatabase,
|
||||||
source_file: &'a SourceFile,
|
source_file: &'a SourceFile,
|
||||||
position: FilePosition,
|
position: FilePosition,
|
||||||
) -> Option<(&'a ast::BindPat, hir::Function)> {
|
) -> Option<(&'a ast::BindPat, hir::SourceAnalyzer)> {
|
||||||
let syntax = source_file.syntax();
|
let syntax = source_file.syntax();
|
||||||
if let Some(binding) = find_node_at_offset::<ast::BindPat>(syntax, position.offset) {
|
if let Some(binding) = find_node_at_offset::<ast::BindPat>(syntax, position.offset) {
|
||||||
let descr =
|
let analyzer = hir::SourceAnalyzer::new(db, position.file_id, binding.syntax(), None);
|
||||||
source_binder::function_from_child_node(db, position.file_id, binding.syntax())?;
|
return Some((binding, analyzer));
|
||||||
return Some((binding, descr));
|
|
||||||
};
|
};
|
||||||
let name_ref = find_node_at_offset::<ast::NameRef>(syntax, position.offset)?;
|
let name_ref = find_node_at_offset::<ast::NameRef>(syntax, position.offset)?;
|
||||||
let descr =
|
let analyzer = hir::SourceAnalyzer::new(db, position.file_id, name_ref.syntax(), None);
|
||||||
source_binder::function_from_child_node(db, position.file_id, name_ref.syntax())?;
|
let resolved = analyzer.resolve_local_name(name_ref)?;
|
||||||
let scope = descr.scopes(db);
|
|
||||||
let resolved = scope.resolve_local_name(name_ref)?;
|
|
||||||
if let Either::A(ptr) = resolved.ptr() {
|
if let Either::A(ptr) = resolved.ptr() {
|
||||||
if let ast::PatKind::BindPat(binding) = ptr.to_node(source_file).kind() {
|
if let ast::PatKind::BindPat(binding) = ptr.to_node(source_file).kind() {
|
||||||
return Some((binding, descr));
|
return Some((binding, analyzer));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
None
|
None
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue