Ensure that line comments always get newlines after them. #1325
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.