aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAleksey Kladov <[email protected]>2020-02-07 10:51:16 +0000
committerAleksey Kladov <[email protected]>2020-02-07 11:30:39 +0000
commit36ee9ecb678d775609bf3825f1c4fd8e0c56bf32 (patch)
treeb5e467df6bf55bcd6fcdb849e5422f3720ec7006
parentaa1234e02b1166c57dd2a3cd27fd0b0b3c6cba7e (diff)
Cleanup early return assist
-rw-r--r--crates/ra_assists/src/assists/early_return.rs11
-rw-r--r--crates/ra_syntax/src/ast/make.rs29
2 files changed, 29 insertions, 11 deletions
diff --git a/crates/ra_assists/src/assists/early_return.rs b/crates/ra_assists/src/assists/early_return.rs
index 3169be2b9..8f30dc586 100644
--- a/crates/ra_assists/src/assists/early_return.rs
+++ b/crates/ra_assists/src/assists/early_return.rs
@@ -10,6 +10,7 @@ use ra_syntax::{
10 10
11use crate::{ 11use crate::{
12 assist_ctx::{Assist, AssistCtx}, 12 assist_ctx::{Assist, AssistCtx},
13 assists::invert_if::invert_boolean_expression,
13 AssistId, 14 AssistId,
14}; 15};
15 16
@@ -99,9 +100,13 @@ pub(crate) fn convert_to_guarded_return(ctx: AssistCtx) -> Option<Assist> {
99 let new_block = match if_let_pat { 100 let new_block = match if_let_pat {
100 None => { 101 None => {
101 // If. 102 // If.
102 let early_expression = &(early_expression.syntax().to_string() + ";"); 103 let new_expr = {
103 let new_expr = if_indent_level 104 let then_branch =
104 .increase_indent(make::if_expression(cond_expr, early_expression)); 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 };
105 replace(new_expr.syntax(), &then_block, &parent_block, &if_expr) 110 replace(new_expr.syntax(), &then_block, &parent_block, &if_expr)
106 } 111 }
107 Some((path, bound_ident)) => { 112 Some((path, bound_ident)) => {
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::Expr>) -> ast::RecordF
33 } 33 }
34} 34}
35 35
36pub 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
36pub fn block_from_expr(e: ast::Expr) -> ast::Block { 51pub fn block_from_expr(e: ast::Expr) -> ast::Block {
37 return from_text(&format!("{{ {} }}", e.syntax())); 52 return from_text(&format!("{{ {} }}", e.syntax()));
38 53
@@ -62,6 +77,9 @@ pub fn expr_return() -> ast::Expr {
62pub fn expr_match(expr: ast::Expr, match_arm_list: ast::MatchArmList) -> ast::Expr { 77pub fn expr_match(expr: ast::Expr, match_arm_list: ast::MatchArmList) -> ast::Expr {
63 expr_from_text(&format!("match {} {}", expr.syntax(), match_arm_list.syntax())) 78 expr_from_text(&format!("match {} {}", expr.syntax(), match_arm_list.syntax()))
64} 79}
80pub fn expr_if(condition: ast::Expr, then_branch: ast::BlockExpr) -> ast::Expr {
81 expr_from_text(&format!("if {} {}", condition.syntax(), then_branch.syntax()))
82}
65pub fn expr_prefix(op: SyntaxKind, expr: ast::Expr) -> ast::Expr { 83pub fn expr_prefix(op: SyntaxKind, expr: ast::Expr) -> ast::Expr {
66 let token = token(op); 84 let token = token(op);
67 expr_from_text(&format!("{}{}", token, expr.syntax())) 85 expr_from_text(&format!("{}{}", token, expr.syntax()))
@@ -162,14 +180,6 @@ pub fn where_clause(preds: impl IntoIterator<Item = ast::WherePred>) -> ast::Whe
162 } 180 }
163} 181}
164 182
165pub 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
173pub fn let_stmt(pattern: ast::Pat, initializer: Option<ast::Expr>) -> ast::LetStmt { 183pub fn let_stmt(pattern: ast::Pat, initializer: Option<ast::Expr>) -> ast::LetStmt {
174 let text = match initializer { 184 let text = match initializer {
175 Some(it) => format!("let {} = {};", pattern.syntax(), it.syntax()), 185 Some(it) => format!("let {} = {};", pattern.syntax(), it.syntax()),
@@ -177,6 +187,9 @@ pub fn let_stmt(pattern: ast::Pat, initializer: Option<ast::Expr>) -> ast::LetSt
177 }; 187 };
178 ast_from_text(&format!("fn f() {{ {} }}", text)) 188 ast_from_text(&format!("fn f() {{ {} }}", text))
179} 189}
190pub fn expr_stmt(expr: ast::Expr) -> ast::ExprStmt {
191 ast_from_text(&format!("fn f() {{ {}; }}", expr.syntax()))
192}
180 193
181pub fn token(kind: SyntaxKind) -> SyntaxToken { 194pub fn token(kind: SyntaxKind) -> SyntaxToken {
182 tokens::SOURCE_FILE 195 tokens::SOURCE_FILE