Skip to content

Added comment reflow functionality - #4736

Open
achowd32 wants to merge 1 commit into
google:mainfrom
achowd32:overflow
Open

Added comment reflow functionality#4736
achowd32 wants to merge 1 commit into
google:mainfrom
achowd32:overflow

Conversation

@achowd32

@achowd32 achowd32 commented Aug 7, 2026

Copy link
Copy Markdown
Contributor

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 FormatCommentsBetween will push entire contiguous, multiline comments in one PrefixedReflow block. 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.

@achowd32

Copy link
Copy Markdown
Contributor Author

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 dplassgit left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

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

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

What if there's a contiguous 101 characters with no spaces?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

It will not reflow that line (which is what I intended). We should never split words. I will add a test to reflect this.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

done!

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Not done - did you forget to push?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

The new test is there on line 1351 - right below this test. I didn't delete the tests I had already written.

Comment thread xls/dslx/fmt/pretty_print.cc Outdated
}
const std::string& prefix = prefixed.prefix;

// Determines whether to emit prefix.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Remove redundant comment

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

done!

Comment thread xls/dslx/fmt/pretty_print.cc Outdated

for (size_t i = 0; i < lines.size(); ++i) {
std::string_view line = lines[i];
// Strip trailing whitespace per line.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Remove redundant comment

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

done!

@achowd32

Copy link
Copy Markdown
Contributor Author

Is there interaction with // dslx-fmt::off? I.e., can you turn off reflow?

// dslx-fmt::off and #[dslx_format_disable] work as usual - they will disable any reflow. Lines above 100 characters just stay as is (but of course this means the formatter will not adopt the old mechanism either, where long lines are broken up but do not reflow smoothly into one another).

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.

@dplassgit

Copy link
Copy Markdown
Contributor

Is there interaction with // dslx-fmt::off? I.e., can you turn off reflow?

// dslx-fmt::off and #[dslx_format_disable] work as usual - they will disable any reflow. Lines above 100 characters just stay as is (but of course this means the formatter will not adopt the old mechanism either, where long lines are broken up but do not reflow smoothly into one another).

^ That's what I was asking, thanks for the clarification.

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.

^ I didn't expect that.

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.

DSLX formatter should reflow multi-line comments as a whole

2 participants