mirror of
https://github.com/rust-lang/rust-analyzer.git
synced 2025-09-30 13:51:31 +00:00
Sourcify some more
This commit is contained in:
parent
920848940a
commit
bd8af6a413
2 changed files with 23 additions and 13 deletions
|
@ -141,13 +141,16 @@ impl SourceAnalyzer {
|
||||||
node: &SyntaxNode,
|
node: &SyntaxNode,
|
||||||
offset: Option<TextUnit>,
|
offset: Option<TextUnit>,
|
||||||
) -> SourceAnalyzer {
|
) -> SourceAnalyzer {
|
||||||
let def_with_body = def_with_body_from_child_node(db, Source::new(file_id.into(), node));
|
let node_source = Source::new(file_id.into(), node);
|
||||||
|
let def_with_body = def_with_body_from_child_node(db, node_source);
|
||||||
if let Some(def) = def_with_body {
|
if let Some(def) = def_with_body {
|
||||||
let source_map = def.body_source_map(db);
|
let source_map = def.body_source_map(db);
|
||||||
let scopes = def.expr_scopes(db);
|
let scopes = def.expr_scopes(db);
|
||||||
let scope = match offset {
|
let scope = match offset {
|
||||||
None => scope_for(&scopes, &source_map, file_id.into(), &node),
|
None => scope_for(&scopes, &source_map, node_source),
|
||||||
Some(offset) => scope_for_offset(&scopes, &source_map, file_id.into(), offset),
|
Some(offset) => {
|
||||||
|
scope_for_offset(&scopes, &source_map, Source::new(file_id.into(), offset))
|
||||||
|
}
|
||||||
};
|
};
|
||||||
let resolver = expr::resolver_for_scope(db, def, scope);
|
let resolver = expr::resolver_for_scope(db, def, scope);
|
||||||
SourceAnalyzer {
|
SourceAnalyzer {
|
||||||
|
@ -287,7 +290,8 @@ impl SourceAnalyzer {
|
||||||
let name = name_ref.as_name();
|
let name = name_ref.as_name();
|
||||||
let source_map = self.body_source_map.as_ref()?;
|
let source_map = self.body_source_map.as_ref()?;
|
||||||
let scopes = self.scopes.as_ref()?;
|
let scopes = self.scopes.as_ref()?;
|
||||||
let scope = scope_for(scopes, source_map, self.file_id.into(), name_ref.syntax())?;
|
let scope =
|
||||||
|
scope_for(scopes, source_map, Source::new(self.file_id.into(), name_ref.syntax()))?;
|
||||||
let entry = scopes.resolve_name_in_scope(scope, &name)?;
|
let entry = scopes.resolve_name_in_scope(scope, &name)?;
|
||||||
Some(ScopeEntryWithSyntax {
|
Some(ScopeEntryWithSyntax {
|
||||||
name: entry.name().clone(),
|
name: entry.name().clone(),
|
||||||
|
@ -408,20 +412,19 @@ impl SourceAnalyzer {
|
||||||
fn scope_for(
|
fn scope_for(
|
||||||
scopes: &ExprScopes,
|
scopes: &ExprScopes,
|
||||||
source_map: &BodySourceMap,
|
source_map: &BodySourceMap,
|
||||||
file_id: HirFileId,
|
node: Source<&SyntaxNode>,
|
||||||
node: &SyntaxNode,
|
|
||||||
) -> Option<ScopeId> {
|
) -> Option<ScopeId> {
|
||||||
node.ancestors()
|
node.ast
|
||||||
|
.ancestors()
|
||||||
.filter_map(ast::Expr::cast)
|
.filter_map(ast::Expr::cast)
|
||||||
.filter_map(|it| source_map.node_expr(Source { file_id, ast: &it }))
|
.filter_map(|it| source_map.node_expr(Source::new(node.file_id, &it)))
|
||||||
.find_map(|it| scopes.scope_for(it))
|
.find_map(|it| scopes.scope_for(it))
|
||||||
}
|
}
|
||||||
|
|
||||||
fn scope_for_offset(
|
fn scope_for_offset(
|
||||||
scopes: &ExprScopes,
|
scopes: &ExprScopes,
|
||||||
source_map: &BodySourceMap,
|
source_map: &BodySourceMap,
|
||||||
file_id: HirFileId,
|
offset: Source<TextUnit>,
|
||||||
offset: TextUnit,
|
|
||||||
) -> Option<ScopeId> {
|
) -> Option<ScopeId> {
|
||||||
scopes
|
scopes
|
||||||
.scope_by_expr()
|
.scope_by_expr()
|
||||||
|
@ -429,7 +432,7 @@ fn scope_for_offset(
|
||||||
.filter_map(|(id, scope)| {
|
.filter_map(|(id, scope)| {
|
||||||
let source = source_map.expr_syntax(*id)?;
|
let source = source_map.expr_syntax(*id)?;
|
||||||
// FIXME: correctly handle macro expansion
|
// FIXME: correctly handle macro expansion
|
||||||
if source.file_id != file_id {
|
if source.file_id != offset.file_id {
|
||||||
return None;
|
return None;
|
||||||
}
|
}
|
||||||
let syntax_node_ptr =
|
let syntax_node_ptr =
|
||||||
|
@ -438,9 +441,14 @@ fn scope_for_offset(
|
||||||
})
|
})
|
||||||
// find containing scope
|
// find containing scope
|
||||||
.min_by_key(|(ptr, _scope)| {
|
.min_by_key(|(ptr, _scope)| {
|
||||||
(!(ptr.range().start() <= offset && offset <= ptr.range().end()), ptr.range().len())
|
(
|
||||||
|
!(ptr.range().start() <= offset.ast && offset.ast <= ptr.range().end()),
|
||||||
|
ptr.range().len(),
|
||||||
|
)
|
||||||
|
})
|
||||||
|
.map(|(ptr, scope)| {
|
||||||
|
adjust(scopes, source_map, ptr, offset.file_id, offset.ast).unwrap_or(*scope)
|
||||||
})
|
})
|
||||||
.map(|(ptr, scope)| adjust(scopes, source_map, ptr, file_id, offset).unwrap_or(*scope))
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// XXX: during completion, cursor might be outside of any particular
|
// XXX: during completion, cursor might be outside of any particular
|
||||||
|
|
|
@ -226,6 +226,8 @@ impl<N: AstNode> AstId<N> {
|
||||||
#[derive(Debug, PartialEq, Eq, Clone, Copy, Hash)]
|
#[derive(Debug, PartialEq, Eq, Clone, Copy, Hash)]
|
||||||
pub struct Source<T> {
|
pub struct Source<T> {
|
||||||
pub file_id: HirFileId,
|
pub file_id: HirFileId,
|
||||||
|
// FIXME: this stores all kind of things, not only `ast`.
|
||||||
|
// There should be a better name...
|
||||||
pub ast: T,
|
pub ast: T,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue