Added comment reflow functionality - #4736
Conversation
|
Just updated the commit - the previously failing tests should now be okay and everything should pass CI now. Sorry about that, still getting the hang of this a little! |
dplassgit
left a comment
There was a problem hiding this comment.
Is there interaction with // dslx-fmt::off? I.e., can you turn off reflow?
| TEST_F(FunctionFmtTest, SecondCommentLineReflowsUpIntoAvailableSpace) { | ||
| const std::string_view original = | ||
| R"(fn f() { | ||
| // one two three four five six seven eight nine ten eleven twelve thirteen fourteen fifteen sixteen seventeen eighteen |
There was a problem hiding this comment.
What if there's a contiguous 101 characters with no spaces?
There was a problem hiding this comment.
It will not reflow that line (which is what I intended). We should never split words. I will add a test to reflect this.
There was a problem hiding this comment.
Not done - did you forget to push?
There was a problem hiding this comment.
The new test is there on line 1351 - right below this test. I didn't delete the tests I had already written.
| } | ||
| const std::string& prefix = prefixed.prefix; | ||
|
|
||
| // Determines whether to emit prefix. |
There was a problem hiding this comment.
Remove redundant comment
|
|
||
| for (size_t i = 0; i < lines.size(); ++i) { | ||
| std::string_view line = lines[i]; | ||
| // Strip trailing whitespace per line. |
There was a problem hiding this comment.
Remove redundant comment
But if you are asking whether there is functionality to turn off specifically reflow, or toggle between the old and new way of reflowing, I did not add this functionality. |
^ That's what I was asking, thanks for the clarification.
^ I didn't expect that. |
An attempt to close #1687. The comments now reflow as desired. Since it was clarified that we should only ever reflow downward, that is what has been implemented.
The gist of the new change is that
FormatCommentsBetweenwill push entire contiguous, multiline comments in onePrefixedReflowblock. Hardlines are not emitted between comments any more. This allows the modified pretty printer to split each comment block into lines (splitting on\n), and emit carriage returns only where necessary (when overflowing to a new line, or when one comment line fits onto the current line being emitted). That's the best brief explanation I can give of the overarching changes, but I'm happy to answer any questions. I've left comments that hopefully make the changes clear.One thing I'd like to note is that I didn't include any functionality that determines comment structure based on comment content (e.g. "don't reflow onto a line that starts with
-"). It is possible some intentional formatting is disturbed by this and may require manual correction, but I decided to make this first PR since it solves an issue that requires manual correction regardless - hopefully the amount of required manual modifications goes down after this change. That said, if including heuristics like these is critical, I am happy to go back and revise these changes.Please let me know what you think, I am open to any and all feedback.