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

feat(fmt): improve newline insertion when comments are in between leaf spans #4548

Merged
merged 8 commits into from
May 9, 2023

Conversation

eightfilms
Copy link
Contributor

@eightfilms eightfilms commented May 8, 2023

Closes #4185

Description

The solution we adopted as a start would have us take the space between leaf spans, except a problem was that a doc comment would be a leaf span but a regular comment would not be a leaf span. Usually this would not be an issue, but in certain cases where we perhaps want to comment code out temporarily to test changes, the formatter would move comments around unintentionally.

For example, a doc comment followed by a commented out function (as shown in the issue) would cause the commented out function to move away from the doc comment:

contract;

abi MyContract {
    fn test_function() -> bool;
}

impl MyContract for Contract {
    /// This is documentation for a commented out function <-- a newline is inserted after this comment
    // fn commented_out_function() {
    //}

    fn test_function() -> bool {
        true
    }
}

Situations like these could ruin developer experience.

The main problem is that the entire whitespace gap between 2 leaf spans could have multiple comments with multiple newline sequences, and all these would be squished to the end of the first leaf span when inserting the newlines.

The fix is to take 'snippets' while we're scanning for newline sequences to insert and try to detect comments within these snippets. We then split insertion of newline sequences into two phases: one before the first comment (which is described above, which caused the issue above), one after the last comment.

Checklist

  • I have linked to any relevant issues.
  • I have commented my code, particularly in hard-to-understand areas.
  • I have updated the documentation where relevant (API docs, the reference, and the Sway book).
  • I have added tests that prove my fix is effective or that my feature works.
  • I have added (or requested a maintainer to add) the necessary Breaking* or New Feature labels where relevant.
  • I have done my best to ensure that my PR adheres to the Fuel Labs Code Review Standards.
  • I have requested a review from the relevant team or maintainers.

The solution we adopted as a start would have us take the space between
leaf spans, except a problem was that a doc comment would be a leaf span
but a regular comment would not be a leaf span. Usually this would not
be an issue, but in certain cases where we perhaps want to comment code
out temporarily to test changes, the formatter would move comments around
unintentionally. For example, a doc comment followed by a commented out
function would cause the commented out function to move away from the doc
comment. Situations like these could ruin developer experience.

The main problem is that the entire whitespace gap between 2 leaf spans
could have multiple comments with multiple newline sequences, and all these
would be squished to the end of the first leaf span when inserting the
newlines.

The fix is to take 'snippets' while we're scanning for newline sequences
to insert and try to detect comments within these snippets. We then split
insertion of newline sequences into two phases: one before the first comment
(which is described above, which caused the issue above), one after
the last comment.
@eightfilms eightfilms self-assigned this May 8, 2023
@eightfilms eightfilms marked this pull request as ready for review May 8, 2023 09:03
@eightfilms eightfilms requested a review from a team May 8, 2023 09:07
@eightfilms eightfilms changed the title feat: improve newline insertion when comments are in between leaf spans feat(fmt): improve newline insertion when comments are in between leaf spans May 8, 2023
Copy link
Member

@kayagokalp kayagokalp left a comment

Choose a reason for hiding this comment

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

Overall lgtm, left a single question just to make sure 👍

swayfmt/src/utils/map/newline.rs Outdated Show resolved Hide resolved
@kayagokalp kayagokalp requested a review from a team May 8, 2023 15:54
Copy link
Member

@kayagokalp kayagokalp left a comment

Choose a reason for hiding this comment

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

Thanks for the fix, LGTM :)

@kayagokalp kayagokalp requested a review from a team May 9, 2023 11:25
@eightfilms eightfilms enabled auto-merge (squash) May 9, 2023 12:57
@eightfilms eightfilms merged commit 1532078 into master May 9, 2023
@eightfilms eightfilms deleted the bing/fmt-handle-comments-in-newline-insertion branch May 9, 2023 23:24
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Weird formatting with commented out functions and docs
3 participants