mirror of
https://github.com/rust-lang/rust-analyzer.git
synced 2025-09-28 12:54:58 +00:00
Merge #3741
3741: More general ctor for ifs r=matklad a=matklad
bors r+
🤖
Co-authored-by: Aleksey Kladov <aleksey.kladov@gmail.com>
This commit is contained in:
commit
f9cf86475c
2 changed files with 11 additions and 2 deletions
|
@ -104,7 +104,7 @@ pub(crate) fn convert_to_guarded_return(ctx: AssistCtx) -> Option<Assist> {
|
||||||
let then_branch =
|
let then_branch =
|
||||||
make::block_expr(once(make::expr_stmt(early_expression).into()), None);
|
make::block_expr(once(make::expr_stmt(early_expression).into()), None);
|
||||||
let cond = invert_boolean_expression(cond_expr);
|
let cond = invert_boolean_expression(cond_expr);
|
||||||
let e = make::expr_if(cond, then_branch);
|
let e = make::expr_if(make::condition(cond, None), then_branch);
|
||||||
if_indent_level.increase_indent(e)
|
if_indent_level.increase_indent(e)
|
||||||
};
|
};
|
||||||
replace(new_expr.syntax(), &then_block, &parent_block, &if_expr)
|
replace(new_expr.syntax(), &then_block, &parent_block, &if_expr)
|
||||||
|
|
|
@ -108,7 +108,7 @@ pub fn expr_return() -> ast::Expr {
|
||||||
pub fn expr_match(expr: ast::Expr, match_arm_list: ast::MatchArmList) -> 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))
|
expr_from_text(&format!("match {} {}", expr, match_arm_list))
|
||||||
}
|
}
|
||||||
pub fn expr_if(condition: ast::Expr, then_branch: ast::BlockExpr) -> ast::Expr {
|
pub fn expr_if(condition: ast::Condition, then_branch: ast::BlockExpr) -> ast::Expr {
|
||||||
expr_from_text(&format!("if {} {}", condition, then_branch))
|
expr_from_text(&format!("if {} {}", condition, then_branch))
|
||||||
}
|
}
|
||||||
pub fn expr_prefix(op: SyntaxKind, expr: ast::Expr) -> ast::Expr {
|
pub fn expr_prefix(op: SyntaxKind, expr: ast::Expr) -> ast::Expr {
|
||||||
|
@ -123,6 +123,15 @@ pub fn try_expr_from_text(text: &str) -> Option<ast::Expr> {
|
||||||
try_ast_from_text(&format!("const C: () = {};", text))
|
try_ast_from_text(&format!("const C: () = {};", text))
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub fn condition(expr: ast::Expr, pattern: Option<ast::Pat>) -> ast::Condition {
|
||||||
|
match pattern {
|
||||||
|
None => ast_from_text(&format!("const _: () = while {} {{}};", expr)),
|
||||||
|
Some(pattern) => {
|
||||||
|
ast_from_text(&format!("const _: () = while {} = {} {{}};", pattern, expr))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
pub fn bind_pat(name: ast::Name) -> ast::BindPat {
|
pub fn bind_pat(name: ast::Name) -> ast::BindPat {
|
||||||
return from_text(name.text());
|
return from_text(name.text());
|
||||||
|
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue