diff --git a/clang-tools-extra/clangd/Config.h b/clang-tools-extra/clangd/Config.h --- a/clang-tools-extra/clangd/Config.h +++ b/clang-tools-extra/clangd/Config.h @@ -116,6 +116,12 @@ /// scopes. bool AllScopes = true; } Completion; + + /// Configures hover feature. + struct { + /// Whether hover show a.k.a type. + bool AKAPrint = false; + } Hover; }; } // namespace clangd diff --git a/clang-tools-extra/clangd/ConfigCompile.cpp b/clang-tools-extra/clangd/ConfigCompile.cpp --- a/clang-tools-extra/clangd/ConfigCompile.cpp +++ b/clang-tools-extra/clangd/ConfigCompile.cpp @@ -196,6 +196,7 @@ compile(std::move(F.Index)); compile(std::move(F.Diagnostics)); compile(std::move(F.Completion)); + compile(std::move(F.Hover)); } void compile(Fragment::IfBlock &&F) { @@ -507,6 +508,14 @@ } } + void compile(Fragment::HoverBlock &&F) { + if (F.AKAPrint) { + Out.Apply.push_back([AKAPrint(**F.AKAPrint)](const Params &, Config &C) { + C.Hover.AKAPrint = AKAPrint; + }); + } + } + constexpr static llvm::SourceMgr::DiagKind Error = llvm::SourceMgr::DK_Error; constexpr static llvm::SourceMgr::DiagKind Warning = llvm::SourceMgr::DK_Warning; diff --git a/clang-tools-extra/clangd/ConfigFragment.h b/clang-tools-extra/clangd/ConfigFragment.h --- a/clang-tools-extra/clangd/ConfigFragment.h +++ b/clang-tools-extra/clangd/ConfigFragment.h @@ -266,6 +266,13 @@ llvm::Optional> AllScopes; }; CompletionBlock Completion; + + /// Describes hover preferences. + struct HoverBlock { + /// Whether hover show a.k.a type. + llvm::Optional> AKAPrint; + }; + HoverBlock Hover; }; } // namespace config diff --git a/clang-tools-extra/clangd/ConfigYAML.cpp b/clang-tools-extra/clangd/ConfigYAML.cpp --- a/clang-tools-extra/clangd/ConfigYAML.cpp +++ b/clang-tools-extra/clangd/ConfigYAML.cpp @@ -65,6 +65,7 @@ Dict.handle("Style", [&](Node &N) { parse(F.Style, N); }); Dict.handle("Diagnostics", [&](Node &N) { parse(F.Diagnostics, N); }); Dict.handle("Completion", [&](Node &N) { parse(F.Completion, N); }); + Dict.handle("Hover", [&](Node &N) { parse(F.Hover, N); }); Dict.parse(N); return !(N.failed() || HadError); } @@ -204,6 +205,19 @@ Dict.parse(N); } + void parse(Fragment::HoverBlock &F, Node &N) { + DictParser Dict("Hover", this); + Dict.handle("AKAPrint", [&](Node &N) { + if (auto Value = scalarValue(N, "AKAPrint")) { + if (auto AKAPrint = llvm::yaml::parseBool(**Value)) + F.AKAPrint = *AKAPrint; + else + warning("AKAPrint should be a boolean", N); + } + }); + Dict.parse(N); + } + // Helper for parsing mapping nodes (dictionaries). // We don't use YamlIO as we want to control over unknown keys. class DictParser { 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 @@ -10,6 +10,7 @@ #include "AST.h" #include "CodeCompletionStrings.h" +#include "Config.h" #include "FindTarget.h" #include "ParsedAST.h" #include "Selection.h" @@ -666,7 +667,9 @@ std::string Result; llvm::raw_string_ostream OS(Result); OS << PType.Type; - if (PType.AKA) + + const Config &Cfg = Config::current(); + if (PType.AKA && Cfg.Hover.AKAPrint) OS << " // aka: " << *PType.AKA; OS.flush(); return Result; @@ -1240,7 +1243,9 @@ llvm::raw_ostream &operator<<(llvm::raw_ostream &OS, const HoverInfo::PrintedType &T) { OS << T.Type; - if (T.AKA) + + const Config &Cfg = Config::current(); + if (T.AKA && Cfg.Hover.AKAPrint) OS << " (aka " << *T.AKA << ")"; return OS; } diff --git a/clang-tools-extra/clangd/unittests/ConfigYAMLTests.cpp b/clang-tools-extra/clangd/unittests/ConfigYAMLTests.cpp --- a/clang-tools-extra/clangd/unittests/ConfigYAMLTests.cpp +++ b/clang-tools-extra/clangd/unittests/ConfigYAMLTests.cpp @@ -215,6 +215,19 @@ ASSERT_EQ(Results.size(), 1u); EXPECT_THAT(Results[0].Completion.AllScopes, testing::Eq(llvm::None)); } + +TEST(ParseYAML, AKAPrint) { + CapturedDiags Diags; + Annotations YAML(R"yaml( +Hover: + AKAPrint: True + )yaml"); + auto Results = + Fragment::parseYAML(YAML.code(), "config.yaml", Diags.callback()); + ASSERT_THAT(Diags.Diagnostics, IsEmpty()); + ASSERT_EQ(Results.size(), 1u); + EXPECT_THAT(Results[0].Hover.AKAPrint, llvm::ValueIs(Val(true))); +} } // namespace } // namespace config } // namespace clangd 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 @@ -8,6 +8,7 @@ #include "AST.h" #include "Annotations.h" +#include "Config.h" #include "Hover.h" #include "TestIndex.h" #include "TestTU.h" @@ -2510,7 +2511,9 @@ // fixed one to make sure tests passes on different platform. TU.ExtraArgs.push_back("--target=x86_64-pc-linux-gnu"); auto AST = TU.build(); - + Config Cfg; + Cfg.Hover.AKAPrint = true; + WithContextValue WithCfg(Config::Key, std::move(Cfg)); auto H = getHover(AST, T.point(), format::getLLVMStyle(), Index.get()); ASSERT_TRUE(H); HoverInfo Expected; @@ -2873,6 +2876,9 @@ for (const auto &C : Cases) { HoverInfo HI; C.Builder(HI); + Config Cfg; + Cfg.Hover.AKAPrint = true; + WithContextValue WithCfg(Config::Key, std::move(Cfg)); EXPECT_EQ(HI.present().asPlainText(), C.ExpectedRender); } }