Prepare SourceAnalyzer for macros
This commit is contained in:
parent
bd8af6a413
commit
0404e647e6
@ -91,7 +91,7 @@ fn def_with_body_from_child_node(
|
|||||||
#[derive(Debug)]
|
#[derive(Debug)]
|
||||||
pub struct SourceAnalyzer {
|
pub struct SourceAnalyzer {
|
||||||
// FIXME: this doesn't handle macros at all
|
// FIXME: this doesn't handle macros at all
|
||||||
file_id: FileId,
|
file_id: HirFileId,
|
||||||
resolver: Resolver,
|
resolver: Resolver,
|
||||||
body_owner: Option<DefWithBody>,
|
body_owner: Option<DefWithBody>,
|
||||||
body_source_map: Option<Arc<BodySourceMap>>,
|
body_source_map: Option<Arc<BodySourceMap>>,
|
||||||
@ -159,7 +159,7 @@ impl SourceAnalyzer {
|
|||||||
body_source_map: Some(source_map),
|
body_source_map: Some(source_map),
|
||||||
infer: Some(def.infer(db)),
|
infer: Some(def.infer(db)),
|
||||||
scopes: Some(scopes),
|
scopes: Some(scopes),
|
||||||
file_id,
|
file_id: file_id.into(),
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
SourceAnalyzer {
|
SourceAnalyzer {
|
||||||
@ -171,18 +171,18 @@ impl SourceAnalyzer {
|
|||||||
body_source_map: None,
|
body_source_map: None,
|
||||||
infer: None,
|
infer: None,
|
||||||
scopes: None,
|
scopes: None,
|
||||||
file_id,
|
file_id: file_id.into(),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fn expr_id(&self, expr: &ast::Expr) -> Option<ExprId> {
|
fn expr_id(&self, expr: &ast::Expr) -> Option<ExprId> {
|
||||||
let src = Source { file_id: self.file_id.into(), ast: expr };
|
let src = Source { file_id: self.file_id, ast: expr };
|
||||||
self.body_source_map.as_ref()?.node_expr(src)
|
self.body_source_map.as_ref()?.node_expr(src)
|
||||||
}
|
}
|
||||||
|
|
||||||
fn pat_id(&self, pat: &ast::Pat) -> Option<PatId> {
|
fn pat_id(&self, pat: &ast::Pat) -> Option<PatId> {
|
||||||
let src = Source { file_id: self.file_id.into(), ast: pat };
|
let src = Source { file_id: self.file_id, ast: pat };
|
||||||
self.body_source_map.as_ref()?.node_pat(src)
|
self.body_source_map.as_ref()?.node_pat(src)
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -290,8 +290,7 @@ 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 =
|
let scope = scope_for(scopes, source_map, Source::new(self.file_id, name_ref.syntax()))?;
|
||||||
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(),
|
||||||
|
@ -223,6 +223,7 @@ impl<N: AstNode> AstId<N> {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// FIXME: https://github.com/matklad/with ?
|
||||||
#[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,
|
||||||
@ -236,11 +237,16 @@ impl<T> Source<T> {
|
|||||||
Source { file_id, ast }
|
Source { file_id, ast }
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Similarly, naming here is stupid...
|
||||||
|
pub fn with_ast<U>(&self, ast: U) -> Source<U> {
|
||||||
|
Source::new(self.file_id, ast)
|
||||||
|
}
|
||||||
|
|
||||||
pub fn map<F: FnOnce(T) -> U, U>(self, f: F) -> Source<U> {
|
pub fn map<F: FnOnce(T) -> U, U>(self, f: F) -> Source<U> {
|
||||||
Source { file_id: self.file_id, ast: f(self.ast) }
|
Source::new(self.file_id, f(self.ast))
|
||||||
}
|
}
|
||||||
pub fn as_ref(&self) -> Source<&T> {
|
pub fn as_ref(&self) -> Source<&T> {
|
||||||
Source { file_id: self.file_id, ast: &self.ast }
|
self.with_ast(&self.ast)
|
||||||
}
|
}
|
||||||
pub fn file_syntax(&self, db: &impl db::AstDatabase) -> SyntaxNode {
|
pub fn file_syntax(&self, db: &impl db::AstDatabase) -> SyntaxNode {
|
||||||
db.parse_or_expand(self.file_id).expect("source created from invalid file")
|
db.parse_or_expand(self.file_id).expect("source created from invalid file")
|
||||||
|
Loading…
x
Reference in New Issue
Block a user