Skip to content

Commit

Permalink
first draft, tests OK
Browse files Browse the repository at this point in the history
  • Loading branch information
Sjael committed Jan 19, 2024
1 parent 6356fca commit 58fd26a
Show file tree
Hide file tree
Showing 4 changed files with 70 additions and 3 deletions.
34 changes: 32 additions & 2 deletions src/expr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1102,8 +1102,14 @@ impl<'a> Rewrite for ControlFlow<'a> {
};
let block_str = {
let old_val = context.is_if_else_block.replace(self.else_block.is_some());
let result =
rewrite_block_with_visitor(context, "", self.block, None, None, block_shape, true);
let allow_single_line =
allow_single_line_if(&cond_str, self.block) && self.keyword == "if";

let result = if allow_single_line {
rewrite_block_inner(self.block, None, None, true, context, block_shape)
} else {
rewrite_block_with_visitor(context, "", self.block, None, None, block_shape, true)
};
context.is_if_else_block.replace(old_val);
result?
};
Expand Down Expand Up @@ -1158,6 +1164,30 @@ impl<'a> Rewrite for ControlFlow<'a> {
}
}

fn allow_single_line_if(result: &str, block: &ast::Block) -> bool {
if result.contains('\n') {
return false;
}

if block.stmts.len() == 0 {
return true;
}
if block.stmts.len() == 1 {
return is_simple_stmt(&block.stmts[0]);
}
false
}

fn is_simple_stmt(stmt: &ast::Stmt) -> bool {
match stmt.kind {
ast::StmtKind::Expr(ref expr) => match expr.kind {
ast::ExprKind::Ret(..) | ast::ExprKind::Continue(..) | ast::ExprKind::Break(..) => true,
_ => false,
},
_ => false,
}
}

fn rewrite_label(opt_label: Option<ast::Label>) -> Cow<'static, str> {
match opt_label {
Some(label) => Cow::from(format!("{}: ", label.ident)),
Expand Down
2 changes: 1 addition & 1 deletion tests/source/expr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ fn bar() {
let bar = 5 ;
let nonsense = (10 .. 0)..(0..10);

loop{if true {break}}
loop{if true {break;}}

let x = (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa &&
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,
Expand Down
16 changes: 16 additions & 0 deletions tests/source/single-line-if-else.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,22 @@ fn main() {
do_something()
}

let a = if x { 1 } else { 3 };

// if may be formatted on a single line if it is "short"
// and only contain a single expression
if true { return }

if true {
return
}

if true { return; }

if a { let y = 1; return y }

for i in 0..2 { if g == true { continue } }

let x = if veeeeeeeeery_loooooong_condition() { aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa } else { bbbbbbbbbb };

let x = if veeeeeeeeery_loooooong_condition() { aaaaaaaaaaaaaaaaaaaaaaaaa } else {
Expand Down
21 changes: 21 additions & 0 deletions tests/target/single-line-if-else.rs
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,27 @@ fn main() {
do_something()
}

let a = if x { 1 } else { 3 };

// if may be formatted on a single line if it is "short"
// and only contain a single expression
if true { return }

if true { return }

if true {
return;
}

if a {
let y = 1;
return y;
}

for i in 0..2 {
if g == true { continue }
}

let x = if veeeeeeeeery_loooooong_condition() {
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
} else {
Expand Down

0 comments on commit 58fd26a

Please sign in to comment.