Strip leading spaces from Java codegen doc comments. This is better for consistency, and prevents nonfunctional codegen diffs for editions transforms post-proto formatting.

PiperOrigin-RevId: 600874574
pull/15548/head
Sandy Zhang 10 months ago committed by Copybara-Service
parent 083e3e38e6
commit c73fa81a3e
  1. 24
      src/google/protobuf/compiler/java/doc_comment.cc

@ -11,6 +11,10 @@
#include "google/protobuf/compiler/java/doc_comment.h" #include "google/protobuf/compiler/java/doc_comment.h"
#include <stddef.h>
#include <algorithm>
#include <cctype>
#include <string> #include <string>
#include <vector> #include <vector>
@ -142,14 +146,20 @@ static void WriteDocCommentBodyForLocation(io::Printer* printer,
printer->Print(" * <pre>\n"); printer->Print(" * <pre>\n");
} }
for (int i = 0; i < lines.size(); i++) { for (size_t i = 0; i < lines.size(); i++) {
// Most lines should start with a space. Watch out for lines that start // Lines should start with a single space and any extraneous leading
// with a /, since putting that right after the leading asterisk will // spaces should be stripped. For lines starting with a /, the leading
// close the comment. // space will prevent putting it right after the leading asterick from
if (!lines[i].empty() && lines[i][0] == '/') { // closing the comment.
printer->Print(" * $line$\n", "line", lines[i]); std::string line = lines[i];
line.erase(line.begin(),
std::find_if(line.begin(), line.end(), [](unsigned char ch) {
return !std::isspace(ch);
}));
if (!line.empty()) {
printer->Print(" * $line$\n", "line", line);
} else { } else {
printer->Print(" *$line$\n", "line", lines[i]); printer->Print(" *\n");
} }
} }

Loading…
Cancel
Save