]> git.lizzy.rs Git - rust.git/commitdiff
Split out `complete_macro_in_item_position`
authoruHOOCCOOHu <hooccooh1896@gmail.com>
Wed, 11 Sep 2019 14:44:44 +0000 (22:44 +0800)
committeruHOOCCOOHu <hooccooh1896@gmail.com>
Wed, 11 Sep 2019 14:44:44 +0000 (22:44 +0800)
crates/ra_ide_api/src/completion.rs
crates/ra_ide_api/src/completion/complete_macro_in_item_position.rs [new file with mode: 0644]
crates/ra_ide_api/src/completion/complete_scope.rs

index a4f080adc693ce2be835953cdaa4e79e42ad7059..0ad4148311c1048f538322afacaf7d1fd6ad96b0 100644 (file)
@@ -12,6 +12,7 @@
 mod complete_path;
 mod complete_scope;
 mod complete_postfix;
+mod complete_macro_in_item_position;
 
 use ra_db::SourceDatabase;
 
@@ -69,5 +70,6 @@ pub(crate) fn completions(db: &db::RootDatabase, position: FilePosition) -> Opti
     complete_record_pattern::complete_record_pattern(&mut acc, &ctx);
     complete_pattern::complete_pattern(&mut acc, &ctx);
     complete_postfix::complete_postfix(&mut acc, &ctx);
+    complete_macro_in_item_position::complete_macro_in_item_position(&mut acc, &ctx);
     Some(acc)
 }
diff --git a/crates/ra_ide_api/src/completion/complete_macro_in_item_position.rs b/crates/ra_ide_api/src/completion/complete_macro_in_item_position.rs
new file mode 100644 (file)
index 0000000..708dc97
--- /dev/null
@@ -0,0 +1,50 @@
+use crate::completion::{CompletionContext, Completions};
+
+pub(super) fn complete_macro_in_item_position(acc: &mut Completions, ctx: &CompletionContext) {
+    // Show only macros in top level.
+    if ctx.is_new_item {
+        for (name, res) in ctx.analyzer.all_names(ctx.db) {
+            if res.get_macros().is_some() {
+                acc.add_resolution(ctx, name.to_string(), &res.only_macros());
+            }
+        }
+    }
+}
+
+#[cfg(test)]
+mod tests {
+    use crate::completion::{do_completion, CompletionItem, CompletionKind};
+    use insta::assert_debug_snapshot;
+
+    fn do_reference_completion(code: &str) -> Vec<CompletionItem> {
+        do_completion(code, CompletionKind::Reference)
+    }
+
+    #[test]
+    fn completes_macros_as_item() {
+        assert_debug_snapshot!(
+            do_reference_completion(
+                "
+                //- /main.rs
+                macro_rules! foo {
+                    () => {}
+                }
+
+                fn foo() {}
+
+                <|>
+                "
+            ),
+            @r##"[
+    CompletionItem {
+        label: "foo",
+        source_range: [46; 46),
+        delete: [46; 46),
+        insert: "foo!",
+        kind: Macro,
+        detail: "macro_rules! foo",
+    },
+]"##
+        );
+    }
+}
index e2e1d7872ae37f16e98a0181c0d65419132c7eae..2062e7300c975bd36616793c12e0cad829ae20e8 100644 (file)
@@ -6,15 +6,6 @@
 use crate::completion::{CompletionContext, CompletionItem, CompletionKind, Completions};
 
 pub(super) fn complete_scope(acc: &mut Completions, ctx: &CompletionContext) {
-    // Show only macros in top level.
-    if ctx.is_new_item {
-        for (name, res) in ctx.analyzer.all_names(ctx.db) {
-            if res.get_macros().is_some() {
-                acc.add_resolution(ctx, name.to_string(), &res.only_macros());
-            }
-        }
-    }
-
     if !ctx.is_trivial_path {
         return;
     }
@@ -730,34 +721,6 @@ fn main() {
         kind: Function,
         detail: "fn main()",
     },
-]"##
-        );
-    }
-
-    #[test]
-    fn completes_macros_as_item() {
-        assert_debug_snapshot!(
-            do_reference_completion(
-                "
-                //- /main.rs
-                macro_rules! foo {
-                    () => {}
-                }
-
-                fn foo() {}
-
-                <|>
-                "
-            ),
-            @r##"[
-    CompletionItem {
-        label: "foo",
-        source_range: [46; 46),
-        delete: [46; 46),
-        insert: "foo!",
-        kind: Macro,
-        detail: "macro_rules! foo",
-    },
 ]"##
         );
     }