[clang-doc] add code comments to comment template (#150648)

Serializes Doxygen code comments in HTML templates. Modifies the
basic-project to add a code example.
This commit is contained in:
Erick Velez 2025-07-25 13:10:15 -07:00 committed by GitHub
parent 445006d3a9
commit 4df8f72608
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
5 changed files with 48 additions and 7 deletions

View File

@ -103,6 +103,18 @@ static json::Value extractTextComments(Object *ParagraphComment) {
return *ParagraphComment->get("Children"); return *ParagraphComment->get("Children");
} }
static json::Value extractVerbatimComments(json::Array VerbatimLines) {
json::Value TextArray = json::Array();
auto &TextArrayRef = *TextArray.getAsArray();
for (auto &Line : VerbatimLines)
TextArrayRef.push_back(*Line.getAsObject()
->get("VerbatimBlockLineComment")
->getAsObject()
->get("Text"));
return TextArray;
}
static Object serializeComment(const CommentInfo &I, Object &Description) { static Object serializeComment(const CommentInfo &I, Object &Description) {
// taken from PR #142273 // taken from PR #142273
Object Obj = Object(); Object Obj = Object();
@ -157,13 +169,11 @@ static Object serializeComment(const CommentInfo &I, Object &Description) {
} }
case CommentKind::CK_VerbatimBlockComment: { case CommentKind::CK_VerbatimBlockComment: {
Child.insert({"Text", I.Text}); if (I.CloseName == "endcode") {
if (!I.CloseName.empty()) // We don't support \code language specification
Child.insert({"CloseName", I.CloseName}); auto TextCommentsArray = extractVerbatimComments(CARef);
Child.insert({"Children", ChildArr}); insertComment(Description, TextCommentsArray, "CodeComments");
if (I.CloseName == "endcode") } else if (I.CloseName == "endverbatim")
insertComment(Description, ChildVal, "CodeComments");
else if (I.CloseName == "endverbatim")
insertComment(Description, ChildVal, "VerbatimComments"); insertComment(Description, ChildVal, "VerbatimComments");
return Obj; return Obj;
} }

View File

@ -469,3 +469,7 @@ a, a:visited, a:hover, a:active {
text-decoration: none; text-decoration: none;
color: inherit; color: inherit;
} }
.code-block {
white-space: pre-line;
}

View File

@ -40,6 +40,20 @@
{{/.}} {{/.}}
{{/ReturnComments}} {{/ReturnComments}}
{{/HasReturnComments}} {{/HasReturnComments}}
{{#HasCodeComments}}
<h3>Code</h3>
{{#CodeComments}}
<div>
<pre class="code-block">
<code>
{{#.}}
{{.}}
{{/.}}
</code>
</pre>
</div>
{{/CodeComments}}
{{/HasCodeComments}}
{{#BlockCommandComment}} {{#BlockCommandComment}}
<div class="block-command-comment__command"> <div class="block-command-comment__command">
<div class="block-command-command"> <div class="block-command-command">

View File

@ -26,6 +26,10 @@ public:
/** /**
* @brief Calculates the perimeter of the circle. * @brief Calculates the perimeter of the circle.
* *
* @code
* Circle circle(5.0);
* double perimeter = circle.perimeter();
* @endcode
* @return double The perimeter of the circle. * @return double The perimeter of the circle.
*/ */
double perimeter() const override; double perimeter() const override;

View File

@ -729,6 +729,15 @@ HTML-CIRCLE: <p></p>
HTML-CIRCLE: </div> HTML-CIRCLE: </div>
HTML-CIRCLE: <h3>Returns</h3> HTML-CIRCLE: <h3>Returns</h3>
HTML-CIRCLE: <p> double The perimeter of the circle.</p> HTML-CIRCLE: <p> double The perimeter of the circle.</p>
HTML-CIRCLE: <h3>Code</h3>
HTML-CIRCLE: <div>
HTML-CIRCLE: <pre class="code-block">
HTML-CIRCLE: <code>
HTML-CIRCLE: Circle circle(5.0);
HTML-CIRCLE: double perimeter = circle.perimeter();
HTML-CIRCLE: </code>
HTML-CIRCLE: </pre>
HTML-CIRCLE: </div>
HTML-CIRCLE: </div> HTML-CIRCLE: </div>
HTML-CIRCLE: </div> HTML-CIRCLE: </div>
HTML-CIRCLE: </div> HTML-CIRCLE: </div>