Skip to content

Commit 15cfa9a

Browse files
committed
Fix a bunch of false-positives in join-lines
1 parent 23c8896 commit 15cfa9a

File tree

5 files changed

+83
-14
lines changed

5 files changed

+83
-14
lines changed

crates/ra_fmt/src/lib.rs

Lines changed: 11 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -57,18 +57,17 @@ pub fn extract_trivial_expression(block: &ast::BlockExpr) -> Option<ast::Expr> {
5757
return None;
5858
}
5959
return Some(expr);
60-
} else {
61-
// Unwrap `{ continue; }`
62-
let (stmt,) = block.statements().next_tuple()?;
63-
if let ast::Stmt::ExprStmt(expr_stmt) = stmt {
64-
if has_anything_else(expr_stmt.syntax()) {
65-
return None;
66-
}
67-
let expr = expr_stmt.expr()?;
68-
match expr.syntax().kind() {
69-
CONTINUE_EXPR | BREAK_EXPR | RETURN_EXPR => return Some(expr),
70-
_ => (),
71-
}
60+
}
61+
// Unwrap `{ continue; }`
62+
let (stmt,) = block.statements().next_tuple()?;
63+
if let ast::Stmt::ExprStmt(expr_stmt) = stmt {
64+
if has_anything_else(expr_stmt.syntax()) {
65+
return None;
66+
}
67+
let expr = expr_stmt.expr()?;
68+
match expr.syntax().kind() {
69+
CONTINUE_EXPR | BREAK_EXPR | RETURN_EXPR => return Some(expr),
70+
_ => (),
7271
}
7372
}
7473
None

crates/ra_ide/src/join_lines.rs

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -131,6 +131,9 @@ fn has_comma_after(node: &SyntaxNode) -> bool {
131131
fn join_single_expr_block(edit: &mut TextEditBuilder, token: &SyntaxToken) -> Option<()> {
132132
let block = ast::Block::cast(token.parent())?;
133133
let block_expr = ast::BlockExpr::cast(block.syntax().parent()?)?;
134+
if !block_expr.is_standalone() {
135+
return None;
136+
}
134137
let expr = extract_trivial_expression(&block_expr)?;
135138

136139
let block_range = block_expr.syntax().text_range();
@@ -662,4 +665,67 @@ fn main() {
662665
",
663666
)
664667
}
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"
685+
fn foo() {
686+
<|>if true {
687+
92
688+
}
689+
}
690+
",
691+
r"
692+
fn foo() {
693+
<|>if true { 92
694+
}
695+
}
696+
",
697+
);
698+
699+
check_join_lines(
700+
r"
701+
fn foo() {
702+
<|>loop {
703+
92
704+
}
705+
}
706+
",
707+
r"
708+
fn foo() {
709+
<|>loop { 92
710+
}
711+
}
712+
",
713+
);
714+
715+
check_join_lines(
716+
r"
717+
fn foo() {
718+
<|>unsafe {
719+
92
720+
}
721+
}
722+
",
723+
r"
724+
fn foo() {
725+
<|>unsafe { 92
726+
}
727+
}
728+
",
729+
);
730+
}
665731
}

crates/ra_syntax/src/ast/expr_extensions.rs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -368,12 +368,15 @@ impl ast::BlockExpr {
368368
/// const FOO: () = { stand_alone };
369369
/// ```
370370
pub fn is_standalone(&self) -> bool {
371+
if self.unsafe_token().is_some() || self.async_token().is_some() {
372+
return false;
373+
}
371374
let kind = match self.syntax().parent() {
372375
None => return true,
373376
Some(it) => it.kind(),
374377
};
375378
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,
377380
_ => true,
378381
}
379382
}

crates/ra_syntax/src/ast/generated/nodes.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -554,6 +554,7 @@ impl ast::AttrsOwner for BlockExpr {}
554554
impl BlockExpr {
555555
pub fn label(&self) -> Option<Label> { support::child(&self.syntax) }
556556
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]) }
557558
pub fn block(&self) -> Option<Block> { support::child(&self.syntax) }
558559
}
559560

xtask/src/ast_src.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -451,7 +451,7 @@ pub(crate) const AST_SRC: AstSrc = AstSrc {
451451
struct ContinueExpr: AttrsOwner { T![continue], T![lifetime] }
452452
struct BreakExpr: AttrsOwner { T![break], T![lifetime], Expr }
453453
struct Label { T![lifetime] }
454-
struct BlockExpr: AttrsOwner { Label, T![unsafe], Block }
454+
struct BlockExpr: AttrsOwner { Label, T![unsafe], T![async], Block }
455455
struct ReturnExpr: AttrsOwner { Expr }
456456
struct CallExpr: ArgListOwner { Expr }
457457
struct MethodCallExpr: AttrsOwner, ArgListOwner {

0 commit comments

Comments
 (0)