From 3f82989153adb57b9a4dbf08886ca35451bfab04 Mon Sep 17 00:00:00 2001 From: Anatol Ulrich Date: Tue, 26 Oct 2021 20:09:14 +0200 Subject: [PATCH 1/4] fix: make `expand_macro` multi-token mapping aware --- crates/ide/src/expand_macro.rs | 38 +++++++++++++++++++++++----------- 1 file changed, 26 insertions(+), 12 deletions(-) diff --git a/crates/ide/src/expand_macro.rs b/crates/ide/src/expand_macro.rs index 5f34ba839197..fd9d1ff45667 100644 --- a/crates/ide/src/expand_macro.rs +++ b/crates/ide/src/expand_macro.rs @@ -32,19 +32,32 @@ pub(crate) fn expand_macro(db: &RootDatabase, position: FilePosition) -> Option< _ => 0, })?; - let descended = sema.descend_into_macros_single(tok.clone()); - if let Some(attr) = descended.ancestors().find_map(ast::Attr::cast) { - if let Some((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(""), - }); - } + // due to how Rust Analyzer works internally, we need to special case derive attributes, + // otherwise they might not get found, e.g. here only `#[attr]` would expand: + // ``` + // #[attr] + // #[derive($0Foo)] + // struct Bar; + // ``` + + let derive = sema.descend_into_macros(tok.clone()).iter().find_map(|descended| { + 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 @@ -356,6 +369,7 @@ fn main() { #[rustc_builtin_macro] pub macro Clone {} +#[doc = ""] #[derive(C$0lone)] struct Foo {} "#, From 04f2eb0fba5485b360b1f18bd550744974802619 Mon Sep 17 00:00:00 2001 From: Anatol Ulrich Date: Tue, 26 Oct 2021 20:10:09 +0200 Subject: [PATCH 2/4] wording --- crates/ide/src/expand_macro.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/crates/ide/src/expand_macro.rs b/crates/ide/src/expand_macro.rs index fd9d1ff45667..8feaa3b7be95 100644 --- a/crates/ide/src/expand_macro.rs +++ b/crates/ide/src/expand_macro.rs @@ -33,7 +33,7 @@ pub(crate) fn expand_macro(db: &RootDatabase, position: FilePosition) -> Option< })?; // due to how Rust Analyzer works internally, we need to special case derive attributes, - // otherwise they might not get found, e.g. here only `#[attr]` would expand: + // otherwise they might not get found, e.g. here with the cursor at $0 `#[attr]` would expand: // ``` // #[attr] // #[derive($0Foo)] From e6913be47bd158af6813af1603c4339ddab47b8e Mon Sep 17 00:00:00 2001 From: Anatol Ulrich Date: Tue, 26 Oct 2021 20:15:25 +0200 Subject: [PATCH 3/4] fix test --- crates/ide/src/expand_macro.rs | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/crates/ide/src/expand_macro.rs b/crates/ide/src/expand_macro.rs index 8feaa3b7be95..68eeae226eab 100644 --- a/crates/ide/src/expand_macro.rs +++ b/crates/ide/src/expand_macro.rs @@ -366,10 +366,12 @@ fn main() { fn macro_expand_derive() { check( r#" +//- proc_macros: identity + #[rustc_builtin_macro] pub macro Clone {} -#[doc = ""] +#[proc_macros::identity] #[derive(C$0lone)] struct Foo {} "#, From b42093915ab14fe61688ef1705505dbd859cb89a Mon Sep 17 00:00:00 2001 From: Anatol Ulrich <45840+spookyvision@users.noreply.github.com> Date: Tue, 26 Oct 2021 20:17:47 +0200 Subject: [PATCH 4/4] Update crates/ide/src/expand_macro.rs Co-authored-by: Lukas Wirth --- crates/ide/src/expand_macro.rs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/crates/ide/src/expand_macro.rs b/crates/ide/src/expand_macro.rs index 68eeae226eab..c389d895f285 100644 --- a/crates/ide/src/expand_macro.rs +++ b/crates/ide/src/expand_macro.rs @@ -47,10 +47,10 @@ pub(crate) fn expand_macro(db: &RootDatabase, position: FilePosition) -> Option< let mut tt = tt.syntax().children_with_tokens().skip(1).join(""); tt.pop(); let expansions = sema.expand_derive_macro(&attr)?; - return Some(ExpandedMacro { + Some(ExpandedMacro { name: tt, expansion: expansions.into_iter().map(insert_whitespaces).join(""), - }); + }) } else { None }