Skip to content

Commit 2c05365

Browse files
committed
Add short_item_threshold config option
Allow custom short item threshold values via config
1 parent 272fb42 commit 2c05365

File tree

9 files changed

+103
-5
lines changed

9 files changed

+103
-5
lines changed

Configurations.md

+31
Original file line numberDiff line numberDiff line change
@@ -2200,6 +2200,37 @@ 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+
2211+
#### `10` (default):
2212+
```rust
2213+
fn main() {
2214+
pub const FORMAT_TEST: [u64; 5] = [
2215+
0x0000000000000000,
2216+
0xaaaaaaaaaaaaaaaa,
2217+
0xbbbbbbbbbbbbbbbb,
2218+
0xcccccccccccccccc,
2219+
0xdddddddddddddddd,
2220+
];
2221+
}
2222+
```
2223+
#### `20`:
2224+
```rust
2225+
fn main() {
2226+
pub const FORMAT_TEST: [u64; 5] = [
2227+
0x0000000000000000, 0xaaaaaaaaaaaaaaaa, 0xbbbbbbbbbbbbbbbb, 0xcccccccccccccccc,
2228+
0xdddddddddddddddd,
2229+
];
2230+
}
2231+
```
2232+
See also [`max_width`](#max_width).
2233+
22032234
## `skip_children`
22042235

22052236
Don't reformat out of line modules

src/config/mod.rs

+2
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

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,12 @@
1+
// rustfmt-max_width: 20
2+
// rustfmt-short_item_threshold: 30
3+
4+
fn main() {
5+
pub const FORMAT_TEST: [u64; 5] = [
6+
0x0000000000000000,
7+
0xaaaaaaaaaaaaaaaa,
8+
0xbbbbbbbbbbbbbbbb,
9+
0xcccccccccccccccc,
10+
0xdddddddddddddddd,
11+
];
12+
}
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+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
// rustfmt-max_width: 20
2+
// rustfmt-short_item_threshold: 30
3+
4+
fn main() {
5+
pub const FORMAT_TEST: [u64; 5] = [
6+
0x0000000000000000,
7+
0xaaaaaaaaaaaaaaaa,
8+
0xbbbbbbbbbbbbbbbb,
9+
0xcccccccccccccccc,
10+
0xdddddddddddddddd,
11+
];
12+
}

0 commit comments

Comments
 (0)