From 24e98121d81b75bafcd9c6005548776c00de8401 Mon Sep 17 00:00:00 2001 From: Florian Diebold Date: Sat, 7 Mar 2020 15:27:03 +0100 Subject: Try to complete within macros --- crates/ra_hir_expand/src/db.rs | 53 ++++++++++++++++++++++++++++++++++------- crates/ra_hir_expand/src/lib.rs | 9 ++++++- 2 files changed, 53 insertions(+), 9 deletions(-) (limited to 'crates/ra_hir_expand/src') diff --git a/crates/ra_hir_expand/src/db.rs b/crates/ra_hir_expand/src/db.rs index f3a84cacc..a7aa60fc9 100644 --- a/crates/ra_hir_expand/src/db.rs +++ b/crates/ra_hir_expand/src/db.rs @@ -129,16 +129,43 @@ pub(crate) fn macro_arg( pub(crate) fn macro_expand( db: &dyn AstDatabase, id: MacroCallId, +) -> Result, String> { + macro_expand_with_arg(db, id, None) +} + +// TODO hack +pub fn expander( + db: &dyn AstDatabase, + id: MacroCallId, +) -> Option> { + let lazy_id = match id { + MacroCallId::LazyMacro(id) => id, + MacroCallId::EagerMacro(_id) => { + // TODO + unimplemented!() + } + }; + + let loc = db.lookup_intern_macro(lazy_id); + let macro_rules = db.macro_def(loc.def)?; + Some(macro_rules) +} + +pub(crate) fn macro_expand_with_arg( + db: &dyn AstDatabase, + id: MacroCallId, + arg: Option>, ) -> Result, String> { let lazy_id = match id { MacroCallId::LazyMacro(id) => id, MacroCallId::EagerMacro(id) => { + // TODO return Ok(db.lookup_intern_eager_expansion(id).subtree); } }; let loc = db.lookup_intern_macro(lazy_id); - let macro_arg = db.macro_arg(id).ok_or("Fail to args in to tt::TokenTree")?; + let macro_arg = arg.or_else(|| db.macro_arg(id)).ok_or("Fail to args in to tt::TokenTree")?; let macro_rules = db.macro_def(loc.def).ok_or("Fail to find macro definition")?; let tt = macro_rules.0.expand(db, lazy_id, ¯o_arg.0).map_err(|err| format!("{:?}", err))?; @@ -162,12 +189,24 @@ pub(crate) fn parse_or_expand(db: &dyn AstDatabase, file_id: HirFileId) -> Optio pub(crate) fn parse_macro( db: &dyn AstDatabase, macro_file: MacroFile, +) -> Option<(Parse, Arc)> { + parse_macro_with_arg(db, macro_file, None) +} + +pub fn parse_macro_with_arg( + db: &dyn AstDatabase, + macro_file: MacroFile, + arg: Option>, ) -> Option<(Parse, Arc)> { let _p = profile("parse_macro_query"); let macro_call_id = macro_file.macro_call_id; - let tt = db - .macro_expand(macro_call_id) + let expansion = if let Some(arg) = arg { + macro_expand_with_arg(db, macro_call_id, Some(arg)) + } else { + db.macro_expand(macro_call_id) + }; + let tt = expansion .map_err(|err| { // Note: // The final goal we would like to make all parse_macro success, @@ -185,15 +224,13 @@ pub(crate) fn parse_macro( .collect::>() .join("\n"); - log::warn!( + eprintln!( "fail on macro_parse: (reason: {} macro_call: {:#}) parents: {}", - err, - node.value, - parents + err, node.value, parents ); } _ => { - log::warn!("fail on macro_parse: (reason: {})", err); + eprintln!("fail on macro_parse: (reason: {})", err); } } }) diff --git a/crates/ra_hir_expand/src/lib.rs b/crates/ra_hir_expand/src/lib.rs index 3fce73e8a..92f3902dd 100644 --- a/crates/ra_hir_expand/src/lib.rs +++ b/crates/ra_hir_expand/src/lib.rs @@ -157,6 +157,13 @@ impl HirFileId { } } } + + pub fn macro_file(self) -> Option { + match self.0 { + HirFileIdRepr::FileId(_) => None, + HirFileIdRepr::MacroFile(m) => Some(m), + } + } } #[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)] @@ -296,7 +303,7 @@ pub struct ExpansionInfo { exp_map: Arc, } -pub use mbe::Origin; +pub use mbe::{syntax_node_to_token_tree, Origin}; use ra_parser::FragmentKind; impl ExpansionInfo { -- cgit v1.2.3