mirror of
https://github.com/rust-lang/rust-analyzer.git
synced 2025-10-02 06:41:48 +00:00
slight encapsulation
This commit is contained in:
parent
f9e825d956
commit
b260641e0c
2 changed files with 13 additions and 9 deletions
|
@ -18,7 +18,7 @@ impl_arena_id!(ScopeId);
|
||||||
pub struct ExprScopes {
|
pub struct ExprScopes {
|
||||||
body: Arc<Body>,
|
body: Arc<Body>,
|
||||||
scopes: Arena<ScopeId, ScopeData>,
|
scopes: Arena<ScopeId, ScopeData>,
|
||||||
pub(crate) scope_for: FxHashMap<ExprId, ScopeId>,
|
scope_by_expr: FxHashMap<ExprId, ScopeId>,
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Debug, PartialEq, Eq)]
|
#[derive(Debug, PartialEq, Eq)]
|
||||||
|
@ -54,7 +54,7 @@ impl ExprScopes {
|
||||||
let mut scopes = ExprScopes {
|
let mut scopes = ExprScopes {
|
||||||
body: body.clone(),
|
body: body.clone(),
|
||||||
scopes: Arena::default(),
|
scopes: Arena::default(),
|
||||||
scope_for: FxHashMap::default(),
|
scope_by_expr: FxHashMap::default(),
|
||||||
};
|
};
|
||||||
let root = scopes.root_scope();
|
let root = scopes.root_scope();
|
||||||
scopes.add_params_bindings(root, body.params());
|
scopes.add_params_bindings(root, body.params());
|
||||||
|
@ -73,6 +73,14 @@ impl ExprScopes {
|
||||||
std::iter::successors(scope, move |&scope| self.scopes[scope].parent)
|
std::iter::successors(scope, move |&scope| self.scopes[scope].parent)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub(crate) fn scope_for(&self, expr: ExprId) -> Option<ScopeId> {
|
||||||
|
self.scope_by_expr.get(&expr).map(|&scope| scope)
|
||||||
|
}
|
||||||
|
|
||||||
|
pub(crate) fn scope_by_expr(&self) -> &FxHashMap<ExprId, ScopeId> {
|
||||||
|
&self.scope_by_expr
|
||||||
|
}
|
||||||
|
|
||||||
fn root_scope(&mut self) -> ScopeId {
|
fn root_scope(&mut self) -> ScopeId {
|
||||||
self.scopes.alloc(ScopeData { parent: None, entries: vec![] })
|
self.scopes.alloc(ScopeData { parent: None, entries: vec![] })
|
||||||
}
|
}
|
||||||
|
@ -99,11 +107,7 @@ impl ExprScopes {
|
||||||
}
|
}
|
||||||
|
|
||||||
fn set_scope(&mut self, node: ExprId, scope: ScopeId) {
|
fn set_scope(&mut self, node: ExprId, scope: ScopeId) {
|
||||||
self.scope_for.insert(node, scope);
|
self.scope_by_expr.insert(node, scope);
|
||||||
}
|
|
||||||
|
|
||||||
pub(crate) fn scope_for(&self, expr: ExprId) -> Option<ScopeId> {
|
|
||||||
self.scope_for.get(&expr).map(|&scope| scope)
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -368,7 +368,7 @@ fn scope_for_offset(
|
||||||
offset: TextUnit,
|
offset: TextUnit,
|
||||||
) -> Option<ScopeId> {
|
) -> Option<ScopeId> {
|
||||||
scopes
|
scopes
|
||||||
.scope_for
|
.scope_by_expr()
|
||||||
.iter()
|
.iter()
|
||||||
.filter_map(|(id, scope)| Some((source_map.expr_syntax(*id)?, scope)))
|
.filter_map(|(id, scope)| Some((source_map.expr_syntax(*id)?, scope)))
|
||||||
// find containing scope
|
// find containing scope
|
||||||
|
@ -388,7 +388,7 @@ fn adjust(
|
||||||
) -> Option<ScopeId> {
|
) -> Option<ScopeId> {
|
||||||
let r = ptr.range();
|
let r = ptr.range();
|
||||||
let child_scopes = scopes
|
let child_scopes = scopes
|
||||||
.scope_for
|
.scope_by_expr()
|
||||||
.iter()
|
.iter()
|
||||||
.filter_map(|(id, scope)| Some((source_map.expr_syntax(*id)?, scope)))
|
.filter_map(|(id, scope)| Some((source_map.expr_syntax(*id)?, scope)))
|
||||||
.map(|(ptr, scope)| (ptr.range(), scope))
|
.map(|(ptr, scope)| (ptr.range(), scope))
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue