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

Ensure that line comments always get newlines after them. #1325

Merged
merged 2 commits into from
Nov 21, 2023

Conversation

munificent
Copy link
Member

The formatter currently mostly relies on the surrounding piece's setAllowedWhitespace() rules to make sure that a line comment forces the surrounding piece to split in a way that ensures there is a newline after the comment.

This works correctly in lots of places where line comments are actually expected, but isn't reliable when a line comment occurs in a weird location. When that happens, it's important that we still always put a newline after the comment. Otherwise the code is meaningfully changed.

This does that. Whenever a TextPiece ends in a line comment, it will know that it has a trailing newline. It then always writes that to the CodeWriter. That leads to redundant newlines in cases where the surrounding piece is already going to put a newline, so I also moved some of the "pending" newline collapsing code from PieceWriter farther down the pipeline to CodeWriter.

This doesn't change the behavior of any of the current tests. It will fix many untested corners of the language. I'm working on some new tests for comments in if statements that will be fixed by this change, but I'm also changing the actual formatting of if statements, so I want to do that in a separate change.

The formatter currently mostly relies on the surrounding piece's
setAllowedWhitespace() rules to make sure that a line comment forces
the surrounding piece to split in a way that ensures there is a newline
after the comment.

This works correctly in lots of places where line comments are actually
expected, but isn't reliable when a line comment occurs in a weird
location. When that happens, it's important that we still always put a
newline after the comment. Otherwise the code is meaningfully changed.

This does that. Whenever a TextPiece ends in a line comment, it will
know that it has a trailing newline. It then always writes that to the
CodeWriter. That leads to redundant newlines in cases where the
surrounding piece is already going to put a newline, so I also moved
some of the "pending" newline collapsing code from PieceWriter farther
down the pipeline to CodeWriter.

This doesn't change the behavior of any of the current tests. It will
fix many untested corners of the language. I'm working on some new tests
for comments in if statements that will be fixed by this change, but
I'm also changing the actual formatting of if statements, so I want to
do that in a separate change.
@munificent munificent merged commit 967d15e into main Nov 21, 2023
7 checks passed
@munificent munificent deleted the line-comment-newline branch November 21, 2023 01:13
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 this pull request may close these issues.

3 participants