aboutsummaryrefslogtreecommitdiff
path: root/crates/assists/src/handlers/replace_let_with_if_let.rs
blob: ed6d0c29be8dc6d75d2f8ddf2f83d0785431a3af (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
96
97
98
99
100
use std::iter::once;

use syntax::{
    ast::{
        self,
        edit::{AstNodeEdit, IndentLevel},
        make,
    },
    AstNode, T,
};

use crate::{utils::TryEnum, AssistContext, AssistId, AssistKind, Assists};

// Assist: replace_let_with_if_let
//
// Replaces `let` with an `if-let`.
//
// ```
// # enum Option<T> { Some(T), None }
//
// fn main(action: Action) {
//     <|>let x = compute();
// }
//
// fn compute() -> Option<i32> { None }
// ```
// ->
// ```
// # enum Option<T> { Some(T), None }
//
// fn main(action: Action) {
//     if let Some(x) = compute() {
//     }
// }
//
// fn compute() -> Option<i32> { None }
// ```
pub(crate) fn replace_let_with_if_let(acc: &mut Assists, ctx: &AssistContext) -> Option<()> {
    let let_kw = ctx.find_token_at_offset(T![let])?;
    let let_stmt = let_kw.ancestors().find_map(ast::LetStmt::cast)?;
    let init = let_stmt.initializer()?;
    let original_pat = let_stmt.pat()?;
    let ty = ctx.sema.type_of_expr(&init)?;
    let happy_variant = TryEnum::from_ty(&ctx.sema, &ty).map(|it| it.happy_case());

    let target = let_kw.text_range();
    acc.add(
        AssistId("replace_let_with_if_let", AssistKind::RefactorRewrite),
        "Replace with if-let",
        target,
        |edit| {
            let with_placeholder: ast::Pat = match happy_variant {
                None => make::wildcard_pat().into(),
                Some(var_name) => make::tuple_struct_pat(
                    make::path_unqualified(make::path_segment(make::name_ref(var_name))),
                    once(make::wildcard_pat().into()),
                )
                .into(),
            };
            let block =
                make::block_expr(None, None).indent(IndentLevel::from_node(let_stmt.syntax()));
            let if_ = make::expr_if(make::condition(init, Some(with_placeholder)), block);
            let stmt = make::expr_stmt(if_);

            let placeholder = stmt.syntax().descendants().find_map(ast::WildcardPat::cast).unwrap();
            let stmt = stmt.replace_descendant(placeholder.into(), original_pat);

            edit.replace_ast(ast::Stmt::from(let_stmt), ast::Stmt::from(stmt));
        },
    )
}

#[cfg(test)]
mod tests {
    use crate::tests::check_assist;

    use super::*;

    #[test]
    fn replace_let_unknown_enum() {
        check_assist(
            replace_let_with_if_let,
            r"
enum E<T> { X(T), Y(T) }

fn main() {
    <|>let x = E::X(92);
}
            ",
            r"
enum E<T> { X(T), Y(T) }

fn main() {
    if let x = E::X(92) {
    }
}
            ",
        )
    }
}