Fix csharp doc commments (#15955)

Fix C# document comments when there is an extra `/` at the start of a comment line.
See https://github.com/grpc/grpc/issues/35905

Related to https://github.com/grpc/grpc/pull/36000 which also fixes the issue in the gRPC C# plugin

Closes #15955

COPYBARA_INTEGRATE_REVIEW=https://github.com/protocolbuffers/protobuf/pull/15955 from tonydnewell:csharp-comment-fix d80e09ae20
PiperOrigin-RevId: 610784971
pull/15958/head
tony 12 months ago committed by Copybara-Service
parent 8a17f5ddeb
commit fe60e5b971
  1. 110
      src/google/protobuf/compiler/csharp/csharp_doc_comment.cc

@ -21,71 +21,85 @@ namespace compiler {
namespace csharp { namespace csharp {
// Functions to create C# XML documentation comments. // Functions to create C# XML documentation comments.
// Currently this only includes documentation comments containing text specified as comments // Currently this only includes documentation comments containing text specified
// in the .proto file; documentation comments generated just from field/message/enum/proto names // as comments in the .proto file; documentation comments generated just from
// is inlined in the relevant code. If more control is required, that code can be moved here. // field/message/enum/proto names is inlined in the relevant code. If more
// control is required, that code can be moved here.
void WriteDocCommentBodyImpl(io::Printer* printer, SourceLocation location) { void WriteDocCommentBodyImpl(io::Printer* printer, SourceLocation location) {
std::string comments = location.leading_comments.empty() ? std::string comments = location.leading_comments.empty()
location.trailing_comments : location.leading_comments; ? location.trailing_comments
if (comments.empty()) { : location.leading_comments;
return; if (comments.empty()) {
} return;
// XML escaping... no need for apostrophes etc as the whole text is going to be a child }
// node of a summary element, not part of an attribute. // XML escaping... no need for apostrophes etc as the whole text is going to
comments = absl::StrReplaceAll(comments, {{"&", "&amp;"}, {"<", "&lt;"}}); // be a child node of a summary element, not part of an attribute.
std::vector<std::string> lines; comments = absl::StrReplaceAll(comments, {{"&", "&amp;"}, {"<", "&lt;"}});
lines = absl::StrSplit(comments, "\n", absl::AllowEmpty()); std::vector<std::string> lines;
// TODO: We really should work out which part to put in the summary and which to put in the remarks... lines = absl::StrSplit(comments, "\n", absl::AllowEmpty());
// but that needs to be part of a bigger effort to understand the markdown better anyway. // TODO: We really should work out which part to put in the summary and which
printer->Print("/// <summary>\n"); // to put in the remarks... but that needs to be part of a bigger effort to
bool last_was_empty = false; // understand the markdown better anyway.
// We squash multiple blank lines down to one, and remove any trailing blank lines. We need printer->Print("/// <summary>\n");
// to preserve the blank lines themselves, as this is relevant in the markdown. bool last_was_empty = false;
// Note that we can't remove leading or trailing whitespace as *that's* relevant in markdown too. // We squash multiple blank lines down to one, and remove any trailing blank
// (We don't skip "just whitespace" lines, either.) // lines. We need to preserve the blank lines themselves, as this is relevant
for (std::vector<std::string>::iterator it = lines.begin(); // in the markdown. Note that we can't remove leading or trailing whitespace
it != lines.end(); ++it) { // as *that's* relevant in markdown too. (We don't skip "just whitespace"
std::string line = *it; // lines, either.)
if (line.empty()) { for (std::vector<std::string>::iterator it = lines.begin(); it != lines.end();
last_was_empty = true; ++it) {
} else { std::string line = *it;
if (last_was_empty) { if (line.empty()) {
printer->Print("///\n"); last_was_empty = true;
} } else {
last_was_empty = false; if (last_was_empty) {
printer->Print("///$line$\n", "line", *it); printer->Print("///\n");
}
last_was_empty = false;
// If the comment has an extra slash at the start then this can cause the
// C# compiler to complain when generating the XML documentation Issue
// https://github.com/grpc/grpc/issues/35905
if (line[0] == '/') {
line.replace(0, 1, "&#x2F;");
} }
printer->Print("///$line$\n", "line", line);
} }
printer->Print("/// </summary>\n"); }
printer->Print("/// </summary>\n");
} }
template <typename DescriptorType> template <typename DescriptorType>
static void WriteDocCommentBody( static void WriteDocCommentBody(io::Printer* printer,
io::Printer* printer, const DescriptorType* descriptor) { const DescriptorType* descriptor) {
SourceLocation location; SourceLocation location;
if (descriptor->GetSourceLocation(&location)) { if (descriptor->GetSourceLocation(&location)) {
WriteDocCommentBodyImpl(printer, location); WriteDocCommentBodyImpl(printer, location);
} }
} }
void WriteMessageDocComment(io::Printer* printer, const Descriptor* message) { void WriteMessageDocComment(io::Printer* printer, const Descriptor* message) {
WriteDocCommentBody(printer, message); WriteDocCommentBody(printer, message);
} }
void WritePropertyDocComment(io::Printer* printer, const FieldDescriptor* field) { void WritePropertyDocComment(io::Printer* printer,
WriteDocCommentBody(printer, field); const FieldDescriptor* field) {
WriteDocCommentBody(printer, field);
} }
void WriteEnumDocComment(io::Printer* printer, const EnumDescriptor* enumDescriptor) { void WriteEnumDocComment(io::Printer* printer,
WriteDocCommentBody(printer, enumDescriptor); const EnumDescriptor* enumDescriptor) {
WriteDocCommentBody(printer, enumDescriptor);
} }
void WriteEnumValueDocComment(io::Printer* printer, const EnumValueDescriptor* value) { void WriteEnumValueDocComment(io::Printer* printer,
WriteDocCommentBody(printer, value); const EnumValueDescriptor* value) {
WriteDocCommentBody(printer, value);
} }
void WriteMethodDocComment(io::Printer* printer, const MethodDescriptor* method) { void WriteMethodDocComment(io::Printer* printer,
WriteDocCommentBody(printer, method); const MethodDescriptor* method) {
WriteDocCommentBody(printer, method);
} }
} // namespace csharp } // namespace csharp

Loading…
Cancel
Save