Skip to content

Commit

Permalink
Add short_item_threshold config option
Browse files Browse the repository at this point in the history
Allow custom short item threshold values via config
  • Loading branch information
123vivekr committed Feb 13, 2022
1 parent 1e78a2b commit b071504
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 5 deletions.
8 changes: 8 additions & 0 deletions Configurations.md
Original file line number Diff line number Diff line change
Expand Up @@ -2200,6 +2200,14 @@ specific version of rustfmt is used in your CI, use this option.
- **Possible values**: any published version (e.g. `"0.3.8"`)
- **Stable**: No (tracking issue: [#3386](https://github.com/rust-lang/rustfmt/issues/3386))

## `short_item_threshold`

Maximum width threshold for a short item.

- **Default value**: `10`
- **Possible values**: any positive integer that is less than or equal to the value specified for [`max_width`](#max_width)
- **Stable**: Yes

## `skip_children`

Don't reformat out of line modules
Expand Down
18 changes: 18 additions & 0 deletions src/config/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,7 @@ create_config! {
// Misc.
remove_nested_parens: bool, true, true, "Remove nested parens";
combine_control_expr: bool, true, false, "Combine control expressions with function calls";
short_item_threshold: usize, 10, true, "Maximum width threshold for a short item";
overflow_delimited_expr: bool, false, false,
"Allow trailing bracket/brace delimited expressions to overflow";
struct_field_align_threshold: usize, 0, false,
Expand Down Expand Up @@ -589,6 +590,7 @@ spaces_around_ranges = false
binop_separator = "Front"
remove_nested_parens = true
combine_control_expr = true
short_item_threshold = 10
overflow_delimited_expr = false
struct_field_align_threshold = 0
enum_discrim_align_threshold = 0
Expand Down Expand Up @@ -902,6 +904,15 @@ make_backup = false
assert_eq!(config.single_line_if_else_max_width(), 70);
}

#[test]
fn test_short_item_threshold_config_exceeds_max_width() {
let toml = r#"
short_item_threshold = 10
"#;
let config = Config::from_toml(toml, Path::new("")).unwrap();
assert_eq!(config.short_item_threshold(), 10);
}

#[test]
fn test_override_fn_call_width_exceeds_max_width() {
let mut config = Config::default();
Expand Down Expand Up @@ -950,5 +961,12 @@ make_backup = false
config.override_value("single_line_if_else_max_width", "101");
assert_eq!(config.single_line_if_else_max_width(), 100);
}

#[test]
fn test_override_short_item_threshold_exceeds_max_width() {
let mut config = Config::default();
config.override_value("short_item_threshold", "20");
assert_eq!(config.short_item_threshold(), 20);
}
}
}
10 changes: 5 additions & 5 deletions src/overflow.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,6 @@ use crate::spanned::Spanned;
use crate::types::{can_be_overflowed_type, SegmentParam};
use crate::utils::{count_newlines, extra_offset, first_line_width, last_line_width, mk_sp};

const SHORT_ITEM_THRESHOLD: usize = 10;

/// A list of `format!`-like macros, that take a long format string and a list of arguments to
/// format.
///
Expand Down Expand Up @@ -572,7 +570,9 @@ impl<'a> Context<'a> {
if one_line {
tactic = DefinitiveListTactic::SpecialMacro(num_args_before);
};
} else if is_every_expr_simple(&self.items) && no_long_items(list_items) {
} else if is_every_expr_simple(&self.items)
&& no_long_items(list_items, self.context.config.short_item_threshold())
{
tactic = DefinitiveListTactic::Mixed;
}
}
Expand Down Expand Up @@ -755,9 +755,9 @@ fn shape_from_indent_style(
}
}

fn no_long_items(list: &[ListItem]) -> bool {
fn no_long_items(list: &[ListItem], short_item_threshold: usize) -> bool {
list.iter()
.all(|item| item.inner_as_ref().len() <= SHORT_ITEM_THRESHOLD)
.all(|item| item.inner_as_ref().len() <= short_item_threshold)
}

/// In case special-case style is required, returns an offset from which we start horizontal layout.
Expand Down

0 comments on commit b071504

Please sign in to comment.