Skip to content

Commit

Permalink
[clangd] Show struct members when hovering over a typedef
Browse files Browse the repository at this point in the history
  • Loading branch information
HighCommander4 committed Apr 22, 2024
1 parent d98c95b commit 457545f
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 1 deletion.
10 changes: 10 additions & 0 deletions clang-tools-extra/clangd/Hover.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -152,6 +152,16 @@ std::string printDefinition(const Decl *D, PrintingPolicy PP,
std::string Definition;
llvm::raw_string_ostream OS(Definition);
D->print(OS, PP);

// For a typedef to a TagDecl, also print the underlying TagDecl
if (const auto *TND = dyn_cast<TypedefNameDecl>(D)) {
if (const auto *TT = dyn_cast<TagType>(
TND->getUnderlyingType().getCanonicalType().getTypePtr())) {
OS << ";\n";
TT->getDecl()->print(OS, PP);
}
}

OS.flush();
return Definition;
}
Expand Down
16 changes: 15 additions & 1 deletion clang-tools-extra/clangd/unittests/HoverTests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1666,6 +1666,20 @@ TEST(Hover, All) {
HI.NamespaceScope = "ns1::";
HI.Definition = "struct MyClass {\n int field1;\n int field2;\n}";
}},
{
R"cpp(// Typedef to struct
struct Point { int x; int y; };
typedef Point TPoint;
[[TP^oint]] tp;
)cpp",
[](HoverInfo &HI) {
HI.Name = "TPoint";
HI.Kind = index::SymbolKind::TypeAlias;
HI.NamespaceScope = "";
HI.Type = "struct Point";
HI.Definition = "typedef Point TPoint;\nstruct Point {\n int x;\n int y;\n}";
}
},
{
R"cpp(// Class
namespace ns1 {
Expand Down Expand Up @@ -1889,7 +1903,7 @@ TEST(Hover, All) {
HI.Name = "Foo";
HI.Kind = index::SymbolKind::TypeAlias;
HI.NamespaceScope = "";
HI.Definition = "typedef struct Bar Foo";
HI.Definition = "typedef struct Bar Foo;\nstruct Bar {}";
HI.Type = "struct Bar";
HI.Documentation = "Typedef with embedded definition";
}},
Expand Down

0 comments on commit 457545f

Please sign in to comment.