[clang-tools-extra] f9c8602 - clangd: Provide hover info for include directives
Sam McCall via cfe-commits
cfe-commits at lists.llvm.org
Thu Aug 12 15:14:36 PDT 2021
Author: Christian Kandeler
Date: 2021-08-13T00:07:23+02:00
New Revision: f9c8602b53fd277e1428cb6e8e63485b14520a82
URL: https://github.com/llvm/llvm-project/commit/f9c8602b53fd277e1428cb6e8e63485b14520a82
DIFF: https://github.com/llvm/llvm-project/commit/f9c8602b53fd277e1428cb6e8e63485b14520a82.diff
LOG: clangd: Provide hover info for include directives
It's quite useful to be able to hover over an #include and see the full
path to the header file.
Reviewed By: sammccall
Differential Revision: https://reviews.llvm.org/D107137
Added:
Modified:
clang-tools-extra/clangd/Hover.cpp
clang-tools-extra/clangd/Hover.h
clang-tools-extra/clangd/unittests/HoverTests.cpp
Removed:
################################################################################
diff --git a/clang-tools-extra/clangd/Hover.cpp b/clang-tools-extra/clangd/Hover.cpp
index d16a2ebcbfd7..763125cd10f5 100644
--- a/clang-tools-extra/clangd/Hover.cpp
+++ b/clang-tools-extra/clangd/Hover.cpp
@@ -920,6 +920,22 @@ llvm::Optional<HoverInfo> getHover(ParsedAST &AST, Position Pos,
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 @@ llvm::Optional<HoverInfo> getHover(ParsedAST &AST, Position Pos,
markup::Document HoverInfo::present() const {
markup::Document Output;
+
// Header contains a text of the form:
// variable `var`
//
@@ -1098,7 +1115,8 @@ markup::Document HoverInfo::present() const {
: 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/Hover.h b/clang-tools-extra/clangd/Hover.h
index 44ee9b7d7979..4e6314dfc5ed 100644
--- a/clang-tools-extra/clangd/Hover.h
+++ b/clang-tools-extra/clangd/Hover.h
@@ -58,7 +58,7 @@ struct HoverInfo {
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/unittests/HoverTests.cpp b/clang-tools-extra/clangd/unittests/HoverTests.cpp
index 54a27b748351..457cc55dd19a 100644
--- a/clang-tools-extra/clangd/unittests/HoverTests.cpp
+++ b/clang-tools-extra/clangd/unittests/HoverTests.cpp
@@ -2760,6 +2760,15 @@ Passed by const reference as arg_a (converted to int)
// 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) {
More information about the cfe-commits
mailing list