Skip to content

Commit 13553d5

Browse files
committed
[clangd] Rearrange type, returntype and parameters in hover card
Summary: Moves type/returntype into its own line as it is more readable in cases where the type is long. Also gives parameter lists a heading, `Parameters:` to make them stand out. Leaves the `right arrow` instead of `Returns: ` before Return Type to make output more symmetric. ``` function foo Returns: ret_type Parameters: - int x ``` vs ``` function foo 🡺 ret_type Parameters: - int x ``` Reviewers: sammccall, ilya-biryukov Subscribers: MaskRay, jkorous, arphaman, usaxena95, cfe-commits Tags: #clang Differential Revision: https://reviews.llvm.org/D72623 (cherry picked from commit 44f9c7a)
1 parent a6f550e commit 13553d5

File tree

3 files changed

+31
-25
lines changed

3 files changed

+31
-25
lines changed

clang-tools-extra/clangd/Hover.cpp

Lines changed: 24 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -510,41 +510,45 @@ llvm::Optional<HoverInfo> getHover(ParsedAST &AST, Position Pos,
510510
markup::Document HoverInfo::present() const {
511511
markup::Document Output;
512512
// Header contains a text of the form:
513-
// variable `var` : `int`
513+
// variable `var`
514514
//
515515
// class `X`
516516
//
517-
// function `foo` → `int`
517+
// function `foo`
518+
//
519+
// expression
518520
//
519-
// expression : `int`
520521
// Note that we are making use of a level-3 heading because VSCode renders
521522
// level 1 and 2 headers in a huge font, see
522523
// https://github.com/microsoft/vscode/issues/88417 for details.
523524
markup::Paragraph &Header = Output.addHeading(3);
524525
Header.appendText(index::getSymbolKindString(Kind));
525526
assert(!Name.empty() && "hover triggered on a nameless symbol");
526527
Header.appendCode(Name);
527-
if (ReturnType) {
528-
Header.appendText("");
529-
Header.appendCode(*ReturnType);
530-
} else if (Type) {
531-
Header.appendText(":");
532-
Header.appendCode(*Type);
533-
}
534528

535529
// Put a linebreak after header to increase readability.
536530
Output.addRuler();
537-
// For functions we display signature in a list form, e.g.:
538-
// - `bool param1`
539-
// - `int param2 = 5`
540-
if (Parameters && !Parameters->empty()) {
541-
markup::BulletList &L = Output.addBulletList();
542-
for (const auto &Param : *Parameters) {
543-
std::string Buffer;
544-
llvm::raw_string_ostream OS(Buffer);
545-
OS << Param;
546-
L.addItem().addParagraph().appendCode(std::move(OS.str()));
531+
// Print Types on their own lines to reduce chances of getting line-wrapped by
532+
// editor, as they might be long.
533+
if (ReturnType) {
534+
// For functions we display signature in a list form, e.g.:
535+
// 🡺 `x`
536+
// Parameters:
537+
// - `bool param1`
538+
// - `int param2 = 5`
539+
Output.addParagraph().appendText("🡺").appendCode(*ReturnType);
540+
if (Parameters && !Parameters->empty()) {
541+
Output.addParagraph().appendText("Parameters:");
542+
markup::BulletList &L = Output.addBulletList();
543+
for (const auto &Param : *Parameters) {
544+
std::string Buffer;
545+
llvm::raw_string_ostream OS(Buffer);
546+
OS << Param;
547+
L.addItem().addParagraph().appendCode(std::move(OS.str()));
548+
}
547549
}
550+
} else if (Type) {
551+
Output.addParagraph().appendText("Type: ").appendCode(*Type);
548552
}
549553

550554
if (Value) {

clang-tools-extra/clangd/test/hover.test

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
# CHECK-NEXT: "result": {
1010
# CHECK-NEXT: "contents": {
1111
# CHECK-NEXT: "kind": "plaintext",
12-
# CHECK-NEXT: "value": "function foo void\n\nvoid foo()"
12+
# CHECK-NEXT: "value": "function foo\n\n🡺 void\n\nvoid foo()"
1313
# CHECK-NEXT: },
1414
# CHECK-NEXT: "range": {
1515
# CHECK-NEXT: "end": {

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

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1722,8 +1722,10 @@ template <typename T, typename C = bool> class Foo {})",
17221722
HI.NamespaceScope = "ns::";
17231723
HI.Definition = "ret_type foo(params) {}";
17241724
},
1725-
R"(function foo → ret_type
1725+
R"(function foo
17261726
1727+
🡺 ret_type
1728+
Parameters:
17271729
-
17281730
- type
17291731
- type foo
@@ -1741,8 +1743,9 @@ ret_type foo(params) {})",
17411743
HI.Type = "type";
17421744
HI.Definition = "def";
17431745
},
1744-
R"(variable foo : type
1746+
R"(variable foo
17451747
1748+
Type: type
17461749
Value = value
17471750
17481751
// In test::bar
@@ -1763,9 +1766,8 @@ TEST(Hover, PresentHeadings) {
17631766
HoverInfo HI;
17641767
HI.Kind = index::SymbolKind::Variable;
17651768
HI.Name = "foo";
1766-
HI.Type = "type";
17671769

1768-
EXPECT_EQ(HI.present().asMarkdown(), "### variable `foo` \\: `type`");
1770+
EXPECT_EQ(HI.present().asMarkdown(), "### variable `foo`");
17691771
}
17701772

17711773
// This is a separate test as rulers behave differently in markdown vs

0 commit comments

Comments
 (0)