Skip to content

fix(generator): preserve comment introducers across embedded newlines - #16332

Merged
colinmoy merged 2 commits into
googleapis:mainfrom
colinmoy:fix-format-comment-block
Aug 18, 2026
Merged

fix(generator): preserve comment introducers across embedded newlines#16332
colinmoy merged 2 commits into
googleapis:mainfrom
colinmoy:fix-format-comment-block

Conversation

@colinmoy

Copy link
Copy Markdown
Contributor

No description provided.

@colinmoy
colinmoy requested a review from a team as a code ownerAugust 7, 2026 21:00

@gemini-code-assistgemini-code-assistBot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Code Review

This pull request updates FormatCommentBlock in codegen_utils.cc to correctly handle multi-line comments and paragraph splitting, and adds corresponding unit tests. The review feedback recommends optimizing memory usage by using absl::string_view instead of std::string to avoid unnecessary heap allocations, and refactoring the final formatting loop to eliminate an unnecessary else block, aligning with the repository's style guide.

Comment threadgenerator/internal/codegen_utils.cc Outdated
Comment on lines 334 to 357
std::vector<std::string> lines;
std::size_t start_pos = 0;
while (start_pos != std::string::npos) {
std::size_t boundary = start_pos + comment_width;
std::size_t end_pos = boundary;
if (boundary < comment.length()) {
// Look backward from the boundary for the last word
end_pos = comment.rfind(' ', boundary);
// If there is only one word, find and use its boundary
if (end_pos == std::string::npos || end_pos < start_pos) {
end_pos = comment.find(' ', boundary);
std::vector<absl::string_view> paragraphs = absl::StrSplit(comment, '\n');
for (auto const& paragraph : paragraphs) {
if (paragraph.empty()) {
lines.emplace_back("");
continue;
}
std::size_t start_pos = 0;
while (start_pos != absl::string_view::npos) {
std::size_t boundary = start_pos + comment_width;
std::size_t end_pos = boundary;
if (boundary < paragraph.length()) {
// Look backward from the boundary for the last word
end_pos = paragraph.rfind(' ', boundary);
// If there is only one word, find and use its boundary
if (end_pos == absl::string_view::npos || end_pos < start_pos) {
end_pos = paragraph.find(' ', boundary);
}
}
lines.push_back(
std::string(paragraph.substr(start_pos, end_pos - start_pos)));
start_pos = paragraph.find_first_not_of(' ', end_pos);
}
lines.push_back(comment.substr(start_pos, end_pos - start_pos));
start_pos = comment.find_first_not_of(' ', end_pos);
}

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

medium

Using std::vector<std::string> causes unnecessary memory allocations and copies for each line of the comment block. Since the comment parameter is passed as a const std::string& and its lifetime outlives this function, we can use std::vector<absl::string_view> instead to avoid these heap allocations.

std::vector<absl::string_view>lines;
std::vector<absl::string_view>paragraphs=absl::StrSplit(comment, '\n');
for (auto const&paragraph : paragraphs) {
if (paragraph.empty()) {
lines.emplace_back();
continue;
}
std::size_tstart_pos=0;
while (start_pos!=absl::string_view::npos) {
std::size_tboundary=start_pos+comment_width;
std::size_tend_pos=boundary;
if (boundary<paragraph.length()) {
// Look backward from the boundary for the last wordend_pos=paragraph.rfind(' ', boundary);
// If there is only one word, find and use its boundaryif (end_pos==absl::string_view::npos||end_pos<start_pos) {
end_pos=paragraph.find(' ', boundary);
}
}
lines.push_back(paragraph.substr(start_pos, end_pos-start_pos));
start_pos=paragraph.find_first_not_of(' ', end_pos);
}
}
References
  1. Scrutinize copies of non-fundamental C++ types. Is it necessary to copy the data? (link)

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

use std::string_view instead of absl::string_view

Copy link
Copy Markdown
ContributorAuthor

Choose a reason for hiding this comment

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

Done

Comment threadgenerator/internal/codegen_utils.cc
@colinmoy
colinmoyforce-pushed the fix-format-comment-block branch from f41554a to 2235e98CompareAugust 7, 2026 22:12
@colinmoy
colinmoyforce-pushed the fix-format-comment-block branch from e8e2c42 to 2737a0eCompareAugust 17, 2026 22:33
@codecov

codecovBot commented Aug 17, 2026

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 92.24%. Comparing base (10083ab) to head (f58b6f2).
⚠️ Report is 1 commits behind head on main.

Additional details and impacted files
@@ Coverage Diff @@## main #16332 +/- ##
=======================================
Coverage 92.23% 92.24% =======================================
Files 2227 2227 Lines 209543 209563 +20 =======================================
+ Hits 193278 193308 +30 + Misses 16265 16255 -10 

☔ View full report in Codecov by Harness.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@colinmoy
colinmoy merged commit a20125d into googleapis:mainAug 18, 2026
63 checks passed
@colinmoy
colinmoy deleted the fix-format-comment-block branch August 18, 2026 17:17
Sign up for freeto join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants

@colinmoy@scotthart