Skip to content

Commit dd1fbca

Browse files
committed
Replace various small width heuristics with a single option
Closes #1984
1 parent 96886cd commit dd1fbca

File tree

56 files changed

+214
-915
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

56 files changed

+214
-915
lines changed

Configurations.md

+61-157
Original file line numberDiff line numberDiff line change
@@ -6,49 +6,13 @@ A possible content of `rustfmt.toml` or `.rustfmt.toml` might look like this:
66

77
```toml
88
indent_style = "Block"
9-
array_width = 80
109
reorder_imported_names = true
1110
```
1211

1312
# Configuration Options
1413

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

17-
## `array_horizontal_layout_threshold`
18-
19-
How many elements array must have before rustfmt uses horizontal layout.
20-
Use this option to prevent a huge array from being vertically formatted.
21-
22-
- **Default value**: `0`
23-
- **Possible values**: any positive integer
24-
25-
**Note:** A value of `0` results in [`indent_style`](#indent_style) being applied regardless of a line's width.
26-
27-
#### `0` (default):
28-
29-
```rust
30-
// Each element will be placed on its own line.
31-
let a = vec![
32-
0,
33-
1,
34-
2,
35-
3,
36-
4,
37-
...
38-
999,
39-
1000,
40-
];
41-
```
42-
43-
#### `1000`:
44-
45-
```rust
46-
// Each element will be placed on the same line as much as possible.
47-
let a = vec![
48-
0, 1, 2, 3, 4, ...
49-
..., 999, 1000,
50-
];
51-
```
5216

5317
## `indent_style`
5418

@@ -279,22 +243,6 @@ fn lorem<Ipsum, Dolor, Sit, Amet>() -> T
279243

280244
See also: [`where_density`](#where_density), [`where_layout`](#where_layout).
281245

282-
## `array_width`
283-
284-
Maximum width of an array literal before falling back to vertical formatting
285-
286-
- **Default value**: `60`
287-
- **Possible values**: any positive integer
288-
289-
**Note:** A value of `0` results in [`indent_style`](#indent_style) being applied regardless of a line's width.
290-
291-
#### Lines shorter than `array_width`:
292-
```rust
293-
let lorem = vec!["ipsum", "dolor", "sit", "amet", "consectetur", "adipiscing", "elit"];
294-
```
295-
296-
#### Lines longer than `array_width`:
297-
See [`indent_style`](#indent_style).
298246

299247
## `same_line_attributes`
300248

@@ -341,6 +289,66 @@ enum Lorem {
341289
}
342290
```
343291

292+
## `use_small_heuristics`
293+
294+
Whether to use different formatting for items and expressions if they satisfy a heuristic notion of 'small'.
295+
296+
- **Default value**: `true`
297+
- **Possible values**: `true`, `false`
298+
299+
#### `true` (default):
300+
301+
```rust
302+
enum Lorem {
303+
Ipsum,
304+
Dolor(bool),
305+
Sit { amet: Consectetur, adipiscing: Elit },
306+
}
307+
308+
fn main() {
309+
lorem(
310+
"lorem",
311+
"ipsum",
312+
"dolor",
313+
"sit",
314+
"amet",
315+
"consectetur",
316+
"adipiscing",
317+
);
318+
319+
let lorem = Lorem { ipsum: dolor, sit: amet };
320+
321+
let lorem = if ipsum { dolor } else { sit };
322+
}
323+
```
324+
325+
#### `false`:
326+
327+
```rust
328+
enum Lorem {
329+
Ipsum,
330+
Dolor(bool),
331+
Sit {
332+
amet: Consectetur,
333+
adipiscing: Elit,
334+
},
335+
}
336+
337+
fn main() {
338+
lorem("lorem", "ipsum", "dolor", "sit", "amet", "consectetur", "adipiscing");
339+
340+
let lorem = Lorem {
341+
ipsum: dolor,
342+
sit: amet,
343+
};
344+
345+
let lorem = if ipsum {
346+
dolor
347+
} else {
348+
sit
349+
};
350+
}
351+
```
344352

345353
## `binop_separator`
346354

@@ -409,22 +417,6 @@ let lorem = ipsum.dolor()
409417
.elit();
410418
```
411419

412-
See also [`chain_width`](#chain_width).
413-
414-
## `chain_width`
415-
416-
Maximum length of a chain to fit on a single line
417-
418-
- **Default value**: `60`
419-
- **Possible values**: any positive integer
420-
421-
#### Lines shorter than `chain_width`:
422-
```rust
423-
let lorem = ipsum.dolor().sit().amet().consectetur().adipiscing().elit();
424-
```
425-
426-
#### Lines longer than `chain_width`:
427-
See [`chain_indent`](#chain_indent).
428420

429421

430422
## `combine_control_expr`
@@ -883,23 +875,7 @@ struct Dolor<T>
883875
}
884876
```
885877

886-
## `fn_call_width`
887-
888-
Maximum width of the args of a function call before falling back to vertical formatting
889-
890-
- **Default value**: `60`
891-
- **Possible values**: any positive integer
892-
893-
**Note:** A value of `0` results in vertical formatting being applied regardless of a line's width.
894-
895-
#### Function call shorter than `fn_call_width`:
896-
```rust
897-
lorem("lorem", "ipsum", "dolor", "sit", "amet", "consectetur", "adipiscing", "elit");
898-
```
899-
900-
#### Function call longer than `fn_call_width`:
901878

902-
See [`indent_style`](#indent_style).
903879

904880
## `fn_empty_single_line`
905881

@@ -1533,30 +1509,6 @@ it contains a `#X` (with `X` being a number) in parentheses following the
15331509

15341510
See also [`report_todo`](#report_todo).
15351511

1536-
## `single_line_if_else_max_width`
1537-
1538-
Maximum line length for single line if-else expressions.
1539-
1540-
- **Default value**: `50`
1541-
- **Possible values**: any positive integer
1542-
1543-
**Note:** A value of `0` results in if-else expressions being broken regardless of their line's width.
1544-
1545-
#### Lines shorter than `single_line_if_else_max_width`:
1546-
```rust
1547-
let lorem = if ipsum { dolor } else { sit };
1548-
```
1549-
1550-
#### Lines longer than `single_line_if_else_max_width`:
1551-
```rust
1552-
let lorem = if ipsum {
1553-
dolor
1554-
} else {
1555-
sit
1556-
};
1557-
```
1558-
1559-
See also: [`control_brace_style`](#control_brace_style).
15601512

15611513
## `skip_children`
15621514

@@ -1768,56 +1720,8 @@ let lorem = Lorem {
17681720
};
17691721
```
17701722

1771-
See also: [`indent_style`](#indent_style), [`struct_lit_width`](#struct_lit_width).
1772-
1773-
## `struct_lit_width`
1774-
1775-
Maximum width in the body of a struct lit before falling back to vertical formatting
1776-
1777-
- **Default value**: `18`
1778-
- **Possible values**: any positive integer
1779-
1780-
**Note:** A value of `0` results in vertical formatting being applied regardless of a line's width.
1781-
1782-
#### Lines shorter than `struct_lit_width`:
1783-
```rust
1784-
let lorem = Lorem { ipsum: dolor, sit: amet };
1785-
```
1786-
1787-
#### Lines longer than `struct_lit_width`:
1788-
See [`indent_style`](#indent_style).
1789-
1790-
See also: [`struct_lit_single_line`](#struct_lit_single_line), [`indent_style`](#indent_style).
1791-
1792-
## `struct_variant_width`
1793-
1794-
Maximum width in the body of a struct variant before falling back to vertical formatting
1795-
1796-
- **Default value**: `35`
1797-
- **Possible values**: any positive integer
1798-
1799-
**Note:** A value of `0` results in vertical formatting being applied regardless of a line's width.
1800-
1801-
#### Struct variants shorter than `struct_variant_width`:
1802-
```rust
1803-
enum Lorem {
1804-
Ipsum,
1805-
Dolor(bool),
1806-
Sit { amet: Consectetur, adipiscing: Elit },
1807-
}
1808-
```
1723+
See also: [`indent_style`](#indent_style).
18091724

1810-
#### Struct variants longer than `struct_variant_width`:
1811-
```rust
1812-
enum Lorem {
1813-
Ipsum,
1814-
Dolor(bool),
1815-
Sit {
1816-
amet: Consectetur,
1817-
adipiscing: Elit,
1818-
},
1819-
}
1820-
```
18211725

18221726
## `tab_spaces`
18231727

src/chains.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -161,7 +161,7 @@ pub fn rewrite_chain(expr: &ast::Expr, context: &RewriteContext, shape: Shape) -
161161
let one_line_budget = if rewrites.is_empty() {
162162
shape.width
163163
} else {
164-
min(shape.width, context.config.chain_width())
164+
min(shape.width, context.config.width_heuristics().chain_width)
165165
};
166166
let all_in_one_line = !parent_rewrite_contains_newline
167167
&& rewrites.iter().all(|s| !s.contains('\n'))

0 commit comments

Comments
 (0)