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