Skip to content

Commit

Permalink
Add option space_before_fn_sig_paren (#4302)
Browse files Browse the repository at this point in the history
* Add option space_after_function_name

* Rename option and change type to bool

* Revert "Rename option and change type to bool"

This reverts commit 6350a15.

* Cover other styles

* Fix comments from review

* Use simpler interface

* Add indent_style test

* Rename to space_before_fn_sig_paren
  • Loading branch information
dylni authored Jul 24, 2020
1 parent ebf0de7 commit bcf09cb
Show file tree
Hide file tree
Showing 13 changed files with 253 additions and 0 deletions.
46 changes: 46 additions & 0 deletions Configurations.md
Original file line number Diff line number Diff line change
Expand Up @@ -2129,6 +2129,52 @@ fn lorem<T : Eq>(t : T) {

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

## `space_before_fn_sig_paren`

Whether to put a space before the opening paren in function signatures

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

#### `false` (default):

```rust
fn lorem() {
// body
}

fn lorem(ipsum: usize) {
// body
}

fn lorem<T>(ipsum: T)
where
T: Add + Sub + Mul + Div,
{
// body
}
```

#### `true`:

```rust
fn lorem () {
// body
}

fn lorem (ipsum: usize) {
// body
}

fn lorem<T> (ipsum: T)
where
T: Add + Sub + Mul + Div,
{
// body
}
```

## `spaces_around_ranges`

Put spaces around the .., ..=, and ... range operators
Expand Down
3 changes: 3 additions & 0 deletions src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,8 @@ create_config! {
spaces_around_ranges: bool, false, false, "Put spaces around the .. and ..= range operators";
binop_separator: SeparatorPlace, SeparatorPlace::Front, true,
"Where to put a binary operator when a binary expression goes multiline";
space_before_fn_sig_paren: bool, false, false,
"Whether to put a space before the opening paren in function signatures";

// Misc.
remove_nested_parens: bool, true, true, "Remove nested parens";
Expand Down Expand Up @@ -600,6 +602,7 @@ space_after_colon = true
space_around_attr_eq = true
spaces_around_ranges = false
binop_separator = "Front"
space_before_fn_sig_paren = false
remove_nested_parens = true
combine_control_expr = true
overflow_delimited_expr = false
Expand Down
4 changes: 4 additions & 0 deletions src/formatting/items.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2255,6 +2255,10 @@ fn rewrite_fn_base(
.last()
.map_or(false, |l| l.trim_start().len() == 1);

if context.config.space_before_fn_sig_paren() {
result.push(' ');
}

// Note that the width and indent don't really matter, we'll re-layout the
// return type later anyway.
let ret_str = fd
Expand Down
19 changes: 19 additions & 0 deletions tests/source/configs/space_before_fn_sig_paren/example.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
// rustfmt-space_before_fn_sig_paren: true
// rustfmt-max_width: 30
// Function space before function paren

fn foo() {
// ...
}
fn foo_with_multi_lined(a: u32, b: u32, c: u32) {
// ...
}
fn foo<T>(bar: T) {
// ...
}
fn foo<T>(a: T, b: u32, c: u32) {
// ...
}
fn foo<T: Foo + Bar, F: FooBar>(a: T, b: u32, c: u32) {
// ...
}
17 changes: 17 additions & 0 deletions tests/source/configs/space_before_fn_sig_paren/fn.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
// rustfmt-space_before_fn_sig_paren: true
// Function space before function paren

fn lorem() {
// body
}

fn lorem(ipsum: usize) {
// body
}

fn lorem<T>(ipsum: T)
where
T: Add + Sub + Mul + Div,
{
// body
}
20 changes: 20 additions & 0 deletions tests/source/configs/space_before_fn_sig_paren/indent_style.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
// rustfmt-space_before_fn_sig_paren: true
// rustfmt-indent_style: Visual
// rustfmt-max_width: 30
// Function space before function paren

fn foo() {
// ...
}
fn foo_with_multi_lined(a: u32, b: u32, c: u32) {
// ...
}
fn foo<T>(bar: T) {
// ...
}
fn foo<T>(a: T, b: u32, c: u32) {
// ...
}
fn foo<T: Foo + Bar, F: FooBar>(a: T, b: u32, c: u32) {
// ...
}
13 changes: 13 additions & 0 deletions tests/source/configs/space_before_fn_sig_paren/max_width.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
// rustfmt-space_before_fn_sig_paren: true
// rustfmt-max_width: 118
// Trait space before function paren

trait Story {
fn swap_context<T: 'static + Context + Send + Sync>(&mut self, context: T) -> Option<Box<Context + Send + Sync>>;
}

impl Story for () {
fn swap_context<T: 'static + Context + Send + Sync>(&mut self, context: T) -> Option<Box<Context + Send + Sync>> {
// ...
}
}
17 changes: 17 additions & 0 deletions tests/source/configs/space_before_fn_sig_paren/trait.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
// rustfmt-space_before_fn_sig_paren: true
// Trait space before function paren

trait Story {
fn swap_context<T>(&mut self, context: T) -> Option<Box<Context>>
where
T: Context;
}

impl Story {
fn swap_context<T>(&mut self, context: T) -> Option<Box<Context>>
where
T: Context,
{
// ...
}
}
34 changes: 34 additions & 0 deletions tests/target/configs/space_before_fn_sig_paren/example.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
// rustfmt-space_before_fn_sig_paren: true
// rustfmt-max_width: 30
// Function space before function paren

fn foo () {
// ...
}
fn foo_with_multi_lined (
a: u32,
b: u32,
c: u32,
) {
// ...
}
fn foo<T> (bar: T) {
// ...
}
fn foo<T> (
a: T,
b: u32,
c: u32,
) {
// ...
}
fn foo<
T: Foo + Bar,
F: FooBar,
> (
a: T,
b: u32,
c: u32,
) {
// ...
}
17 changes: 17 additions & 0 deletions tests/target/configs/space_before_fn_sig_paren/fn.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
// rustfmt-space_before_fn_sig_paren: true
// Function space before function paren

fn lorem () {
// body
}

fn lorem (ipsum: usize) {
// body
}

fn lorem<T> (ipsum: T)
where
T: Add + Sub + Mul + Div,
{
// body
}
29 changes: 29 additions & 0 deletions tests/target/configs/space_before_fn_sig_paren/indent_style.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
// rustfmt-space_before_fn_sig_paren: true
// rustfmt-indent_style: Visual
// rustfmt-max_width: 30
// Function space before function paren

fn foo () {
// ...
}
fn foo_with_multi_lined (a: u32,
b: u32,
c: u32)
{
// ...
}
fn foo<T> (bar: T) {
// ...
}
fn foo<T> (a: T,
b: u32,
c: u32) {
// ...
}
fn foo<T: Foo + Bar,
F: FooBar> (
a: T,
b: u32,
c: u32) {
// ...
}
17 changes: 17 additions & 0 deletions tests/target/configs/space_before_fn_sig_paren/max_width.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
// rustfmt-space_before_fn_sig_paren: true
// rustfmt-max_width: 118
// Trait space before function paren

trait Story {
fn swap_context<T: 'static + Context + Send + Sync> (&mut self, context: T)
-> Option<Box<Context + Send + Sync>>;
}

impl Story for () {
fn swap_context<T: 'static + Context + Send + Sync> (
&mut self,
context: T,
) -> Option<Box<Context + Send + Sync>> {
// ...
}
}
17 changes: 17 additions & 0 deletions tests/target/configs/space_before_fn_sig_paren/trait.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
// rustfmt-space_before_fn_sig_paren: true
// Trait space before function paren

trait Story {
fn swap_context<T> (&mut self, context: T) -> Option<Box<Context>>
where
T: Context;
}

impl Story {
fn swap_context<T> (&mut self, context: T) -> Option<Box<Context>>
where
T: Context,
{
// ...
}
}

0 comments on commit bcf09cb

Please sign in to comment.