Skip to content

Add array_horizontal_layout_threshold option #1768

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

Merged
merged 3 commits into from
Jul 6, 2017
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
33 changes: 33 additions & 0 deletions Configurations.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,39 @@ reorder_imported_names = true

Below you find a detailed visual guide on all the supported configuration options of rustfmt:

## `array_horizontal_layout_threshold`

How many elements array must have before rustfmt uses horizontal layout.
Use this option to prevent a huge array from being vertically formatted.

- **Default value**: `0`
- **Possible values**: any positive integer

**Note:** A value of `0` results in [`array_layout`](#array_layout) being applied regardless of a line's width.

#### `0`:

```rust
// Each element will be placed on its own line.
let a = vec![
0,
1,
2,
3,
4,
...
999,
1000,
];
```

#### `1000`:
```rust
// Each element will be placed on the same line as much as possible.
let a = vec![0, 1, 2, 3, 4, ...
..., 999, 1000];
```

## `array_layout`

Indent on arrays
Expand Down
2 changes: 2 additions & 0 deletions src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -533,6 +533,8 @@ create_config! {
array_layout: IndentStyle, IndentStyle::Block, "Indent on arrays";
array_width: usize, 60,
"Maximum width of an array literal before falling back to vertical formatting";
array_horizontal_layout_threshold: usize, 0,
"How many elements array must have before rustfmt uses horizontal layout.";
type_punctuation_density: TypeDensity, TypeDensity::Wide,
"Determines if '+' or '=' are wrapped in spaces in the punctuation of types";
where_style: Style, Style::Rfc, "Overall strategy for where clauses";
Expand Down
26 changes: 23 additions & 3 deletions src/expr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -464,8 +464,16 @@ where
1 // "["
};

let nested_shape = match context.config.array_layout() {
IndentStyle::Block => shape.block().block_indent(context.config.tab_spaces()),
let mut nested_shape = match context.config.array_layout() {
IndentStyle::Block => {
try_opt!(
shape
.block()
.block_indent(context.config.tab_spaces())
.with_max_width(context.config)
.sub_width(1)
)
}
IndentStyle::Visual => {
try_opt!(
shape
Expand Down Expand Up @@ -498,7 +506,7 @@ where
.iter()
.any(|li| li.item.as_ref().map(|s| s.len() > 10).unwrap_or(false));

let tactic = match context.config.array_layout() {
let mut tactic = match context.config.array_layout() {
IndentStyle::Block => {
// FIXME wrong shape in one-line case
match shape.width.checked_sub(2 * bracket_size) {
Expand All @@ -522,6 +530,18 @@ where
}
}
};
if context.config.array_horizontal_layout_threshold() > 0 &&
items.len() > context.config.array_horizontal_layout_threshold()
{
tactic = DefinitiveListTactic::Mixed;
if context.config.array_layout() == IndentStyle::Block {
nested_shape = try_opt!(
shape
.visual_indent(bracket_size)
.sub_width(bracket_size * 2)
);
}
}

let fmt = ListFormatting {
tactic: tactic,
Expand Down
107 changes: 107 additions & 0 deletions tests/source/configs-array_horizontal_layout_threshold-1000.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,107 @@
// rustfmt-array_horizontal_layout_threshold: 1000

const ARRAY: [u8; 2048] =
[99, 72, 48, 104, 44, 112, 38, 62, 40, 93, 23, 24, 32, 21, 102, 76, 65, 29, 116,
21, 18, 37, 61, 10, 108, 31, 85, 93, 2, 108, 103, 77, 109, 40, 92, 88, 114, 32, 31,
87, 69, 42, 38, 25, 105, 6, 61, 128, 45, 6, 43, 81, 7, 3, 1, 125, 24, 123, 2,
29, 111, 120, 26, 0, 78, 36, 115, 86, 66, 10, 52, 87, 27, 125, 122, 42, 126, 101,
70, 78, 90, 62, 72, 43, 3, 111, 8, 110, 11, 124, 124, 102, 74, 35, 9, 83, 22, 121,
34, 70, 69, 52, 31, 92, 94, 67, 21, 76, 65, 10, 79, 54, 17, 58, 105, 13, 96, 61, 99,
31, 87, 41, 78, 88, 120, 35, 95, 25, 80, 100, 45, 79, 49, 56, 5, 114, 11, 25, 16, 97,
2, 43, 17, 71, 63, 102, 81, 55, 14, 59, 102, 55, 101, 119, 29, 58, 103, 2, 88,
85, 9, 70, 91, 73, 37, 70, 123, 15, 68, 50, 76, 52, 46, 126, 87, 44, 85, 3, 97,
59, 39, 37, 79, 110, 25, 109, 90, 124, 109, 6, 47, 60, 79, 15, 40, 3, 40, 20, 98,
9, 21, 65, 119, 2, 20, 64, 56, 34, 116, 22, 125, 113, 57, 30, 21, 92, 76, 10, 107,
61, 8, 124, 110, 87, 64, 99, 26, 122, 56, 127, 94, 8, 121, 19, 24, 27, 61, 34,
44, 73, 82, 10, 49, 95, 72, 89, 27, 124, 75, 33, 64, 48, 73, 21, 101, 34, 47,
103, 114, 11, 31, 11, 93, 31, 54, 102, 117, 38, 31, 33, 84, 72, 128, 91, 3, 84, 92,
48, 69, 39, 97, 113, 70, 26, 96, 107, 117, 76, 59, 50, 43, 66, 21, 90, 31, 102, 45,
66, 5, 115, 63, 61, 83, 37, 16, 78, 22, 120, 52, 24, 25, 70, 71, 54, 11, 103, 45,
44, 101, 106, 53, 39, 116, 83, 4, 68, 12, 59, 3, 37, 112, 123, 7, 120, 127, 93,
34, 101, 48, 114, 127, 65, 69, 16, 79, 125, 18, 71, 69, 72, 54, 60, 107, 52, 18,
92, 105, 119, 17, 32, 23, 37, 8, 127, 99, 71, 54, 80, 109, 54, 51, 44, 20, 40,
52, 46, 81, 28, 46, 82, 39, 39, 70, 3, 90, 41, 40, 36, 127, 48, 124, 26, 115,
47, 93, 104, 4, 70, 88, 3, 4, 34, 75, 46, 16, 65, 114, 53, 51, 123, 16, 36, 98,
36, 37, 36, 80, 71, 3, 116, 89, 52, 74, 7, 116, 39, 48, 51, 54, 56, 105, 90, 50, 67,
111, 111, 7, 55, 87, 30, 15, 75, 50, 23, 9, 115, 2, 27, 45, 75, 29, 15, 15, 47, 33,
119, 85, 11, 116, 127, 53, 37, 3, 0, 116, 77, 4, 37, 56, 8, 92, 105, 86, 101,
79, 103, 98, 70, 122, 110, 38, 50, 52, 51, 62, 98, 95, 49, 21, 116, 30, 61, 1,
36, 96, 33, 78, 75, 23, 118, 88, 10, 4, 91, 38, 32, 96, 64, 71, 89, 108, 10, 106,
62, 86, 104, 24, 117, 2, 72, 99, 60, 117, 109, 67, 112, 124, 111, 102, 4, 126, 95,
23, 68, 115, 106, 15, 103, 101, 19, 30, 7, 29, 109, 62, 93, 22, 30, 106, 7, 52, 77,
88, 8, 32, 3, 63, 77, 14, 86, 82, 114, 104, 119, 122, 40, 92, 3, 98, 128, 53,
74, 40, 1, 94, 5, 112, 59, 29, 128, 119, 33, 67, 42, 109, 30, 93, 40, 113, 13, 85,
17, 51, 63, 57, 4, 2, 102, 93, 25, 61, 39, 110, 56, 21, 102, 25, 4, 113, 84, 63,
64, 63, 73, 83, 39, 123, 113, 68, 83, 95, 7, 23, 18, 73, 52, 16, 41, 81, 38, 55, 82,
59, 93, 6, 30, 25, 65, 67, 44, 99, 18, 77, 74, 62, 126, 36, 110, 66, 4, 86, 123,
21, 109, 46, 93, 112, 107, 35, 14, 127, 112, 54, 65, 0, 59, 76, 47, 94, 6, 94, 86,
49, 118, 44, 10, 15, 5, 105, 12, 28, 5, 94, 56, 31, 79, 86, 116, 18, 32, 69, 1,
83, 36, 38, 71, 38, 71, 23, 71, 9, 30, 64, 2, 6, 21, 112, 55, 1, 43, 126, 33, 79, 97,
49, 86, 7, 84, 40, 42, 25, 35, 51, 118, 56, 115, 104, 103, 20, 103, 95, 92, 43,
50, 42, 34, 122, 116, 75, 31, 109, 53, 44, 6, 48, 1, 52, 119, 123, 32, 50, 63,
114, 105, 16, 79, 53, 19, 78, 86, 110, 4, 43, 97, 3, 18, 110, 84, 70, 23, 84, 23,
48, 125, 36, 58, 25, 90, 111, 103, 83, 38, 112, 127, 28, 53, 86, 67, 78, 126, 86,
8, 41, 76, 10, 54, 11, 22, 3, 12, 2, 50, 91, 82, 90, 42, 108, 29, 72, 86, 34, 91,
115, 46, 86, 28, 46, 22, 97, 104, 48, 8, 22, 92, 10, 71, 102, 52, 116, 65, 15, 102,
8, 113, 53, 110, 49, 81, 102, 48, 91, 32, 18, 67, 49, 124, 35, 83, 37, 16, 31, 8,
58, 48, 77, 104, 71, 60, 40, 44, 74, 2, 40, 12, 22, 23, 49, 17, 98, 21, 83, 117, 64,
115, 44, 4, 46, 70, 47, 115, 24, 66, 71, 80, 59, 32, 46, 81, 118, 8, 29, 93, 86, 81,
20, 44, 46, 4, 116, 107, 117, 11, 30, 78, 13, 61, 110, 45, 101, 113, 34, 102, 19, 64,
10, 36, 68, 94, 40, 87, 74, 105, 81, 70, 58, 44, 46, 108, 90, 60, 32, 36, 23, 115,
40, 97, 43, 58, 16, 120, 74, 52, 42, 49, 16, 62, 122, 97, 107, 15, 104, 13, 17,
103, 49, 112, 123, 23, 107, 49, 40, 101, 62, 9, 71, 92, 70, 57, 37, 42, 21, 83, 2,
20, 116, 22, 8, 34, 61, 56, 65, 115, 121, 116, 67, 111, 52, 80, 94, 46, 18, 68, 72,
3, 61, 96, 127, 46, 7, 90, 100, 31, 30, 80, 123, 72, 74, 115, 74, 81, 45, 79, 121,
57, 85, 117, 5, 88, 101, 97, 10, 12, 43, 57, 107, 83, 25, 12, 117, 103, 72, 115, 29,
58, 101, 103, 120, 115, 74, 125, 127, 70, 7, 24, 92, 15, 103, 58, 83, 54, 75, 30, 9,
111, 68, 53, 29, 25, 19, 96, 38, 93, 123, 126, 63, 115, 92, 119, 76, 50, 7, 9,
101, 68, 26, 122, 5, 77, 4, 116, 89, 81, 21, 8, 111, 5, 33, 66, 121, 20, 106, 42,
54, 69, 34, 22, 21, 54, 78, 46, 76, 64, 47, 44, 38, 84, 19, 73, 18, 92, 74, 63,
65, 40, 34, 12, 6, 127, 32, 90, 62, 47, 42, 72, 121, 128, 44, 77, 121, 23, 105, 95,
43, 67, 63, 103, 22, 17, 45, 118, 28, 29, 17, 45, 85, 40, 3, 114, 36, 23, 109, 118,
76, 16, 90, 111, 11, 98, 51, 127, 12, 68, 53, 116, 81, 47, 126, 118, 105, 10, 59, 12,
101, 72, 114, 34, 19, 82, 68, 115, 12, 119, 123, 66, 21, 32, 106, 110, 49, 50, 20,
3, 39, 119, 36, 53, 5, 13, 61, 70, 30, 57, 74, 61, 125, 39, 73, 9, 67, 79, 85,
95, 74, 67, 61, 5, 30, 76, 39, 86, 32, 71, 108, 6, 49, 117, 60, 63, 57, 54, 107,
126, 104, 57, 59, 120, 68, 6, 108, 81, 113, 126, 64, 36, 60, 123, 117, 13, 68, 8,
116, 114, 119, 125, 61, 81, 98, 34, 53, 62, 11, 31, 117, 44, 54, 115, 30, 73, 69, 54,
92, 70, 49, 59, 51, 104, 103, 62, 96, 121, 98, 26, 45, 77, 24, 124, 28, 70, 100,
2, 98, 47, 25, 100, 37, 42, 115, 105, 42, 127, 65, 24, 102, 122, 33, 79, 87, 22, 47,
35, 50, 59, 54, 68, 16, 36, 91, 127, 39, 16, 113, 68, 20, 76, 99, 93, 121, 18,
23, 6, 32, 108, 8, 114, 65, 81, 106, 39, 91, 54, 8, 92, 6, 96, 12, 100, 33, 5,
105, 50, 89, 70, 33, 40, 85, 39, 93, 119, 26, 97, 90, 18, 74, 11, 105, 114, 84,
125, 124, 113, 86, 124, 90, 90, 87, 64, 83, 121, 39, 108, 66, 23, 55, 43, 31,
110, 96, 42, 4, 64, 41, 110, 97, 24, 95, 121, 125, 118, 85, 97, 110, 115, 75, 74, 60,
115, 47, 80, 55, 67, 92, 127, 120, 8, 42, 5, 50, 55, 35, 117, 60, 106, 127, 77, 58,
81, 76, 66, 17, 108, 55, 17, 50, 31, 64, 102, 88, 5, 32, 12, 37, 120, 48, 46, 43, 99,
12, 16, 114, 50, 49, 12, 3, 63, 64, 27, 54, 53, 31, 73, 2, 15, 39, 102, 12, 60, 100,
27, 28, 24, 46, 101, 10, 104, 51, 127, 101, 60, 55, 114, 25, 3, 29, 20, 53, 108, 7,
71, 4, 102, 14, 44, 72, 10, 56, 92, 86, 96, 56, 80, 20, 117, 65, 53, 58, 66, 5,
103, 88, 17, 10, 34, 63, 83, 84, 43, 70, 41, 8, 111, 117, 31, 80, 29, 12, 115, 75,
84, 91, 91, 10, 91, 26, 40, 30, 36, 44, 12, 18, 83, 29, 19, 15, 123, 98, 118, 26,
19, 36, 35, 122, 29, 41, 23, 40, 74, 25, 70, 78, 24, 75, 109, 31, 0, 89, 45,
128, 35, 120, 79, 108, 89, 84, 84, 81, 108, 47, 2, 112, 2, 42, 126, 12, 15, 15,
109, 109, 97, 76, 126, 5, 23, 66, 65, 1, 39, 102, 121, 127, 24, 21, 68, 42, 49, 69,
81, 111, 6, 11, 28, 5, 89, 31, 74, 33, 118, 15, 55, 105, 107, 30, 103, 54, 25,
29, 60, 91, 72, 94, 15, 31, 98, 47, 16, 27, 100, 109, 99, 82, 53, 25, 122, 119,
96, 65, 4, 24, 50, 79, 40, 65, 6, 91, 125, 7, 80, 103, 88, 22, 107, 38, 39, 100,
102, 96, 1, 66, 44, 33, 101, 88, 61, 10, 35, 39, 47, 93, 63, 19, 59, 87, 128, 16, 46,
51, 34, 34, 71, 117, 113, 66, 89, 126, 127, 35, 125, 11, 81, 120, 100, 41, 51,
85, 30, 82, 68, 86, 114, 77, 56, 125, 10, 2, 95, 75, 31, 33, 84, 83, 22, 35, 99,
12, 18, 40, 4, 88, 104, 46, 100, 99, 113, 45, 36, 51, 88, 53, 57, 15, 82, 60, 101, 10,
16, 83, 23, 78, 47, 34, 27, 56, 85, 14, 14, 53, 20, 71, 101, 82, 14, 35, 3, 51,
91, 16, 46, 117, 108, 30, 120, 124, 22, 89, 57, 119, 50, 91, 52, 77, 7, 10, 79,
5, 21, 81, 43, 58, 61, 59, 39, 60, 122, 23, 68, 21, 19, 81, 3, 31, 64, 54, 126, 56,
23, 64, 28, 32, 2, 119, 2, 55, 125, 57, 7, 51, 116, 93, 34, 15, 96, 120, 6, 73,
100, 98, 53, 116, 22, 64, 63, 112, 19, 60, 77, 95, 32, 3, 2, 117, 41, 80, 96, 122,
15, 45, 118, 102, 26, 89, 74, 2, 17, 63, 21, 52, 23, 82, 97, 22, 68, 39, 119,
117, 128, 49, 71, 55, 38, 55, 87, 3, 76, 80, 13, 57, 47, 59, 91, 46, 124, 115,
7, 74, 95, 26, 128, 86, 16, 20, 107, 117, 10, 72, 35, 30, 128, 75, 113, 45, 116,
125, 1, 46, 98, 14, 34, 29, 100, 17, 122, 65, 35, 69, 72, 12, 6, 5, 65, 29, 112,
47, 21, 103, 63, 118, 75, 21, 99, 6, 36, 46, 86, 59, 9, 117, 23, 82, 75, 13, 4, 9,
104, 43, 73, 17, 111, 36, 60, 38, 120, 99, 114, 117, 110, 27, 100, 104, 63, 87, 53,
54, 71, 38, 58, 86, 124, 3, 103, 92, 79, 127, 97, 17, 103, 13, 25, 65, 12, 28, 89,
62, 48, 115, 30, 19, 80, 14, 7, 21, 124, 91, 104, 110, 97, 97, 0, 104, 124, 93, 24,
70, 13, 99, 91, 73, 55, 84, 25, 22, 111, 115, 58, 45, 97, 8, 39, 125, 9, 22, 91,
98, 19, 14, 54, 26, 33, 46, 61, 98, 122, 69, 72, 97, 71, 73, 106, 32, 105, 27, 0, 56,
66, 51, 4, 94, 72, 117, 69];
11 changes: 5 additions & 6 deletions tests/target/comment.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,12 +36,11 @@ fn test() {
// or me

// #1388
const EXCEPTION_PATHS: &'static [&'static str] =
&[
// std crates
"src/libstd/sys/", // Platform-specific code for std lives here.
"src/bootstrap",
];
const EXCEPTION_PATHS: &'static [&'static str] = &[
// std crates
"src/libstd/sys/", // Platform-specific code for std lives here.
"src/bootstrap",
];
}

/// test123
Expand Down
Loading