]> git.lizzy.rs Git - rust.git/blob - crates/ra_ide_api/src/completion/complete_macro_in_item_position.rs
Guess macro braces from docs
[rust.git] / crates / ra_ide_api / src / completion / complete_macro_in_item_position.rs
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     CompletionItem {
42         label: "foo!",
43         source_range: [46; 46),
44         delete: [46; 46),
45         insert: "foo!($0)",
46         kind: Macro,
47         detail: "macro_rules! foo",
48     },
49 ]"##
50         );
51     }
52
53     #[test]
54     fn completes_vec_macros_with_square_brackets() {
55         assert_debug_snapshot!(
56             do_reference_completion(
57                 "
58                 //- /main.rs
59                 /// Creates a [`Vec`] containing the arguments.
60                 ///
61                 /// - Create a [`Vec`] containing a given list of elements:
62                 ///
63                 /// ```
64                 /// let v = vec![1, 2, 3];
65                 /// assert_eq!(v[0], 1);
66                 /// assert_eq!(v[1], 2);
67                 /// assert_eq!(v[2], 3);
68                 /// ```
69                 macro_rules! vec {
70                     () => {}
71                 }
72
73                 fn foo() {}
74
75                 <|>
76                 "
77             ),
78             @r##"[
79     CompletionItem {
80         label: "vec!",
81         source_range: [280; 280),
82         delete: [280; 280),
83         insert: "vec![$0]",
84         kind: Macro,
85         detail: "macro_rules! vec",
86         documentation: Documentation(
87             "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```",
88         ),
89     },
90 ]"##
91         );
92     }
93
94     #[test]
95     fn completes_macros_braces_guessing() {
96         assert_debug_snapshot!(
97             do_reference_completion(
98                 "
99                 //- /main.rs
100                 /// Foo
101                 ///
102                 /// Not call `fooo!()` `fooo!()`, or `_foo![]` `_foo![]`.
103                 /// Call as `let _=foo!  { hello world };`
104                 macro_rules! foo {
105                     () => {}
106                 }
107
108                 fn main() {
109                     <|>
110                 }
111                 "
112             ),
113             @r###"[
114     CompletionItem {
115         label: "foo!",
116         source_range: [163; 163),
117         delete: [163; 163),
118         insert: "foo! {$0}",
119         kind: Macro,
120         detail: "macro_rules! foo",
121         documentation: Documentation(
122             "Foo\n\nNot call `fooo!()` `fooo!()`, or `_foo![]` `_foo![]`.\nCall as `let _=foo!  { hello world };`",
123         ),
124     },
125     CompletionItem {
126         label: "main()",
127         source_range: [163; 163),
128         delete: [163; 163),
129         insert: "main()$0",
130         kind: Function,
131         lookup: "main",
132         detail: "fn main()",
133     },
134 ]
135         "###
136         );
137     }
138 }