Store an AstId for procedural macros

This commit is contained in:
Jonas Schievink 2021-03-18 16:11:18 +01:00
parent 3ab9b39dd4
commit c05a1a6e37
11 changed files with 47 additions and 26 deletions

View file

@ -6,7 +6,7 @@ use hir_def::{
src::{HasChildSource, HasSource as _},
Lookup, VariantId,
};
use hir_expand::InFile;
use hir_expand::{InFile, MacroDefKind};
use syntax::ast;
use crate::{
@ -111,10 +111,17 @@ impl HasSource for TypeAlias {
}
}
impl HasSource for MacroDef {
type Ast = ast::Macro;
type Ast = Either<ast::Macro, ast::Fn>;
fn source(self, db: &dyn HirDatabase) -> Option<InFile<Self::Ast>> {
let ast_id = self.id.ast_id()?;
Some(InFile { file_id: ast_id.file_id, value: ast_id.to_node(db.upcast()) })
Some(match &self.id.kind {
MacroDefKind::Declarative(id)
| MacroDefKind::BuiltIn(_, id)
| MacroDefKind::BuiltInDerive(_, id)
| MacroDefKind::BuiltInEager(_, id) => {
id.with_value(Either::Left(id.to_node(db.upcast())))
}
MacroDefKind::ProcMacro(_, id) => id.map(|_| Either::Right(id.to_node(db.upcast()))),
})
}
}
impl HasSource for Impl {

View file

@ -1144,12 +1144,15 @@ impl MacroDef {
/// XXX: this parses the file
pub fn name(self, db: &dyn HirDatabase) -> Option<Name> {
self.source(db)?.value.name().map(|it| it.as_name())
match self.source(db)?.value {
Either::Left(it) => it.name().map(|it| it.as_name()),
Either::Right(it) => it.name().map(|it| it.as_name()),
}
}
/// Indicate it is a proc-macro
pub fn is_proc_macro(&self) -> bool {
matches!(self.id.kind, MacroDefKind::ProcMacro(_))
matches!(self.id.kind, MacroDefKind::ProcMacro(..))
}
/// Indicate it is a derive macro