From 503cbd3f4b54f3be224d7a4221fa023f0e35d228 Mon Sep 17 00:00:00 2001 From: Edwin Cheng Date: Fri, 27 Mar 2020 04:26:34 +0800 Subject: Implement ra_proc_macro client logic --- crates/ra_hir_expand/src/proc_macro.rs | 27 ++++++++++++++++++++++++++- 1 file changed, 26 insertions(+), 1 deletion(-) (limited to 'crates/ra_hir_expand') diff --git a/crates/ra_hir_expand/src/proc_macro.rs b/crates/ra_hir_expand/src/proc_macro.rs index 4d270e0de..97d1208ec 100644 --- a/crates/ra_hir_expand/src/proc_macro.rs +++ b/crates/ra_hir_expand/src/proc_macro.rs @@ -9,6 +9,15 @@ pub struct ProcMacroExpander { proc_macro_id: ProcMacroId, } +macro_rules! err { + ($fmt:literal, $($tt:tt),*) => { + mbe::ExpandError::ProcMacroError(tt::ExpansionError::Unknown(format!($fmt, $($tt),*))) + }; + ($fmt:literal) => { + mbe::ExpandError::ProcMacroError(tt::ExpansionError::Unknown($fmt.to_string())) + } +} + impl ProcMacroExpander { pub fn new(krate: CrateId, proc_macro_id: ProcMacroId) -> ProcMacroExpander { ProcMacroExpander { krate, proc_macro_id } @@ -25,8 +34,24 @@ impl ProcMacroExpander { .proc_macro .get(self.proc_macro_id.0 as usize) .clone() - .ok_or_else(|| mbe::ExpandError::ConversionError)?; + .ok_or_else(|| err!("No derive macro found."))?; + + let tt = remove_derive_atr(tt, &proc_macro.name) + .ok_or_else(|| err!("Fail to remove derive for custom derive"))?; proc_macro.expander.expand(&tt, None).map_err(mbe::ExpandError::from) } } + +fn remove_derive_atr(tt: &tt::Subtree, _name: &str) -> Option { + // FIXME: proper handle the remove derive + // We assume the first 2 tokens are #[derive(name)] + if tt.token_trees.len() > 2 { + let mut tt = tt.clone(); + tt.token_trees.remove(0); + tt.token_trees.remove(0); + return Some(tt); + } + + None +} -- cgit v1.2.3