From cbb53cf55ca350bbcada5fc759b0119d932e879d Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Fri, 27 Mar 2020 11:38:00 +0100 Subject: More general ctor for ifs --- crates/ra_syntax/src/ast/make.rs | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) (limited to 'crates/ra_syntax/src') diff --git a/crates/ra_syntax/src/ast/make.rs b/crates/ra_syntax/src/ast/make.rs index ffc874a10..6aee39203 100644 --- a/crates/ra_syntax/src/ast/make.rs +++ b/crates/ra_syntax/src/ast/make.rs @@ -108,7 +108,7 @@ 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::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)) } pub fn expr_prefix(op: SyntaxKind, expr: ast::Expr) -> ast::Expr { @@ -123,6 +123,15 @@ pub fn try_expr_from_text(text: &str) -> Option { try_ast_from_text(&format!("const C: () = {};", text)) } +pub fn condition(expr: ast::Expr, pattern: Option) -> 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 { return from_text(name.text()); -- cgit v1.2.3