aboutsummaryrefslogtreecommitdiff
path: root/crates/ide/src/completion/complete_snippet.rs
diff options
context:
space:
mode:
Diffstat (limited to 'crates/ide/src/completion/complete_snippet.rs')
-rw-r--r--crates/ide/src/completion/complete_snippet.rs116
1 files changed, 116 insertions, 0 deletions
diff --git a/crates/ide/src/completion/complete_snippet.rs b/crates/ide/src/completion/complete_snippet.rs
new file mode 100644
index 000000000..4368e4eec
--- /dev/null
+++ b/crates/ide/src/completion/complete_snippet.rs
@@ -0,0 +1,116 @@
1//! FIXME: write short doc here
2
3use crate::completion::{
4 completion_config::SnippetCap, completion_item::Builder, CompletionContext, CompletionItem,
5 CompletionItemKind, CompletionKind, Completions,
6};
7
8fn snippet(ctx: &CompletionContext, cap: SnippetCap, label: &str, snippet: &str) -> Builder {
9 CompletionItem::new(CompletionKind::Snippet, ctx.source_range(), label)
10 .insert_snippet(cap, snippet)
11 .kind(CompletionItemKind::Snippet)
12}
13
14pub(super) fn complete_expr_snippet(acc: &mut Completions, ctx: &CompletionContext) {
15 if !(ctx.is_trivial_path && ctx.function_syntax.is_some()) {
16 return;
17 }
18 let cap = match ctx.config.snippet_cap {
19 Some(it) => it,
20 None => return,
21 };
22
23 snippet(ctx, cap, "pd", "eprintln!(\"$0 = {:?}\", $0);").add_to(acc);
24 snippet(ctx, cap, "ppd", "eprintln!(\"$0 = {:#?}\", $0);").add_to(acc);
25}
26
27pub(super) fn complete_item_snippet(acc: &mut Completions, ctx: &CompletionContext) {
28 if !ctx.is_new_item {
29 return;
30 }
31 let cap = match ctx.config.snippet_cap {
32 Some(it) => it,
33 None => return,
34 };
35
36 snippet(
37 ctx,
38 cap,
39 "tmod (Test module)",
40 "\
41#[cfg(test)]
42mod tests {
43 use super::*;
44
45 #[test]
46 fn ${1:test_name}() {
47 $0
48 }
49}",
50 )
51 .lookup_by("tmod")
52 .add_to(acc);
53
54 snippet(
55 ctx,
56 cap,
57 "tfn (Test function)",
58 "\
59#[test]
60fn ${1:feature}() {
61 $0
62}",
63 )
64 .lookup_by("tfn")
65 .add_to(acc);
66
67 snippet(ctx, cap, "macro_rules", "macro_rules! $1 {\n\t($2) => {\n\t\t$0\n\t};\n}").add_to(acc);
68 snippet(ctx, cap, "pub(crate)", "pub(crate) $0").add_to(acc);
69}
70
71#[cfg(test)]
72mod tests {
73 use expect::{expect, Expect};
74
75 use crate::completion::{test_utils::completion_list, CompletionKind};
76
77 fn check(ra_fixture: &str, expect: Expect) {
78 let actual = completion_list(ra_fixture, CompletionKind::Snippet);
79 expect.assert_eq(&actual)
80 }
81
82 #[test]
83 fn completes_snippets_in_expressions() {
84 check(
85 r#"fn foo(x: i32) { <|> }"#,
86 expect![[r#"
87 sn pd
88 sn ppd
89 "#]],
90 );
91 }
92
93 #[test]
94 fn should_not_complete_snippets_in_path() {
95 check(r#"fn foo(x: i32) { ::foo<|> }"#, expect![[""]]);
96 check(r#"fn foo(x: i32) { ::<|> }"#, expect![[""]]);
97 }
98
99 #[test]
100 fn completes_snippets_in_items() {
101 check(
102 r#"
103#[cfg(test)]
104mod tests {
105 <|>
106}
107"#,
108 expect![[r#"
109 sn macro_rules
110 sn pub(crate)
111 sn tfn (Test function)
112 sn tmod (Test module)
113 "#]],
114 )
115 }
116}