]> git.lizzy.rs Git - rust.git/commitdiff
Move "complete macro call if cursor at `!` token" logic to `MacroRender`
authorunexge <unexge@gmail.com>
Wed, 1 Sep 2021 06:11:20 +0000 (09:11 +0300)
committerunexge <unexge@gmail.com>
Wed, 1 Sep 2021 06:11:20 +0000 (09:11 +0300)
crates/ide_completion/src/completions/unqualified_path.rs
crates/ide_completion/src/context.rs
crates/ide_completion/src/render/macro_.rs

index 8d421fc78d783791dcf032f6556bcf47fcfa5600..d81cb5391cb09e7002bdb556c64b098076b8fd9b 100644 (file)
@@ -304,31 +304,4 @@ pub mod rust_2018 {}
             "#]],
         );
     }
-
-    #[test]
-    fn completes_macro_call_if_cursor_at_bang_token() {
-        // Regression test for https://github.com/rust-analyzer/rust-analyzer/issues/9904
-        cov_mark::check!(completes_macro_call_if_cursor_at_bang_token);
-        check_edit(
-            "foo!",
-            r#"
-macro_rules! foo {
-    () => {}
-}
-
-fn main() {
-    foo!$0
-}
-"#,
-            r#"
-macro_rules! foo {
-    () => {}
-}
-
-fn main() {
-    foo!($0)
-}
-"#,
-        );
-    }
 }
index d9024157ce458e826a1e41c7f557859a04b1419d..a112f573918e8051cdbe94a1429f4578cdc7a45a 100644 (file)
@@ -236,21 +236,14 @@ pub(crate) fn source_range(&self) -> TextRange {
         let kind = self.token.kind();
         if kind == IDENT || kind == LIFETIME_IDENT || kind == UNDERSCORE || kind.is_keyword() {
             cov_mark::hit!(completes_if_prefix_is_keyword);
-            return self.original_token.text_range();
+            self.original_token.text_range()
         } else if kind == CHAR {
             // assume we are completing a lifetime but the user has only typed the '
             cov_mark::hit!(completes_if_lifetime_without_idents);
-            return TextRange::at(self.original_token.text_range().start(), TextSize::from(1));
-        } else if kind == BANG {
-            if let Some(n) = self.token.parent() {
-                if n.kind() == SyntaxKind::MACRO_CALL {
-                    cov_mark::hit!(completes_macro_call_if_cursor_at_bang_token);
-                    return n.text_range();
-                }
-            }
+            TextRange::at(self.original_token.text_range().start(), TextSize::from(1))
+        } else {
+            TextRange::empty(self.position.offset)
         }
-
-        TextRange::empty(self.position.offset)
     }
 
     pub(crate) fn previous_token_is(&self, kind: SyntaxKind) -> bool {
@@ -402,6 +395,10 @@ pub(crate) fn is_item_hidden(&self, item: &hir::ItemInNs) -> bool {
         }
     }
 
+    pub(crate) fn is_immediately_after_macro_bang(&self) -> bool {
+        self.token.kind() == BANG && self.token.parent().map_or(false, |it| it.kind() == MACRO_CALL)
+    }
+
     /// A version of [`SemanticsScope::process_all_names`] that filters out `#[doc(hidden)]` items.
     pub(crate) fn process_all_names(&self, f: &mut dyn FnMut(Name, ScopeDef)) {
         self.scope.process_all_names(&mut |name, def| {
index eb9fb4d8bf0a8a4aec33c33175b14b1098d2dff2..d1b549df1bb9df0339d48e47ffb5755c39001a1c 100644 (file)
@@ -41,8 +41,13 @@ fn new(ctx: RenderContext<'a>, name: hir::Name, macro_: hir::MacroDef) -> MacroR
     }
 
     fn render(&self, import_to_add: Option<ImportEdit>) -> Option<CompletionItem> {
-        let mut item =
-            CompletionItem::new(CompletionKind::Reference, self.ctx.source_range(), &self.label());
+        let source_range = if self.ctx.completion.is_immediately_after_macro_bang() {
+            cov_mark::hit!(completes_macro_call_if_cursor_at_bang_token);
+            self.ctx.completion.token.parent().map(|it| it.text_range())
+        } else {
+            Some(self.ctx.source_range())
+        }?;
+        let mut item = CompletionItem::new(CompletionKind::Reference, source_range, &self.label());
         item.kind(SymbolKind::Macro)
             .set_documentation(self.docs.clone())
             .set_deprecated(self.ctx.is_deprecated(self.macro_))
@@ -230,4 +235,31 @@ fn main() { foo! {$0} }
 "#,
         )
     }
+
+    #[test]
+    fn completes_macro_call_if_cursor_at_bang_token() {
+        // Regression test for https://github.com/rust-analyzer/rust-analyzer/issues/9904
+        cov_mark::check!(completes_macro_call_if_cursor_at_bang_token);
+        check_edit(
+            "foo!",
+            r#"
+macro_rules! foo {
+    () => {}
+}
+
+fn main() {
+    foo!$0
+}
+"#,
+            r#"
+macro_rules! foo {
+    () => {}
+}
+
+fn main() {
+    foo!($0)
+}
+"#,
+        );
+    }
 }