aboutsummaryrefslogtreecommitdiff
path: root/crates/ra_ide_api
diff options
context:
space:
mode:
authoruHOOCCOOHu <[email protected]>2019-09-11 15:44:44 +0100
committeruHOOCCOOHu <[email protected]>2019-09-11 15:44:44 +0100
commit6353b1621f44e1b0db65ebbe414aa7c5f1864b9d (patch)
treedd32501321994bc8e9dcacd634cb4511053b20bf /crates/ra_ide_api
parentc033d18700e86c9bb028344f28e2eb7d670fe241 (diff)
Split out `complete_macro_in_item_position`
Diffstat (limited to 'crates/ra_ide_api')
-rw-r--r--crates/ra_ide_api/src/completion.rs2
-rw-r--r--crates/ra_ide_api/src/completion/complete_macro_in_item_position.rs50
-rw-r--r--crates/ra_ide_api/src/completion/complete_scope.rs37
3 files changed, 52 insertions, 37 deletions
diff --git a/crates/ra_ide_api/src/completion.rs b/crates/ra_ide_api/src/completion.rs
index a4f080adc..0ad414831 100644
--- a/crates/ra_ide_api/src/completion.rs
+++ b/crates/ra_ide_api/src/completion.rs
@@ -12,6 +12,7 @@ mod complete_snippet;
12mod complete_path; 12mod complete_path;
13mod complete_scope; 13mod complete_scope;
14mod complete_postfix; 14mod complete_postfix;
15mod complete_macro_in_item_position;
15 16
16use ra_db::SourceDatabase; 17use ra_db::SourceDatabase;
17 18
@@ -69,5 +70,6 @@ pub(crate) fn completions(db: &db::RootDatabase, position: FilePosition) -> Opti
69 complete_record_pattern::complete_record_pattern(&mut acc, &ctx); 70 complete_record_pattern::complete_record_pattern(&mut acc, &ctx);
70 complete_pattern::complete_pattern(&mut acc, &ctx); 71 complete_pattern::complete_pattern(&mut acc, &ctx);
71 complete_postfix::complete_postfix(&mut acc, &ctx); 72 complete_postfix::complete_postfix(&mut acc, &ctx);
73 complete_macro_in_item_position::complete_macro_in_item_position(&mut acc, &ctx);
72 Some(acc) 74 Some(acc)
73} 75}
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
index 000000000..708dc9777
--- /dev/null
+++ b/crates/ra_ide_api/src/completion/complete_macro_in_item_position.rs
@@ -0,0 +1,50 @@
1use crate::completion::{CompletionContext, Completions};
2
3pub(super) fn complete_macro_in_item_position(acc: &mut Completions, ctx: &CompletionContext) {
4 // Show only macros in top level.
5 if ctx.is_new_item {
6 for (name, res) in ctx.analyzer.all_names(ctx.db) {
7 if res.get_macros().is_some() {
8 acc.add_resolution(ctx, name.to_string(), &res.only_macros());
9 }
10 }
11 }
12}
13
14#[cfg(test)]
15mod tests {
16 use crate::completion::{do_completion, CompletionItem, CompletionKind};
17 use insta::assert_debug_snapshot;
18
19 fn do_reference_completion(code: &str) -> Vec<CompletionItem> {
20 do_completion(code, CompletionKind::Reference)
21 }
22
23 #[test]
24 fn completes_macros_as_item() {
25 assert_debug_snapshot!(
26 do_reference_completion(
27 "
28 //- /main.rs
29 macro_rules! foo {
30 () => {}
31 }
32
33 fn foo() {}
34
35 <|>
36 "
37 ),
38 @r##"[
39 CompletionItem {
40 label: "foo",
41 source_range: [46; 46),
42 delete: [46; 46),
43 insert: "foo!",
44 kind: Macro,
45 detail: "macro_rules! foo",
46 },
47]"##
48 );
49 }
50}
diff --git a/crates/ra_ide_api/src/completion/complete_scope.rs b/crates/ra_ide_api/src/completion/complete_scope.rs
index e2e1d7872..2062e7300 100644
--- a/crates/ra_ide_api/src/completion/complete_scope.rs
+++ b/crates/ra_ide_api/src/completion/complete_scope.rs
@@ -6,15 +6,6 @@ use rustc_hash::FxHashMap;
6use crate::completion::{CompletionContext, CompletionItem, CompletionKind, Completions}; 6use crate::completion::{CompletionContext, CompletionItem, CompletionKind, Completions};
7 7
8pub(super) fn complete_scope(acc: &mut Completions, ctx: &CompletionContext) { 8pub(super) fn complete_scope(acc: &mut Completions, ctx: &CompletionContext) {
9 // Show only macros in top level.
10 if ctx.is_new_item {
11 for (name, res) in ctx.analyzer.all_names(ctx.db) {
12 if res.get_macros().is_some() {
13 acc.add_resolution(ctx, name.to_string(), &res.only_macros());
14 }
15 }
16 }
17
18 if !ctx.is_trivial_path { 9 if !ctx.is_trivial_path {
19 return; 10 return;
20 } 11 }
@@ -733,32 +724,4 @@ mod tests {
733]"## 724]"##
734 ); 725 );
735 } 726 }
736
737 #[test]
738 fn completes_macros_as_item() {
739 assert_debug_snapshot!(
740 do_reference_completion(
741 "
742 //- /main.rs
743 macro_rules! foo {
744 () => {}
745 }
746
747 fn foo() {}
748
749 <|>
750 "
751 ),
752 @r##"[
753 CompletionItem {
754 label: "foo",
755 source_range: [46; 46),
756 delete: [46; 46),
757 insert: "foo!",
758 kind: Macro,
759 detail: "macro_rules! foo",
760 },
761]"##
762 );
763 }
764} 727}