diff options
Diffstat (limited to 'crates')
-rw-r--r-- | crates/ra_ide_api/src/completion.rs | 2 | ||||
-rw-r--r-- | crates/ra_ide_api/src/completion/complete_macro_in_item_position.rs | 50 | ||||
-rw-r--r-- | crates/ra_ide_api/src/completion/complete_scope.rs | 37 |
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; | |||
12 | mod complete_path; | 12 | mod complete_path; |
13 | mod complete_scope; | 13 | mod complete_scope; |
14 | mod complete_postfix; | 14 | mod complete_postfix; |
15 | mod complete_macro_in_item_position; | ||
15 | 16 | ||
16 | use ra_db::SourceDatabase; | 17 | use 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 @@ | |||
1 | use crate::completion::{CompletionContext, Completions}; | ||
2 | |||
3 | pub(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)] | ||
15 | mod 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; | |||
6 | use crate::completion::{CompletionContext, CompletionItem, CompletionKind, Completions}; | 6 | use crate::completion::{CompletionContext, CompletionItem, CompletionKind, Completions}; |
7 | 7 | ||
8 | pub(super) fn complete_scope(acc: &mut Completions, ctx: &CompletionContext) { | 8 | pub(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 | } |