From 2c053653e8fb1e12f0742afa7594bfde4c808736 Mon Sep 17 00:00:00 2001 From: 123vivekr <123vivekr@gmail.com> Date: Sun, 13 Feb 2022 17:54:09 +0530 Subject: [PATCH] Add `short_item_threshold` config option Allow custom short item threshold values via config --- Configurations.md | 31 +++++++++++++++++++ src/config/mod.rs | 2 ++ src/overflow.rs | 10 +++--- .../source/configs/short_item_threshold/10.rs | 11 +++++++ .../source/configs/short_item_threshold/20.rs | 11 +++++++ .../greater_than_max_width.rs | 12 +++++++ .../target/configs/short_item_threshold/10.rs | 11 +++++++ .../target/configs/short_item_threshold/20.rs | 8 +++++ .../greater_than_max_width.rs | 12 +++++++ 9 files changed, 103 insertions(+), 5 deletions(-) create mode 100644 tests/source/configs/short_item_threshold/10.rs create mode 100644 tests/source/configs/short_item_threshold/20.rs create mode 100644 tests/source/configs/short_item_threshold/greater_than_max_width.rs create mode 100644 tests/target/configs/short_item_threshold/10.rs create mode 100644 tests/target/configs/short_item_threshold/20.rs create mode 100644 tests/target/configs/short_item_threshold/greater_than_max_width.rs diff --git a/Configurations.md b/Configurations.md index 2e2b0f7cfbe..4ae3430efd1 100644 --- a/Configurations.md +++ b/Configurations.md @@ -2200,6 +2200,37 @@ 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 + +#### `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 diff --git a/src/config/mod.rs b/src/config/mod.rs index 5041e1e36dd..e97671328d3 100644 --- a/src/config/mod.rs +++ b/src/config/mod.rs @@ -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, @@ -591,6 +592,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 diff --git a/src/overflow.rs b/src/overflow.rs index 3475f5c378c..e01c4caceb0 100644 --- a/src/overflow.rs +++ b/src/overflow.rs @@ -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. /// @@ -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; } } @@ -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. diff --git a/tests/source/configs/short_item_threshold/10.rs b/tests/source/configs/short_item_threshold/10.rs new file mode 100644 index 00000000000..40398a1e6be --- /dev/null +++ b/tests/source/configs/short_item_threshold/10.rs @@ -0,0 +1,11 @@ +// rustfmt-short_item_threshold: 10 + +fn main() { + pub const FORMAT_TEST: [u64; 5] = [ + 0x0000000000000000, + 0xaaaaaaaaaaaaaaaa, + 0xbbbbbbbbbbbbbbbb, + 0xcccccccccccccccc, + 0xdddddddddddddddd, + ]; +} \ No newline at end of file diff --git a/tests/source/configs/short_item_threshold/20.rs b/tests/source/configs/short_item_threshold/20.rs new file mode 100644 index 00000000000..cf10062666a --- /dev/null +++ b/tests/source/configs/short_item_threshold/20.rs @@ -0,0 +1,11 @@ +// rustfmt-short_item_threshold: 20 + +fn main() { + pub const FORMAT_TEST: [u64; 5] = [ + 0x0000000000000000, + 0xaaaaaaaaaaaaaaaa, + 0xbbbbbbbbbbbbbbbb, + 0xcccccccccccccccc, + 0xdddddddddddddddd, + ]; +} \ No newline at end of file diff --git a/tests/source/configs/short_item_threshold/greater_than_max_width.rs b/tests/source/configs/short_item_threshold/greater_than_max_width.rs new file mode 100644 index 00000000000..117c041f785 --- /dev/null +++ b/tests/source/configs/short_item_threshold/greater_than_max_width.rs @@ -0,0 +1,12 @@ +// rustfmt-max_width: 20 +// rustfmt-short_item_threshold: 30 + +fn main() { + pub const FORMAT_TEST: [u64; 5] = [ + 0x0000000000000000, + 0xaaaaaaaaaaaaaaaa, + 0xbbbbbbbbbbbbbbbb, + 0xcccccccccccccccc, + 0xdddddddddddddddd, + ]; +} diff --git a/tests/target/configs/short_item_threshold/10.rs b/tests/target/configs/short_item_threshold/10.rs new file mode 100644 index 00000000000..d9e80bec7d4 --- /dev/null +++ b/tests/target/configs/short_item_threshold/10.rs @@ -0,0 +1,11 @@ +// rustfmt-short_item_threshold: 10 + +fn main() { + pub const FORMAT_TEST: [u64; 5] = [ + 0x0000000000000000, + 0xaaaaaaaaaaaaaaaa, + 0xbbbbbbbbbbbbbbbb, + 0xcccccccccccccccc, + 0xdddddddddddddddd, + ]; +} diff --git a/tests/target/configs/short_item_threshold/20.rs b/tests/target/configs/short_item_threshold/20.rs new file mode 100644 index 00000000000..b2d9d4035c1 --- /dev/null +++ b/tests/target/configs/short_item_threshold/20.rs @@ -0,0 +1,8 @@ +// rustfmt-short_item_threshold: 20 + +fn main() { + pub const FORMAT_TEST: [u64; 5] = [ + 0x0000000000000000, 0xaaaaaaaaaaaaaaaa, 0xbbbbbbbbbbbbbbbb, 0xcccccccccccccccc, + 0xdddddddddddddddd, + ]; +} diff --git a/tests/target/configs/short_item_threshold/greater_than_max_width.rs b/tests/target/configs/short_item_threshold/greater_than_max_width.rs new file mode 100644 index 00000000000..117c041f785 --- /dev/null +++ b/tests/target/configs/short_item_threshold/greater_than_max_width.rs @@ -0,0 +1,12 @@ +// rustfmt-max_width: 20 +// rustfmt-short_item_threshold: 30 + +fn main() { + pub const FORMAT_TEST: [u64; 5] = [ + 0x0000000000000000, + 0xaaaaaaaaaaaaaaaa, + 0xbbbbbbbbbbbbbbbb, + 0xcccccccccccccccc, + 0xdddddddddddddddd, + ]; +}