Skip to content

Commit

Permalink
feat: add Always option to match_arm_wrapping, with tests (rust-lang#…
Browse files Browse the repository at this point in the history
  • Loading branch information
ashvin021 committed Jul 28, 2021
1 parent 77193eb commit 14a4d02
Show file tree
Hide file tree
Showing 4 changed files with 65 additions and 20 deletions.
2 changes: 2 additions & 0 deletions src/config/options.rs
Original file line number Diff line number Diff line change
Expand Up @@ -450,4 +450,6 @@ pub enum MatchArmWrapping {
Default,
/// Don't block wrap when the first line can't fit
NoBlockFirstLine,
/// Always wrap match arms
Always,
}
41 changes: 21 additions & 20 deletions src/matches.rs
Original file line number Diff line number Diff line change
Expand Up @@ -415,27 +415,23 @@ fn rewrite_match_body(
}

let indent_str = shape.indent.to_string_with_newline(context.config);
let (body_prefix, body_suffix) = if context.config.match_arm_wrapping()
== MatchArmWrapping::Default
&& !context.inside_macro()
{
let comma = if context.config.match_block_trailing_comma() {
","
} else {
""
};
let semicolon = if context.config.version() == Version::One {
""
} else {
if semicolon_for_expr(context, body) {
";"
let (body_prefix, body_suffix) = match context.config.match_arm_wrapping() {
MatchArmWrapping::Default | MatchArmWrapping::Always if !context.inside_macro() => {
let comma = if context.config.match_block_trailing_comma() {
","
} else {
""
}
};
("{", format!("{}{}}}{}", semicolon, indent_str, comma))
} else {
("", String::from(","))
};

let semicolon = match context.config.version() {
Version::One => "",
_ if semicolon_for_expr(context, body) => ";",
_ => "",
};

("{", format!("{}{}}}{}", semicolon, indent_str, comma))
}
_ => ("", String::from(",")),
};

let block_sep = match context.config.control_brace_style() {
Expand Down Expand Up @@ -464,7 +460,10 @@ fn rewrite_match_body(
let orig_body_shape = shape
.offset_left(extra_offset(pats_str, shape) + 4)
.and_then(|shape| shape.sub_width(comma.len()));
let orig_body = if forbid_same_line || !arrow_comment.is_empty() {
let orig_body = if forbid_same_line
|| !arrow_comment.is_empty()
|| (!is_block && context.config.match_arm_wrapping() == MatchArmWrapping::Always)
{
None
} else if let Some(body_shape) = orig_body_shape {
let rewrite = nop_block_collapse(
Expand All @@ -485,6 +484,7 @@ fn rewrite_match_body(
} else {
None
};

let orig_budget = orig_body_shape.map_or(0, |shape| shape.width);

// Try putting body on the next line and see if it looks better.
Expand All @@ -493,6 +493,7 @@ fn rewrite_match_body(
format_expr(body, ExprType::Statement, context, next_line_body_shape),
next_line_body_shape.width,
);

match (orig_body, next_line_body) {
(Some(ref orig_str), Some(ref next_line_str))
if prefer_next_line(orig_str, next_line_str, RhsTactics::Default) =>
Expand Down
18 changes: 18 additions & 0 deletions tests/source/configs/match_arm_wrapping/always.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
// rustfmt-match_arm_wrapping: Always
// Wrap match-arms

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

fn main() {
match lorem {
1 => {
foooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooo(x)
}
2 => {
println!("{}", sit)
}
3 => {
panic!()
}
4 => {
()
}
y => {
// Some comment
let ipsum = y - 1;
println!("{}", ipsum);
}
}
}

0 comments on commit 14a4d02

Please sign in to comment.