aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--crates/ra_fmt/src/lib.rs23
-rw-r--r--crates/ra_ide/src/join_lines.rs66
-rw-r--r--crates/ra_syntax/src/ast/expr_extensions.rs5
-rw-r--r--crates/ra_syntax/src/ast/generated/nodes.rs1
-rw-r--r--xtask/src/ast_src.rs2
5 files changed, 83 insertions, 14 deletions
diff --git a/crates/ra_fmt/src/lib.rs b/crates/ra_fmt/src/lib.rs
index 0b4ba1bbe..1a30b2b3a 100644
--- a/crates/ra_fmt/src/lib.rs
+++ b/crates/ra_fmt/src/lib.rs
@@ -57,18 +57,17 @@ pub fn extract_trivial_expression(block: &ast::BlockExpr) -> Option<ast::Expr> {
57 return None; 57 return None;
58 } 58 }
59 return Some(expr); 59 return Some(expr);
60 } else { 60 }
61 // Unwrap `{ continue; }` 61 // Unwrap `{ continue; }`
62 let (stmt,) = block.statements().next_tuple()?; 62 let (stmt,) = block.statements().next_tuple()?;
63 if let ast::Stmt::ExprStmt(expr_stmt) = stmt { 63 if let ast::Stmt::ExprStmt(expr_stmt) = stmt {
64 if has_anything_else(expr_stmt.syntax()) { 64 if has_anything_else(expr_stmt.syntax()) {
65 return None; 65 return None;
66 } 66 }
67 let expr = expr_stmt.expr()?; 67 let expr = expr_stmt.expr()?;
68 match expr.syntax().kind() { 68 match expr.syntax().kind() {
69 CONTINUE_EXPR | BREAK_EXPR | RETURN_EXPR => return Some(expr), 69 CONTINUE_EXPR | BREAK_EXPR | RETURN_EXPR => return Some(expr),
70 _ => (), 70 _ => (),
71 }
72 } 71 }
73 } 72 }
74 None 73 None
diff --git a/crates/ra_ide/src/join_lines.rs b/crates/ra_ide/src/join_lines.rs
index fde0bfa98..d0def7eaa 100644
--- a/crates/ra_ide/src/join_lines.rs
+++ b/crates/ra_ide/src/join_lines.rs
@@ -131,6 +131,9 @@ fn has_comma_after(node: &SyntaxNode) -> bool {
131fn join_single_expr_block(edit: &mut TextEditBuilder, token: &SyntaxToken) -> Option<()> { 131fn join_single_expr_block(edit: &mut TextEditBuilder, token: &SyntaxToken) -> Option<()> {
132 let block = ast::Block::cast(token.parent())?; 132 let block = ast::Block::cast(token.parent())?;
133 let block_expr = ast::BlockExpr::cast(block.syntax().parent()?)?; 133 let block_expr = ast::BlockExpr::cast(block.syntax().parent()?)?;
134 if !block_expr.is_standalone() {
135 return None;
136 }
134 let expr = extract_trivial_expression(&block_expr)?; 137 let expr = extract_trivial_expression(&block_expr)?;
135 138
136 let block_range = block_expr.syntax().text_range(); 139 let block_range = block_expr.syntax().text_range();
@@ -662,4 +665,67 @@ fn main() {
662 ", 665 ",
663 ) 666 )
664 } 667 }
668
669 #[test]
670 fn join_lines_mandatory_blocks_block() {
671 check_join_lines(
672 r"
673<|>fn foo() {
674 92
675}
676 ",
677 r"
678<|>fn foo() { 92
679}
680 ",
681 );
682
683 check_join_lines(
684 r"
685fn foo() {
686 <|>if true {
687 92
688 }
689}
690 ",
691 r"
692fn foo() {
693 <|>if true { 92
694 }
695}
696 ",
697 );
698
699 check_join_lines(
700 r"
701fn foo() {
702 <|>loop {
703 92
704 }
705}
706 ",
707 r"
708fn foo() {
709 <|>loop { 92
710 }
711}
712 ",
713 );
714
715 check_join_lines(
716 r"
717fn foo() {
718 <|>unsafe {
719 92
720 }
721}
722 ",
723 r"
724fn foo() {
725 <|>unsafe { 92
726 }
727}
728 ",
729 );
730 }
665} 731}
diff --git a/crates/ra_syntax/src/ast/expr_extensions.rs b/crates/ra_syntax/src/ast/expr_extensions.rs
index 93aa3d45f..ecf74fd36 100644
--- a/crates/ra_syntax/src/ast/expr_extensions.rs
+++ b/crates/ra_syntax/src/ast/expr_extensions.rs
@@ -368,12 +368,15 @@ impl ast::BlockExpr {
368 /// const FOO: () = { stand_alone }; 368 /// const FOO: () = { stand_alone };
369 /// ``` 369 /// ```
370 pub fn is_standalone(&self) -> bool { 370 pub fn is_standalone(&self) -> bool {
371 if self.unsafe_token().is_some() || self.async_token().is_some() {
372 return false;
373 }
371 let kind = match self.syntax().parent() { 374 let kind = match self.syntax().parent() {
372 None => return true, 375 None => return true,
373 Some(it) => it.kind(), 376 Some(it) => it.kind(),
374 }; 377 };
375 match kind { 378 match kind {
376 FN_DEF | MATCH_ARM | IF_EXPR | WHILE_EXPR | LOOP_EXPR | TRY_BLOCK_EXPR => false, 379 FN_DEF | IF_EXPR | WHILE_EXPR | LOOP_EXPR | TRY_BLOCK_EXPR => false,
377 _ => true, 380 _ => true,
378 } 381 }
379 } 382 }
diff --git a/crates/ra_syntax/src/ast/generated/nodes.rs b/crates/ra_syntax/src/ast/generated/nodes.rs
index 3b5e05af9..d2253d4af 100644
--- a/crates/ra_syntax/src/ast/generated/nodes.rs
+++ b/crates/ra_syntax/src/ast/generated/nodes.rs
@@ -554,6 +554,7 @@ impl ast::AttrsOwner for BlockExpr {}
554impl BlockExpr { 554impl BlockExpr {
555 pub fn label(&self) -> Option<Label> { support::child(&self.syntax) } 555 pub fn label(&self) -> Option<Label> { support::child(&self.syntax) }
556 pub fn unsafe_token(&self) -> Option<SyntaxToken> { support::token(&self.syntax, T![unsafe]) } 556 pub fn unsafe_token(&self) -> Option<SyntaxToken> { support::token(&self.syntax, T![unsafe]) }
557 pub fn async_token(&self) -> Option<SyntaxToken> { support::token(&self.syntax, T![async]) }
557 pub fn block(&self) -> Option<Block> { support::child(&self.syntax) } 558 pub fn block(&self) -> Option<Block> { support::child(&self.syntax) }
558} 559}
559 560
diff --git a/xtask/src/ast_src.rs b/xtask/src/ast_src.rs
index 98c8644e4..c14804aad 100644
--- a/xtask/src/ast_src.rs
+++ b/xtask/src/ast_src.rs
@@ -451,7 +451,7 @@ pub(crate) const AST_SRC: AstSrc = AstSrc {
451 struct ContinueExpr: AttrsOwner { T![continue], T![lifetime] } 451 struct ContinueExpr: AttrsOwner { T![continue], T![lifetime] }
452 struct BreakExpr: AttrsOwner { T![break], T![lifetime], Expr } 452 struct BreakExpr: AttrsOwner { T![break], T![lifetime], Expr }
453 struct Label { T![lifetime] } 453 struct Label { T![lifetime] }
454 struct BlockExpr: AttrsOwner { Label, T![unsafe], Block } 454 struct BlockExpr: AttrsOwner { Label, T![unsafe], T![async], Block }
455 struct ReturnExpr: AttrsOwner { Expr } 455 struct ReturnExpr: AttrsOwner { Expr }
456 struct CallExpr: ArgListOwner { Expr } 456 struct CallExpr: ArgListOwner { Expr }
457 struct MethodCallExpr: AttrsOwner, ArgListOwner { 457 struct MethodCallExpr: AttrsOwner, ArgListOwner {