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

Expose chain_width et al in 1.4 #4782

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
134 changes: 133 additions & 1 deletion Configurations.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,29 @@ To enable unstable options, set `unstable_features = true` in `rustfmt.toml` or

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

## `array_width`

Maximum width of an array literal before falling back to vertical formatting.

- **Default value**: `60`
- **Possible values**: any positive integer that is less than or equal to the value specified for [`max_width`](#max_width)
- **Stable**: Yes

By default this option is set as a percentage of [`max_width`](#max_width) provided by [`use_small_heuristics`](#use_small_heuristics), but a value set directly for `array_width` will take precedence.

See also [`max_width`](#max_width) and [`use_small_heuristics`](#use_small_heuristics)

## `attr_fn_like_width`

Maximum width of the args of a function-like attributes before falling back to vertical formatting.

- **Default value**: `70`
- **Possible values**: any positive integer that is less than or equal to the value specified for [`max_width`](#max_width)
- **Stable**: Yes

By default this option is set as a percentage of [`max_width`](#max_width) provided by [`use_small_heuristics`](#use_small_heuristics), but a value set directly for `attr_fn_like_width` will take precedence.

See also [`max_width`](#max_width) and [`use_small_heuristics`](#use_small_heuristics)

## `binop_separator`

Expand Down Expand Up @@ -272,6 +295,17 @@ where
}
```

## `chain_width`

Maximum width of a chain to fit on one line.

- **Default value**: `60`
- **Possible values**: any positive integer that is less than or equal to the value specified for [`max_width`](#max_width)
- **Stable**: Yes

By default this option is set as a percentage of [`max_width`](#max_width) provided by [`use_small_heuristics`](#use_small_heuristics), but a value set directly for `chain_width` will take precedence.

See also [`max_width`](#max_width) and [`use_small_heuristics`](#use_small_heuristics)

## `color`

Expand Down Expand Up @@ -717,6 +751,17 @@ trait Lorem {
}
```

## `fn_call_width`

Maximum width of the args of a function call before falling back to vertical formatting.

- **Default value**: `60`
- **Possible values**: any positive integer that is less than or equal to the value specified for [`max_width`](#max_width)
- **Stable**: Yes

By default this option is set as a percentage of [`max_width`](#max_width) provided by [`use_small_heuristics`](#use_small_heuristics), but a value set directly for `fn_call_width` will take precedence.

See also [`max_width`](#max_width) and [`use_small_heuristics`](#use_small_heuristics)

## `fn_single_line`

Expand Down Expand Up @@ -2079,6 +2124,18 @@ Don't reformat out of line modules
- **Possible values**: `true`, `false`
- **Stable**: No (tracking issue: #3389)

## `single_line_if_else_max_width`

Maximum line length for single line if-else expressions. A value of `0` (zero) results in if-else expressions always being broken into multiple lines. Note this occurs when `use_small_heuristics` is set to `Off`.

- **Default value**: `50`
- **Possible values**: any positive integer that is less than or equal to the value specified for [`max_width`](#max_width)
- **Stable**: Yes

By default this option is set as a percentage of [`max_width`](#max_width) provided by [`use_small_heuristics`](#use_small_heuristics), but a value set directly for `single_line_if_else_max_width` will take precedence.

See also [`max_width`](#max_width) and [`use_small_heuristics`](#use_small_heuristics)

## `space_after_colon`

Leave a space after the colon.
Expand Down Expand Up @@ -2256,6 +2313,29 @@ fn main() {

See also: [`indent_style`](#indent_style).

## `struct_lit_width`

Maximum width in the body of a struct literal before falling back to vertical formatting. A value of `0` (zero) results in struct literals always being broken into multiple lines. Note this occurs when `use_small_heuristics` is set to `Off`.

- **Default value**: `18`
- **Possible values**: any positive integer that is less than or equal to the value specified for [`max_width`](#max_width)
- **Stable**: Yes

By default this option is set as a percentage of [`max_width`](#max_width) provided by [`use_small_heuristics`](#use_small_heuristics), but a value set directly for `struct_lit_width` will take precedence.

See also [`max_width`](#max_width), [`use_small_heuristics`](#use_small_heuristics), and [`struct_lit_single_line`](#struct_lit_single_line)

## `struct_variant_width`

Maximum width in the body of a struct variant before falling back to vertical formatting. A value of `0` (zero) results in struct literals always being broken into multiple lines. Note this occurs when `use_small_heuristics` is set to `Off`.

- **Default value**: `35`
- **Possible values**: any positive integer that is less than or equal to the value specified for [`max_width`](#max_width)
- **Stable**: Yes

By default this option is set as a percentage of [`max_width`](#max_width) provided by [`use_small_heuristics`](#use_small_heuristics), but a value set directly for `struct_variant_width` will take precedence.

See also [`max_width`](#max_width) and [`use_small_heuristics`](#use_small_heuristics)

## `tab_spaces`

Expand Down Expand Up @@ -2448,13 +2528,43 @@ fn main() {

## `use_small_heuristics`

Whether to use different formatting for items and expressions if they satisfy a heuristic notion of 'small'.
This option can be used to simplify the management and bulk updates of the granular width configuration settings ([`fn_call_width`](#fn_call_width), [`attr_fn_like_width`](#attr_fn_like_width), [`struct_lit_width`](#struct_lit_width), [`struct_variant_width`](#struct_variant_width), [`array_width`](#array_width), [`chain_width`](#chain_width), [`single_line_if_else_max_width`](#single_line_if_else_max_width)), that respectively control when formatted constructs are multi-lined/vertical based on width.

Note that explicitly provided values for the width configuration settings take precedence and override the calculated values determined by `use_small_heuristics`.

- **Default value**: `"Default"`
- **Possible values**: `"Default"`, `"Off"`, `"Max"`
- **Stable**: Yes

#### `Default` (default):
When `use_small_heuristics` is set to `Default`, the values for the granular width settings are calculated as a ratio of the value for `max_width`.

The ratios are:
* [`fn_call_width`](#fn_call_width) - `60%`
* [`attr_fn_like_width`](#attr_fn_like_width) - `70%`
* [`struct_lit_width`](#struct_lit_width) - `18%`
* [`struct_variant_width`](#struct_variant_width) - `35%`
* [`array_width`](#array_width) - `60%`
* [`chain_width`](#chain_width) - `60%`
* [`single_line_if_else_max_width`](#single_line_if_else_max_width) - `50%`

For example when `max_width` is set to `100`, the width settings are:
* `fn_call_width=60`
* `attr_fn_like_width=70`
* `struct_lit_width=18`
* `struct_variant_width=35`
* `array_width=60`
* `chain_width=60`
* `single_line_if_else_max_width=50`

and when `max_width` is set to `200`:
* `fn_call_width=120`
* `attr_fn_like_width=140`
* `struct_lit_width=36`
* `struct_variant_width=70`
* `array_width=120`
* `chain_width=120`
* `single_line_if_else_max_width=100`

```rust
enum Lorem {
Expand Down Expand Up @@ -2485,6 +2595,7 @@ fn main() {
```

#### `Off`:
When `use_small_heuristics` is set to `Off`, the granular width settings are functionally disabled and ignored. See the documentation for the respective width config options for specifics.

```rust
enum Lorem {
Expand Down Expand Up @@ -2513,6 +2624,16 @@ fn main() {
```

#### `Max`:
When `use_small_heuristics` is set to `Max`, then each granular width setting is set to the same value as `max_width`.

So if `max_width` is set to `200`, then all the width settings are also set to `200`.
* `fn_call_width=200`
* `attr_fn_like_width=200`
* `struct_lit_width=200`
* `struct_variant_width=200`
* `array_width=200`
* `chain_width=200`
* `single_line_if_else_max_width=200`

```rust
enum Lorem {
Expand All @@ -2530,6 +2651,17 @@ fn main() {
}
```


See also:
* [`max_width`](#max_width)
* [`fn_call_width`](#fn_call_width)
* [`attr_fn_like_width`](#attr_fn_like_width)
* [`struct_lit_width`](#struct_lit_width)
* [`struct_variant_width`](#struct_variant_width)
* [`array_width`](#array_width)
* [`chain_width`](#chain_width)
* [`single_line_if_else_max_width`](#single_line_if_else_max_width)

## `use_try_shorthand`

Replace uses of the try! macro by the ? shorthand
Expand Down
2 changes: 1 addition & 1 deletion src/attr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -300,7 +300,7 @@ impl Rewrite for ast::MetaItem {
// 1 = "]"
shape.sub_width(1)?,
self.span,
context.config.width_heuristics().attr_fn_like_width,
context.config.attr_fn_like_width(),
Some(if has_trailing_comma {
SeparatorTactic::Always
} else {
Expand Down
2 changes: 1 addition & 1 deletion src/chains.rs
Original file line number Diff line number Diff line change
Expand Up @@ -577,7 +577,7 @@ impl<'a> ChainFormatterShared<'a> {
let one_line_budget = if self.child_count == 1 {
shape.width
} else {
min(shape.width, context.config.width_heuristics().chain_width)
min(shape.width, context.config.chain_width())
}
.saturating_sub(almost_total);

Expand Down
115 changes: 104 additions & 11 deletions src/config/config_type.rs
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,15 @@ macro_rules! create_config {
pub fn $i(&mut self, value: $ty) {
(self.0).$i.2 = value;
match stringify!($i) {
"max_width" | "use_small_heuristics" => self.0.set_heuristics(),
"max_width"
| "use_small_heuristics"
| "fn_call_width"
| "single_line_if_else_max_width"
| "attr_fn_like_width"
| "struct_lit_width"
| "struct_variant_width"
| "array_width"
| "chain_width" => self.0.set_heuristics(),
"license_template_path" => self.0.set_license_template(),
"merge_imports" => self.0.set_merge_imports(),
&_ => (),
Expand Down Expand Up @@ -230,7 +238,15 @@ macro_rules! create_config {
}

match key {
"max_width" | "use_small_heuristics" => self.set_heuristics(),
"max_width"
| "use_small_heuristics"
| "fn_call_width"
| "single_line_if_else_max_width"
| "attr_fn_like_width"
| "struct_lit_width"
| "struct_variant_width"
| "array_width"
| "chain_width" => self.set_heuristics(),
"license_template_path" => self.set_license_template(),
"merge_imports" => self.set_merge_imports(),
&_ => (),
Expand Down Expand Up @@ -281,16 +297,93 @@ macro_rules! create_config {
)+
}

fn set_width_heuristics(&mut self, heuristics: WidthHeuristics) {
let max_width = self.max_width.2;
let get_width_value = |
was_set: bool,
override_value: usize,
heuristic_value: usize,
config_key: &str,
| -> usize {
if !was_set {
return heuristic_value;
}
if override_value > max_width {
eprintln!(
"`{0}` cannot have a value that exceeds `max_width`. \
`{0}` will be set to the same value as `max_width`",
config_key,
);
return max_width;
}
override_value
};

let fn_call_width = get_width_value(
self.was_set().fn_call_width(),
self.fn_call_width.2,
heuristics.fn_call_width,
"fn_call_width",
);
self.fn_call_width.2 = fn_call_width;

let attr_fn_like_width = get_width_value(
self.was_set().attr_fn_like_width(),
self.attr_fn_like_width.2,
heuristics.attr_fn_like_width,
"attr_fn_like_width",
);
self.attr_fn_like_width.2 = attr_fn_like_width;

let struct_lit_width = get_width_value(
self.was_set().struct_lit_width(),
self.struct_lit_width.2,
heuristics.struct_lit_width,
"struct_lit_width",
);
self.struct_lit_width.2 = struct_lit_width;

let struct_variant_width = get_width_value(
self.was_set().struct_variant_width(),
self.struct_variant_width.2,
heuristics.struct_variant_width,
"struct_variant_width",
);
self.struct_variant_width.2 = struct_variant_width;

let array_width = get_width_value(
self.was_set().array_width(),
self.array_width.2,
heuristics.array_width,
"array_width",
);
self.array_width.2 = array_width;

let chain_width = get_width_value(
self.was_set().chain_width(),
self.chain_width.2,
heuristics.chain_width,
"chain_width",
);
self.chain_width.2 = chain_width;

let single_line_if_else_max_width = get_width_value(
self.was_set().single_line_if_else_max_width(),
self.single_line_if_else_max_width.2,
heuristics.single_line_if_else_max_width,
"single_line_if_else_max_width",
);
self.single_line_if_else_max_width.2 = single_line_if_else_max_width;
}

fn set_heuristics(&mut self) {
if self.use_small_heuristics.2 == Heuristics::Default {
let max_width = self.max_width.2;
self.set().width_heuristics(WidthHeuristics::scaled(max_width));
} else if self.use_small_heuristics.2 == Heuristics::Max {
let max_width = self.max_width.2;
self.set().width_heuristics(WidthHeuristics::set(max_width));
} else {
self.set().width_heuristics(WidthHeuristics::null());
}
let max_width = self.max_width.2;
match self.use_small_heuristics.2 {
Heuristics::Default =>
self.set_width_heuristics(WidthHeuristics::scaled(max_width)),
Heuristics::Max => self.set_width_heuristics(WidthHeuristics::set(max_width)),
Heuristics::Off => self.set_width_heuristics(WidthHeuristics::null()),
};
}

fn set_license_template(&mut self) {
Expand Down
Loading