]> git.lizzy.rs Git - rust.git/blob - crates/ide_completion/src/completions/snippet.rs
Remove obsolete is_new_item field on CompletionContext
[rust.git] / crates / ide_completion / src / completions / snippet.rs
1 //! This file provides snippet completions, like `pd` => `eprintln!(...)`.
2
3 use ide_db::helpers::SnippetCap;
4
5 use crate::{
6     item::Builder, CompletionContext, CompletionItem, CompletionItemKind, CompletionKind,
7     Completions,
8 };
9
10 fn snippet(ctx: &CompletionContext, cap: SnippetCap, label: &str, snippet: &str) -> Builder {
11     let mut item = CompletionItem::new(CompletionKind::Snippet, ctx.source_range(), label);
12     item.insert_snippet(cap, snippet).kind(CompletionItemKind::Snippet);
13     item
14 }
15
16 pub(crate) fn complete_expr_snippet(acc: &mut Completions, ctx: &CompletionContext) {
17     if !(ctx.is_trivial_path && ctx.function_def.is_some()) {
18         return;
19     }
20     let cap = match ctx.config.snippet_cap {
21         Some(it) => it,
22         None => return,
23     };
24
25     if ctx.can_be_stmt {
26         snippet(ctx, cap, "pd", "eprintln!(\"$0 = {:?}\", $0);").add_to(acc);
27         snippet(ctx, cap, "ppd", "eprintln!(\"$0 = {:#?}\", $0);").add_to(acc);
28     }
29 }
30
31 pub(crate) fn complete_item_snippet(acc: &mut Completions, ctx: &CompletionContext) {
32     if !ctx.expects_item() {
33         return;
34     }
35     let cap = match ctx.config.snippet_cap {
36         Some(it) => it,
37         None => return,
38     };
39
40     let mut item = snippet(
41         ctx,
42         cap,
43         "tmod (Test module)",
44         "\
45 #[cfg(test)]
46 mod tests {
47     use super::*;
48
49     #[test]
50     fn ${1:test_name}() {
51         $0
52     }
53 }",
54     );
55     item.lookup_by("tmod");
56     item.add_to(acc);
57
58     let mut item = snippet(
59         ctx,
60         cap,
61         "tfn (Test function)",
62         "\
63 #[test]
64 fn ${1:feature}() {
65     $0
66 }",
67     );
68     item.lookup_by("tfn");
69     item.add_to(acc);
70
71     let item = snippet(ctx, cap, "macro_rules", "macro_rules! $1 {\n\t($2) => {\n\t\t$0\n\t};\n}");
72     item.add_to(acc);
73 }
74
75 #[cfg(test)]
76 mod tests {
77     use expect_test::{expect, Expect};
78
79     use crate::{test_utils::completion_list, CompletionKind};
80
81     fn check(ra_fixture: &str, expect: Expect) {
82         let actual = completion_list(ra_fixture, CompletionKind::Snippet);
83         expect.assert_eq(&actual)
84     }
85
86     #[test]
87     fn completes_snippets_in_expressions() {
88         check(
89             r#"fn foo(x: i32) { $0 }"#,
90             expect![[r#"
91                 sn pd
92                 sn ppd
93             "#]],
94         );
95     }
96
97     #[test]
98     fn should_not_complete_snippets_in_path() {
99         check(r#"fn foo(x: i32) { ::foo$0 }"#, expect![[""]]);
100         check(r#"fn foo(x: i32) { ::$0 }"#, expect![[""]]);
101     }
102
103     #[test]
104     fn completes_snippets_in_items() {
105         check(
106             r#"
107 #[cfg(test)]
108 mod tests {
109     $0
110 }
111 "#,
112             expect![[r#"
113                 sn tmod (Test module)
114                 sn tfn (Test function)
115                 sn macro_rules
116             "#]],
117         )
118     }
119 }