Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add short_item_threshold config option #5228

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 34 additions & 0 deletions Configurations.md
Original file line number Diff line number Diff line change
Expand Up @@ -2200,6 +2200,40 @@ 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_array_element_width_threshold`

The width threshold for an array element to be considered "short".

The layout of an array is dependent on the length of each of its elements.
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.

- **Default value**: `10`
- **Possible values**: any positive integer that is less than or equal to the value specified for [`max_width`](#max_width)
123vivekr marked this conversation as resolved.
Show resolved Hide resolved
- **Stable**: Yes

#### `10` (default):
```rust
fn main() {
pub const FORMAT_TEST: [u64; 5] = [
0x0000000000000000,
0xaaaaaaaaaaaaaaaa,
0xbbbbbbbbbbbbbbbb,
0xcccccccccccccccc,
0xdddddddddddddddd,
];
}
```
#### `20`:
```rust
fn main() {
pub const FORMAT_TEST: [u64; 5] = [
0x0000000000000000, 0xaaaaaaaaaaaaaaaa, 0xbbbbbbbbbbbbbbbb, 0xcccccccccccccccc,
0xdddddddddddddddd,
];
}
```
See also [`max_width`](#max_width).

## `skip_children`

Don't reformat out of line modules
Expand Down
3 changes: 3 additions & 0 deletions src/config/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,8 @@ 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_array_element_width_threshold: usize, 10, true,
"Width threshold for an array element to be considered short";
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 @@ -591,6 +593,7 @@ spaces_around_ranges = false
binop_separator = "Front"
remove_nested_parens = true
combine_control_expr = true
short_array_element_width_threshold = 10
overflow_delimited_expr = false
struct_field_align_threshold = 0
enum_discrim_align_threshold = 0
Expand Down
13 changes: 8 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,12 @@ 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_array_element_width_threshold(),
)
{
tactic = DefinitiveListTactic::Mixed;
}
}
Expand Down Expand Up @@ -755,9 +758,9 @@ fn shape_from_indent_style(
}
}

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

/// In case special-case style is required, returns an offset from which we start horizontal layout.
Expand Down
11 changes: 11 additions & 0 deletions tests/source/configs/short_array_element_width_threshold/10.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
// rustfmt-short_array_element_width_threshold: 10

fn main() {
pub const FORMAT_TEST: [u64; 5] = [
0x0000000000000000,
0xaaaaaaaaaaaaaaaa,
0xbbbbbbbbbbbbbbbb,
0xcccccccccccccccc,
0xdddddddddddddddd,
];
}
11 changes: 11 additions & 0 deletions tests/source/configs/short_array_element_width_threshold/20.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
// rustfmt-short_array_element_width_threshold: 20

fn main() {
pub const FORMAT_TEST: [u64; 5] = [
0x0000000000000000,
0xaaaaaaaaaaaaaaaa,
0xbbbbbbbbbbbbbbbb,
0xcccccccccccccccc,
0xdddddddddddddddd,
];
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
// rustfmt-max_width: 20
// rustfmt-short_array_element_width_threshold: 30

fn main() {
pub const FORMAT_TEST: [u64; 5] = [
0x0000000000000000,
0xaaaaaaaaaaaaaaaa,
0xbbbbbbbbbbbbbbbb,
0xcccccccccccccccc,
0xdddddddddddddddd,
];
}
11 changes: 11 additions & 0 deletions tests/target/configs/short_array_element_width_threshold/10.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
// rustfmt-short_array_element_width_threshold: 10

fn main() {
pub const FORMAT_TEST: [u64; 5] = [
0x0000000000000000,
0xaaaaaaaaaaaaaaaa,
0xbbbbbbbbbbbbbbbb,
0xcccccccccccccccc,
0xdddddddddddddddd,
];
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
// rustfmt-short_array_element_width_threshold: 20

fn main() {
pub const FORMAT_TEST: [u64; 5] = [
0x0000000000000000, 0xaaaaaaaaaaaaaaaa, 0xbbbbbbbbbbbbbbbb, 0xcccccccccccccccc,
0xdddddddddddddddd,
];
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
// rustfmt-max_width: 20
// rustfmt-short_array_element_width_threshold: 30

fn main() {
pub const FORMAT_TEST: [u64; 5] = [
0x0000000000000000,
0xaaaaaaaaaaaaaaaa,
0xbbbbbbbbbbbbbbbb,
0xcccccccccccccccc,
0xdddddddddddddddd,
];
}