diff options
Diffstat (limited to 'crates/ra_ide/src/completion/complete_macro_in_item_position.rs')
-rw-r--r-- | crates/ra_ide/src/completion/complete_macro_in_item_position.rs | 143 |
1 files changed, 143 insertions, 0 deletions
diff --git a/crates/ra_ide/src/completion/complete_macro_in_item_position.rs b/crates/ra_ide/src/completion/complete_macro_in_item_position.rs new file mode 100644 index 000000000..faadd1e3f --- /dev/null +++ b/crates/ra_ide/src/completion/complete_macro_in_item_position.rs | |||
@@ -0,0 +1,143 @@ | |||
1 | //! FIXME: write short doc here | ||
2 | |||
3 | use crate::completion::{CompletionContext, Completions}; | ||
4 | |||
5 | pub(super) fn complete_macro_in_item_position(acc: &mut Completions, ctx: &CompletionContext) { | ||
6 | // Show only macros in top level. | ||
7 | if ctx.is_new_item { | ||
8 | ctx.analyzer.process_all_names(ctx.db, &mut |name, res| { | ||
9 | if let hir::ScopeDef::MacroDef(mac) = res { | ||
10 | acc.add_macro(ctx, Some(name.to_string()), mac); | ||
11 | } | ||
12 | }) | ||
13 | } | ||
14 | } | ||
15 | |||
16 | #[cfg(test)] | ||
17 | mod tests { | ||
18 | use crate::completion::{do_completion, CompletionItem, CompletionKind}; | ||
19 | use insta::assert_debug_snapshot; | ||
20 | |||
21 | fn do_reference_completion(code: &str) -> Vec<CompletionItem> { | ||
22 | do_completion(code, CompletionKind::Reference) | ||
23 | } | ||
24 | |||
25 | #[test] | ||
26 | fn completes_macros_as_item() { | ||
27 | assert_debug_snapshot!( | ||
28 | do_reference_completion( | ||
29 | " | ||
30 | //- /main.rs | ||
31 | macro_rules! foo { | ||
32 | () => {} | ||
33 | } | ||
34 | |||
35 | fn foo() {} | ||
36 | |||
37 | <|> | ||
38 | " | ||
39 | ), | ||
40 | @r###" | ||
41 | [ | ||
42 | CompletionItem { | ||
43 | label: "foo!", | ||
44 | source_range: [46; 46), | ||
45 | delete: [46; 46), | ||
46 | insert: "foo!($0)", | ||
47 | kind: Macro, | ||
48 | detail: "macro_rules! foo", | ||
49 | }, | ||
50 | ] | ||
51 | "### | ||
52 | ); | ||
53 | } | ||
54 | |||
55 | #[test] | ||
56 | fn completes_vec_macros_with_square_brackets() { | ||
57 | assert_debug_snapshot!( | ||
58 | do_reference_completion( | ||
59 | " | ||
60 | //- /main.rs | ||
61 | /// Creates a [`Vec`] containing the arguments. | ||
62 | /// | ||
63 | /// - Create a [`Vec`] containing a given list of elements: | ||
64 | /// | ||
65 | /// ``` | ||
66 | /// let v = vec![1, 2, 3]; | ||
67 | /// assert_eq!(v[0], 1); | ||
68 | /// assert_eq!(v[1], 2); | ||
69 | /// assert_eq!(v[2], 3); | ||
70 | /// ``` | ||
71 | macro_rules! vec { | ||
72 | () => {} | ||
73 | } | ||
74 | |||
75 | fn foo() {} | ||
76 | |||
77 | <|> | ||
78 | " | ||
79 | ), | ||
80 | @r###" | ||
81 | [ | ||
82 | CompletionItem { | ||
83 | label: "vec!", | ||
84 | source_range: [280; 280), | ||
85 | delete: [280; 280), | ||
86 | insert: "vec![$0]", | ||
87 | kind: Macro, | ||
88 | detail: "macro_rules! vec", | ||
89 | documentation: Documentation( | ||
90 | "Creates a [`Vec`] containing the arguments.\n\n- Create a [`Vec`] containing a given list of elements:\n\n```\nlet v = vec![1, 2, 3];\nassert_eq!(v[0], 1);\nassert_eq!(v[1], 2);\nassert_eq!(v[2], 3);\n```", | ||
91 | ), | ||
92 | }, | ||
93 | ] | ||
94 | "### | ||
95 | ); | ||
96 | } | ||
97 | |||
98 | #[test] | ||
99 | fn completes_macros_braces_guessing() { | ||
100 | assert_debug_snapshot!( | ||
101 | do_reference_completion( | ||
102 | " | ||
103 | //- /main.rs | ||
104 | /// Foo | ||
105 | /// | ||
106 | /// Not call `fooo!()` `fooo!()`, or `_foo![]` `_foo![]`. | ||
107 | /// Call as `let _=foo! { hello world };` | ||
108 | macro_rules! foo { | ||
109 | () => {} | ||
110 | } | ||
111 | |||
112 | fn main() { | ||
113 | <|> | ||
114 | } | ||
115 | " | ||
116 | ), | ||
117 | @r###" | ||
118 | [ | ||
119 | CompletionItem { | ||
120 | label: "foo!", | ||
121 | source_range: [163; 163), | ||
122 | delete: [163; 163), | ||
123 | insert: "foo! {$0}", | ||
124 | kind: Macro, | ||
125 | detail: "macro_rules! foo", | ||
126 | documentation: Documentation( | ||
127 | "Foo\n\nNot call `fooo!()` `fooo!()`, or `_foo![]` `_foo![]`.\nCall as `let _=foo! { hello world };`", | ||
128 | ), | ||
129 | }, | ||
130 | CompletionItem { | ||
131 | label: "main()", | ||
132 | source_range: [163; 163), | ||
133 | delete: [163; 163), | ||
134 | insert: "main()$0", | ||
135 | kind: Function, | ||
136 | lookup: "main", | ||
137 | detail: "fn main()", | ||
138 | }, | ||
139 | ] | ||
140 | "### | ||
141 | ); | ||
142 | } | ||
143 | } | ||