aboutsummaryrefslogtreecommitdiff
path: root/crates/ra_ide_api/src/completion/complete_postfix.rs
blob: b5c4d2ebd40e1f42fbc9e262ebb3e1310a254e57 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
use crate::{
    completion::{
        completion_item::{
            Completions,
            Builder,
            CompletionKind,
        },
        completion_context::CompletionContext,
    },
    CompletionItem
};
use ra_syntax::{
    ast::AstNode,
    TextRange
};
use ra_text_edit::TextEditBuilder;

fn postfix_snippet(ctx: &CompletionContext, label: &str, snippet: &str) -> Builder {
    let replace_range = ctx.source_range();
    let receiver_range = ctx
        .dot_receiver
        .expect("no receiver available")
        .syntax()
        .range();
    let delete_range = TextRange::from_to(receiver_range.start(), replace_range.start());
    let mut builder = TextEditBuilder::default();
    builder.delete(delete_range);
    CompletionItem::new(CompletionKind::Postfix, replace_range, label)
        .snippet(snippet)
        .text_edit(builder.finish())
}

pub(super) fn complete_postfix(acc: &mut Completions, ctx: &CompletionContext) {
    if let Some(dot_receiver) = ctx.dot_receiver {
        let receiver_text = dot_receiver.syntax().text().to_string();
        postfix_snippet(ctx, "not", &format!("!{}", receiver_text)).add_to(acc);
        postfix_snippet(ctx, "if", &format!("if {} {{$0}}", receiver_text)).add_to(acc);
        postfix_snippet(
            ctx,
            "match",
            &format!("match {} {{\n${{1:_}} => {{$0\\}},\n}}", receiver_text),
        )
        .add_to(acc);
        postfix_snippet(ctx, "while", &format!("while {} {{\n$0\n}}", receiver_text)).add_to(acc);
    }
}

#[cfg(test)]
mod tests {
    use crate::completion::completion_item::CompletionKind;
    use crate::completion::completion_item::check_completion;

    fn check_snippet_completion(test_name: &str, code: &str) {
        check_completion(test_name, code, CompletionKind::Postfix);
    }

    #[test]
    fn test_filter_postfix_completion1() {
        check_snippet_completion(
            "filter_postfix_completion1",
            r#"
            fn main() {
                let bar = "a";
                bar.<|>
            }
            "#,
        );
    }

    #[test]
    fn test_filter_postfix_completion2() {
        check_snippet_completion(
            "filter_postfix_completion2",
            r#"
            fn main() {
                let bar = "a";
                bar.i<|>
            }
            "#,
        );
    }

    #[test]
    fn test_filter_postfix_completion3() {
        check_snippet_completion(
            "filter_postfix_completion3",
            r#"
            fn main() {
                let bar = "a";
                bar.if<|>
            }
            "#,
        );
    }
}