From 36ee9ecb678d775609bf3825f1c4fd8e0c56bf32 Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Fri, 7 Feb 2020 11:51:16 +0100 Subject: Cleanup early return assist --- crates/ra_syntax/src/ast/make.rs | 29 +++++++++++++++++++++-------- 1 file changed, 21 insertions(+), 8 deletions(-) (limited to 'crates/ra_syntax/src/ast') diff --git a/crates/ra_syntax/src/ast/make.rs b/crates/ra_syntax/src/ast/make.rs index 982a7bcdc..862eb1172 100644 --- a/crates/ra_syntax/src/ast/make.rs +++ b/crates/ra_syntax/src/ast/make.rs @@ -33,6 +33,21 @@ pub fn record_field(name: ast::NameRef, expr: Option) -> ast::RecordF } } +pub fn block_expr( + stmts: impl IntoIterator, + tail_expr: Option, +) -> ast::BlockExpr { + let mut text = "{\n".to_string(); + for stmt in stmts.into_iter() { + text += &format!(" {}\n", stmt.syntax()); + } + if let Some(tail_expr) = tail_expr { + text += &format!(" {}\n", tail_expr.syntax()) + } + text += "}"; + ast_from_text(&format!("fn f() {}", text)) +} + pub fn block_from_expr(e: ast::Expr) -> ast::Block { return from_text(&format!("{{ {} }}", e.syntax())); @@ -62,6 +77,9 @@ 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.syntax(), match_arm_list.syntax())) } +pub fn expr_if(condition: ast::Expr, then_branch: ast::BlockExpr) -> ast::Expr { + expr_from_text(&format!("if {} {}", condition.syntax(), then_branch.syntax())) +} pub fn expr_prefix(op: SyntaxKind, expr: ast::Expr) -> ast::Expr { let token = token(op); expr_from_text(&format!("{}{}", token, expr.syntax())) @@ -162,14 +180,6 @@ pub fn where_clause(preds: impl IntoIterator) -> ast::Whe } } -pub fn if_expression(condition: ast::Expr, statement: &str) -> ast::IfExpr { - ast_from_text(&format!( - "fn f() {{ if !{} {{\n {}\n}}\n}}", - condition.syntax().text(), - statement - )) -} - pub fn let_stmt(pattern: ast::Pat, initializer: Option) -> ast::LetStmt { let text = match initializer { Some(it) => format!("let {} = {};", pattern.syntax(), it.syntax()), @@ -177,6 +187,9 @@ pub fn let_stmt(pattern: ast::Pat, initializer: Option) -> ast::LetSt }; ast_from_text(&format!("fn f() {{ {} }}", text)) } +pub fn expr_stmt(expr: ast::Expr) -> ast::ExprStmt { + ast_from_text(&format!("fn f() {{ {}; }}", expr.syntax())) +} pub fn token(kind: SyntaxKind) -> SyntaxToken { tokens::SOURCE_FILE -- cgit v1.2.3