Skip to content

Commit 39b891d

Browse files
committed
[clangd] introduce CommentFormat option
1 parent 1fe2007 commit 39b891d

File tree

11 files changed

+451
-53
lines changed

11 files changed

+451
-53
lines changed

clang-tools-extra/clangd/ClangdLSPServer.cpp

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1262,11 +1262,9 @@ void ClangdLSPServer::onHover(const TextDocumentPositionParams &Params,
12621262
R.contents.kind = HoverContentFormat;
12631263
R.range = (*H)->SymRange;
12641264
switch (HoverContentFormat) {
1265-
case MarkupKind::PlainText:
1266-
R.contents.value = (*H)->present().asPlainText();
1267-
return Reply(std::move(R));
12681265
case MarkupKind::Markdown:
1269-
R.contents.value = (*H)->present().asMarkdown();
1266+
case MarkupKind::PlainText:
1267+
R.contents.value = (*H)->present(HoverContentFormat);
12701268
return Reply(std::move(R));
12711269
};
12721270
llvm_unreachable("unhandled MarkupKind");

clang-tools-extra/clangd/Config.h

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -177,6 +177,19 @@ struct Config {
177177
/// Controls highlighting modifiers that are disabled.
178178
std::vector<std::string> DisabledModifiers;
179179
} SemanticTokens;
180+
181+
enum class CommentFormatPolicy {
182+
/// Treat comments as plain text.
183+
PlainText,
184+
/// Treat comments as Markdown.
185+
Markdown,
186+
/// Treat comments as doxygen.
187+
Doxygen,
188+
};
189+
190+
struct {
191+
CommentFormatPolicy CommentFormat = CommentFormatPolicy::PlainText;
192+
} Documentation;
180193
};
181194

182195
} // namespace clangd

clang-tools-extra/clangd/ConfigCompile.cpp

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -198,6 +198,7 @@ struct FragmentCompiler {
198198
compile(std::move(F.InlayHints));
199199
compile(std::move(F.SemanticTokens));
200200
compile(std::move(F.Style));
201+
compile(std::move(F.Documentation));
201202
}
202203

203204
void compile(Fragment::IfBlock &&F) {
@@ -760,6 +761,21 @@ struct FragmentCompiler {
760761
}
761762
}
762763

764+
void compile(Fragment::DocumentationBlock &&F) {
765+
if (F.CommentFormat) {
766+
if (auto Val =
767+
compileEnum<Config::CommentFormatPolicy>("CommentFormat",
768+
*F.CommentFormat)
769+
.map("Plaintext", Config::CommentFormatPolicy::PlainText)
770+
.map("Markdown", Config::CommentFormatPolicy::Markdown)
771+
.map("Doxygen", Config::CommentFormatPolicy::Doxygen)
772+
.value())
773+
Out.Apply.push_back([Val](const Params &, Config &C) {
774+
C.Documentation.CommentFormat = *Val;
775+
});
776+
}
777+
}
778+
763779
constexpr static llvm::SourceMgr::DiagKind Error = llvm::SourceMgr::DK_Error;
764780
constexpr static llvm::SourceMgr::DiagKind Warning =
765781
llvm::SourceMgr::DK_Warning;

clang-tools-extra/clangd/ConfigFragment.h

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -372,6 +372,17 @@ struct Fragment {
372372
std::vector<Located<std::string>> DisabledModifiers;
373373
};
374374
SemanticTokensBlock SemanticTokens;
375+
376+
/// Configures documentation style and behaviour.
377+
struct DocumentationBlock {
378+
/// Specifies the format of comments in the code.
379+
/// Valid values are enum Config::CommentFormatPolicy values:
380+
/// - Plaintext: Treat comments as plain text.
381+
/// - Markdown: Treat comments as Markdown.
382+
/// - Doxygen: Treat comments as doxygen.
383+
std::optional<Located<std::string>> CommentFormat;
384+
};
385+
DocumentationBlock Documentation;
375386
};
376387

377388
} // namespace config

clang-tools-extra/clangd/ConfigYAML.cpp

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,7 @@ class Parser {
6868
Dict.handle("Hover", [&](Node &N) { parse(F.Hover, N); });
6969
Dict.handle("InlayHints", [&](Node &N) { parse(F.InlayHints, N); });
7070
Dict.handle("SemanticTokens", [&](Node &N) { parse(F.SemanticTokens, N); });
71+
Dict.handle("Documentation", [&](Node &N) { parse(F.Documentation, N); });
7172
Dict.parse(N);
7273
return !(N.failed() || HadError);
7374
}
@@ -299,6 +300,15 @@ class Parser {
299300
Dict.parse(N);
300301
}
301302

303+
void parse(Fragment::DocumentationBlock &F, Node &N) {
304+
DictParser Dict("Documentation", this);
305+
Dict.handle("CommentFormat", [&](Node &N) {
306+
if (auto Value = scalarValue(N, "CommentFormat"))
307+
F.CommentFormat = *Value;
308+
});
309+
Dict.parse(N);
310+
}
311+
302312
// Helper for parsing mapping nodes (dictionaries).
303313
// We don't use YamlIO as we want to control over unknown keys.
304314
class DictParser {

clang-tools-extra/clangd/Hover.cpp

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
#include "Headers.h"
1616
#include "IncludeCleaner.h"
1717
#include "ParsedAST.h"
18+
#include "Protocol.h"
1819
#include "Selection.h"
1920
#include "SourceCode.h"
2021
#include "clang-include-cleaner/Analysis.h"
@@ -1535,6 +1536,26 @@ markup::Document HoverInfo::present() const {
15351536
return Output;
15361537
}
15371538

1539+
std::string HoverInfo::present(MarkupKind Kind) const {
1540+
if (Kind == MarkupKind::Markdown) {
1541+
const Config &Cfg = Config::current();
1542+
if ((Cfg.Documentation.CommentFormat ==
1543+
Config::CommentFormatPolicy::Markdown) ||
1544+
(Cfg.Documentation.CommentFormat ==
1545+
Config::CommentFormatPolicy::Doxygen))
1546+
// If the user prefers Markdown, we use the present() method to generate
1547+
// the Markdown output.
1548+
return present().asMarkdown();
1549+
if (Cfg.Documentation.CommentFormat ==
1550+
Config::CommentFormatPolicy::PlainText)
1551+
// If the user prefers plain text, we use the present() method to generate
1552+
// the plain text output.
1553+
return present().asEscapedMarkdown();
1554+
}
1555+
1556+
return present().asPlainText();
1557+
}
1558+
15381559
// If the backtick at `Offset` starts a probable quoted range, return the range
15391560
// (including the quotes).
15401561
std::optional<llvm::StringRef> getBacktickQuoteRange(llvm::StringRef Line,

clang-tools-extra/clangd/Hover.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -120,6 +120,8 @@ struct HoverInfo {
120120

121121
/// Produce a user-readable information.
122122
markup::Document present() const;
123+
124+
std::string present(MarkupKind Kind) const;
123125
};
124126

125127
inline bool operator==(const HoverInfo::PrintedType &LHS,

0 commit comments

Comments
 (0)