csharp_doc_comment.cc 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. // Protocol Buffers - Google's data interchange format
  2. // Copyright 2008 Google Inc. All rights reserved.
  3. // https://developers.google.com/protocol-buffers/
  4. //
  5. // Redistribution and use in source and binary forms, with or without
  6. // modification, are permitted provided that the following conditions are
  7. // met:
  8. //
  9. // * Redistributions of source code must retain the above copyright
  10. // notice, this list of conditions and the following disclaimer.
  11. // * Redistributions in binary form must reproduce the above
  12. // copyright notice, this list of conditions and the following disclaimer
  13. // in the documentation and/or other materials provided with the
  14. // distribution.
  15. // * Neither the name of Google Inc. nor the names of its
  16. // contributors may be used to endorse or promote products derived from
  17. // this software without specific prior written permission.
  18. //
  19. // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  20. // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  21. // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  22. // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  23. // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  24. // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  25. // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  26. // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  27. // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  28. // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  29. // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  30. // Author: kenton@google.com (Kenton Varda)
  31. // Based on original Protocol Buffers design by
  32. // Sanjay Ghemawat, Jeff Dean, and others.
  33. #include <google/protobuf/compiler/csharp/csharp_doc_comment.h>
  34. #include <google/protobuf/descriptor.h>
  35. #include <google/protobuf/io/printer.h>
  36. #include <google/protobuf/stubs/strutil.h>
  37. namespace google {
  38. namespace protobuf {
  39. namespace compiler {
  40. namespace csharp {
  41. // Functions to create C# XML documentation comments.
  42. // Currently this only includes documentation comments containing text specified as comments
  43. // in the .proto file; documentation comments generated just from field/message/enum/proto names
  44. // is inlined in the relevant code. If more control is required, that code can be moved here.
  45. void WriteDocCommentBodyImpl(io::Printer* printer, SourceLocation location) {
  46. string comments = location.leading_comments.empty() ?
  47. location.trailing_comments : location.leading_comments;
  48. if (comments.empty()) {
  49. return;
  50. }
  51. // XML escaping... no need for apostrophes etc as the whole text is going to be a child
  52. // node of a summary element, not part of an attribute.
  53. comments = StringReplace(comments, "&", "&amp;", true);
  54. comments = StringReplace(comments, "<", "&lt;", true);
  55. std::vector<string> lines = Split(comments, "\n", false /* skip_empty */);
  56. // TODO: We really should work out which part to put in the summary and which to put in the remarks...
  57. // but that needs to be part of a bigger effort to understand the markdown better anyway.
  58. printer->Print("/// <summary>\n");
  59. bool last_was_empty = false;
  60. // We squash multiple blank lines down to one, and remove any trailing blank lines. We need
  61. // to preserve the blank lines themselves, as this is relevant in the markdown.
  62. // Note that we can't remove leading or trailing whitespace as *that's* relevant in markdown too.
  63. // (We don't skip "just whitespace" lines, either.)
  64. for (std::vector<string>::iterator it = lines.begin(); it != lines.end(); ++it) {
  65. string line = *it;
  66. if (line.empty()) {
  67. last_was_empty = true;
  68. } else {
  69. if (last_was_empty) {
  70. printer->Print("///\n");
  71. }
  72. last_was_empty = false;
  73. printer->Print("///$line$\n", "line", *it);
  74. }
  75. }
  76. printer->Print("/// </summary>\n");
  77. }
  78. template <typename DescriptorType>
  79. static void WriteDocCommentBody(
  80. io::Printer* printer, const DescriptorType* descriptor) {
  81. SourceLocation location;
  82. if (descriptor->GetSourceLocation(&location)) {
  83. WriteDocCommentBodyImpl(printer, location);
  84. }
  85. }
  86. void WriteMessageDocComment(io::Printer* printer, const Descriptor* message) {
  87. WriteDocCommentBody(printer, message);
  88. }
  89. void WritePropertyDocComment(io::Printer* printer, const FieldDescriptor* field) {
  90. WriteDocCommentBody(printer, field);
  91. }
  92. void WriteEnumDocComment(io::Printer* printer, const EnumDescriptor* enumDescriptor) {
  93. WriteDocCommentBody(printer, enumDescriptor);
  94. }
  95. void WriteEnumValueDocComment(io::Printer* printer, const EnumValueDescriptor* value) {
  96. WriteDocCommentBody(printer, value);
  97. }
  98. void WriteMethodDocComment(io::Printer* printer, const MethodDescriptor* method) {
  99. WriteDocCommentBody(printer, method);
  100. }
  101. } // namespace csharp
  102. } // namespace compiler
  103. } // namespace protobuf
  104. } // namespace google