From 44c76d6550081552c3c5106b0535a7e5bf265aec Mon Sep 17 00:00:00 2001 From: Lukas Wirth Date: Sat, 5 Dec 2020 15:41:36 +0100 Subject: Add replace_match_with_if_let assist --- crates/syntax/src/ast/make.rs | 13 +++++++++++-- 1 file changed, 11 insertions(+), 2 deletions(-) (limited to 'crates/syntax') diff --git a/crates/syntax/src/ast/make.rs b/crates/syntax/src/ast/make.rs index 876659a2b..cc09b77a5 100644 --- a/crates/syntax/src/ast/make.rs +++ b/crates/syntax/src/ast/make.rs @@ -171,8 +171,17 @@ pub fn expr_return() -> ast::Expr { pub fn expr_match(expr: ast::Expr, match_arm_list: ast::MatchArmList) -> ast::Expr { expr_from_text(&format!("match {} {}", expr, match_arm_list)) } -pub fn expr_if(condition: ast::Condition, then_branch: ast::BlockExpr) -> ast::Expr { - expr_from_text(&format!("if {} {}", condition, then_branch)) +pub fn expr_if( + condition: ast::Condition, + then_branch: ast::BlockExpr, + else_branch: Option, +) -> ast::Expr { + let else_branch = match else_branch { + Some(ast::ElseBranch::Block(block)) => format!("else {}", block), + Some(ast::ElseBranch::IfExpr(if_expr)) => format!("else {}", if_expr), + None => String::new(), + }; + expr_from_text(&format!("if {} {} {}", condition, then_branch, else_branch)) } pub fn expr_prefix(op: SyntaxKind, expr: ast::Expr) -> ast::Expr { let token = token(op); -- cgit v1.2.3