diff options
author | bors[bot] <26634292+bors[bot]@users.noreply.github.com> | 2020-02-07 11:34:31 +0000 |
---|---|---|
committer | GitHub <[email protected]> | 2020-02-07 11:34:31 +0000 |
commit | 1479dd6f2df1eecdd432cf72c91c4ee988b54c77 (patch) | |
tree | b5e467df6bf55bcd6fcdb849e5422f3720ec7006 /crates/ra_syntax/src/ast | |
parent | 5aba5a756a19a54d5c4edd51d8055db36182688b (diff) | |
parent | 36ee9ecb678d775609bf3825f1c4fd8e0c56bf32 (diff) |
Merge #3045
3045: Cleanup early return assist r=matklad a=matklad
Co-authored-by: Aleksey Kladov <[email protected]>
Diffstat (limited to 'crates/ra_syntax/src/ast')
-rw-r--r-- | crates/ra_syntax/src/ast/make.rs | 35 |
1 files changed, 26 insertions, 9 deletions
diff --git a/crates/ra_syntax/src/ast/make.rs b/crates/ra_syntax/src/ast/make.rs index 02966a3ff..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 | ||
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 | |||
36 | pub fn block_from_expr(e: ast::Expr) -> ast::Block { | 51 | pub 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,13 @@ pub fn expr_return() -> ast::Expr { | |||
62 | pub fn expr_match(expr: ast::Expr, match_arm_list: ast::MatchArmList) -> ast::Expr { | 77 | pub 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 | } |
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 | } | ||
83 | pub fn expr_prefix(op: SyntaxKind, expr: ast::Expr) -> ast::Expr { | ||
84 | let token = token(op); | ||
85 | expr_from_text(&format!("{}{}", token, expr.syntax())) | ||
86 | } | ||
65 | fn expr_from_text(text: &str) -> ast::Expr { | 87 | fn expr_from_text(text: &str) -> ast::Expr { |
66 | ast_from_text(&format!("const C: () = {};", text)) | 88 | ast_from_text(&format!("const C: () = {};", text)) |
67 | } | 89 | } |
@@ -158,14 +180,6 @@ pub fn where_clause(preds: impl IntoIterator<Item = ast::WherePred>) -> ast::Whe | |||
158 | } | 180 | } |
159 | } | 181 | } |
160 | 182 | ||
161 | pub fn if_expression(condition: ast::Expr, statement: &str) -> ast::IfExpr { | ||
162 | ast_from_text(&format!( | ||
163 | "fn f() {{ if !{} {{\n {}\n}}\n}}", | ||
164 | condition.syntax().text(), | ||
165 | statement | ||
166 | )) | ||
167 | } | ||
168 | |||
169 | pub fn let_stmt(pattern: ast::Pat, initializer: Option<ast::Expr>) -> ast::LetStmt { | 183 | pub fn let_stmt(pattern: ast::Pat, initializer: Option<ast::Expr>) -> ast::LetStmt { |
170 | let text = match initializer { | 184 | let text = match initializer { |
171 | Some(it) => format!("let {} = {};", pattern.syntax(), it.syntax()), | 185 | Some(it) => format!("let {} = {};", pattern.syntax(), it.syntax()), |
@@ -173,6 +187,9 @@ pub fn let_stmt(pattern: ast::Pat, initializer: Option<ast::Expr>) -> ast::LetSt | |||
173 | }; | 187 | }; |
174 | ast_from_text(&format!("fn f() {{ {} }}", text)) | 188 | ast_from_text(&format!("fn f() {{ {} }}", text)) |
175 | } | 189 | } |
190 | pub fn expr_stmt(expr: ast::Expr) -> ast::ExprStmt { | ||
191 | ast_from_text(&format!("fn f() {{ {}; }}", expr.syntax())) | ||
192 | } | ||
176 | 193 | ||
177 | pub fn token(kind: SyntaxKind) -> SyntaxToken { | 194 | pub fn token(kind: SyntaxKind) -> SyntaxToken { |
178 | tokens::SOURCE_FILE | 195 | tokens::SOURCE_FILE |
@@ -203,7 +220,7 @@ pub mod tokens { | |||
203 | use once_cell::sync::Lazy; | 220 | use once_cell::sync::Lazy; |
204 | 221 | ||
205 | pub(super) static SOURCE_FILE: Lazy<Parse<SourceFile>> = | 222 | pub(super) static SOURCE_FILE: Lazy<Parse<SourceFile>> = |
206 | Lazy::new(|| SourceFile::parse("const C: <()>::Item = (1 != 1, 2 == 2)\n;")); | 223 | Lazy::new(|| SourceFile::parse("const C: <()>::Item = (1 != 1, 2 == 2, !true)\n;")); |
207 | 224 | ||
208 | pub fn comma() -> SyntaxToken { | 225 | pub fn comma() -> SyntaxToken { |
209 | SOURCE_FILE | 226 | SOURCE_FILE |