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

Preserve more newlines #201

Merged
merged 3 commits into from
May 28, 2024
Merged

Preserve more newlines #201

merged 3 commits into from
May 28, 2024

Conversation

infinisil
Copy link
Member

@infinisil infinisil commented May 14, 2024

This fixes #188 in a better way, as an alternative to #193

Previously, newlines after the last list/attrs/let-in item would not be preserved. So e.g.

[
  "foo"

]

would wrongly turn into

[
  "foo"
]

Subsequently it would turn into

[ "foo" ]

This PR fixes it, such that the original is preserved in this case.

Whereas previously all the newlines were stripped already during parsing, in this new commit all newlines are preserved, which both fixes the above problem and simplifies the code.

The argument was never used for anything else
@infinisil infinisil requested review from piegamesde and Sereja313 May 14, 2024 20:44
Copy link

github-actions bot commented May 14, 2024

Nixpkgs diff

@infinisil infinisil force-pushed the preserve-more-newlines branch 3 times, most recently from 2ed49bb to 1b4093a Compare May 14, 2024 21:32
Previously, newlines after the last list/attrs/let-in item would not be
preserved. So e.g.

    [
      "foo"

    ]

would wrongly turn into

    [
      "foo"
    ]

Subsequently it would turn into

    [ "foo" ]

This commit fixes it, such that the original is preserved in this case.

Whereas previously all the newlines were stripped already during
parsing, in this new commit all newlines are preserved, which both fixes
the above problem and simplifies the code.

This does introduce one odd edge case due to interaction with another
feature. That feature is that comments before a let-in's `in` part, get
moved below it. The following:

    let
      x = 10;
    # X
    in
    x

Gets reformatted into

    let
      x = 10;
    in
    # X
    x

Due to how the new code preserves newlines, those newly also get moved
further down. So whereas previously the following:

    let
      x = 10;

    in
    x

Would wrongly remove the newline, turning it into:

    let
      x = 10;
    in
    x

With this change it won't anymore, but it will move the newline down:

    let
      x = 10;
    in

    x

While this could be handled with a special case, I'm not sure if that's
worth it. It might be better to rethink this moving of comments further
down idea, I'm not sure if that's necessary anymore.
@infinisil infinisil force-pushed the preserve-more-newlines branch from 1b4093a to 7071b1a Compare May 14, 2024 21:50
This fixes the odd case introduced in the parent commit
@infinisil infinisil force-pushed the preserve-more-newlines branch from 7071b1a to 84b3b6c Compare May 14, 2024 21:50
@nixos-discourse
Copy link

This pull request has been mentioned on NixOS Discourse. There might be relevant details there:

https://discourse.nixos.org/t/formatting-team-meeting-2024-05-14/45433/1

@piegamesde
Copy link
Member

I am not really a fan of the diff and think that the format specification should be changed instead. The rule of not preserving trailing empty lines should be easy to formalize and IMO is a good heuristic for desired behavior.

@infinisil
Copy link
Member Author

@piegamesde The diff is a bit confusing, because all the extra lines it "adds" are ones that were already there before. E.g. the newline in this section was previously removed, but is now preserved.

So while this causes the result to have more lines, the diff is actually smaller!

  • Before it's 32,453 changed files with 2,233,339 additions and 1,189,212 deletions.
  • After it's 32,439 changed files with 2,233,635 additions and 1,184,971 deletions.

Reasons for having this:

  • The code becomes much simpler
  • It's consistent with the standard
  • People can manually control these newlines, the formatter doesn't enforce adding them

Copy link
Contributor

@tomberek tomberek left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Reviewed live during fmt meeting.

@tomberek tomberek merged commit 3b79463 into master May 28, 2024
2 checks passed
@tomberek tomberek deleted the preserve-more-newlines branch May 28, 2024 18:22
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
Status: Done
Development

Successfully merging this pull request may close these issues.

Reaching a stable list formatting requires formatting twice
5 participants