aboutsummaryrefslogtreecommitdiff
path: root/crates/ra_analysis/src/completion/complete_snippet.rs
diff options
context:
space:
mode:
Diffstat (limited to 'crates/ra_analysis/src/completion/complete_snippet.rs')
-rw-r--r--crates/ra_analysis/src/completion/complete_snippet.rs76
1 files changed, 76 insertions, 0 deletions
diff --git a/crates/ra_analysis/src/completion/complete_snippet.rs b/crates/ra_analysis/src/completion/complete_snippet.rs
new file mode 100644
index 000000000..ccd68832b
--- /dev/null
+++ b/crates/ra_analysis/src/completion/complete_snippet.rs
@@ -0,0 +1,76 @@
1use crate::completion::{CompletionItem, Completions, CompletionKind::*, CompletionContext};
2
3pub(super) fn complete_expr_snippet(acc: &mut Completions, ctx: &CompletionContext) {
4 if !(ctx.is_trivial_path && ctx.enclosing_fn.is_some()) {
5 return;
6 }
7 CompletionItem::new("pd")
8 .snippet("eprintln!(\"$0 = {:?}\", $0);")
9 .kind(Snippet)
10 .add_to(acc);
11 CompletionItem::new("ppd")
12 .snippet("eprintln!(\"$0 = {:#?}\", $0);")
13 .kind(Snippet)
14 .add_to(acc);
15}
16
17pub(super) fn complete_item_snippet(acc: &mut Completions, ctx: &CompletionContext) {
18 if !ctx.is_new_item {
19 return;
20 }
21 CompletionItem::new("Test function")
22 .lookup_by("tfn")
23 .snippet(
24 "\
25#[test]
26fn ${1:feature}() {
27 $0
28}",
29 )
30 .kind(Snippet)
31 .add_to(acc);
32 CompletionItem::new("pub(crate)")
33 .snippet("pub(crate) $0")
34 .kind(Snippet)
35 .add_to(acc);
36}
37
38#[cfg(test)]
39mod tests {
40 use crate::completion::{CompletionKind, check_completion};
41 fn check_snippet_completion(code: &str, expected_completions: &str) {
42 check_completion(code, expected_completions, CompletionKind::Snippet);
43 }
44
45 #[test]
46 fn completes_snippets_in_expressions() {
47 check_snippet_completion(
48 r"fn foo(x: i32) { <|> }",
49 r##"
50 pd "eprintln!(\"$0 = {:?}\", $0);"
51 ppd "eprintln!(\"$0 = {:#?}\", $0);"
52 "##,
53 );
54 }
55
56 #[test]
57 fn completes_snippets_in_items() {
58 // check_snippet_completion(r"
59 // <|>
60 // ",
61 // r##"[CompletionItem { label: "Test function", lookup: None, snippet: Some("#[test]\nfn test_${1:feature}() {\n$0\n}"##,
62 // );
63 check_snippet_completion(
64 r"
65 #[cfg(test)]
66 mod tests {
67 <|>
68 }
69 ",
70 r##"
71 tfn "Test function" "#[test]\nfn ${1:feature}() {\n $0\n}"
72 pub(crate) "pub(crate) $0"
73 "##,
74 );
75 }
76}