aboutsummaryrefslogtreecommitdiff
path: root/crates/ra_ide/src/completion/complete_snippet.rs
diff options
context:
space:
mode:
Diffstat (limited to 'crates/ra_ide/src/completion/complete_snippet.rs')
-rw-r--r--crates/ra_ide/src/completion/complete_snippet.rs120
1 files changed, 120 insertions, 0 deletions
diff --git a/crates/ra_ide/src/completion/complete_snippet.rs b/crates/ra_ide/src/completion/complete_snippet.rs
new file mode 100644
index 000000000..1f2988b36
--- /dev/null
+++ b/crates/ra_ide/src/completion/complete_snippet.rs
@@ -0,0 +1,120 @@
1//! FIXME: write short doc here
2
3use crate::completion::{
4 completion_item::Builder, CompletionContext, CompletionItem, CompletionItemKind,
5 CompletionKind, Completions,
6};
7
8fn snippet(ctx: &CompletionContext, label: &str, snippet: &str) -> Builder {
9 CompletionItem::new(CompletionKind::Snippet, ctx.source_range(), label)
10 .insert_snippet(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
19 snippet(ctx, "pd", "eprintln!(\"$0 = {:?}\", $0);").add_to(acc);
20 snippet(ctx, "ppd", "eprintln!(\"$0 = {:#?}\", $0);").add_to(acc);
21}
22
23pub(super) fn complete_item_snippet(acc: &mut Completions, ctx: &CompletionContext) {
24 if !ctx.is_new_item {
25 return;
26 }
27 snippet(
28 ctx,
29 "Test function",
30 "\
31#[test]
32fn ${1:feature}() {
33 $0
34}",
35 )
36 .lookup_by("tfn")
37 .add_to(acc);
38
39 snippet(ctx, "pub(crate)", "pub(crate) $0").add_to(acc);
40}
41
42#[cfg(test)]
43mod tests {
44 use crate::completion::{do_completion, CompletionItem, CompletionKind};
45 use insta::assert_debug_snapshot;
46
47 fn do_snippet_completion(code: &str) -> Vec<CompletionItem> {
48 do_completion(code, CompletionKind::Snippet)
49 }
50
51 #[test]
52 fn completes_snippets_in_expressions() {
53 assert_debug_snapshot!(
54 do_snippet_completion(r"fn foo(x: i32) { <|> }"),
55 @r###"
56 [
57 CompletionItem {
58 label: "pd",
59 source_range: [17; 17),
60 delete: [17; 17),
61 insert: "eprintln!(\"$0 = {:?}\", $0);",
62 kind: Snippet,
63 },
64 CompletionItem {
65 label: "ppd",
66 source_range: [17; 17),
67 delete: [17; 17),
68 insert: "eprintln!(\"$0 = {:#?}\", $0);",
69 kind: Snippet,
70 },
71 ]
72 "###
73 );
74 }
75
76 #[test]
77 fn should_not_complete_snippets_in_path() {
78 assert_debug_snapshot!(
79 do_snippet_completion(r"fn foo(x: i32) { ::foo<|> }"),
80 @"[]"
81 );
82 assert_debug_snapshot!(
83 do_snippet_completion(r"fn foo(x: i32) { ::<|> }"),
84 @"[]"
85 );
86 }
87
88 #[test]
89 fn completes_snippets_in_items() {
90 assert_debug_snapshot!(
91 do_snippet_completion(
92 r"
93 #[cfg(test)]
94 mod tests {
95 <|>
96 }
97 "
98 ),
99 @r###"
100 [
101 CompletionItem {
102 label: "Test function",
103 source_range: [78; 78),
104 delete: [78; 78),
105 insert: "#[test]\nfn ${1:feature}() {\n $0\n}",
106 kind: Snippet,
107 lookup: "tfn",
108 },
109 CompletionItem {
110 label: "pub(crate)",
111 source_range: [78; 78),
112 delete: [78; 78),
113 insert: "pub(crate) $0",
114 kind: Snippet,
115 },
116 ]
117 "###
118 );
119 }
120}