Skip to content

Commit

Permalink
feat: add FitEntireBody as new match_arm_wrapping option, and rename …
Browse files Browse the repository at this point in the history
…NoBlockFirstLine to FitFirstLine (rust-lang#4896)
  • Loading branch information
ashvin021 committed Jul 27, 2021
1 parent 740dfb8 commit f17c8fb
Show file tree
Hide file tree
Showing 4 changed files with 12 additions and 13 deletions.
2 changes: 1 addition & 1 deletion src/config/config_type.rs
Original file line number Diff line number Diff line change
Expand Up @@ -435,7 +435,7 @@ macro_rules! create_config {
self.match_arm_wrapping.2 = if self.match_arm_blocks() {
MatchArmWrapping::Default
} else {
MatchArmWrapping::NoBlockFirstLine
MatchArmWrapping::FitFirstLine
};
}
}
Expand Down
12 changes: 3 additions & 9 deletions src/config/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -745,10 +745,7 @@ make_backup = false
match_arm_blocks = false
"#;
let config = Config::from_toml(toml, Path::new("")).unwrap();
assert_eq!(
config.match_arm_wrapping(),
MatchArmWrapping::NoBlockFirstLine
);
assert_eq!(config.match_arm_wrapping(), MatchArmWrapping::FitFirstLine);
}

#[test]
Expand Down Expand Up @@ -786,15 +783,12 @@ make_backup = false
}
let toml = r#"
unstable_features = true
match_arm_wrapping = "NoBlockFirstLine"
match_arm_wrapping = "FitFirstLine"
"#;
let mut config = Config::from_toml(toml, Path::new("")).unwrap();
config.override_value("match_arm_blocks", "false");
// no effect: the new option always takes precedence
assert_eq!(
config.match_arm_wrapping(),
MatchArmWrapping::NoBlockFirstLine
);
assert_eq!(config.match_arm_wrapping(), MatchArmWrapping::FitFirstLine);
}
}

Expand Down
5 changes: 4 additions & 1 deletion src/config/options.rs
Original file line number Diff line number Diff line change
Expand Up @@ -450,9 +450,12 @@ pub enum MatchArmWrapping {
Default,
/// 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,
FitFirstLine,
/// Always block wrap match arms
Always,
/// Preserve the block wrapping on match arms
Preserve,
/// Same as Default, except wrap the match arm if the entire body cannot fit on the same line
/// as the `=>`.
FitEntireBody,
}
6 changes: 4 additions & 2 deletions src/matches.rs
Original file line number Diff line number Diff line change
Expand Up @@ -420,7 +420,7 @@ 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::NoBlockFirstLine => ("", String::from(",")),
MatchArmWrapping::FitFirstLine => ("", String::from(",")),
_ if !context.inside_macro() => {
let comma = if context.config.match_block_trailing_comma() {
","
Expand Down Expand Up @@ -501,7 +501,9 @@ fn rewrite_match_body(

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) =>
if prefer_next_line(orig_str, next_line_str, RhsTactics::Default)
|| (context.config.match_arm_wrapping() == MatchArmWrapping::FitEntireBody
&& orig_str.contains('\n')) =>
{
combine_next_line_body(next_line_str)
}
Expand Down

0 comments on commit f17c8fb

Please sign in to comment.