File tree Expand file tree Collapse file tree 5 files changed +48
-1
lines changed
Expand file tree Collapse file tree 5 files changed +48
-1
lines changed Original file line number Diff line number Diff line change @@ -164,6 +164,19 @@ std::string indentLines(llvm::StringRef Input) {
164164 }
165165 return IndentedR;
166166}
167+
168+ class Heading : public Paragraph {
169+ public:
170+ Heading (size_t Level) : Level(Level) {}
171+ void renderMarkdown (llvm::raw_ostream &OS) const override {
172+ OS << std::string (Level, ' #' ) << ' ' ;
173+ Paragraph::renderMarkdown (OS);
174+ }
175+
176+ private:
177+ size_t Level;
178+ };
179+
167180} // namespace
168181
169182std::string Block::asMarkdown () const {
@@ -278,6 +291,12 @@ BulletList &Document::addBulletList() {
278291 Children.emplace_back (std::make_unique<BulletList>());
279292 return *static_cast <BulletList *>(Children.back ().get ());
280293}
294+
295+ Paragraph &Document::addHeading (size_t Level) {
296+ assert (Level > 0 );
297+ Children.emplace_back (std::make_unique<Heading>(Level));
298+ return *static_cast <Paragraph *>(Children.back ().get ());
299+ }
281300} // namespace markup
282301} // namespace clangd
283302} // namespace clang
Original file line number Diff line number Diff line change 1414#define LLVM_CLANG_TOOLS_EXTRA_CLANGD_FORMATTEDSTRING_H
1515
1616#include " llvm/Support/raw_ostream.h"
17+ #include < cstddef>
1718#include < memory>
1819#include < string>
1920#include < vector>
@@ -86,6 +87,9 @@ class Document {
8687 // / Adds a block of code. This translates to a ``` block in markdown. In plain
8788 // / text representation, the code block will be surrounded by newlines.
8889 void addCodeBlock (std::string Code, std::string Language = " cpp" );
90+ // / Heading is a special type of paragraph that will be prepended with \p
91+ // / Level many '#'s in markdown.
92+ Paragraph &addHeading (size_t Level);
8993
9094 BulletList &addBulletList ();
9195
Original file line number Diff line number Diff line change @@ -469,7 +469,10 @@ markup::Document HoverInfo::present() const {
469469 // class `X`
470470 //
471471 // function `foo` → `int`
472- markup::Paragraph &Header = Output.addParagraph ();
472+ // Note that we are making use of a level-3 heading because VSCode renders
473+ // level 1 and 2 headers in a huge font, see
474+ // https://github.com/microsoft/vscode/issues/88417 for details.
475+ markup::Paragraph &Header = Output.addHeading (3 );
473476 Header.appendText (index::getSymbolKindString (Kind));
474477 assert (!Name.empty () && " hover triggered on a nameless symbol" );
475478 Header.appendCode (Name);
Original file line number Diff line number Diff line change @@ -145,6 +145,15 @@ TEST(Document, Spacer) {
145145 EXPECT_EQ (D.asPlainText (), " foo\n\n bar" );
146146}
147147
148+ TEST (Document, Heading) {
149+ Document D;
150+ D.addHeading (1 ).appendText (" foo" );
151+ D.addHeading (2 ).appendText (" bar" );
152+ D.addParagraph ().appendText (" baz" );
153+ EXPECT_EQ (D.asMarkdown (), " # foo \n ## bar \n baz" );
154+ EXPECT_EQ (D.asPlainText (), " foo\n bar\n baz" );
155+ }
156+
148157TEST (CodeBlock, Render) {
149158 Document D;
150159 // Code blocks preserves any extra spaces.
Original file line number Diff line number Diff line change @@ -1631,6 +1631,7 @@ TEST(Hover, DocsFromMostSpecial) {
16311631 }
16321632 }
16331633}
1634+
16341635TEST (Hover, Present) {
16351636 struct {
16361637 const std::function<void (HoverInfo &)> Builder;
@@ -1720,6 +1721,17 @@ def)",
17201721 }
17211722}
17221723
1724+ // This is a separate test as headings don't create any differences in plaintext
1725+ // mode.
1726+ TEST (Hover, PresentHeadings) {
1727+ HoverInfo HI;
1728+ HI.Kind = index::SymbolKind::Variable;
1729+ HI.Name = " foo" ;
1730+ HI.Type = " type" ;
1731+
1732+ EXPECT_EQ (HI.present ().asMarkdown (), " ### variable `foo` \\ : `type`" );
1733+ }
1734+
17231735} // namespace
17241736} // namespace clangd
17251737} // namespace clang
You can’t perform that action at this time.
0 commit comments