Skip to content

Configuration option to preserve blank lines at beginning of block #2868

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
cmbrandenburg opened this issue Jul 27, 2018 · 6 comments · Fixed by #4303
Closed

Configuration option to preserve blank lines at beginning of block #2868

cmbrandenburg opened this issue Jul 27, 2018 · 6 comments · Fixed by #4303
Milestone

Comments

@cmbrandenburg
Copy link
Contributor

It would be nice if rustfmt had a configuration setting for preserving blank lines at the beginning of blocks.

For example, it would be nice if rustfmt converted this program…

fn main() {
    
    /* blah bleh bluh */
        println!("Hello world");
}

…into the following program:

fn main() {
    
    /* blah bleh bluh */
    println!("Hello world");
}

This preservation of space is especially important when the function signature spans several lines, as sometimes it's nice to have a visual break between a complex signature and the body of code.

There once was a configuration option called remove_blank_lines_at_start_or_end_of_block that did this, but it was removed as part of #1974 for reasons unapparent.

@nrc
Copy link
Member

nrc commented Jul 28, 2018

We're unlikely to do this, tbh. In the complex signature case, there is always either a line with just { or indentation to guide the eye. There is a cost in implementation and UI complexity in having many options, so we want to make sure they pull their weight and this one has not been requested much.

@cmbrandenburg
Copy link
Contributor Author

Thanks for keeping this ticket open despite its inactivity.

I understand your concern for having configuration options pull their weight. Perhaps what I want doesn't require a new option type but instead could be expressed as an alternative brace style?

I've distilled the “blank line” feature to the specific case that bugs me, as shown in the example below.

In this example, the second function is easier on my eyes because the signature is more clearly separated from the body. I can't imagine I'm alone on this. Yet the first function is what rustfmt gives by default—a wall of text.

impl MyType {
    fn normal_len_fn_name_1(
        &mut self,
        param1: SomeOtherType,
        param2: SomeOtherType, 
    ) -> Result<i32, Error> { 
        let a = param1.blah();
        let b = param2.bleh();
        let c = param2.bluh();
        return a + b + c;
    }

    fn normal_len_fn_name_2(
        &mut self,
        param1: SomeOtherType,
        param2: SomeOtherType) 
        -> Result<i32, Error> 
    {   
        let a = param1.blah();
        let b = param2.bleh();
        let c = param2.bluh();
        return a + b + c;
    }
}

Is it feasible to add or extend a configuration option to make rustfmt put the brace on its own line for multi-line function signatures? I think this a change that would better pull its weight.

@Rantanen
Copy link
Contributor

Rantanen commented Oct 20, 2019

we want to make sure they pull their weight and this one has not been requested much.

I don't know where to 'request' that so I'll just add a comment here. I don't expect one comment to change anything really, but at least I can pretend it's counted. :)

(I was slightly disappointed to not find this as a feature, despite indication to make it a configuration option in the original RFC: rust-lang/style-team#57 (comment) - I know priorities change and rustfmt has been actively aiming for "less options")

My workaround for now is to enable brace_style = AlwaysNextLine to force a bit of breathing space between the signature and the body.

Edit: I guess the option was implemented as stated in that RFC discussion, but then later culled as unused.

@jcramb
Copy link

jcramb commented May 15, 2020

I've been running into this recently and it's been infuriating trying to find a way to configure it to leave blank lines at the start of a block. This is something I'd consider to be a vital configuration option, especially looking at some of the other options that are possible. If there's any way this feature can be brought back I'd really appreciate it!

@topecongiro topecongiro added this to the 2.0.1 milestone Jun 29, 2020
@ayazhafiz
Copy link
Contributor

@topecongiro @calebcartwright I am interested in taking a look at this if that's good

@calebcartwright
Copy link
Member

That'd be great @ayazhafiz!

ayazhafiz added a commit to ayazhafiz/rustfmt that referenced this issue Jul 2, 2020
Adds the option for

- Functions
- Control flow blocks
- Expression statements (i.e. `let x = { ... };`)
- Traits
- Trait Impls
- Mods
- Foreign Mods

If I am missing a kind of item, let me know :)

Closes rust-lang#2868
ayazhafiz added a commit to ayazhafiz/rustfmt that referenced this issue Jul 8, 2020
Adds the option for

- Functions
- Control flow blocks
- Expression statements (i.e. `let x = { ... };`)
- Traits
- Trait Impls
- Mods
- Foreign Mods

If I am missing a kind of item, let me know :)

Closes rust-lang#2868
ayazhafiz added a commit to ayazhafiz/rustfmt that referenced this issue Jul 10, 2020
Adds the option for

- Functions
- Control flow blocks
- Expression statements (i.e. `let x = { ... };`)
- Traits
- Trait Impls
- Mods
- Foreign Mods

If I am missing a kind of item, let me know :)

Closes rust-lang#2868
calebcartwright pushed a commit that referenced this issue Jul 14, 2020
* Configuration option to preserve blank lines at start of block

Adds the option for

- Functions
- Control flow blocks
- Expression statements (i.e. `let x = { ... };`)
- Traits
- Trait Impls
- Mods
- Foreign Mods

If I am missing a kind of item, let me know :)

Closes #2868

* fixup! Configuration option to preserve blank lines at start of block

* Simplify `advance_to_first_block_item`

* fixup! Simplify `advance_to_first_block_item`
murchu27 pushed a commit to murchu27/rustfmt that referenced this issue Jan 21, 2022
Cherry-picked from ayazhafiz, branch i/2868. That commit was originally
implemented on the 2.0 branch, while this commit adds it to stable.

Original commit message:

> Adds the option for
>
> - Functions
> - Control flow blocks
> - Expression statements (i.e. `let x = { ... };`)
> - Traits
> - Trait Impls
> - Mods
> - Foreign Mods
>
> If I am missing a kind of item, let me know :)
>
> Closes rust-lang#2868
wafrelka pushed a commit to wafrelka/rustfmt that referenced this issue May 31, 2022
…lang#4303)

* Configuration option to preserve blank lines at start of block

Adds the option for

- Functions
- Control flow blocks
- Expression statements (i.e. `let x = { ... };`)
- Traits
- Trait Impls
- Mods
- Foreign Mods

If I am missing a kind of item, let me know :)

Closes rust-lang#2868

* fixup! Configuration option to preserve blank lines at start of block

* Simplify `advance_to_first_block_item`

* fixup! Simplify `advance_to_first_block_item`
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

7 participants