fix: make expand_macro multi-token mapping aware

This commit is contained in:
Anatol Ulrich 2021-10-26 20:09:14 +02:00
parent ee1d6cffbf
commit 3f82989153

View file

@ -32,19 +32,32 @@ pub(crate) fn expand_macro(db: &RootDatabase, position: FilePosition) -> Option<
_ => 0, _ => 0,
})?; })?;
let descended = sema.descend_into_macros_single(tok.clone()); // due to how Rust Analyzer works internally, we need to special case derive attributes,
if let Some(attr) = descended.ancestors().find_map(ast::Attr::cast) { // otherwise they might not get found, e.g. here only `#[attr]` would expand:
if let Some((path, tt)) = attr.as_simple_call() { // ```
if path == "derive" { // #[attr]
let mut tt = tt.syntax().children_with_tokens().skip(1).join(""); // #[derive($0Foo)]
tt.pop(); // struct Bar;
let expansions = sema.expand_derive_macro(&attr)?; // ```
return Some(ExpandedMacro {
name: tt, let derive = sema.descend_into_macros(tok.clone()).iter().find_map(|descended| {
expansion: expansions.into_iter().map(insert_whitespaces).join(""), let attr = descended.ancestors().find_map(ast::Attr::cast)?;
}); let (path, tt) = attr.as_simple_call()?;
} if path == "derive" {
let mut tt = tt.syntax().children_with_tokens().skip(1).join("");
tt.pop();
let expansions = sema.expand_derive_macro(&attr)?;
return Some(ExpandedMacro {
name: tt,
expansion: expansions.into_iter().map(insert_whitespaces).join(""),
});
} else {
None
} }
});
if derive.is_some() {
return derive;
} }
// FIXME: Intermix attribute and bang! expansions // FIXME: Intermix attribute and bang! expansions
@ -356,6 +369,7 @@ fn main() {
#[rustc_builtin_macro] #[rustc_builtin_macro]
pub macro Clone {} pub macro Clone {}
#[doc = ""]
#[derive(C$0lone)] #[derive(C$0lone)]
struct Foo {} struct Foo {}
"#, "#,