Skip to content

Commit

Permalink
feat: add Preserve option to match_arm_wrapping (rust-lang#4896)
Browse files Browse the repository at this point in the history
  • Loading branch information
ashvin021 committed Jul 27, 2021
1 parent 663d7bc commit 740dfb8
Show file tree
Hide file tree
Showing 6 changed files with 67 additions and 17 deletions.
7 changes: 5 additions & 2 deletions src/config/options.rs
Original file line number Diff line number Diff line change
Expand Up @@ -448,8 +448,11 @@ pub enum MatchArmLeadingPipe {
pub enum MatchArmWrapping {
/// Follow the Style Guide Prescription
Default,
/// Don't block wrap when the first line can't fit
/// Same as Default, except don't block wrap match arms when the opening line of its body
/// can't fit on the same line as the `=>`.
NoBlockFirstLine,
/// Always wrap match arms
/// Always block wrap match arms
Always,
/// Preserve the block wrapping on match arms
Preserve,
}
7 changes: 6 additions & 1 deletion src/matches.rs
Original file line number Diff line number Diff line change
Expand Up @@ -326,6 +326,10 @@ fn flatten_arm_body<'a>(
if let ast::ExprKind::Block(..) = expr.kind {
flatten_arm_body(context, expr, None)
} else {
if context.config.match_arm_wrapping() == MatchArmWrapping::Preserve {
return (false, body);
}

let cond_becomes_muti_line = opt_shape
.and_then(|shape| rewrite_cond(context, expr, shape))
.map_or(false, |cond| cond.contains('\n'));
Expand Down Expand Up @@ -416,7 +420,8 @@ fn rewrite_match_body(

let indent_str = shape.indent.to_string_with_newline(context.config);
let (body_prefix, body_suffix) = match context.config.match_arm_wrapping() {
MatchArmWrapping::Default | MatchArmWrapping::Always if !context.inside_macro() => {
MatchArmWrapping::NoBlockFirstLine => ("", String::from(",")),
_ if !context.inside_macro() => {
let comma = if context.config.match_block_trailing_comma() {
","
} else {
Expand Down
14 changes: 7 additions & 7 deletions tests/source/configs/match_arm_wrapping/always.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,16 @@

fn main() {
match lorem {
1 => foooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooo(x),
2 => {
1000 => foooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooo(x),
2000 => {
println!("{}", sit)
}
3 => panic!(),
4 => (),
y => {
3000 => panic!(),
4000 => (),
ipsum => {
// Some comment
let ipsum = y - 1;
println!("{}", ipsum);
let dolor = ipsum % 2;
println!("{}", dolor);
}
}
}
20 changes: 20 additions & 0 deletions tests/source/configs/match_arm_wrapping/preserve.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
// rustfmt-match_arm_wrapping: Preserve
// Wrap match-arms

fn main() {
match lorem {
1000 => foooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooo(x),
2000 => {
println!("{}", sit)
}
3000 => panic!(),
4000 => {
()
}
ipsum => {
// Some comment
let dolor = ipsum % 2;
println!("{}", dolor);
}
}
}
14 changes: 7 additions & 7 deletions tests/target/configs/match_arm_wrapping/always.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,22 +3,22 @@

fn main() {
match lorem {
1 => {
1000 => {
foooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooo(x)
}
2 => {
2000 => {
println!("{}", sit)
}
3 => {
3000 => {
panic!()
}
4 => {
4000 => {
()
}
y => {
ipsum => {
// Some comment
let ipsum = y - 1;
println!("{}", ipsum);
let dolor = ipsum % 2;
println!("{}", dolor);
}
}
}
22 changes: 22 additions & 0 deletions tests/target/configs/match_arm_wrapping/preserve.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
// rustfmt-match_arm_wrapping: Preserve
// Wrap match-arms

fn main() {
match lorem {
1000 => {
foooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooo(x)
}
2000 => {
println!("{}", sit)
}
3000 => panic!(),
4000 => {
()
}
ipsum => {
// Some comment
let dolor = ipsum % 2;
println!("{}", dolor);
}
}
}

0 comments on commit 740dfb8

Please sign in to comment.