Skip to content

Added new option "chain_count" to limit number of chained functions per line. #5514

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

Closed
wants to merge 1 commit into from
Closed
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
52 changes: 52 additions & 0 deletions Configurations.md
Original file line number Diff line number Diff line change
Expand Up @@ -307,6 +307,58 @@ By default this option is set as a percentage of [`max_width`](#max_width) provi

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

## `chain_count`

Maximum number of chained function calls to fit on one line.

- **Default value**: `0`
- **Possible values**: any positive integer less than `4294967296`, although anything larger than one quarter the value of [`chain_width`](#chain_width) will have no effect as the minimum size of a function chain is 4 (e.g. `.a()`).
- **Stable**: No (tracking issue: [#2263](https://github.com/rust-lang/rustfmt/issues/2263))

This option co-exists with [`chain_width`](#chain_width) and chained
method calls will be wrapped if either option is triggered. Any line
with more method chains than defined in this option will have every
chained call put on a new line.

Setting this option to `0` disables this rule, and `1` puts every single
chained call onto a newline.

#### `0` (default):

```rust
fn main() {
a.foo().bar().baz();
}
```

#### `1`:

```rust
fn main() {
a.foo()
.bar()
.baz();
}
```

#### `2`:

```rust
fn main() {
a.foo()
.bar()
.baz();
}
```

#### `3`:

```rust
fn main() {
a.foo().bar().baz();
}
```

## `color`

Whether to use colored output or not.
Expand Down
5 changes: 4 additions & 1 deletion src/chains.rs
Original file line number Diff line number Diff line change
Expand Up @@ -717,7 +717,10 @@ impl<'a> ChainFormatterShared<'a> {
}

fn join_rewrites(&self, context: &RewriteContext<'_>, child_shape: Shape) -> Option<String> {
let connector = if self.fits_single_line {
let force_multiline =
self.child_count > context.config.chain_count() && context.config.chain_count() != 0;

let connector = if self.fits_single_line && !force_multiline {
// Yay, we can put everything on one line.
Cow::from("")
} else {
Expand Down
2 changes: 2 additions & 0 deletions src/config/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ create_config! {
array_width: usize, 60, true, "Maximum width of an array literal before falling \
back to vertical formatting.";
chain_width: usize, 60, true, "Maximum length of a chain to fit on a single line.";
chain_count: usize, 0, false, "Maximum number of chained method calls to fit on a single line.";
single_line_if_else_max_width: usize, 50, true, "Maximum line length for single line if-else \
expressions. A value of zero means always break if-else expressions.";

Expand Down Expand Up @@ -618,6 +619,7 @@ struct_lit_width = 18
struct_variant_width = 35
array_width = 60
chain_width = 60
chain_count = 0
single_line_if_else_max_width = 50
wrap_comments = false
format_code_in_doc_comments = false
Expand Down
1 change: 1 addition & 0 deletions tests/config/issue-2263.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
chain_count = 3
8 changes: 8 additions & 0 deletions tests/source/chains.rs
Original file line number Diff line number Diff line change
Expand Up @@ -214,6 +214,14 @@ impl Foo {
}
}

// #2263
// Limit chain functions per line.
fn issue2263() {
let a = "test";

a.to_string().to_string().to_string();
}

// #2415
// Avoid orphan in chain
fn issue2415() {
Expand Down
10 changes: 10 additions & 0 deletions tests/source/issue-2263.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
fn main() {
let foo = "foo";

foo.to_string().to_string();
foo.to_string().to_string().to_string();
foo.to_string()
.to_string()
.to_string();
foo.to_string().to_string().to_string().to_string();
}
8 changes: 8 additions & 0 deletions tests/target/chains.rs
Original file line number Diff line number Diff line change
Expand Up @@ -252,6 +252,14 @@ impl Foo {
}
}

// #2263
// Limit chain functions per line.
fn issue2263() {
let a = "test";

a.to_string().to_string().to_string();
}

// #2415
// Avoid orphan in chain
fn issue2415() {
Expand Down
11 changes: 11 additions & 0 deletions tests/target/issue-2263.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
fn main() {
let foo = "foo";

foo.to_string().to_string();
foo.to_string().to_string().to_string();
foo.to_string().to_string().to_string();
foo.to_string()
.to_string()
.to_string()
.to_string();
}