aboutsummaryrefslogtreecommitdiff
path: root/crates/ra_ide_api/src/completion/complete_postfix.rs
diff options
context:
space:
mode:
Diffstat (limited to 'crates/ra_ide_api/src/completion/complete_postfix.rs')
-rw-r--r--crates/ra_ide_api/src/completion/complete_postfix.rs95
1 files changed, 95 insertions, 0 deletions
diff --git a/crates/ra_ide_api/src/completion/complete_postfix.rs b/crates/ra_ide_api/src/completion/complete_postfix.rs
new file mode 100644
index 000000000..cf0252a00
--- /dev/null
+++ b/crates/ra_ide_api/src/completion/complete_postfix.rs
@@ -0,0 +1,95 @@
1use crate::{
2 completion::{
3 completion_item::{
4 Completions,
5 Builder,
6 CompletionKind,
7 },
8 completion_context::CompletionContext,
9 },
10 CompletionItem
11};
12use ra_syntax::{
13 ast::AstNode,
14 TextRange
15};
16use ra_text_edit::TextEditBuilder;
17
18fn postfix_snippet(ctx: &CompletionContext, label: &str, snippet: &str) -> Builder {
19 let replace_range = ctx.source_range();
20 let receiver_range = ctx
21 .dot_receiver
22 .expect("no receiver available")
23 .syntax()
24 .range();
25 let delete_range = TextRange::from_to(receiver_range.start(), replace_range.start());
26 let mut builder = TextEditBuilder::default();
27 builder.delete(delete_range);
28 CompletionItem::new(CompletionKind::Postfix, replace_range, label)
29 .snippet(snippet)
30 .text_edit(builder.finish())
31}
32
33pub(super) fn complete_postfix(acc: &mut Completions, ctx: &CompletionContext) {
34 if let Some(dot_receiver) = ctx.dot_receiver {
35 let receiver_text = dot_receiver.syntax().text().to_string();
36 postfix_snippet(ctx, "not", "!not").add_to(acc);
37 postfix_snippet(ctx, "if", &format!("if {} {{$0}}", receiver_text)).add_to(acc);
38 postfix_snippet(
39 ctx,
40 "match",
41 &format!("match {} {{\n${{1:_}} => {{$0\\}},\n}}", receiver_text),
42 )
43 .add_to(acc);
44 postfix_snippet(ctx, "while", &format!("while {} {{\n$0\n}}", receiver_text)).add_to(acc);
45 }
46}
47
48#[cfg(test)]
49mod tests {
50 use crate::completion::completion_item::CompletionKind;
51 use crate::completion::completion_item::check_completion;
52
53 fn check_snippet_completion(code: &str, expected_completions: &str) {
54 check_completion(code, expected_completions, CompletionKind::Postfix);
55 }
56
57 #[test]
58 fn test_filter_postfix_completion1() {
59 check_snippet_completion(
60 "filter_postfix_completion1",
61 r#"
62 fn main() {
63 let bar = "a";
64 bar.<|>
65 }
66 "#,
67 );
68 }
69
70 #[test]
71 fn test_filter_postfix_completion2() {
72 check_snippet_completion(
73 "filter_postfix_completion2",
74 r#"
75 fn main() {
76 let bar = "a";
77 bar.i<|>
78 }
79 "#,
80 );
81 }
82
83 #[test]
84 fn test_filter_postfix_completion3() {
85 check_snippet_completion(
86 "filter_postfix_completion3",
87 r#"
88 fn main() {
89 let bar = "a";
90 bar.if<|>
91 }
92 "#,
93 );
94 }
95}