aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAleksey Kladov <[email protected]>2020-02-24 16:17:05 +0000
committerAleksey Kladov <[email protected]>2020-02-24 16:17:05 +0000
commitf551e50c16d189a724885ce5f208595a31af49cc (patch)
treebf41cbde88fdf1003854bb0ccf8ef1dd39d40e46
parentcba3c991c8188e87363bbff190e9528606140808 (diff)
When joining lines, unwrap trivial diverging blocks
-rw-r--r--crates/ra_fmt/src/lib.rs36
-rw-r--r--crates/ra_ide/src/join_lines.rs25
2 files changed, 53 insertions, 8 deletions
diff --git a/crates/ra_fmt/src/lib.rs b/crates/ra_fmt/src/lib.rs
index 4bca27b5c..db27f9b83 100644
--- a/crates/ra_fmt/src/lib.rs
+++ b/crates/ra_fmt/src/lib.rs
@@ -43,15 +43,35 @@ pub fn unwrap_trivial_block(block: ast::BlockExpr) -> ast::Expr {
43 43
44pub fn extract_trivial_expression(block: &ast::BlockExpr) -> Option<ast::Expr> { 44pub fn extract_trivial_expression(block: &ast::BlockExpr) -> Option<ast::Expr> {
45 let block = block.block()?; 45 let block = block.block()?;
46 let expr = block.expr()?; 46 let has_anything_else = |thing: &SyntaxNode| -> bool {
47 let non_trivial_children = block.syntax().children().filter(|it| match it.kind() { 47 let mut non_trivial_children =
48 WHITESPACE | T!['{'] | T!['}'] => false, 48 block.syntax().children_with_tokens().filter(|it| match it.kind() {
49 _ => it != expr.syntax(), 49 WHITESPACE | T!['{'] | T!['}'] => false,
50 }); 50 _ => it.as_node() != Some(thing),
51 if non_trivial_children.count() > 0 { 51 });
52 return None; 52 non_trivial_children.next().is_some()
53 };
54
55 if let Some(expr) = block.expr() {
56 if has_anything_else(expr.syntax()) {
57 return None;
58 }
59 return Some(expr);
60 } else {
61 // Unwrap `{ continue; }`
62 let (stmt,) = block.statements().next_tuple()?;
63 if has_anything_else(stmt.syntax()) {
64 return None;
65 }
66 if let ast::Stmt::ExprStmt(expr_stmt) = stmt {
67 let expr = expr_stmt.expr()?;
68 match expr.syntax().kind() {
69 CONTINUE_EXPR | BREAK_EXPR | RETURN_EXPR => return Some(expr),
70 _ => (),
71 }
72 }
53 } 73 }
54 Some(expr) 74 None
55} 75}
56 76
57pub fn compute_ws(left: SyntaxKind, right: SyntaxKind) -> &'static str { 77pub fn compute_ws(left: SyntaxKind, right: SyntaxKind) -> &'static str {
diff --git a/crates/ra_ide/src/join_lines.rs b/crates/ra_ide/src/join_lines.rs
index 01fb32b3d..7d70dab9c 100644
--- a/crates/ra_ide/src/join_lines.rs
+++ b/crates/ra_ide/src/join_lines.rs
@@ -228,6 +228,31 @@ fn foo() {
228 } 228 }
229 229
230 #[test] 230 #[test]
231 fn test_join_lines_diverging_block() {
232 let before = r"
233 fn foo() {
234 loop {
235 match x {
236 92 => <|>{
237 continue;
238 }
239 }
240 }
241 }
242 ";
243 let after = r"
244 fn foo() {
245 loop {
246 match x {
247 92 => <|>continue,
248 }
249 }
250 }
251 ";
252 check_join_lines(before, after);
253 }
254
255 #[test]
231 fn join_lines_adds_comma_for_block_in_match_arm() { 256 fn join_lines_adds_comma_for_block_in_match_arm() {
232 check_join_lines( 257 check_join_lines(
233 r" 258 r"