Skip to content

Commit deb2ec3

Browse files
committed
Add short_item_threshold config option
Allow custom short item threshold values via config
1 parent 281bf03 commit deb2ec3

File tree

7 files changed

+72
-5
lines changed

7 files changed

+72
-5
lines changed

Configurations.md

+8
Original file line numberDiff line numberDiff line change
@@ -2200,6 +2200,14 @@ specific version of rustfmt is used in your CI, use this option.
22002200
- **Possible values**: any published version (e.g. `"0.3.8"`)
22012201
- **Stable**: No (tracking issue: [#3386](https://github.com/rust-lang/rustfmt/issues/3386))
22022202

2203+
## `short_item_threshold`
2204+
2205+
Maximum width threshold for a short item.
2206+
2207+
- **Default value**: `10`
2208+
- **Possible values**: any positive integer that is less than or equal to the value specified for [`max_width`](#max_width)
2209+
- **Stable**: Yes
2210+
22032211
## `skip_children`
22042212

22052213
Don't reformat out of line modules

src/config/mod.rs

+18
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,7 @@ create_config! {
106106
// Misc.
107107
remove_nested_parens: bool, true, true, "Remove nested parens";
108108
combine_control_expr: bool, true, false, "Combine control expressions with function calls";
109+
short_item_threshold: usize, 10, true, "Maximum width threshold for a short item";
109110
overflow_delimited_expr: bool, false, false,
110111
"Allow trailing bracket/brace delimited expressions to overflow";
111112
struct_field_align_threshold: usize, 0, false,
@@ -591,6 +592,7 @@ spaces_around_ranges = false
591592
binop_separator = "Front"
592593
remove_nested_parens = true
593594
combine_control_expr = true
595+
short_item_threshold = 10
594596
overflow_delimited_expr = false
595597
struct_field_align_threshold = 0
596598
enum_discrim_align_threshold = 0
@@ -904,6 +906,15 @@ make_backup = false
904906
assert_eq!(config.single_line_if_else_max_width(), 70);
905907
}
906908

909+
#[test]
910+
fn test_short_item_threshold_config_exceeds_max_width() {
911+
let toml = r#"
912+
short_item_threshold = 10
913+
"#;
914+
let config = Config::from_toml(toml, Path::new("")).unwrap();
915+
assert_eq!(config.short_item_threshold(), 10);
916+
}
917+
907918
#[test]
908919
fn test_override_fn_call_width_exceeds_max_width() {
909920
let mut config = Config::default();
@@ -952,5 +963,12 @@ make_backup = false
952963
config.override_value("single_line_if_else_max_width", "101");
953964
assert_eq!(config.single_line_if_else_max_width(), 100);
954965
}
966+
967+
#[test]
968+
fn test_override_short_item_threshold_exceeds_max_width() {
969+
let mut config = Config::default();
970+
config.override_value("short_item_threshold", "20");
971+
assert_eq!(config.short_item_threshold(), 20);
972+
}
955973
}
956974
}

src/overflow.rs

+5-5
Original file line numberDiff line numberDiff line change
@@ -26,8 +26,6 @@ use crate::spanned::Spanned;
2626
use crate::types::{can_be_overflowed_type, SegmentParam};
2727
use crate::utils::{count_newlines, extra_offset, first_line_width, last_line_width, mk_sp};
2828

29-
const SHORT_ITEM_THRESHOLD: usize = 10;
30-
3129
/// A list of `format!`-like macros, that take a long format string and a list of arguments to
3230
/// format.
3331
///
@@ -572,7 +570,9 @@ impl<'a> Context<'a> {
572570
if one_line {
573571
tactic = DefinitiveListTactic::SpecialMacro(num_args_before);
574572
};
575-
} else if is_every_expr_simple(&self.items) && no_long_items(list_items) {
573+
} else if is_every_expr_simple(&self.items)
574+
&& no_long_items(list_items, self.context.config.short_item_threshold())
575+
{
576576
tactic = DefinitiveListTactic::Mixed;
577577
}
578578
}
@@ -755,9 +755,9 @@ fn shape_from_indent_style(
755755
}
756756
}
757757

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

763763
/// In case special-case style is required, returns an offset from which we start horizontal layout.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
// rustfmt-short_item_threshold: 10
2+
3+
fn main() {
4+
pub const FORMAT_TEST: [u64; 5] = [
5+
0x0000000000000000,
6+
0xaaaaaaaaaaaaaaaa,
7+
0xbbbbbbbbbbbbbbbb,
8+
0xcccccccccccccccc,
9+
0xdddddddddddddddd,
10+
];
11+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
// rustfmt-short_item_threshold: 20
2+
3+
fn main() {
4+
pub const FORMAT_TEST: [u64; 5] = [
5+
0x0000000000000000,
6+
0xaaaaaaaaaaaaaaaa,
7+
0xbbbbbbbbbbbbbbbb,
8+
0xcccccccccccccccc,
9+
0xdddddddddddddddd,
10+
];
11+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
// rustfmt-short_item_threshold: 10
2+
3+
fn main() {
4+
pub const FORMAT_TEST: [u64; 5] = [
5+
0x0000000000000000,
6+
0xaaaaaaaaaaaaaaaa,
7+
0xbbbbbbbbbbbbbbbb,
8+
0xcccccccccccccccc,
9+
0xdddddddddddddddd,
10+
];
11+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
// rustfmt-short_item_threshold: 20
2+
3+
fn main() {
4+
pub const FORMAT_TEST: [u64; 5] = [
5+
0x0000000000000000, 0xaaaaaaaaaaaaaaaa, 0xbbbbbbbbbbbbbbbb, 0xcccccccccccccccc,
6+
0xdddddddddddddddd,
7+
];
8+
}

0 commit comments

Comments
 (0)