Skip to content

fn_width config option #6046

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

Open
wants to merge 7 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 2 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
14 changes: 14 additions & 0 deletions Configurations.md
Original file line number Diff line number Diff line change
Expand Up @@ -768,6 +768,20 @@ 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)


## `fn_width`

Maximum width of the declaration of a function signature before falling back to formatting chosen with `fn_param_layout`.

- **Default value**: `100`
- **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_width` will take precedence.

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


## `fn_params_layout`

Control the layout of parameters in function signatures.
Expand Down
10 changes: 10 additions & 0 deletions src/config/config_type.rs
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,7 @@ macro_rules! create_config {
| "fn_call_width"
| "single_line_if_else_max_width"
| "single_line_let_else_max_width"
| "fn_width"
| "attr_fn_like_width"
| "struct_lit_width"
| "struct_variant_width"
Expand Down Expand Up @@ -273,6 +274,7 @@ macro_rules! create_config {
| "fn_call_width"
| "single_line_if_else_max_width"
| "single_line_let_else_max_width"
| "fn_width"
| "attr_fn_like_width"
| "struct_lit_width"
| "struct_variant_width"
Expand Down Expand Up @@ -421,6 +423,14 @@ macro_rules! create_config {
"single_line_let_else_max_width",
);
self.single_line_let_else_max_width.2 = single_line_let_else_max_width;

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

fn set_heuristics(&mut self) {
Expand Down
3 changes: 3 additions & 0 deletions src/config/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ create_config! {
single_line_let_else_max_width: usize, 50, true, "Maximum line length for single line \
let-else statements. A value of zero means always format the divergent `else` block \
over multiple lines.";
fn_width: usize, 100, true, "Maximum line length for function declarations.";

// Comments. macros, and strings
wrap_comments: bool, false, false, "Break comments to fit on the line";
Expand Down Expand Up @@ -490,6 +491,7 @@ mod test {
single_line_let_else_max_width: usize, 50, false, "Maximum line length for single \
line let-else statements. A value of zero means always format the divergent \
`else` block over multiple lines.";
fn_width: usize, 100, true, "Maximum line length for function declarations.";

// Options that are used by the tests
stable_option: bool, false, true, "A stable option";
Expand Down Expand Up @@ -634,6 +636,7 @@ array_width = 60
chain_width = 60
single_line_if_else_max_width = 50
single_line_let_else_max_width = 50
fn_width = 100
wrap_comments = false
format_code_in_doc_comments = false
doc_comment_code_block_width = 100
Expand Down
5 changes: 5 additions & 0 deletions src/config/options.rs
Original file line number Diff line number Diff line change
Expand Up @@ -241,6 +241,8 @@ pub struct WidthHeuristics {
// Maximum line length for single line let-else statements. A value of zero means
// always format the divergent `else` block over multiple lines.
pub(crate) single_line_let_else_max_width: usize,
// Maximum line length for function declarations.
pub(crate) fn_width: usize,
}

impl fmt::Display for WidthHeuristics {
Expand All @@ -261,6 +263,7 @@ impl WidthHeuristics {
chain_width: usize::max_value(),
single_line_if_else_max_width: 0,
single_line_let_else_max_width: 0,
fn_width: usize::max_value(),
}
}

Expand All @@ -274,6 +277,7 @@ impl WidthHeuristics {
chain_width: max_width,
single_line_if_else_max_width: max_width,
single_line_let_else_max_width: max_width,
fn_width: max_width,
}
}

Expand All @@ -296,6 +300,7 @@ impl WidthHeuristics {
chain_width: (60.0 * max_width_ratio).round() as usize,
single_line_if_else_max_width: (50.0 * max_width_ratio).round() as usize,
single_line_let_else_max_width: (50.0 * max_width_ratio).round() as usize,
fn_width: (100.0 * max_width_ratio).round() as usize,
}
}
}
Expand Down
13 changes: 11 additions & 2 deletions src/items.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2353,7 +2353,13 @@ fn rewrite_fn_base(
2
};
let used_width = last_line_used_width(&result, indent.width());
let one_line_budget = context.budget(used_width + overhead);
let one_line_budget = std::cmp::min(
context.budget(used_width + overhead),
context
.config
.fn_width()
.saturating_sub(used_width + overhead),
);
let shape = Shape {
width: one_line_budget,
indent,
Expand Down Expand Up @@ -2797,7 +2803,10 @@ fn compute_budgets_for_params(
FnBraceStyle::SameLine => used_space += 2, // 2 = `{}`
FnBraceStyle::NextLine => (),
}
let one_line_budget = context.budget(used_space);
let one_line_budget = std::cmp::min(
context.budget(used_space),
context.config.fn_width().saturating_sub(used_space),
);

if one_line_budget > 0 {
// 4 = "() {".len()
Expand Down
2 changes: 2 additions & 0 deletions tests/config/fn_width_100.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
max_width = 100
fn_width = 100
2 changes: 2 additions & 0 deletions tests/config/fn_width_150.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
max_width = 150
fn_width = 150
2 changes: 2 additions & 0 deletions tests/config/fn_width_50.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
max_width = 50
fn_width = 50
17 changes: 17 additions & 0 deletions tests/source/fn_width/100.rs
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

let's move these tests to tests/source/config/{config_name}/{value}.rs

Copy link
Author

@Sjael Sjael Feb 22, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I assume you meant the configs folder, but it throws errors when I put the test files in there so I made a new config folder which is somewhat confusing but no errors.

Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
// rustfmt-config: fn_width_100.toml

impl Trait {
fn lorem(first: First, second: Second);
fn lorem(first: FirstParameter, second: SecondParameter, third: ThirdParameter);
fn lorem(first: FirstParameter, second: SecondParameter, third: ThirdParameter, fourth: FourthParameter);

fn lorem(first: First, second: Second) {
// block
}
fn lorem(first: FirstParameter, second: SecondParameter, third: ThirdParameter) {
// block
}
fn lorem(first: FirstParameter, second: SecondParameter, third: ThirdParameter, fourth: FourthParameter) {
// block
}
}
17 changes: 17 additions & 0 deletions tests/source/fn_width/150.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
// rustfmt-config: fn_width_150.toml

impl Trait {
fn lorem(first: First, second: Second);
fn lorem(first: FirstParameter, second: SecondParameter, third: ThirdParameter);
fn lorem(first: FirstParameter, second: SecondParameter, third: ThirdParameter, fourth: FourthParameter);

fn lorem(first: First, second: Second) {
// block
}
fn lorem(first: FirstParameter, second: SecondParameter, third: ThirdParameter) {
// block
}
fn lorem(first: FirstParameter, second: SecondParameter, third: ThirdParameter, fourth: FourthParameter) {
// block
}
}
17 changes: 17 additions & 0 deletions tests/source/fn_width/50.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
// rustfmt-config: fn_width_50.toml

impl Trait {
fn lorem(first: First, second: Second);
fn lorem(first: FirstParameter, second: SecondParameter, third: ThirdParameter);
fn lorem(first: FirstParameter, second: SecondParameter, third: ThirdParameter, fourth: FourthParameter);

fn lorem(first: First, second: Second) {
// block
}
fn lorem(first: FirstParameter, second: SecondParameter, third: ThirdParameter) {
// block
}
fn lorem(first: FirstParameter, second: SecondParameter, third: ThirdParameter, fourth: FourthParameter) {
// block
}
}
27 changes: 27 additions & 0 deletions tests/target/fn_width/100.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
// rustfmt-config: fn_width_100.toml

impl Trait {
fn lorem(first: First, second: Second);
fn lorem(first: FirstParameter, second: SecondParameter, third: ThirdParameter);
fn lorem(
first: FirstParameter,
second: SecondParameter,
third: ThirdParameter,
fourth: FourthParameter,
);

fn lorem(first: First, second: Second) {
// block
}
fn lorem(first: FirstParameter, second: SecondParameter, third: ThirdParameter) {
// block
}
fn lorem(
first: FirstParameter,
second: SecondParameter,
third: ThirdParameter,
fourth: FourthParameter,
) {
// block
}
}
17 changes: 17 additions & 0 deletions tests/target/fn_width/150.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
// rustfmt-config: fn_width_150.toml

impl Trait {
fn lorem(first: First, second: Second);
fn lorem(first: FirstParameter, second: SecondParameter, third: ThirdParameter);
fn lorem(first: FirstParameter, second: SecondParameter, third: ThirdParameter, fourth: FourthParameter);

fn lorem(first: First, second: Second) {
// block
}
fn lorem(first: FirstParameter, second: SecondParameter, third: ThirdParameter) {
// block
}
fn lorem(first: FirstParameter, second: SecondParameter, third: ThirdParameter, fourth: FourthParameter) {
// block
}
}
35 changes: 35 additions & 0 deletions tests/target/fn_width/50.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
// rustfmt-config: fn_width_50.toml

impl Trait {
fn lorem(first: First, second: Second);
fn lorem(
first: FirstParameter,
second: SecondParameter,
third: ThirdParameter,
);
fn lorem(
first: FirstParameter,
second: SecondParameter,
third: ThirdParameter,
fourth: FourthParameter,
);

fn lorem(first: First, second: Second) {
// block
}
fn lorem(
first: FirstParameter,
second: SecondParameter,
third: ThirdParameter,
) {
// block
}
fn lorem(
first: FirstParameter,
second: SecondParameter,
third: ThirdParameter,
fourth: FourthParameter,
) {
// block
}
}