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 <stddef.h>
#include <algorithm>
#include <cctype>
#include <string>
#include <vector>
@ -142,14 +146,20 @@ static void WriteDocCommentBodyForLocation(io::Printer* printer,
printer->Print(" * <pre>\n");
}
for (int i = 0; i < lines.size(); i++) {
// Most lines should start with a space. Watch out for lines that start
// with a /, since putting that right after the leading asterisk will
// close the comment.
if (!lines[i].empty() && lines[i][0] == '/') {
printer->Print(" * $line$\n", "line", lines[i]);
for (size_t i = 0; i < lines.size(); i++) {
// Lines should start with a single space and any extraneous leading
// spaces should be stripped. For lines starting with a /, the leading
// space will prevent putting it right after the leading asterick from
// closing the comment.
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 {
printer->Print(" *$line$\n", "line", lines[i]);
printer->Print(" *\n");
}
}

Loading…
Cancel
Save