Skip to content

Commit 5e5b87c

Browse files
committed
fix note, warning and retval command.
Change arguments of doxygen commands to be rendered as monospaced
1 parent aa491fc commit 5e5b87c

File tree

5 files changed

+227
-92
lines changed

5 files changed

+227
-92
lines changed

clang-tools-extra/clangd/Hover.cpp

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1589,13 +1589,11 @@ markup::Document HoverInfo::presentDoxygen() const {
15891589
P.appendText(" - ");
15901590
SymbolDoc.returnToMarkup(P);
15911591
}
1592+
1593+
SymbolDoc.retvalsToMarkup(Output);
15921594
Output.addRuler();
15931595
}
15941596

1595-
// add specially handled doxygen commands.
1596-
SymbolDoc.warningsToMarkup(Output);
1597-
SymbolDoc.notesToMarkup(Output);
1598-
15991597
// add any other documentation.
16001598
SymbolDoc.docToMarkup(Output);
16011599

clang-tools-extra/clangd/SymbolDocumentation.cpp

Lines changed: 63 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -34,10 +34,20 @@ void commandToMarkup(markup::Paragraph &Out, StringRef Command,
3434
StringRef Args) {
3535
Out.appendBoldText(commandMarkerAsString(CommandMarker) + Command.str());
3636
Out.appendSpace();
37-
if (!Args.empty()) {
38-
Out.appendEmphasizedText(Args.str());
37+
if (!Args.empty())
38+
Out.appendCode(Args.str());
39+
}
40+
41+
template <typename T> std::string getArgText(const T *Command) {
42+
std::string ArgText;
43+
for (unsigned I = 0; I < Command->getNumArgs(); ++I) {
44+
if (!ArgText.empty())
45+
ArgText += " ";
46+
ArgText += Command->getArgText(I);
3947
}
48+
return ArgText;
4049
}
50+
4151
} // namespace
4252

4353
class ParagraphToMarkupDocument
@@ -70,12 +80,7 @@ class ParagraphToMarkupDocument
7080
void visitInlineCommandComment(const comments::InlineCommandComment *C) {
7181

7282
if (C->getNumArgs() > 0) {
73-
std::string ArgText;
74-
for (unsigned I = 0; I < C->getNumArgs(); ++I) {
75-
if (!ArgText.empty())
76-
ArgText += " ";
77-
ArgText += C->getArgText(I);
78-
}
83+
std::string ArgText = getArgText(C);
7984

8085
switch (C->getRenderKind()) {
8186
case comments::InlineCommandRenderKind::Monospaced:
@@ -158,10 +163,9 @@ class ParagraphToString
158163
void visitInlineCommandComment(const comments::InlineCommandComment *C) {
159164
Out << commandMarkerAsString(C->getCommandMarker());
160165
Out << C->getCommandName(Traits);
161-
if (C->getNumArgs() > 0) {
162-
for (unsigned I = 0; I < C->getNumArgs(); ++I)
163-
Out << " " << C->getArgText(I);
164-
}
166+
std::string ArgText = getArgText(C);
167+
if (!ArgText.empty())
168+
Out << " " << ArgText;
165169
Out << " ";
166170
}
167171

@@ -210,16 +214,27 @@ class BlockCommentToMarkupDocument
210214
Traits)
211215
.visit(B->getParagraph());
212216
break;
217+
case comments::CommandTraits::KCI_note:
218+
case comments::CommandTraits::KCI_warning:
219+
commandToHeadedParagraph(B);
220+
break;
221+
case comments::CommandTraits::KCI_retval: {
222+
// The \retval command describes the return value given as its single
223+
// argument in the corresponding paragraph.
224+
// Note: We know that we have exactly one argument but not if it has an
225+
// associated paragraph.
226+
auto &P = Out.addParagraph().appendCode(getArgText(B));
227+
if (B->getParagraph() && !B->getParagraph()->isWhitespace()) {
228+
P.appendText(" - ");
229+
ParagraphToMarkupDocument(P, Traits).visit(B->getParagraph());
230+
}
231+
return;
232+
}
213233
default: {
214234
// Some commands have arguments, like \throws.
215235
// The arguments are not part of the paragraph.
216236
// We need reconstruct them here.
217-
std::string ArgText;
218-
for (unsigned I = 0; I < B->getNumArgs(); ++I) {
219-
if (!ArgText.empty())
220-
ArgText += " ";
221-
ArgText += B->getArgText(I);
222-
}
237+
std::string ArgText = getArgText(B);
223238
auto &P = Out.addParagraph();
224239
commandToMarkup(P, B->getCommandName(Traits), B->getCommandMarker(),
225240
ArgText);
@@ -262,6 +277,19 @@ class BlockCommentToMarkupDocument
262277
markup::Document &Out;
263278
const comments::CommandTraits &Traits;
264279
StringRef CommentEscapeMarker;
280+
281+
/// Emphasize the given command in a paragraph.
282+
/// Uses the command name with the first letter capitalized as the heading.
283+
void commandToHeadedParagraph(const comments::BlockCommandComment *B) {
284+
Out.addRuler();
285+
auto &P = Out.addParagraph();
286+
std::string Heading = B->getCommandName(Traits).slice(0, 1).upper() +
287+
B->getCommandName(Traits).drop_front().str();
288+
P.appendBoldText(Heading + ":");
289+
P.appendText(" \n");
290+
ParagraphToMarkupDocument(P, Traits).visit(B->getParagraph());
291+
Out.addRuler();
292+
}
265293
};
266294

267295
void SymbolDocCommentVisitor::visitBlockCommandComment(
@@ -282,36 +310,22 @@ void SymbolDocCommentVisitor::visitBlockCommandComment(
282310
}
283311
break;
284312
case comments::CommandTraits::KCI_retval:
285-
RetvalParagraphs.push_back(B->getParagraph());
286-
return;
287-
case comments::CommandTraits::KCI_warning:
288-
WarningParagraphs.push_back(B->getParagraph());
289-
return;
290-
case comments::CommandTraits::KCI_note:
291-
NoteParagraphs.push_back(B->getParagraph());
313+
// Only consider retval commands having an argument.
314+
// The argument contains the described return value which is needed to
315+
// convert it to markup.
316+
if (B->getNumArgs() == 1)
317+
RetvalCommands.push_back(B);
292318
return;
293319
default:
294320
break;
295321
}
296322

297-
// For all other commands, we store them in the UnhandledCommands map.
323+
// For all other commands, we store them in the BlockCommands map.
298324
// This allows us to keep the order of the comments.
299-
UnhandledCommands[CommentPartIndex] = B;
325+
BlockCommands[CommentPartIndex] = B;
300326
CommentPartIndex++;
301327
}
302328

303-
void SymbolDocCommentVisitor::paragraphsToMarkup(
304-
markup::Document &Out,
305-
const llvm::SmallVectorImpl<const comments::ParagraphComment *> &Paragraphs)
306-
const {
307-
if (Paragraphs.empty())
308-
return;
309-
310-
for (const auto *P : Paragraphs) {
311-
ParagraphToMarkupDocument(Out.addParagraph(), Traits).visit(P);
312-
}
313-
}
314-
315329
void SymbolDocCommentVisitor::briefToMarkup(markup::Paragraph &Out) const {
316330
if (!BriefParagraph)
317331
return;
@@ -324,14 +338,6 @@ void SymbolDocCommentVisitor::returnToMarkup(markup::Paragraph &Out) const {
324338
ParagraphToMarkupDocument(Out, Traits).visit(ReturnParagraph);
325339
}
326340

327-
void SymbolDocCommentVisitor::notesToMarkup(markup::Document &Out) const {
328-
paragraphsToMarkup(Out, NoteParagraphs);
329-
}
330-
331-
void SymbolDocCommentVisitor::warningsToMarkup(markup::Document &Out) const {
332-
paragraphsToMarkup(Out, WarningParagraphs);
333-
}
334-
335341
void SymbolDocCommentVisitor::parameterDocToMarkup(
336342
StringRef ParamName, markup::Paragraph &Out) const {
337343
if (ParamName.empty())
@@ -354,7 +360,7 @@ void SymbolDocCommentVisitor::parameterDocToString(
354360

355361
void SymbolDocCommentVisitor::docToMarkup(markup::Document &Out) const {
356362
for (unsigned I = 0; I < CommentPartIndex; ++I) {
357-
if (const auto *BC = UnhandledCommands.lookup(I)) {
363+
if (const auto *BC = BlockCommands.lookup(I)) {
358364
BlockCommentToMarkupDocument(Out, Traits).visit(BC);
359365
} else if (const auto *P = FreeParagraphs.lookup(I)) {
360366
ParagraphToMarkupDocument(Out.addParagraph(), Traits).visit(P);
@@ -382,5 +388,14 @@ void SymbolDocCommentVisitor::templateTypeParmDocToString(
382388
}
383389
}
384390

391+
void SymbolDocCommentVisitor::retvalsToMarkup(markup::Document &Out) const {
392+
if (RetvalCommands.empty())
393+
return;
394+
markup::BulletList &BL = Out.addBulletList();
395+
for (const auto *P : RetvalCommands) {
396+
BlockCommentToMarkupDocument(BL.addItem(), Traits).visit(P);
397+
}
398+
}
399+
385400
} // namespace clangd
386401
} // namespace clang

clang-tools-extra/clangd/SymbolDocumentation.h

Lines changed: 7 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -114,22 +114,14 @@ class SymbolDocCommentVisitor
114114

115115
bool hasReturnCommand() const { return ReturnParagraph; }
116116

117-
bool hasRetvalCommands() const { return !RetvalParagraphs.empty(); }
118-
119-
bool hasNoteCommands() const { return !NoteParagraphs.empty(); }
120-
121-
bool hasWarningCommands() const { return !WarningParagraphs.empty(); }
122-
123117
/// Converts all unhandled comment commands to a markup document.
124118
void docToMarkup(markup::Document &Out) const;
125119
/// Converts the "brief" command(s) to a markup document.
126120
void briefToMarkup(markup::Paragraph &Out) const;
127121
/// Converts the "return" command(s) to a markup document.
128122
void returnToMarkup(markup::Paragraph &Out) const;
129-
/// Converts the "note" command(s) to a markup document.
130-
void notesToMarkup(markup::Document &Out) const;
131-
/// Converts the "warning" command(s) to a markup document.
132-
void warningsToMarkup(markup::Document &Out) const;
123+
/// Converts the "retval" command(s) to a markup document.
124+
void retvalsToMarkup(markup::Document &Out) const;
133125

134126
void visitBlockCommandComment(const comments::BlockCommandComment *B);
135127

@@ -173,19 +165,13 @@ class SymbolDocCommentVisitor
173165
/// Paragraph of the "return" command.
174166
const comments::ParagraphComment *ReturnParagraph = nullptr;
175167

176-
/// Paragraph(s) of the "note" command(s)
177-
llvm::SmallVector<const comments::ParagraphComment *> RetvalParagraphs;
168+
/// All the "retval" command(s)
169+
llvm::SmallVector<const comments::BlockCommandComment *> RetvalCommands;
178170

179-
/// Paragraph(s) of the "note" command(s)
180-
llvm::SmallVector<const comments::ParagraphComment *> NoteParagraphs;
181-
182-
/// Paragraph(s) of the "warning" command(s)
183-
llvm::SmallVector<const comments::ParagraphComment *> WarningParagraphs;
184-
185-
/// All the paragraphs we don't have any special handling for,
186-
/// e.g. "details".
171+
/// All the parsed doxygen block commands.
172+
/// They might have special handling internally like \\note or \\warning
187173
llvm::SmallDenseMap<unsigned, const comments::BlockCommandComment *>
188-
UnhandledCommands;
174+
BlockCommands;
189175

190176
/// Parsed paragaph(s) of the "param" comamnd(s)
191177
llvm::SmallDenseMap<StringRef, const comments::ParamCommandComment *>
@@ -198,11 +184,6 @@ class SymbolDocCommentVisitor
198184
/// All "free" text paragraphs.
199185
llvm::SmallDenseMap<unsigned, const comments::ParagraphComment *>
200186
FreeParagraphs;
201-
202-
void paragraphsToMarkup(
203-
markup::Document &Out,
204-
const llvm::SmallVectorImpl<const comments::ParagraphComment *>
205-
&Paragraphs) const;
206187
};
207188

208189
} // namespace clangd

clang-tools-extra/clangd/unittests/HoverTests.cpp

Lines changed: 39 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4045,9 +4045,19 @@ brief doc
40454045
longer doc)"},
40464046
{[](HoverInfo &HI) {
40474047
HI.Kind = index::SymbolKind::Function;
4048-
HI.Documentation = "@brief brief doc\n\n"
4049-
"longer doc\n@param a this is a param\n@return it "
4050-
"returns something";
4048+
HI.Documentation = R"(@brief brief doc
4049+
4050+
longer doc
4051+
@note this is a note
4052+
4053+
As you see, notes are "inlined".
4054+
@warning this is a warning
4055+
4056+
As well as warnings
4057+
@param a this is a param
4058+
@return it returns something
4059+
@retval 0 if successful
4060+
@retval 1 if failed)";
40514061
HI.Definition = "int foo(int a)";
40524062
HI.ReturnType = "int";
40534063
HI.Name = "foo";
@@ -4068,8 +4078,16 @@ longer doc)"},
40684078
@brief brief doc
40694079
40704080
longer doc
4081+
@note this is a note
4082+
4083+
As you see, notes are "inlined".
4084+
@warning this is a warning
4085+
4086+
As well as warnings
40714087
@param a this is a param
40724088
@return it returns something
4089+
@retval 0 if successful
4090+
@retval 1 if failed
40734091
40744092
---
40754093
```cpp
@@ -4095,8 +4113,25 @@ brief doc
40954113
40964114
`int` - it returns something
40974115
4116+
- `0` - if successful
4117+
- `1` - if failed
4118+
40984119
---
4099-
longer doc)"},
4120+
longer doc
4121+
4122+
---
4123+
**Note:**
4124+
this is a note
4125+
4126+
---
4127+
As you see, notes are "inlined".
4128+
4129+
---
4130+
**Warning:**
4131+
this is a warning
4132+
4133+
---
4134+
As well as warnings)"},
41004135
{[](HoverInfo &HI) {
41014136
HI.Kind = index::SymbolKind::Function;
41024137
HI.Documentation = "@brief brief doc\n\n"

0 commit comments

Comments
 (0)