-
Notifications
You must be signed in to change notification settings - Fork 299
Fix formatter non-convergence for continuation EOL comments #2543
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
base: master
Are you sure you want to change the base?
Changes from 2 commits
b59898d
61d8d5f
80bed97
e7017a1
0e7dbcc
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -26,7 +26,6 @@ | |
| #include <string_view> | ||
| #include <vector> | ||
|
|
||
| #include "absl/base/attributes.h" | ||
| #include "absl/log/die_if_null.h" | ||
| #include "absl/status/status.h" | ||
| #include "absl/status/statusor.h" | ||
|
|
@@ -50,7 +49,6 @@ | |
| #include "verible/common/util/expandable-tree-view.h" | ||
| #include "verible/common/util/interval-set.h" | ||
| #include "verible/common/util/interval.h" | ||
| #include "verible/common/util/iterator-range.h" | ||
| #include "verible/common/util/logging.h" | ||
| #include "verible/common/util/spacer.h" | ||
| #include "verible/common/util/tree-operations.h" | ||
|
|
@@ -743,31 +741,19 @@ class ContinuationCommentAligner { | |
| return column; | ||
| } | ||
|
|
||
| static void AdjustColumnUsingTokenSpacing( | ||
| const verible::FormattedToken &token, int *column) { | ||
| switch (token.before.action) { | ||
| case verible::SpacingDecision::kPreserve: { | ||
| if (token.before.preserved_space_start != | ||
| verible::string_view_null_iterator()) { | ||
| *column += token.OriginalLeadingSpaces().length(); | ||
| } else { | ||
| *column += token.before.spaces; | ||
| } | ||
| break; | ||
| } | ||
| case verible::SpacingDecision::kWrap: | ||
| *column = 0; | ||
| ABSL_FALLTHROUGH_INTENDED; | ||
| case verible::SpacingDecision::kAlign: | ||
| case verible::SpacingDecision::kAppend: | ||
| *column += token.before.spaces; | ||
| break; | ||
| } | ||
| } | ||
|
|
||
| static int CalculateEolCommentColumn(const verible::FormattedExcerpt &line) { | ||
| // Compute the starting column of the trailing EOL comment the same way | ||
| // FormattedExcerpt::FormattedText emits spaces, including: | ||
| // * wrap indents (SpacingDecision::kWrap), and | ||
| // * preserved leading whitespace that may contain newlines (common when | ||
| // an original line break is kept). Counting those newlines as width | ||
| // made continuation comments land on the wrong column and fail to | ||
| // converge on re-format (GitHub issue 2542). | ||
| if (line.Tokens().empty()) return 0; | ||
|
|
||
| int column = 0; | ||
| const auto &front = line.Tokens().front(); | ||
| const auto &tokens = line.Tokens(); | ||
| const auto &front = tokens.front(); | ||
|
|
||
| if (front.before.action != verible::SpacingDecision::kPreserve) { | ||
| column += line.IndentationSpaces(); | ||
|
|
@@ -777,12 +763,39 @@ class ContinuationCommentAligner { | |
| } | ||
| column += front.token->text().length(); | ||
|
|
||
| for (const auto &ftoken : verible::make_range(line.Tokens().begin() + 1, | ||
| line.Tokens().end() - 1)) { | ||
| AdjustColumnUsingTokenSpacing(ftoken, &column); | ||
| column += ftoken.token->text().length(); | ||
| for (size_t i = 1; i < tokens.size(); ++i) { | ||
| const auto &ftoken = tokens[i]; | ||
| switch (ftoken.before.action) { | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The previous implementation, that put the logic into its own function (
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I've created a new helper function here to help with readability. |
||
| case verible::SpacingDecision::kPreserve: { | ||
| if (ftoken.before.preserved_space_start != | ||
| verible::string_view_null_iterator()) { | ||
| const std::string_view leading = ftoken.OriginalLeadingSpaces(); | ||
| const auto last_nl = leading.find_last_of('\n'); | ||
| if (last_nl == std::string_view::npos) { | ||
| column += leading.length(); | ||
| } else { | ||
| column = static_cast<int>(leading.length() - last_nl - 1); | ||
| } | ||
| } else { | ||
| column += ftoken.before.spaces; | ||
| } | ||
| break; | ||
| } | ||
| case verible::SpacingDecision::kWrap: | ||
| // Newline then only the wrap indent (same as FormattedToken emit). | ||
| column = ftoken.before.spaces; | ||
| break; | ||
| case verible::SpacingDecision::kAlign: | ||
| case verible::SpacingDecision::kAppend: | ||
| column += ftoken.before.spaces; | ||
| break; | ||
| } | ||
| // Do not add the last token's length: that is the EOL comment whose | ||
| // starting column we want. | ||
| if (i + 1 < tokens.size()) { | ||
| column += ftoken.token->text().length(); | ||
| } | ||
| } | ||
| AdjustColumnUsingTokenSpacing(line.Tokens().back(), &column); | ||
|
|
||
| CHECK_GE(column, 0); | ||
| return column; | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
since we're using tokens here first, I'd probably move the
const auto &tokens = line.Tokens();right in front of the for loop.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done.