diff --git a/clang-tools-extra/clangd/Hover.h b/clang-tools-extra/clangd/Hover.h --- a/clang-tools-extra/clangd/Hover.h +++ b/clang-tools-extra/clangd/Hover.h @@ -58,7 +58,7 @@ std::string Documentation; /// Source code containing the definition of the symbol. std::string Definition; - + const char *DefinitionLanguage = "cpp"; /// Access specifier for declarations inside class/struct/unions, empty for /// others. std::string AccessSpecifier; diff --git a/clang-tools-extra/clangd/Hover.cpp b/clang-tools-extra/clangd/Hover.cpp --- a/clang-tools-extra/clangd/Hover.cpp +++ b/clang-tools-extra/clangd/Hover.cpp @@ -920,6 +920,22 @@ if (TokensTouchingCursor.empty()) return llvm::None; + // Show full header file path if cursor is on include directive. + if (const auto MainFilePath = + getCanonicalPath(SM.getFileEntryForID(SM.getMainFileID()), SM)) { + for (const auto &Inc : AST.getIncludeStructure().MainFileIncludes) { + if (Inc.Resolved.empty() || Inc.HashLine != Pos.line) + continue; + HoverInfo HI; + HI.Name = std::string(llvm::sys::path::filename(Inc.Resolved)); + // FIXME: We don't have a fitting value for Kind. + HI.Definition = + URIForFile::canonicalize(Inc.Resolved, *MainFilePath).file().str(); + HI.DefinitionLanguage = ""; + return HI; + } + } + // To be used as a backup for highlighting the selected token, we use back as // it aligns better with biases elsewhere (editors tend to send the position // for the left of the hovered token). @@ -998,6 +1014,7 @@ markup::Document HoverInfo::present() const { markup::Document Output; + // Header contains a text of the form: // variable `var` // @@ -1098,7 +1115,8 @@ : Definition; // Note that we don't print anything for global namespace, to not annoy // non-c++ projects or projects that are not making use of namespaces. - Output.addCodeBlock(ScopeComment + DefinitionWithAccess); + Output.addCodeBlock(ScopeComment + DefinitionWithAccess, + DefinitionLanguage); } return Output; diff --git a/clang-tools-extra/clangd/unittests/HoverTests.cpp b/clang-tools-extra/clangd/unittests/HoverTests.cpp --- a/clang-tools-extra/clangd/unittests/HoverTests.cpp +++ b/clang-tools-extra/clangd/unittests/HoverTests.cpp @@ -2760,6 +2760,15 @@ // In test::Bar int foo = 3)", + }, + { + [](HoverInfo &HI) { + HI.Name = "stdio.h"; + HI.Definition = "/usr/include/stdio.h"; + }, + R"(stdio.h + +/usr/include/stdio.h)", }}; for (const auto &C : Cases) {