about summary refs log tree commit diff
diff options
context:
space:
mode:
authorAnatol Ulrich <anatol.ulrich@ferrous-systems.com>2021-10-26 20:09:14 +0200
committerAnatol Ulrich <anatol.ulrich@ferrous-systems.com>2021-10-26 20:09:14 +0200
commit3f82989153adb57b9a4dbf08886ca35451bfab04 (patch)
treee05c6a90d8a2bbbba3d834f21e8b56fda36e1406
parentee1d6cffbfbecc99a70be77fe697af817ed6cfac (diff)
downloadrust-3f82989153adb57b9a4dbf08886ca35451bfab04.tar.gz
rust-3f82989153adb57b9a4dbf08886ca35451bfab04.zip
fix: make `expand_macro` multi-token mapping aware
-rw-r--r--crates/ide/src/expand_macro.rs38
1 files changed, 26 insertions, 12 deletions
diff --git a/crates/ide/src/expand_macro.rs b/crates/ide/src/expand_macro.rs
index 5f34ba83919..fd9d1ff4566 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 {}
 "#,