Skip to content

Commit 15078d7

Browse files
committed
[clangd] Render header of hover card as a heading
Reviewers: sammccall Subscribers: ilya-biryukov, MaskRay, jkorous, arphaman, usaxena95, cfe-commits Tags: #clang Differential Revision: https://reviews.llvm.org/D72625
1 parent 2b53005 commit 15078d7

File tree

5 files changed

+48
-1
lines changed

5 files changed

+48
-1
lines changed

clang-tools-extra/clangd/FormattedString.cpp

+19
Original file line numberDiff line numberDiff 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

169182
std::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

clang-tools-extra/clangd/FormattedString.h

+4
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
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

clang-tools-extra/clangd/Hover.cpp

+4-1
Original file line numberDiff line numberDiff 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);

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

+9
Original file line numberDiff line numberDiff line change
@@ -145,6 +145,15 @@ TEST(Document, Spacer) {
145145
EXPECT_EQ(D.asPlainText(), "foo\n\nbar");
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 \nbaz");
154+
EXPECT_EQ(D.asPlainText(), "foo\nbar\nbaz");
155+
}
156+
148157
TEST(CodeBlock, Render) {
149158
Document D;
150159
// Code blocks preserves any extra spaces.

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

+12
Original file line numberDiff line numberDiff line change
@@ -1631,6 +1631,7 @@ TEST(Hover, DocsFromMostSpecial) {
16311631
}
16321632
}
16331633
}
1634+
16341635
TEST(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

0 commit comments

Comments
 (0)