Restore AdjustColumnUsingTokenSpacing for EOL comment column calc Move tokens lookup before the loop and factor spacing logic back into AdjustColumnUsingTokenSpacing with the #2542 newline-aware preserve fix (PR review feedback).
diff --git a/verible/verilog/formatting/formatter.cc b/verible/verilog/formatting/formatter.cc index 8384bef..9bd3dd2 100644 --- a/verible/verilog/formatting/formatter.cc +++ b/verible/verilog/formatting/formatter.cc
@@ -741,6 +741,37 @@ 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()) { + const std::string_view leading = token.OriginalLeadingSpaces(); + const auto last_nl = leading.find_last_of('\n'); + if (last_nl == std::string_view::npos) { + *column += leading.length(); + } else { + // Reset column after the last newline, same as FormattedToken + // emit (GitHub issue 2542). + *column = static_cast<int>(leading.length() - last_nl - 1); + } + } else { + *column += token.before.spaces; + } + break; + } + case verible::SpacingDecision::kWrap: + // Newline then only the wrap indent (same as FormattedToken emit). + *column = token.before.spaces; + break; + 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: @@ -752,8 +783,7 @@ if (line.Tokens().empty()) return 0; int column = 0; - const auto &tokens = line.Tokens(); - const auto &front = tokens.front(); + const auto &front = line.Tokens().front(); if (front.before.action != verible::SpacingDecision::kPreserve) { column += line.IndentationSpaces(); @@ -763,33 +793,10 @@ } column += front.token->text().length(); + const auto &tokens = line.Tokens(); for (size_t i = 1; i < tokens.size(); ++i) { const auto &ftoken = tokens[i]; - switch (ftoken.before.action) { - 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; - } + AdjustColumnUsingTokenSpacing(ftoken, &column); // Do not add the last token's length: that is the EOL comment whose // starting column we want. if (i + 1 < tokens.size()) {