-
Notifications
You must be signed in to change notification settings - Fork 14.7k
[clangd] introduce doxygen parser #150790
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 1 commit
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
78c7cc2
[clangd] introduce doxygen parser
tcottin dfa8278
[clangd] fix review findings
tcottin f34993d
[clangd] fix review findings
tcottin 8abf5ae
[clangd] fix more review findings
tcottin f9d4827
Merge branch 'main' into introduce-doxygen-parsing
emaxx-google File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -18,6 +18,7 @@ | |
#include "Protocol.h" | ||
#include "Selection.h" | ||
#include "SourceCode.h" | ||
#include "SymbolDocumentation.h" | ||
#include "clang-include-cleaner/Analysis.h" | ||
#include "clang-include-cleaner/IncludeSpeller.h" | ||
#include "clang-include-cleaner/Types.h" | ||
|
@@ -41,6 +42,7 @@ | |
#include "clang/AST/Type.h" | ||
#include "clang/Basic/CharInfo.h" | ||
#include "clang/Basic/LLVM.h" | ||
#include "clang/Basic/LangOptions.h" | ||
#include "clang/Basic/SourceLocation.h" | ||
#include "clang/Basic/SourceManager.h" | ||
#include "clang/Basic/Specifiers.h" | ||
|
@@ -627,6 +629,9 @@ HoverInfo getHoverContents(const NamedDecl *D, const PrintingPolicy &PP, | |
HI.Name = printName(Ctx, *D); | ||
const auto *CommentD = getDeclForComment(D); | ||
HI.Documentation = getDeclComment(Ctx, *CommentD); | ||
// safe the language options to be able to create the comment::CommandTraits | ||
tcottin marked this conversation as resolved.
Show resolved
Hide resolved
|
||
// to parse the documentation | ||
HI.CommentOpts = D->getASTContext().getLangOpts().CommentOpts; | ||
enhanceFromIndex(HI, *CommentD, Index); | ||
if (HI.Documentation.empty()) | ||
HI.Documentation = synthesizeDocumentation(D); | ||
|
@@ -1388,9 +1393,170 @@ static std::string formatOffset(uint64_t OffsetInBits) { | |
return Offset; | ||
} | ||
|
||
markup::Document HoverInfo::present() const { | ||
markup::Document HoverInfo::presentDoxygen() const { | ||
// NOTE: this function is currently almost identical to presentDefault(). | ||
emaxx-google marked this conversation as resolved.
Show resolved
Hide resolved
|
||
// This is to have a minimal change when introducing the doxygen parser. | ||
// This function will be changed when rearranging the output for doxygen | ||
// parsed documentation. | ||
|
||
markup::Document Output; | ||
// Header contains a text of the form: | ||
// variable `var` | ||
// | ||
// class `X` | ||
// | ||
// function `foo` | ||
// | ||
// expression | ||
// | ||
// Note that we are making use of a level-3 heading because VSCode renders | ||
// level 1 and 2 headers in a huge font, see | ||
// https://github.com/microsoft/vscode/issues/88417 for details. | ||
markup::Paragraph &Header = Output.addHeading(3); | ||
if (Kind != index::SymbolKind::Unknown) | ||
Header.appendText(index::getSymbolKindString(Kind)).appendSpace(); | ||
assert(!Name.empty() && "hover triggered on a nameless symbol"); | ||
|
||
Header.appendCode(Name); | ||
|
||
if (!Provider.empty()) { | ||
markup::Paragraph &DI = Output.addParagraph(); | ||
DI.appendText("provided by"); | ||
DI.appendSpace(); | ||
DI.appendCode(Provider); | ||
Output.addRuler(); | ||
} | ||
|
||
// Put a linebreak after header to increase readability. | ||
Output.addRuler(); | ||
// Print Types on their own lines to reduce chances of getting line-wrapped by | ||
// editor, as they might be long. | ||
if (ReturnType) { | ||
// For functions we display signature in a list form, e.g.: | ||
// → `x` | ||
// Parameters: | ||
// - `bool param1` | ||
// - `int param2 = 5` | ||
Output.addParagraph().appendText("→ ").appendCode( | ||
llvm::to_string(*ReturnType)); | ||
} | ||
|
||
SymbolDocCommentVisitor SymbolDoc(Documentation, CommentOpts); | ||
|
||
if (Parameters && !Parameters->empty()) { | ||
Output.addParagraph().appendText("Parameters:"); | ||
markup::BulletList &L = Output.addBulletList(); | ||
for (const auto &Param : *Parameters) { | ||
markup::Paragraph &P = L.addItem().addParagraph(); | ||
P.appendCode(llvm::to_string(Param)); | ||
|
||
if (SymbolDoc.isParameterDocumented(llvm::to_string(Param.Name))) { | ||
P.appendText(" -"); | ||
SymbolDoc.parameterDocToMarkup(llvm::to_string(Param.Name), P); | ||
} | ||
} | ||
} | ||
// Don't print Type after Parameters or ReturnType as this will just duplicate | ||
// the information | ||
if (Type && !ReturnType && !Parameters) | ||
Output.addParagraph().appendText("Type: ").appendCode( | ||
llvm::to_string(*Type)); | ||
|
||
if (Value) { | ||
markup::Paragraph &P = Output.addParagraph(); | ||
P.appendText("Value = "); | ||
P.appendCode(*Value); | ||
} | ||
|
||
if (Offset) | ||
Output.addParagraph().appendText("Offset: " + formatOffset(*Offset)); | ||
if (Size) { | ||
auto &P = Output.addParagraph().appendText("Size: " + formatSize(*Size)); | ||
if (Padding && *Padding != 0) { | ||
P.appendText( | ||
llvm::formatv(" (+{0} padding)", formatSize(*Padding)).str()); | ||
} | ||
if (Align) | ||
P.appendText(", alignment " + formatSize(*Align)); | ||
} | ||
|
||
if (CalleeArgInfo) { | ||
assert(CallPassType); | ||
std::string Buffer; | ||
llvm::raw_string_ostream OS(Buffer); | ||
OS << "Passed "; | ||
if (CallPassType->PassBy != HoverInfo::PassType::Value) { | ||
OS << "by "; | ||
if (CallPassType->PassBy == HoverInfo::PassType::ConstRef) | ||
OS << "const "; | ||
OS << "reference "; | ||
} | ||
if (CalleeArgInfo->Name) | ||
OS << "as " << CalleeArgInfo->Name; | ||
else if (CallPassType->PassBy == HoverInfo::PassType::Value) | ||
OS << "by value"; | ||
if (CallPassType->Converted && CalleeArgInfo->Type) | ||
OS << " (converted to " << CalleeArgInfo->Type->Type << ")"; | ||
Output.addParagraph().appendText(OS.str()); | ||
} | ||
|
||
if (Kind == index::SymbolKind::Parameter) { | ||
if (SymbolDoc.isParameterDocumented(Name)) | ||
SymbolDoc.parameterDocToMarkup(Name, Output.addParagraph()); | ||
} else | ||
SymbolDoc.docToMarkup(Output); | ||
|
||
if (!Definition.empty()) { | ||
Output.addRuler(); | ||
std::string Buffer; | ||
|
||
if (!Definition.empty()) { | ||
// Append scope comment, dropping trailing "::". | ||
// Note that we don't print anything for global namespace, to not annoy | ||
// non-c++ projects or projects that are not making use of namespaces. | ||
if (!LocalScope.empty()) { | ||
// Container name, e.g. class, method, function. | ||
// We might want to propagate some info about container type to print | ||
// function foo, class X, method X::bar, etc. | ||
Buffer += | ||
"// In " + llvm::StringRef(LocalScope).rtrim(':').str() + '\n'; | ||
} else if (NamespaceScope && !NamespaceScope->empty()) { | ||
Buffer += "// In namespace " + | ||
llvm::StringRef(*NamespaceScope).rtrim(':').str() + '\n'; | ||
} | ||
|
||
if (!AccessSpecifier.empty()) { | ||
Buffer += AccessSpecifier + ": "; | ||
} | ||
|
||
Buffer += Definition; | ||
} | ||
|
||
Output.addCodeBlock(Buffer, DefinitionLanguage); | ||
} | ||
|
||
if (!UsedSymbolNames.empty()) { | ||
Output.addRuler(); | ||
markup::Paragraph &P = Output.addParagraph(); | ||
P.appendText("provides "); | ||
|
||
const std::vector<std::string>::size_type SymbolNamesLimit = 5; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. At least this constant deserves to be shared between two branches? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I refactored the common parts into own functions now |
||
auto Front = llvm::ArrayRef(UsedSymbolNames).take_front(SymbolNamesLimit); | ||
|
||
llvm::interleave( | ||
Front, [&](llvm::StringRef Sym) { P.appendCode(Sym); }, | ||
[&] { P.appendText(", "); }); | ||
if (UsedSymbolNames.size() > Front.size()) { | ||
P.appendText(" and "); | ||
P.appendText(std::to_string(UsedSymbolNames.size() - Front.size())); | ||
P.appendText(" more"); | ||
} | ||
} | ||
return Output; | ||
} | ||
|
||
markup::Document HoverInfo::presentDefault() const { | ||
markup::Document Output; | ||
// Header contains a text of the form: | ||
// variable `var` | ||
// | ||
|
@@ -1538,21 +1704,22 @@ markup::Document HoverInfo::present() const { | |
std::string HoverInfo::present(MarkupKind Kind) const { | ||
if (Kind == MarkupKind::Markdown) { | ||
const Config &Cfg = Config::current(); | ||
if ((Cfg.Documentation.CommentFormat == | ||
Config::CommentFormatPolicy::Markdown) || | ||
(Cfg.Documentation.CommentFormat == | ||
Config::CommentFormatPolicy::Doxygen)) | ||
// If the user prefers Markdown, we use the present() method to generate | ||
// the Markdown output. | ||
return present().asMarkdown(); | ||
if (Cfg.Documentation.CommentFormat == | ||
Config::CommentFormatPolicy::Markdown) | ||
return presentDefault().asMarkdown(); | ||
if (Cfg.Documentation.CommentFormat == | ||
Config::CommentFormatPolicy::Doxygen) { | ||
std::string T = presentDoxygen().asMarkdown(); | ||
tcottin marked this conversation as resolved.
Show resolved
Hide resolved
|
||
return T; | ||
} | ||
if (Cfg.Documentation.CommentFormat == | ||
Config::CommentFormatPolicy::PlainText) | ||
// If the user prefers plain text, we use the present() method to generate | ||
// the plain text output. | ||
return present().asEscapedMarkdown(); | ||
return presentDefault().asEscapedMarkdown(); | ||
} | ||
|
||
return present().asPlainText(); | ||
return presentDefault().asPlainText(); | ||
} | ||
|
||
// If the backtick at `Offset` starts a probable quoted range, return the range | ||
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.