Skip to content
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

Customization: option to prevent joining lines #40

Closed
bruno-medeiros opened this issue Nov 16, 2016 · 4 comments
Closed

Customization: option to prevent joining lines #40

bruno-medeiros opened this issue Nov 16, 2016 · 4 comments

Comments

@bruno-medeiros
Copy link

I'd like to make a case that one of the customization options for rustfmt should be an option to be conservative about joining lines.

Recently in another issue @nikomatsakis commented that rustfmt turns this code:

    pub fn iter_parents<'a>(&'a self) -> impl Iterator<Item=&'a Environment>+'a {
        let parent = self.parent.as_ref();
        Box::new(once(self)
                 .chain(
                     parent.into_iter()
                           .flat_map(|e| e.iter_parents())))
            as Box<Iterator<Item=&'a Environment>>
    }

into this:

    pub fn iter_parents<'a>(&'a self) -> impl TODO {
        let parent = self.parent.as_ref();
        Box::new(once(self).chain(parent.into_iter()
            .flat_map(|e| e.iter_parents()))) as Box<Iterator<Item = &'a Environment>>
    }

which wasn't desirable. This was in the context of the #8 discussion, however the issue of joining lines is a more general issue that goes beyond what indentation style one chooses.

Let me me give a motivating example. Suppose you have this code and a column limit of 100:

impl Foo {
    pub fn function_one(some_parameter: SomeParameter) 
       -> Response 
    {
        // ...
    }
    
    pub fn function_two(some_parameter: SomeParameter, another_parameter: SomeParameter) 
        -> Response 
    {
        // ...
    }
}

If you invoke the formatter and it forcibly joins lines when it can, it can join function_one into:

    pub fn function_one(some_parameter: SomeParameter)  -> Response {

because function_one signature can still fit in the column limit. However, if function_one and function_two are conceptually related to one another, you might want for them to maintain a similar line-break structure, because they will look similar and be easier to read (IMO), due to being more uniform.

Similarly, but suppose instead the column limit is 80. You could have this code to start with:

impl Foo {
    pub fn function_one(
        some_parameter: SomeParameter
    ) {
        // ...
    }
    
    pub fn function_two(
         some_parameter: SomeParameter, 
         another_parameter: SomeParameter
    ) {
        // ...
    }
}

Running the formatter could join function_one into:

    pub fn function_one(some_parameter: SomeParameter) {

because that would fit the column limit. But again you might want to preserve line-breaks in order to be more consistent with how the other related function is formatted.

It also makes it easier to add/remove parameters to either function, since you just need to add/remove the line with the parameter. Editing whole lines is usually easier/quicker than segments of a line, in most editors.

Also, it's more "stable" if lines are not joined: renaming a parameter name (or the type) to something shorter does not risk joining lines. Whereas otherwise it might, if the formatter always joins lines.

I would find annoying if the whole signature gets reformatted and joined, just because I rename function_two's some_parameter to some_param or so. Again, I admit this can be subjective. If you care about not wasting vertical space (I don't) you might prefer the opposite.

@nrc
Copy link
Member

nrc commented Nov 16, 2016

This is not really possible the way Rustfmt is written. It doesn't really take the source text into account, it parses the source, and then reformats the AST from scratch. An early version of Rustfmt tried a 'modifying' rather than re-writing approach, but it didn't work out very well - as soon as you start correcting in even minor ways, you end up needing to shuffle around lots of surrounding code and then it is really difficult to know what preserving is meant to do. So, I think it is possible to do this, but it would be a rewrite of Rustfmt and much more difficult to do well than the current approach,

@nrc
Copy link
Member

nrc commented Nov 29, 2016

Closing since this is not really possible in Rustfmt, and because this would more likely be an option in Rustfmt rather than part of the style specification.

@nrc nrc closed this as completed Nov 29, 2016
@bruno-medeiros
Copy link
Author

Closing since this is not really possible in Rustfmt, and because this would more likely be an option in Rustfmt rather than part of the style specification.

Are you sure? It would have to mean the style would not enforce/specify that lines should be joined in such situations.

@nrc
Copy link
Member

nrc commented Dec 1, 2016

Are you sure?

Yes. We try do something like this for Strings and comments, and it is horrible.

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

No branches or pull requests

2 participants