diff --git a/crates/ra_hir/src/source_binder.rs b/crates/ra_hir/src/source_binder.rs index b80aaeb9011..c5a920688fd 100644 --- a/crates/ra_hir/src/source_binder.rs +++ b/crates/ra_hir/src/source_binder.rs @@ -21,7 +21,6 @@ }; use hir_expand::{ hygiene::Hygiene, name::AsName, AstId, HirFileId, InFile, MacroCallId, MacroCallKind, - MacroFileKind, }; use ra_syntax::{ ast::{self, AstNode}, @@ -142,7 +141,6 @@ pub struct ReferenceDescriptor { #[derive(Debug)] pub struct Expansion { - macro_file_kind: MacroFileKind, macro_call_id: MacroCallId, } @@ -157,7 +155,7 @@ pub fn map_token_down( } pub fn file_id(&self) -> HirFileId { - self.macro_call_id.as_file(self.macro_file_kind) + self.macro_call_id.as_file() } } @@ -456,10 +454,7 @@ pub fn expand( macro_call.file_id, db.ast_id_map(macro_call.file_id).ast_id(macro_call.value), ); - Some(Expansion { - macro_call_id: def.as_call_id(db, MacroCallKind::FnLike(ast_id)), - macro_file_kind: to_macro_file_kind(macro_call.value), - }) + Some(Expansion { macro_call_id: def.as_call_id(db, MacroCallKind::FnLike(ast_id)) }) } } @@ -543,35 +538,3 @@ 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 - } - } -} diff --git a/crates/ra_hir_def/src/body.rs b/crates/ra_hir_def/src/body.rs index 7b385f3fd82..b3bc336cf92 100644 --- a/crates/ra_hir_def/src/body.rs +++ b/crates/ra_hir_def/src/body.rs @@ -6,9 +6,7 @@ use std::{ops::Index, sync::Arc}; use either::Either; -use hir_expand::{ - hygiene::Hygiene, AstId, HirFileId, InFile, MacroCallKind, MacroDefId, MacroFileKind, -}; +use hir_expand::{hygiene::Hygiene, AstId, HirFileId, InFile, MacroCallKind, MacroDefId}; use ra_arena::{map::ArenaMap, Arena}; use ra_syntax::{ast, AstNode, AstPtr}; use rustc_hash::FxHashMap; @@ -49,7 +47,7 @@ fn enter_expand( if let Some(path) = macro_call.path().and_then(|path| self.parse_path(path)) { if let Some(def) = self.resolve_path_as_macro(db, &path) { let call_id = def.as_call_id(db, MacroCallKind::FnLike(ast_id)); - let file_id = call_id.as_file(MacroFileKind::Expr); + let file_id = call_id.as_file(); if let Some(node) = db.parse_or_expand(file_id) { if let Some(expr) = ast::Expr::cast(node) { log::debug!("macro expansion {:#?}", expr.syntax()); diff --git a/crates/ra_hir_def/src/nameres/collector.rs b/crates/ra_hir_def/src/nameres/collector.rs index 08693cb13a1..6a01e3ab7b9 100644 --- a/crates/ra_hir_def/src/nameres/collector.rs +++ b/crates/ra_hir_def/src/nameres/collector.rs @@ -7,7 +7,7 @@ builtin_derive::find_builtin_derive, builtin_macro::find_builtin_macro, name::{self, AsName, Name}, - HirFileId, MacroCallId, MacroCallKind, MacroDefId, MacroDefKind, MacroFileKind, + HirFileId, MacroCallId, MacroCallKind, MacroDefId, MacroDefKind, }; use ra_cfg::CfgOptions; use ra_db::{CrateId, FileId}; @@ -545,7 +545,7 @@ fn collect_macro_expansion( self.macro_stack_monitor.increase(macro_def_id); if !self.macro_stack_monitor.is_poison(macro_def_id) { - let file_id: HirFileId = macro_call_id.as_file(MacroFileKind::Items); + let file_id: HirFileId = macro_call_id.as_file(); let raw_items = self.db.raw_items(file_id); let mod_dir = self.mod_dirs[&module_id].clone(); ModCollector { diff --git a/crates/ra_hir_expand/src/builtin_derive.rs b/crates/ra_hir_expand/src/builtin_derive.rs index 78fa9b09a27..5746376022b 100644 --- a/crates/ra_hir_expand/src/builtin_derive.rs +++ b/crates/ra_hir_expand/src/builtin_derive.rs @@ -208,7 +208,7 @@ fn partial_ord_expand( #[cfg(test)] mod tests { use super::*; - use crate::{test_db::TestDB, AstId, MacroCallKind, MacroCallLoc, MacroFileKind}; + use crate::{test_db::TestDB, AstId, MacroCallKind, MacroCallLoc}; use ra_db::{fixture::WithFixture, SourceDatabase}; fn expand_builtin_derive(s: &str, expander: BuiltinDeriveExpander) -> String { @@ -229,7 +229,7 @@ fn expand_builtin_derive(s: &str, expander: BuiltinDeriveExpander) -> String { }; let id = db.intern_macro(loc); - let parsed = db.parse_or_expand(id.as_file(MacroFileKind::Items)).unwrap(); + let parsed = db.parse_or_expand(id.as_file()).unwrap(); // FIXME text() for syntax nodes parsed from token tree looks weird // because there's no whitespace, see below diff --git a/crates/ra_hir_expand/src/builtin_macro.rs b/crates/ra_hir_expand/src/builtin_macro.rs index 99303188bf2..c7071fe85a3 100644 --- a/crates/ra_hir_expand/src/builtin_macro.rs +++ b/crates/ra_hir_expand/src/builtin_macro.rs @@ -2,8 +2,7 @@ use crate::db::AstDatabase; use crate::{ ast::{self, AstNode}, - name, AstId, CrateId, HirFileId, MacroCallId, MacroDefId, MacroDefKind, MacroFileKind, - TextUnit, + name, AstId, CrateId, HirFileId, MacroCallId, MacroDefId, MacroDefKind, TextUnit, }; use crate::quote; @@ -90,7 +89,7 @@ fn line_expand( let arg = loc.kind.arg(db).ok_or_else(|| mbe::ExpandError::UnexpectedToken)?; let arg_start = arg.text_range().start(); - let file = id.as_file(MacroFileKind::Expr); + let file = id.as_file(); let line_num = to_line_number(db, file, arg_start); let expanded = quote! { @@ -158,7 +157,7 @@ fn column_expand( let _arg = macro_call.token_tree().ok_or_else(|| mbe::ExpandError::UnexpectedToken)?; let col_start = macro_call.syntax().text_range().start(); - let file = id.as_file(MacroFileKind::Expr); + let file = id.as_file(); let col_num = to_col_number(db, file, col_start); let expanded = quote! { @@ -269,7 +268,7 @@ fn expand_builtin_macro(s: &str, expander: BuiltinFnLikeExpander) -> String { }; let id = db.intern_macro(loc); - let parsed = db.parse_or_expand(id.as_file(MacroFileKind::Expr)).unwrap(); + let parsed = db.parse_or_expand(id.as_file()).unwrap(); parsed.text().to_string() } diff --git a/crates/ra_hir_expand/src/db.rs b/crates/ra_hir_expand/src/db.rs index 013a6c8ba95..4bdb41619c8 100644 --- a/crates/ra_hir_expand/src/db.rs +++ b/crates/ra_hir_expand/src/db.rs @@ -6,11 +6,11 @@ use ra_db::{salsa, SourceDatabase}; use ra_parser::FragmentKind; use ra_prof::profile; -use ra_syntax::{AstNode, Parse, SyntaxNode}; +use ra_syntax::{AstNode, Parse, SyntaxKind::*, SyntaxNode}; use crate::{ ast_id_map::AstIdMap, BuiltinDeriveExpander, BuiltinFnLikeExpander, HirFileId, HirFileIdRepr, - MacroCallId, MacroCallLoc, MacroDefId, MacroDefKind, MacroFile, MacroFileKind, + MacroCallId, MacroCallLoc, MacroDefId, MacroDefKind, MacroFile, }; #[derive(Debug, Clone, Eq, PartialEq)] @@ -155,11 +155,42 @@ pub(crate) fn parse_macro( }) .ok()?; - let fragment_kind = match macro_file.macro_file_kind { - MacroFileKind::Items => FragmentKind::Items, - MacroFileKind::Expr => FragmentKind::Expr, - MacroFileKind::Statements => FragmentKind::Statements, - }; + let fragment_kind = to_fragment_kind(db, macro_call_id); + let (parse, rev_token_map) = mbe::token_tree_to_syntax_node(&tt, fragment_kind).ok()?; Some((parse, Arc::new(rev_token_map))) } + +/// Given a `MacroCallId`, return what `FragmentKind` it belongs to. +/// FIXME: Not completed +fn to_fragment_kind(db: &dyn AstDatabase, macro_call_id: MacroCallId) -> FragmentKind { + let syn = db.lookup_intern_macro(macro_call_id).kind.node(db).value; + + 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 FragmentKind::Expr; + } + }; + + match parent.kind() { + MACRO_ITEMS | SOURCE_FILE => FragmentKind::Items, + LET_STMT => { + // FIXME: Handle Pattern + FragmentKind::Expr + } + EXPR_STMT => FragmentKind::Statements, + BLOCK => FragmentKind::Statements, + ARG_LIST => FragmentKind::Expr, + TRY_EXPR => FragmentKind::Expr, + TUPLE_EXPR => FragmentKind::Expr, + _ => { + // Unknown , Just guess it is `Items` + FragmentKind::Items + } + } +} diff --git a/crates/ra_hir_expand/src/lib.rs b/crates/ra_hir_expand/src/lib.rs index 0a5da7e5479..94e1e466a56 100644 --- a/crates/ra_hir_expand/src/lib.rs +++ b/crates/ra_hir_expand/src/lib.rs @@ -117,14 +117,6 @@ pub fn expansion_info(self, db: &dyn db::AstDatabase) -> Option { #[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)] pub struct MacroFile { macro_call_id: MacroCallId, - macro_file_kind: MacroFileKind, -} - -#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)] -pub enum MacroFileKind { - Items, - Expr, - Statements, } /// `MacroCallId` identifies a particular macro invocation, like @@ -205,9 +197,8 @@ pub fn arg(&self, db: &dyn db::AstDatabase) -> Option { } impl MacroCallId { - pub fn as_file(self, kind: MacroFileKind) -> HirFileId { - let macro_file = MacroFile { macro_call_id: self, macro_file_kind: kind }; - macro_file.into() + pub fn as_file(self) -> HirFileId { + MacroFile { macro_call_id: self }.into() } }