diff --git a/crates/ra_fmt/src/lib.rs b/crates/ra_fmt/src/lib.rs index 0b4ba1bbe6..1a30b2b3ab 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 { return None; } return Some(expr); - } else { - // Unwrap `{ continue; }` - let (stmt,) = block.statements().next_tuple()?; - if let ast::Stmt::ExprStmt(expr_stmt) = stmt { - if has_anything_else(expr_stmt.syntax()) { - return None; - } - let expr = expr_stmt.expr()?; - match expr.syntax().kind() { - CONTINUE_EXPR | BREAK_EXPR | RETURN_EXPR => return Some(expr), - _ => (), - } + } + // Unwrap `{ continue; }` + let (stmt,) = block.statements().next_tuple()?; + if let ast::Stmt::ExprStmt(expr_stmt) = stmt { + if has_anything_else(expr_stmt.syntax()) { + return None; + } + let expr = expr_stmt.expr()?; + match expr.syntax().kind() { + CONTINUE_EXPR | BREAK_EXPR | RETURN_EXPR => return Some(expr), + _ => (), } } None diff --git a/crates/ra_ide/src/join_lines.rs b/crates/ra_ide/src/join_lines.rs index fde0bfa98b..d0def7eaaf 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 { fn join_single_expr_block(edit: &mut TextEditBuilder, token: &SyntaxToken) -> Option<()> { let block = ast::Block::cast(token.parent())?; let block_expr = ast::BlockExpr::cast(block.syntax().parent()?)?; + if !block_expr.is_standalone() { + return None; + } let expr = extract_trivial_expression(&block_expr)?; let block_range = block_expr.syntax().text_range(); @@ -662,4 +665,67 @@ fn main() { ", ) } + + #[test] + fn join_lines_mandatory_blocks_block() { + check_join_lines( + r" +<|>fn foo() { + 92 +} + ", + r" +<|>fn foo() { 92 +} + ", + ); + + check_join_lines( + r" +fn foo() { + <|>if true { + 92 + } +} + ", + r" +fn foo() { + <|>if true { 92 + } +} + ", + ); + + check_join_lines( + r" +fn foo() { + <|>loop { + 92 + } +} + ", + r" +fn foo() { + <|>loop { 92 + } +} + ", + ); + + check_join_lines( + r" +fn foo() { + <|>unsafe { + 92 + } +} + ", + r" +fn foo() { + <|>unsafe { 92 + } +} + ", + ); + } } diff --git a/crates/ra_syntax/src/ast/expr_extensions.rs b/crates/ra_syntax/src/ast/expr_extensions.rs index 93aa3d45fa..ecf74fd366 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 { /// const FOO: () = { stand_alone }; /// ``` pub fn is_standalone(&self) -> bool { + if self.unsafe_token().is_some() || self.async_token().is_some() { + return false; + } let kind = match self.syntax().parent() { None => return true, Some(it) => it.kind(), }; match kind { - FN_DEF | MATCH_ARM | IF_EXPR | WHILE_EXPR | LOOP_EXPR | TRY_BLOCK_EXPR => false, + FN_DEF | IF_EXPR | WHILE_EXPR | LOOP_EXPR | TRY_BLOCK_EXPR => false, _ => true, } } diff --git a/crates/ra_syntax/src/ast/generated/nodes.rs b/crates/ra_syntax/src/ast/generated/nodes.rs index 3b5e05af92..d2253d4af2 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 {} impl BlockExpr { pub fn label(&self) -> Option