Skip to content

Commit 0e66105

Browse files
committed
[clangd] Tweak "provides" hover card when symbols have the same name
Previously for overloaded functions we'd show: Provides: foo, bar bar bar bar The symbol name is duplicated ==> only show unique names, since we're not displaying the signature Commas are missing ==> fix the logic which was checking for "last element" by value (though after the above fix this bug is dead anyway) While here, remove a redundant bounds check before take_front(). Differential Revision: https://reviews.llvm.org/D150683
1 parent 8497dfd commit 0e66105

File tree

2 files changed

+11
-11
lines changed

2 files changed

+11
-11
lines changed

clang-tools-extra/clangd/Hover.cpp

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1268,6 +1268,9 @@ void maybeAddUsedSymbols(ParsedAST &AST, HoverInfo &HI, const Inclusion &Inc) {
12681268
for (const auto &UsedSymbolDecl : UsedSymbols)
12691269
HI.UsedSymbolNames.push_back(getSymbolName(UsedSymbolDecl));
12701270
llvm::sort(HI.UsedSymbolNames);
1271+
HI.UsedSymbolNames.erase(
1272+
std::unique(HI.UsedSymbolNames.begin(), HI.UsedSymbolNames.end()),
1273+
HI.UsedSymbolNames.end());
12711274
}
12721275

12731276
} // namespace
@@ -1529,16 +1532,11 @@ markup::Document HoverInfo::present() const {
15291532
P.appendText("provides ");
15301533

15311534
const std::vector<std::string>::size_type SymbolNamesLimit = 5;
1532-
auto Front =
1533-
llvm::ArrayRef(UsedSymbolNames)
1534-
.take_front(std::min(UsedSymbolNames.size(), SymbolNamesLimit));
1535-
1536-
for (const auto &Sym : Front) {
1537-
P.appendCode(Sym);
1538-
if (Sym != Front.back())
1539-
P.appendText(", ");
1540-
}
1535+
auto Front = llvm::ArrayRef(UsedSymbolNames).take_front(SymbolNamesLimit);
15411536

1537+
llvm::interleave(
1538+
Front, [&](llvm::StringRef Sym) { P.appendCode(Sym); },
1539+
[&] { P.appendText(", "); });
15421540
if (UsedSymbolNames.size() > Front.size()) {
15431541
P.appendText(" and ");
15441542
P.appendText(std::to_string(UsedSymbolNames.size() - Front.size()));

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

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3052,6 +3052,7 @@ TEST(Hover, UsedSymbols) {
30523052
} Cases[] = {{R"cpp(
30533053
#include ^"bar.h"
30543054
int fstBar = bar1();
3055+
int another= bar1(0);
30553056
int sndBar = bar2();
30563057
Bar bar;
30573058
int macroBar = BAR;
@@ -3075,6 +3076,7 @@ TEST(Hover, UsedSymbols) {
30753076
#define BAR 5
30763077
int bar1();
30773078
int bar2();
3079+
int bar1(double);
30783080
class Bar {};
30793081
)cpp");
30803082
TU.AdditionalFiles["system/vector"] = guard(R"cpp(
@@ -3487,11 +3489,11 @@ int foo = 3)",
34873489
},
34883490
{[](HoverInfo &HI) {
34893491
HI.Name = "foo.h";
3490-
HI.UsedSymbolNames = {"Foo", "Bar", "Baz"};
3492+
HI.UsedSymbolNames = {"Foo", "Bar", "Bar"};
34913493
},
34923494
R"(foo.h
34933495
3494-
provides Foo, Bar, Baz)"},
3496+
provides Foo, Bar, Bar)"},
34953497
{[](HoverInfo &HI) {
34963498
HI.Name = "foo.h";
34973499
HI.UsedSymbolNames = {"Foo", "Bar", "Baz", "Foobar", "Qux", "Quux"};

0 commit comments

Comments
 (0)