Skip to content

Commit 6cf7e3b

Browse files
committed
Add short_item_threshold config option
Allow custom short item threshold values via config
1 parent a36dc36 commit 6cf7e3b

File tree

9 files changed

+110
-5
lines changed

9 files changed

+110
-5
lines changed

Configurations.md

+34
Original file line numberDiff line numberDiff line change
@@ -2200,6 +2200,40 @@ 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_array_element_width_threshold`
2204+
2205+
The width threshold for an array element to be considered "short".
2206+
2207+
The layout of an array is dependent on the length of each of its elements.
2208+
If the length of every element in an array is below this threshold (all elements are "short") then the array can be formatted in the mixed/compressed style, but if any one element has a length that exceeds this threshold then the array elements will have to be formatted vertically.
2209+
2210+
- **Default value**: `10`
2211+
- **Possible values**: any positive integer that is less than or equal to the value specified for [`max_width`](#max_width)
2212+
- **Stable**: Yes
2213+
2214+
#### `10` (default):
2215+
```rust
2216+
fn main() {
2217+
pub const FORMAT_TEST: [u64; 5] = [
2218+
0x0000000000000000,
2219+
0xaaaaaaaaaaaaaaaa,
2220+
0xbbbbbbbbbbbbbbbb,
2221+
0xcccccccccccccccc,
2222+
0xdddddddddddddddd,
2223+
];
2224+
}
2225+
```
2226+
#### `20`:
2227+
```rust
2228+
fn main() {
2229+
pub const FORMAT_TEST: [u64; 5] = [
2230+
0x0000000000000000, 0xaaaaaaaaaaaaaaaa, 0xbbbbbbbbbbbbbbbb, 0xcccccccccccccccc,
2231+
0xdddddddddddddddd,
2232+
];
2233+
}
2234+
```
2235+
See also [`max_width`](#max_width).
2236+
22032237
## `skip_children`
22042238

22052239
Don't reformat out of line modules

src/config/mod.rs

+3
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,8 @@ 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_array_element_width_threshold: usize, 10, true,
110+
"Width threshold for an array element to be considered short";
109111
overflow_delimited_expr: bool, false, false,
110112
"Allow trailing bracket/brace delimited expressions to overflow";
111113
struct_field_align_threshold: usize, 0, false,
@@ -591,6 +593,7 @@ spaces_around_ranges = false
591593
binop_separator = "Front"
592594
remove_nested_parens = true
593595
combine_control_expr = true
596+
short_array_element_width_threshold = 10
594597
overflow_delimited_expr = false
595598
struct_field_align_threshold = 0
596599
enum_discrim_align_threshold = 0

src/overflow.rs

+8-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,12 @@ 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(
575+
list_items,
576+
self.context.config.short_array_element_width_threshold(),
577+
)
578+
{
576579
tactic = DefinitiveListTactic::Mixed;
577580
}
578581
}
@@ -755,9 +758,9 @@ fn shape_from_indent_style(
755758
}
756759
}
757760

758-
fn no_long_items(list: &[ListItem]) -> bool {
761+
fn no_long_items(list: &[ListItem], short_array_element_width_threshold: usize) -> bool {
759762
list.iter()
760-
.all(|item| item.inner_as_ref().len() <= SHORT_ITEM_THRESHOLD)
763+
.all(|item| item.inner_as_ref().len() <= short_array_element_width_threshold)
761764
}
762765

763766
/// 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_array_element_width_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_array_element_width_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_array_element_width_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_array_element_width_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_array_element_width_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_array_element_width_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)