Skip to content

Feature: add spaces_within_parenthesized_items option. #5434

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 5 commits into
base: master
Choose a base branch
from
Open
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
57 changes: 56 additions & 1 deletion Configurations.md
Original file line number Diff line number Diff line change
Expand Up @@ -2528,6 +2528,61 @@ fn lorem<T : Eq>(t : T) {

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

## `spaces_within_parenthesized_items`

Put one space after '(' and before ')' on parenthesized items.
Note that spaces are not added within grouping parentheses.

- **Default value**: `false`
- **Possible values**: `true`, `false`
- **Stable**: No

#### `false` (default):

```rust
#[cfg(test)]
fn lorem() {}

fn lorem(ipsum: usize, dolor: usize) -> i32 {
println!("ipsum!");
bar();
foo(1, 2, 3);
let (x, y) = (1, 2);
(1 * ((2 + 3) * 4)) // grouping parentheses
}

pub(crate) type Bar = fn(i32, i32) -> ();

struct Dummy;

impl Foo for Dummy {
fn lorem(self, ipsum: usize, dolor: usize) {}
}
```

#### `true`:

```rust
#[cfg( test )]
fn lorem() {}

fn lorem( ipsum: usize, dolor: usize ) -> i32 {
println!( "ipsum!" );
bar();
foo( 1, 2, 3 );
let ( x, y ) = ( 1, 2 );
(1 * ((2 + 3) * 4)) // grouping parentheses
}

pub(crate) type Bar = fn( i32, i32 ) -> ();

struct Dummy;

impl Foo for Dummy {
fn lorem( self, ipsum: usize, dolor: usize ) {}
}
```

## `spaces_around_ranges`

Put spaces around the .., ..=, and ... range operators
Expand Down Expand Up @@ -3132,4 +3187,4 @@ Internal option, use `--backup`

## `print_misformatted_file_names`

Internal option, use `-l` or `--files-with-diff`
Internal option, use `-l` or `--files-with-diff`
3 changes: 3 additions & 0 deletions src/config/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,8 @@ create_config! {
spaces_around_ranges: bool, false, false, "Put spaces around the .. and ..= range operators";
binop_separator: SeparatorPlace, SeparatorPlace::Front, false,
"Where to put a binary operator when a binary expression goes multiline";
spaces_within_parenthesized_items: bool, false, false,
"Put one space after '(' and before ')' on parenthesized items";

// Misc.
remove_nested_parens: bool, true, true, "Remove nested parens";
Expand Down Expand Up @@ -653,6 +655,7 @@ space_before_colon = false
space_after_colon = true
spaces_around_ranges = false
binop_separator = "Front"
spaces_within_parenthesized_items = false
remove_nested_parens = true
combine_control_expr = true
short_array_element_width_threshold = 10
Expand Down
19 changes: 18 additions & 1 deletion src/items.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2324,13 +2324,22 @@ fn rewrite_fn_base(
result.push_str("fn ");

// Generics.
let overhead = if let FnBraceStyle::SameLine = fn_brace_style {
let mut overhead = if let FnBraceStyle::SameLine = fn_brace_style {
// 4 = `() {`
4
} else {
// 2 = `()`
2
};

let can_have_spaces_within_parentheses =
context.config.spaces_within_parenthesized_items() && !fn_sig.decl.inputs.is_empty();

overhead += if can_have_spaces_within_parentheses {
2
} else {
0
};
let used_width = last_line_used_width(&result, indent.width());
let one_line_budget = context.budget(used_width + overhead);
let shape = Shape {
Expand Down Expand Up @@ -2425,6 +2434,9 @@ fn rewrite_fn_base(
result.push_str(&indent.to_string_with_newline(context.config));
result.push(')');
} else {
if can_have_spaces_within_parentheses {
result.push(' ');
}
result.push_str(&param_str);
let used_width = last_line_used_width(&result, indent.width()) + first_line_width(&ret_str);
// Put the closing brace on the next line if it overflows the max width.
Expand All @@ -2448,11 +2460,16 @@ fn rewrite_fn_base(
result.push(')');
no_params_and_over_max_width = true;
} else {
if can_have_spaces_within_parentheses {
result.push(' ');
}
result.push(')');
}
} else {
if closing_paren_overflow_max_width || params_last_line_contains_comment {
result.push_str(&indent.to_string_with_newline(context.config));
} else if can_have_spaces_within_parentheses {
result.push(' ');
}
result.push(')');
}
Expand Down
33 changes: 30 additions & 3 deletions src/overflow.rs
Original file line number Diff line number Diff line change
Expand Up @@ -658,21 +658,48 @@ impl<'a> Context<'a> {
.block()
.indent
.to_string_with_newline(self.context.config);

let can_have_spaces_within_parenthesis = !items_str.is_empty()
&& self.context.config.spaces_within_parenthesized_items()
&& prefix == "("
&& suffix == ")";

let num_spaces_within_parenthesis = if can_have_spaces_within_parenthesis {
2
} else {
0
};

let mut result = String::with_capacity(
self.ident.len() + items_str.len() + 2 + indent_str.len() + nested_indent_str.len(),
self.ident.len()
+ items_str.len()
+ 2
+ indent_str.len()
+ nested_indent_str.len()
+ num_spaces_within_parenthesis,
);

result.push_str(self.ident);
result.push_str(prefix);
let force_single_line = if self.context.config.version() == Version::Two {
!self.context.use_block_indent() || (is_extendable && extend_width <= shape.width)
} else {
let fits_one_line = if can_have_spaces_within_parenthesis {
items_str.len() + 4 <= shape.width
// 2 = `()`
let fits_one_line = items_str.len() + 2 <= shape.width;
} else {
items_str.len() + 2 <= shape.width
};

!self.context.use_block_indent()
|| (self.context.inside_macro() && !items_str.contains('\n') && fits_one_line)
|| (is_extendable && extend_width <= shape.width)
};
if force_single_line {
if force_single_line && can_have_spaces_within_parenthesis {
result.push(' ');
result.push_str(items_str);
result.push(' ');
} else if force_single_line {
result.push_str(items_str);
} else {
if !items_str.is_empty() {
Expand Down
11 changes: 7 additions & 4 deletions src/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -369,11 +369,14 @@ where
(write_list(&item_vec, &fmt)?, tactic)
};

let args = if tactic == DefinitiveListTactic::Horizontal
|| !context.use_block_indent()
|| is_inputs_empty
{
let args = if is_inputs_empty {
format!("({})", list_str)
} else if tactic == DefinitiveListTactic::Horizontal || !context.use_block_indent() {
if context.config.spaces_within_parenthesized_items() {
format!("( {} )", list_str)
} else {
format!("({})", list_str)
}
} else {
format!(
"({}{}{})",
Expand Down
77 changes: 77 additions & 0 deletions tests/source/configs/spaces_within_parenthesized_items/false.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
// rustfmt-spaces_within_parenthesized_items: false
#[cfg( test )]
fn foo1() {}

fn foo1_a(/* comment */) {}

fn foo1_b(/* comment */ /* comment */) {}

fn foo2( arg1: i32 ) -> i32 {
0
}

fn foo2_a( /* comment */ arg1: i32 ) -> i32 {
0
}

fn foo2_b( arg1: i32 /*comment */ ) -> i32 {
0
}

fn foo3<T, U>( arg1: T, arg2: U ) -> T {
Dummy( 1 );
let ( x, y ) = ( 1, 2 );
( /* comment */ 1, 2 );
( 1, /* comment */ 2 );
( 1, 2 /* comment */ );
0
}

fn foo3_a<T, U>( arg1: T, /* comment */ arg2: U ) -> T {
0
}

fn foo4(
arggggggggggggggggggggggggggggggggggggggg1: i32,
arggggggggggggggggggggggggggggggggggggggg2: i32,
) {
}

fn foo4_a(
/* comment */
arggggggggggggggggggggggggggggggggggggggg1: i32,
arggggggggggggggggggggggggggggggggggggggg2: i32,
) {
}

fn foo5() -> i32 {
foo1();
foo1(/* comment */);
foo2( 1 );
foo2( /* comment */ 1 );
foo2( 1 /* comment */ );
foo3( 1, 2 );
foo3( 1, /* comment */ 2 );
foo4(
000000000000000000000001111111111,
000000000000000000000002222222222,
);
foo2( 000000000000000000000000000000000000000000000000000000000000000000000000000000000000001 );
foo2(
000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001,
);
foo2(
0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001,
);
println!( "{}", "hello" );
vec![1, 2, 3, 4].iter().contains( |x| x == 1 );
(1 * ((2 + 1) * 3))
}

type Bar = fn( i32, i32 ) -> ();

macro_rules! add {
($a:expr,$b:expr) => {{
$a + $b
}};
}
73 changes: 73 additions & 0 deletions tests/source/configs/spaces_within_parenthesized_items/true.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
// rustfmt-spaces_within_parenthesized_items: true
#[cfg(test)]
fn foo1() {}

fn foo1_a(/* comment */) {}

fn foo1_b(/* comment */ /* comment */) {}

fn foo2(arg1: i32) -> i32 {
0
}

fn foo2_a( /* comment */ arg1: i32 ) -> i32 {
0
}

fn foo2_b( arg1: i32 /*comment */ ) -> i32 {
0
}

fn foo3<T, U>(arg1: T, arg2: U) -> T {
Dummy(1);
let (x, y) = (1, 2);
(/* comment */1, 2);
(1, /* comment */ 2);
(1, 2 /* comment */);
0
}

fn foo3_a<T, U>(arg1: T, /* comment */ arg2: U) -> T {
0
}

fn foo4(
arggggggggggggggggggggggggggggggggggggggg1: i32,
arggggggggggggggggggggggggggggggggggggggg2: i32,
) {
}

fn foo4_a( /* comment */
arggggggggggggggggggggggggggggggggggggggg1: i32,
arggggggggggggggggggggggggggggggggggggggg2: i32,
) {
}

fn foo5() -> i32 {
foo1();
foo1(/* comment */);
foo2(1);
foo2(/* comment */ 1);
foo2(1, /* comment */);
foo3(1, 2);
foo3(1, /* comment */ 2);
foo4(
000000000000000000000001111111111,
000000000000000000000002222222222,
);
foo2(000000000000000000000000000000000000000000000000000000000000000000000000000000000000001);
foo2(000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001);
foo2(0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001);
println!("{}","hello");
vec![1, 2, 3, 4].iter().contains( |x| x == 1 );
(1 * ((2 + 1) * 3))
}


type Bar = fn(i32, i32) -> ();

macro_rules! add {
($a:expr,$b:expr) => {{
$a + $b
}};
}
Loading