Skip to content

Commit 36ee9ec

Browse files
committed
Cleanup early return assist
1 parent aa1234e commit 36ee9ec

File tree

2 files changed

+29
-11
lines changed

2 files changed

+29
-11
lines changed

crates/ra_assists/src/assists/early_return.rs

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ use ra_syntax::{
1010

1111
use crate::{
1212
assist_ctx::{Assist, AssistCtx},
13+
assists::invert_if::invert_boolean_expression,
1314
AssistId,
1415
};
1516

@@ -99,9 +100,13 @@ pub(crate) fn convert_to_guarded_return(ctx: AssistCtx) -> Option<Assist> {
99100
let new_block = match if_let_pat {
100101
None => {
101102
// If.
102-
let early_expression = &(early_expression.syntax().to_string() + ";");
103-
let new_expr = if_indent_level
104-
.increase_indent(make::if_expression(cond_expr, early_expression));
103+
let new_expr = {
104+
let then_branch =
105+
make::block_expr(once(make::expr_stmt(early_expression).into()), None);
106+
let cond = invert_boolean_expression(cond_expr);
107+
let e = make::expr_if(cond, then_branch);
108+
if_indent_level.increase_indent(e)
109+
};
105110
replace(new_expr.syntax(), &then_block, &parent_block, &if_expr)
106111
}
107112
Some((path, bound_ident)) => {

crates/ra_syntax/src/ast/make.rs

Lines changed: 21 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,21 @@ pub fn record_field(name: ast::NameRef, expr: Option<ast::Expr>) -> ast::RecordF
3333
}
3434
}
3535

36+
pub fn block_expr(
37+
stmts: impl IntoIterator<Item = ast::Stmt>,
38+
tail_expr: Option<ast::Expr>,
39+
) -> ast::BlockExpr {
40+
let mut text = "{\n".to_string();
41+
for stmt in stmts.into_iter() {
42+
text += &format!(" {}\n", stmt.syntax());
43+
}
44+
if let Some(tail_expr) = tail_expr {
45+
text += &format!(" {}\n", tail_expr.syntax())
46+
}
47+
text += "}";
48+
ast_from_text(&format!("fn f() {}", text))
49+
}
50+
3651
pub fn block_from_expr(e: ast::Expr) -> ast::Block {
3752
return from_text(&format!("{{ {} }}", e.syntax()));
3853

@@ -62,6 +77,9 @@ pub fn expr_return() -> ast::Expr {
6277
pub fn expr_match(expr: ast::Expr, match_arm_list: ast::MatchArmList) -> ast::Expr {
6378
expr_from_text(&format!("match {} {}", expr.syntax(), match_arm_list.syntax()))
6479
}
80+
pub fn expr_if(condition: ast::Expr, then_branch: ast::BlockExpr) -> ast::Expr {
81+
expr_from_text(&format!("if {} {}", condition.syntax(), then_branch.syntax()))
82+
}
6583
pub fn expr_prefix(op: SyntaxKind, expr: ast::Expr) -> ast::Expr {
6684
let token = token(op);
6785
expr_from_text(&format!("{}{}", token, expr.syntax()))
@@ -162,21 +180,16 @@ pub fn where_clause(preds: impl IntoIterator<Item = ast::WherePred>) -> ast::Whe
162180
}
163181
}
164182

165-
pub fn if_expression(condition: ast::Expr, statement: &str) -> ast::IfExpr {
166-
ast_from_text(&format!(
167-
"fn f() {{ if !{} {{\n {}\n}}\n}}",
168-
condition.syntax().text(),
169-
statement
170-
))
171-
}
172-
173183
pub fn let_stmt(pattern: ast::Pat, initializer: Option<ast::Expr>) -> ast::LetStmt {
174184
let text = match initializer {
175185
Some(it) => format!("let {} = {};", pattern.syntax(), it.syntax()),
176186
None => format!("let {};", pattern.syntax()),
177187
};
178188
ast_from_text(&format!("fn f() {{ {} }}", text))
179189
}
190+
pub fn expr_stmt(expr: ast::Expr) -> ast::ExprStmt {
191+
ast_from_text(&format!("fn f() {{ {}; }}", expr.syntax()))
192+
}
180193

181194
pub fn token(kind: SyntaxKind) -> SyntaxToken {
182195
tokens::SOURCE_FILE

0 commit comments

Comments
 (0)