From bcf09cbcce52c05da113353a2e924ca815349c10 Mon Sep 17 00:00:00 2001 From: dylni <46035563+dylni@users.noreply.github.com> Date: Thu, 23 Jul 2020 23:52:43 -0400 Subject: [PATCH] Add option space_before_fn_sig_paren (#4302) * Add option space_after_function_name * Rename option and change type to bool * Revert "Rename option and change type to bool" This reverts commit 6350a1506fef0d599527849db4a7f58e643b9668. * Cover other styles * Fix comments from review * Use simpler interface * Add indent_style test * Rename to space_before_fn_sig_paren --- Configurations.md | 46 +++++++++++++++++++ src/config.rs | 3 ++ src/formatting/items.rs | 4 ++ .../space_before_fn_sig_paren/example.rs | 19 ++++++++ .../configs/space_before_fn_sig_paren/fn.rs | 17 +++++++ .../space_before_fn_sig_paren/indent_style.rs | 20 ++++++++ .../space_before_fn_sig_paren/max_width.rs | 13 ++++++ .../space_before_fn_sig_paren/trait.rs | 17 +++++++ .../space_before_fn_sig_paren/example.rs | 34 ++++++++++++++ .../configs/space_before_fn_sig_paren/fn.rs | 17 +++++++ .../space_before_fn_sig_paren/indent_style.rs | 29 ++++++++++++ .../space_before_fn_sig_paren/max_width.rs | 17 +++++++ .../space_before_fn_sig_paren/trait.rs | 17 +++++++ 13 files changed, 253 insertions(+) create mode 100644 tests/source/configs/space_before_fn_sig_paren/example.rs create mode 100644 tests/source/configs/space_before_fn_sig_paren/fn.rs create mode 100644 tests/source/configs/space_before_fn_sig_paren/indent_style.rs create mode 100644 tests/source/configs/space_before_fn_sig_paren/max_width.rs create mode 100644 tests/source/configs/space_before_fn_sig_paren/trait.rs create mode 100644 tests/target/configs/space_before_fn_sig_paren/example.rs create mode 100644 tests/target/configs/space_before_fn_sig_paren/fn.rs create mode 100644 tests/target/configs/space_before_fn_sig_paren/indent_style.rs create mode 100644 tests/target/configs/space_before_fn_sig_paren/max_width.rs create mode 100644 tests/target/configs/space_before_fn_sig_paren/trait.rs diff --git a/Configurations.md b/Configurations.md index 9c69a242c84..1f1aed66913 100644 --- a/Configurations.md +++ b/Configurations.md @@ -2129,6 +2129,52 @@ fn lorem(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(ipsum: T) +where + T: Add + Sub + Mul + Div, +{ + // body +} +``` + +#### `true`: + +```rust +fn lorem () { + // body +} + +fn lorem (ipsum: usize) { + // body +} + +fn lorem (ipsum: T) +where + T: Add + Sub + Mul + Div, +{ + // body +} +``` + ## `spaces_around_ranges` Put spaces around the .., ..=, and ... range operators diff --git a/src/config.rs b/src/config.rs index 4c28c0866ee..fabe9c30b59 100644 --- a/src/config.rs +++ b/src/config.rs @@ -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"; @@ -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 diff --git a/src/formatting/items.rs b/src/formatting/items.rs index 8da3618054c..cee0d660228 100644 --- a/src/formatting/items.rs +++ b/src/formatting/items.rs @@ -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 diff --git a/tests/source/configs/space_before_fn_sig_paren/example.rs b/tests/source/configs/space_before_fn_sig_paren/example.rs new file mode 100644 index 00000000000..8ed230e4324 --- /dev/null +++ b/tests/source/configs/space_before_fn_sig_paren/example.rs @@ -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(bar: T) { + // ... +} +fn foo(a: T, b: u32, c: u32) { + // ... +} +fn foo(a: T, b: u32, c: u32) { + // ... +} diff --git a/tests/source/configs/space_before_fn_sig_paren/fn.rs b/tests/source/configs/space_before_fn_sig_paren/fn.rs new file mode 100644 index 00000000000..4dbf161d2ab --- /dev/null +++ b/tests/source/configs/space_before_fn_sig_paren/fn.rs @@ -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(ipsum: T) +where + T: Add + Sub + Mul + Div, +{ + // body +} diff --git a/tests/source/configs/space_before_fn_sig_paren/indent_style.rs b/tests/source/configs/space_before_fn_sig_paren/indent_style.rs new file mode 100644 index 00000000000..a2fb456ef2e --- /dev/null +++ b/tests/source/configs/space_before_fn_sig_paren/indent_style.rs @@ -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(bar: T) { + // ... +} +fn foo(a: T, b: u32, c: u32) { + // ... +} +fn foo(a: T, b: u32, c: u32) { + // ... +} diff --git a/tests/source/configs/space_before_fn_sig_paren/max_width.rs b/tests/source/configs/space_before_fn_sig_paren/max_width.rs new file mode 100644 index 00000000000..6f5a699e6e8 --- /dev/null +++ b/tests/source/configs/space_before_fn_sig_paren/max_width.rs @@ -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(&mut self, context: T) -> Option>; +} + +impl Story for () { + fn swap_context(&mut self, context: T) -> Option> { + // ... + } +} diff --git a/tests/source/configs/space_before_fn_sig_paren/trait.rs b/tests/source/configs/space_before_fn_sig_paren/trait.rs new file mode 100644 index 00000000000..fc00d8cbf53 --- /dev/null +++ b/tests/source/configs/space_before_fn_sig_paren/trait.rs @@ -0,0 +1,17 @@ +// rustfmt-space_before_fn_sig_paren: true +// Trait space before function paren + +trait Story { + fn swap_context(&mut self, context: T) -> Option> + where + T: Context; +} + +impl Story { + fn swap_context(&mut self, context: T) -> Option> + where + T: Context, + { + // ... + } +} diff --git a/tests/target/configs/space_before_fn_sig_paren/example.rs b/tests/target/configs/space_before_fn_sig_paren/example.rs new file mode 100644 index 00000000000..53e10ac8de1 --- /dev/null +++ b/tests/target/configs/space_before_fn_sig_paren/example.rs @@ -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 (bar: T) { + // ... +} +fn foo ( + a: T, + b: u32, + c: u32, +) { + // ... +} +fn foo< + T: Foo + Bar, + F: FooBar, +> ( + a: T, + b: u32, + c: u32, +) { + // ... +} diff --git a/tests/target/configs/space_before_fn_sig_paren/fn.rs b/tests/target/configs/space_before_fn_sig_paren/fn.rs new file mode 100644 index 00000000000..a6e4cc7ddea --- /dev/null +++ b/tests/target/configs/space_before_fn_sig_paren/fn.rs @@ -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 (ipsum: T) +where + T: Add + Sub + Mul + Div, +{ + // body +} diff --git a/tests/target/configs/space_before_fn_sig_paren/indent_style.rs b/tests/target/configs/space_before_fn_sig_paren/indent_style.rs new file mode 100644 index 00000000000..010b0d7592b --- /dev/null +++ b/tests/target/configs/space_before_fn_sig_paren/indent_style.rs @@ -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 (bar: T) { + // ... +} +fn foo (a: T, + b: u32, + c: u32) { + // ... +} +fn foo ( + a: T, + b: u32, + c: u32) { + // ... +} diff --git a/tests/target/configs/space_before_fn_sig_paren/max_width.rs b/tests/target/configs/space_before_fn_sig_paren/max_width.rs new file mode 100644 index 00000000000..cdf4bc915c6 --- /dev/null +++ b/tests/target/configs/space_before_fn_sig_paren/max_width.rs @@ -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 (&mut self, context: T) + -> Option>; +} + +impl Story for () { + fn swap_context ( + &mut self, + context: T, + ) -> Option> { + // ... + } +} diff --git a/tests/target/configs/space_before_fn_sig_paren/trait.rs b/tests/target/configs/space_before_fn_sig_paren/trait.rs new file mode 100644 index 00000000000..34c6c1ea00d --- /dev/null +++ b/tests/target/configs/space_before_fn_sig_paren/trait.rs @@ -0,0 +1,17 @@ +// rustfmt-space_before_fn_sig_paren: true +// Trait space before function paren + +trait Story { + fn swap_context (&mut self, context: T) -> Option> + where + T: Context; +} + +impl Story { + fn swap_context (&mut self, context: T) -> Option> + where + T: Context, + { + // ... + } +}