From 59e7234546590d9fe639ecebab4a768c02ca6389 Mon Sep 17 00:00:00 2001 From: Edwin Cheng Date: Fri, 22 Nov 2019 02:34:49 +0800 Subject: Add to_macro_file_kind --- crates/ra_hir/src/source_binder.rs | 40 ++++++++++++++++++++++++++++++++++++-- 1 file changed, 38 insertions(+), 2 deletions(-) (limited to 'crates/ra_hir') diff --git a/crates/ra_hir/src/source_binder.rs b/crates/ra_hir/src/source_binder.rs index c42ceabdf..797f90d50 100644 --- a/crates/ra_hir/src/source_binder.rs +++ b/crates/ra_hir/src/source_binder.rs @@ -131,6 +131,7 @@ pub struct ReferenceDescriptor { } pub struct Expansion { + macro_file_kind: MacroFileKind, macro_call_id: MacroCallId, } @@ -145,7 +146,7 @@ impl Expansion { } pub fn file_id(&self) -> HirFileId { - self.macro_call_id.as_file(MacroFileKind::Items) + self.macro_call_id.as_file(self.macro_file_kind) } } @@ -439,7 +440,10 @@ impl SourceAnalyzer { db.ast_id_map(macro_call.file_id).ast_id(macro_call.value), ); let macro_call_loc = MacroCallLoc { def, ast_id }; - Some(Expansion { macro_call_id: db.intern_macro(macro_call_loc) }) + Some(Expansion { + macro_call_id: db.intern_macro(macro_call_loc), + macro_file_kind: to_macro_file_kind(macro_call.value), + }) } #[cfg(test)] @@ -538,3 +542,35 @@ fn adjust( }) .map(|(_ptr, scope)| *scope) } + +/// Given a `ast::MacroCall`, return what `MacroKindFile` it belongs to. +/// FIXME: Not completed +fn to_macro_file_kind(macro_call: &ast::MacroCall) -> MacroFileKind { + let syn = macro_call.syntax(); + let parent = match syn.parent() { + Some(it) => it, + None => { + // FIXME: + // If it is root, which means the parent HirFile + // MacroKindFile must be non-items + // return expr now. + return MacroFileKind::Expr; + } + }; + + match parent.kind() { + MACRO_ITEMS | SOURCE_FILE => MacroFileKind::Items, + LET_STMT => { + // FIXME: Handle Pattern + MacroFileKind::Expr + } + EXPR_STMT => MacroFileKind::Statements, + BLOCK => MacroFileKind::Statements, + ARG_LIST => MacroFileKind::Expr, + TRY_EXPR => MacroFileKind::Expr, + _ => { + // Unknown , Just guess it is `Items` + MacroFileKind::Items + } + } +} -- cgit v1.2.3