[clangd] Represent Hover result using FormattedString

Reviewers: sammccall, kadircet

Reviewed By: kadircet

Subscribers: MaskRay, jkorous, arphaman, kadircet, cfe-commits

Tags: #clang

Differential Revision: https://reviews.llvm.org/D61601

llvm-svn: 361940
This commit is contained in:
Ilya Biryukov 2019-05-29 10:01:00 +00:00
parent d2042d3dd7
commit f9169d0896
11 changed files with 319 additions and 95 deletions

View File

@ -8,6 +8,7 @@
#include "ClangdLSPServer.h" #include "ClangdLSPServer.h"
#include "Diagnostics.h" #include "Diagnostics.h"
#include "FormattedString.h"
#include "Protocol.h" #include "Protocol.h"
#include "SourceCode.h" #include "SourceCode.h"
#include "Trace.h" #include "Trace.h"
@ -358,6 +359,7 @@ void ClangdLSPServer::onInitialize(const InitializeParams &Params,
SupportsHierarchicalDocumentSymbol = SupportsHierarchicalDocumentSymbol =
Params.capabilities.HierarchicalDocumentSymbol; Params.capabilities.HierarchicalDocumentSymbol;
SupportFileStatus = Params.initializationOptions.FileStatus; SupportFileStatus = Params.initializationOptions.FileStatus;
HoverContentFormat = Params.capabilities.HoverContentFormat;
llvm::json::Object Result{ llvm::json::Object Result{
{{"capabilities", {{"capabilities",
llvm::json::Object{ llvm::json::Object{
@ -843,17 +845,27 @@ void ClangdLSPServer::onHover(const TextDocumentPositionParams &Params,
Callback<llvm::Optional<Hover>> Reply) { Callback<llvm::Optional<Hover>> Reply) {
Server->findHover(Params.textDocument.uri.file(), Params.position, Server->findHover(Params.textDocument.uri.file(), Params.position,
Bind( Bind(
[](decltype(Reply) Reply, [this](decltype(Reply) Reply,
llvm::Expected<llvm::Optional<HoverInfo>> HIorErr) { llvm::Expected<llvm::Optional<HoverInfo>> H) {
if (!HIorErr) if (!H)
return Reply(HIorErr.takeError()); return Reply(H.takeError());
const auto &HI = HIorErr.get(); if (!*H)
if (!HI)
return Reply(llvm::None); return Reply(llvm::None);
Hover H;
H.range = HI->SymRange; Hover R;
H.contents = HI->render(); R.contents.kind = HoverContentFormat;
return Reply(H); R.range = (*H)->SymRange;
switch (HoverContentFormat) {
case MarkupKind::PlainText:
R.contents.value =
(*H)->present().renderAsPlainText();
return Reply(std::move(R));
case MarkupKind::Markdown:
R.contents.value =
(*H)->present().renderAsMarkdown();
return Reply(std::move(R));
};
llvm_unreachable("unhandled MarkupKind");
}, },
std::move(Reply))); std::move(Reply)));
} }

View File

@ -154,7 +154,10 @@ private:
bool SupportsHierarchicalDocumentSymbol = false; bool SupportsHierarchicalDocumentSymbol = false;
/// Whether the client supports showing file status. /// Whether the client supports showing file status.
bool SupportFileStatus = false; bool SupportFileStatus = false;
// Store of the current versions of the open documents. /// Which kind of markup should we use in textDocument/hover responses.
MarkupKind HoverContentFormat = MarkupKind::PlainText;
/// Store of the current versions of the open documents.
DraftStore DraftMgr; DraftStore DraftMgr;
// The CDB is created by the "initialize" LSP method. // The CDB is created by the "initialize" LSP method.

View File

@ -10,11 +10,13 @@
#include "ClangdUnit.h" #include "ClangdUnit.h"
#include "CodeComplete.h" #include "CodeComplete.h"
#include "FindSymbols.h" #include "FindSymbols.h"
#include "FormattedString.h"
#include "Headers.h" #include "Headers.h"
#include "Protocol.h" #include "Protocol.h"
#include "SourceCode.h" #include "SourceCode.h"
#include "TUScheduler.h" #include "TUScheduler.h"
#include "Trace.h" #include "Trace.h"
#include "XRefs.h"
#include "index/CanonicalIncludes.h" #include "index/CanonicalIncludes.h"
#include "index/FileIndex.h" #include "index/FileIndex.h"
#include "index/Merge.h" #include "index/Merge.h"
@ -462,7 +464,7 @@ void ClangdServer::findDocumentHighlights(
void ClangdServer::findHover(PathRef File, Position Pos, void ClangdServer::findHover(PathRef File, Position Pos,
Callback<llvm::Optional<HoverInfo>> CB) { Callback<llvm::Optional<HoverInfo>> CB) {
auto Action = [Pos](Callback<llvm::Optional<HoverInfo>> CB, Path File, auto Action = [Pos](decltype(CB) CB, Path File,
llvm::Expected<InputsAndAST> InpAST) { llvm::Expected<InputsAndAST> InpAST) {
if (!InpAST) if (!InpAST)
return CB(InpAST.takeError()); return CB(InpAST.takeError());

View File

@ -14,6 +14,7 @@
#include "ClangdUnit.h" #include "ClangdUnit.h"
#include "CodeComplete.h" #include "CodeComplete.h"
#include "FSProvider.h" #include "FSProvider.h"
#include "FormattedString.h"
#include "Function.h" #include "Function.h"
#include "GlobalCompilationDatabase.h" #include "GlobalCompilationDatabase.h"
#include "Protocol.h" #include "Protocol.h"

View File

@ -9,6 +9,7 @@
#include "clang/Basic/CharInfo.h" #include "clang/Basic/CharInfo.h"
#include "llvm/ADT/StringRef.h" #include "llvm/ADT/StringRef.h"
#include "llvm/Support/ErrorHandling.h" #include "llvm/Support/ErrorHandling.h"
#include "llvm/Support/FormatVariadic.h"
#include <cstddef> #include <cstddef>
#include <string> #include <string>
@ -169,5 +170,27 @@ std::string FormattedString::renderAsPlainText() const {
R.pop_back(); R.pop_back();
return R; return R;
} }
std::string FormattedString::renderForTests() const {
std::string R;
for (const auto &C : Chunks) {
switch (C.Kind) {
case ChunkKind::PlainText:
R += "text[" + C.Contents + "]";
break;
case ChunkKind::InlineCodeBlock:
R += "code[" + C.Contents + "]";
break;
case ChunkKind::CodeBlock:
if (!R.empty())
R += "\n";
R += llvm::formatv("codeblock({0}) [\n{1}\n]\n", C.Language, C.Contents);
break;
}
}
while (!R.empty() && isWhitespace(R.back()))
R.pop_back();
return R;
}
} // namespace clangd } // namespace clangd
} // namespace clang } // namespace clang

View File

@ -35,6 +35,7 @@ public:
std::string renderAsMarkdown() const; std::string renderAsMarkdown() const;
std::string renderAsPlainText() const; std::string renderAsPlainText() const;
std::string renderForTests() const;
private: private:
enum class ChunkKind { enum class ChunkKind {

View File

@ -303,6 +303,17 @@ bool fromJSON(const llvm::json::Value &Params, ClientCapabilities &R) {
DocumentSymbol->getBoolean("hierarchicalDocumentSymbolSupport")) DocumentSymbol->getBoolean("hierarchicalDocumentSymbolSupport"))
R.HierarchicalDocumentSymbol = *HierarchicalSupport; R.HierarchicalDocumentSymbol = *HierarchicalSupport;
} }
if (auto *Hover = TextDocument->getObject("hover")) {
if (auto *ContentFormat = Hover->getArray("contentFormat")) {
for (const auto &Format : *ContentFormat) {
MarkupKind K = MarkupKind::PlainText;
if (fromJSON(Format, K)) {
R.HoverContentFormat = K;
break;
}
}
}
}
} }
if (auto *Workspace = O->getObject("workspace")) { if (auto *Workspace = O->getObject("workspace")) {
if (auto *Symbol = Workspace->getObject("symbol")) { if (auto *Symbol = Workspace->getObject("symbol")) {
@ -684,6 +695,27 @@ static llvm::StringRef toTextKind(MarkupKind Kind) {
llvm_unreachable("Invalid MarkupKind"); llvm_unreachable("Invalid MarkupKind");
} }
bool fromJSON(const llvm::json::Value &V, MarkupKind &K) {
auto Str = V.getAsString();
if (!Str) {
elog("Failed to parse markup kind: expected a string");
return false;
}
if (*Str == "plaintext")
K = MarkupKind::PlainText;
else if (*Str == "markdown")
K = MarkupKind::Markdown;
else {
elog("Unknown markup kind: {0}", *Str);
return false;
}
return true;
}
llvm::raw_ostream &operator<<(llvm::raw_ostream &OS, MarkupKind K) {
return OS << toTextKind(K);
}
llvm::json::Value toJSON(const MarkupContent &MC) { llvm::json::Value toJSON(const MarkupContent &MC) {
if (MC.value.empty()) if (MC.value.empty())
return nullptr; return nullptr;

View File

@ -353,6 +353,15 @@ llvm::json::Value toJSON(const OffsetEncoding &);
bool fromJSON(const llvm::json::Value &, OffsetEncoding &); bool fromJSON(const llvm::json::Value &, OffsetEncoding &);
llvm::raw_ostream &operator<<(llvm::raw_ostream &, OffsetEncoding); llvm::raw_ostream &operator<<(llvm::raw_ostream &, OffsetEncoding);
// Describes the content type that a client supports in various result literals
// like `Hover`, `ParameterInfo` or `CompletionItem`.
enum class MarkupKind {
PlainText,
Markdown,
};
bool fromJSON(const llvm::json::Value &, MarkupKind &);
llvm::raw_ostream &operator<<(llvm::raw_ostream &OS, MarkupKind);
// This struct doesn't mirror LSP! // This struct doesn't mirror LSP!
// The protocol defines deeply nested structures for client capabilities. // The protocol defines deeply nested structures for client capabilities.
// Instead of mapping them all, this just parses out the bits we care about. // Instead of mapping them all, this just parses out the bits we care about.
@ -391,6 +400,9 @@ struct ClientCapabilities {
/// Supported encodings for LSP character offsets. (clangd extension). /// Supported encodings for LSP character offsets. (clangd extension).
llvm::Optional<std::vector<OffsetEncoding>> offsetEncoding; llvm::Optional<std::vector<OffsetEncoding>> offsetEncoding;
/// The content format that should be used for Hover requests.
MarkupKind HoverContentFormat = MarkupKind::PlainText;
}; };
bool fromJSON(const llvm::json::Value &, ClientCapabilities &); bool fromJSON(const llvm::json::Value &, ClientCapabilities &);
@ -861,11 +873,6 @@ struct CompletionParams : TextDocumentPositionParams {
}; };
bool fromJSON(const llvm::json::Value &, CompletionParams &); bool fromJSON(const llvm::json::Value &, CompletionParams &);
enum class MarkupKind {
PlainText,
Markdown,
};
struct MarkupContent { struct MarkupContent {
MarkupKind kind = MarkupKind::PlainText; MarkupKind kind = MarkupKind::PlainText;
std::string value; std::string value;

View File

@ -9,6 +9,7 @@
#include "AST.h" #include "AST.h"
#include "CodeCompletionStrings.h" #include "CodeCompletionStrings.h"
#include "FindSymbols.h" #include "FindSymbols.h"
#include "FormattedString.h"
#include "Logger.h" #include "Logger.h"
#include "Protocol.h" #include "Protocol.h"
#include "SourceCode.h" #include "SourceCode.h"
@ -1155,32 +1156,26 @@ getTypeHierarchy(ParsedAST &AST, Position Pos, int ResolveLevels,
return Result; return Result;
} }
MarkupContent HoverInfo::render() const { FormattedString HoverInfo::present() const {
MarkupContent Content; FormattedString Output;
Content.kind = MarkupKind::PlainText;
std::vector<std::string> Output;
if (NamespaceScope) { if (NamespaceScope) {
llvm::raw_string_ostream Out(Content.value); Output.appendText("Declared in");
Out << "Declared in ";
// Drop trailing "::". // Drop trailing "::".
if (!LocalScope.empty()) if (!LocalScope.empty())
Out << *NamespaceScope << llvm::StringRef(LocalScope).drop_back(2); Output.appendInlineCode(llvm::StringRef(LocalScope).drop_back(2));
else if (NamespaceScope->empty()) else if (NamespaceScope->empty())
Out << "global namespace"; Output.appendInlineCode("global namespace");
else else
Out << llvm::StringRef(*NamespaceScope).drop_back(2); Output.appendInlineCode(llvm::StringRef(*NamespaceScope).drop_back(2));
Out << "\n\n";
} }
if (!Definition.empty()) { if (!Definition.empty()) {
Output.push_back(Definition); Output.appendCodeBlock(Definition);
} else { } else {
// Builtin types // Builtin types
Output.push_back(Name); Output.appendCodeBlock(Name);
} }
Content.value += llvm::join(Output, " "); return Output;
return Content;
} }
llvm::raw_ostream &operator<<(llvm::raw_ostream &OS, llvm::raw_ostream &operator<<(llvm::raw_ostream &OS,

View File

@ -14,6 +14,7 @@
#define LLVM_CLANG_TOOLS_EXTRA_CLANGD_XREFS_H #define LLVM_CLANG_TOOLS_EXTRA_CLANGD_XREFS_H
#include "ClangdUnit.h" #include "ClangdUnit.h"
#include "FormattedString.h"
#include "Protocol.h" #include "Protocol.h"
#include "index/Index.h" #include "index/Index.h"
#include "index/SymbolLocation.h" #include "index/SymbolLocation.h"
@ -103,8 +104,8 @@ struct HoverInfo {
/// Set for all templates(function, class, variable). /// Set for all templates(function, class, variable).
llvm::Optional<std::vector<Param>> TemplateParameters; llvm::Optional<std::vector<Param>> TemplateParameters;
/// Lower to LSP struct. /// Produce a user-readable information.
MarkupContent render() const; FormattedString present() const;
}; };
llvm::raw_ostream &operator<<(llvm::raw_ostream &, const HoverInfo::Param &); llvm::raw_ostream &operator<<(llvm::raw_ostream &, const HoverInfo::Param &);
inline bool operator==(const HoverInfo::Param &LHS, inline bool operator==(const HoverInfo::Param &LHS,

View File

@ -893,7 +893,10 @@ TEST(Hover, All) {
int test1 = bonjour; int test1 = bonjour;
} }
)cpp", )cpp",
"Declared in main\n\nint bonjour", "text[Declared in]code[main]\n"
"codeblock(cpp) [\n"
"int bonjour\n"
"]",
}, },
{ {
R"cpp(// Local variable in method R"cpp(// Local variable in method
@ -904,7 +907,10 @@ TEST(Hover, All) {
} }
}; };
)cpp", )cpp",
"Declared in s::method\n\nint bonjour", "text[Declared in]code[s::method]\n"
"codeblock(cpp) [\n"
"int bonjour\n"
"]",
}, },
{ {
R"cpp(// Struct R"cpp(// Struct
@ -915,7 +921,10 @@ TEST(Hover, All) {
ns1::My^Class* Params; ns1::My^Class* Params;
} }
)cpp", )cpp",
"Declared in ns1\n\nstruct MyClass {}", "text[Declared in]code[ns1]\n"
"codeblock(cpp) [\n"
"struct MyClass {}\n"
"]",
}, },
{ {
R"cpp(// Class R"cpp(// Class
@ -926,7 +935,10 @@ TEST(Hover, All) {
ns1::My^Class* Params; ns1::My^Class* Params;
} }
)cpp", )cpp",
"Declared in ns1\n\nclass MyClass {}", "text[Declared in]code[ns1]\n"
"codeblock(cpp) [\n"
"class MyClass {}\n"
"]",
}, },
{ {
R"cpp(// Union R"cpp(// Union
@ -937,7 +949,10 @@ TEST(Hover, All) {
ns1::My^Union Params; ns1::My^Union Params;
} }
)cpp", )cpp",
"Declared in ns1\n\nunion MyUnion {}", "text[Declared in]code[ns1]\n"
"codeblock(cpp) [\n"
"union MyUnion {}\n"
"]",
}, },
{ {
R"cpp(// Function definition via pointer R"cpp(// Function definition via pointer
@ -946,7 +961,10 @@ TEST(Hover, All) {
auto *X = &^foo; auto *X = &^foo;
} }
)cpp", )cpp",
"Declared in global namespace\n\nint foo(int)", "text[Declared in]code[global namespace]\n"
"codeblock(cpp) [\n"
"int foo(int)\n"
"]",
}, },
{ {
R"cpp(// Function declaration via call R"cpp(// Function declaration via call
@ -955,7 +973,10 @@ TEST(Hover, All) {
return ^foo(42); return ^foo(42);
} }
)cpp", )cpp",
"Declared in global namespace\n\nint foo(int)", "text[Declared in]code[global namespace]\n"
"codeblock(cpp) [\n"
"int foo(int)\n"
"]",
}, },
{ {
R"cpp(// Field R"cpp(// Field
@ -965,7 +986,10 @@ TEST(Hover, All) {
bar.^x; bar.^x;
} }
)cpp", )cpp",
"Declared in Foo\n\nint x", "text[Declared in]code[Foo]\n"
"codeblock(cpp) [\n"
"int x\n"
"]",
}, },
{ {
R"cpp(// Field with initialization R"cpp(// Field with initialization
@ -975,7 +999,10 @@ TEST(Hover, All) {
bar.^x; bar.^x;
} }
)cpp", )cpp",
"Declared in Foo\n\nint x = 5", "text[Declared in]code[Foo]\n"
"codeblock(cpp) [\n"
"int x = 5\n"
"]",
}, },
{ {
R"cpp(// Static field R"cpp(// Static field
@ -984,7 +1011,10 @@ TEST(Hover, All) {
Foo::^x; Foo::^x;
} }
)cpp", )cpp",
"Declared in Foo\n\nstatic int x", "text[Declared in]code[Foo]\n"
"codeblock(cpp) [\n"
"static int x\n"
"]",
}, },
{ {
R"cpp(// Field, member initializer R"cpp(// Field, member initializer
@ -993,7 +1023,10 @@ TEST(Hover, All) {
Foo() : ^x(0) {} Foo() : ^x(0) {}
}; };
)cpp", )cpp",
"Declared in Foo\n\nint x", "text[Declared in]code[Foo]\n"
"codeblock(cpp) [\n"
"int x\n"
"]",
}, },
{ {
R"cpp(// Field, GNU old-style field designator R"cpp(// Field, GNU old-style field designator
@ -1002,7 +1035,10 @@ TEST(Hover, All) {
Foo bar = { ^x : 1 }; Foo bar = { ^x : 1 };
} }
)cpp", )cpp",
"Declared in Foo\n\nint x", "text[Declared in]code[Foo]\n"
"codeblock(cpp) [\n"
"int x\n"
"]",
}, },
{ {
R"cpp(// Field, field designator R"cpp(// Field, field designator
@ -1011,7 +1047,10 @@ TEST(Hover, All) {
Foo bar = { .^x = 2 }; Foo bar = { .^x = 2 };
} }
)cpp", )cpp",
"Declared in Foo\n\nint x", "text[Declared in]code[Foo]\n"
"codeblock(cpp) [\n"
"int x\n"
"]",
}, },
{ {
R"cpp(// Method call R"cpp(// Method call
@ -1021,7 +1060,10 @@ TEST(Hover, All) {
bar.^x(); bar.^x();
} }
)cpp", )cpp",
"Declared in Foo\n\nint x()", "text[Declared in]code[Foo]\n"
"codeblock(cpp) [\n"
"int x()\n"
"]",
}, },
{ {
R"cpp(// Static method call R"cpp(// Static method call
@ -1030,7 +1072,10 @@ TEST(Hover, All) {
Foo::^x(); Foo::^x();
} }
)cpp", )cpp",
"Declared in Foo\n\nstatic int x()", "text[Declared in]code[Foo]\n"
"codeblock(cpp) [\n"
"static int x()\n"
"]",
}, },
{ {
R"cpp(// Typedef R"cpp(// Typedef
@ -1039,7 +1084,10 @@ TEST(Hover, All) {
^Foo bar; ^Foo bar;
} }
)cpp", )cpp",
"Declared in global namespace\n\ntypedef int Foo", "text[Declared in]code[global namespace]\n"
"codeblock(cpp) [\n"
"typedef int Foo\n"
"]",
}, },
{ {
R"cpp(// Namespace R"cpp(// Namespace
@ -1048,7 +1096,10 @@ TEST(Hover, All) {
} // namespace ns } // namespace ns
int main() { ^ns::Foo::bar(); } int main() { ^ns::Foo::bar(); }
)cpp", )cpp",
"Declared in global namespace\n\nnamespace ns {}", "text[Declared in]code[global namespace]\n"
"codeblock(cpp) [\n"
"namespace ns {}\n"
"]",
}, },
{ {
R"cpp(// Anonymous namespace R"cpp(// Anonymous namespace
@ -1059,7 +1110,10 @@ TEST(Hover, All) {
} // namespace ns } // namespace ns
int main() { ns::f^oo++; } int main() { ns::f^oo++; }
)cpp", )cpp",
"Declared in ns::(anonymous)\n\nint foo", "text[Declared in]code[ns::(anonymous)]\n"
"codeblock(cpp) [\n"
"int foo\n"
"]",
}, },
{ {
R"cpp(// Macro R"cpp(// Macro
@ -1069,14 +1123,18 @@ TEST(Hover, All) {
#define MACRO 2 #define MACRO 2
#undef macro #undef macro
)cpp", )cpp",
"#define MACRO 1", "codeblock(cpp) [\n"
"#define MACRO 1\n"
"]",
}, },
{ {
R"cpp(// Macro R"cpp(// Macro
#define MACRO 0 #define MACRO 0
#define MACRO2 ^MACRO #define MACRO2 ^MACRO
)cpp", )cpp",
"#define MACRO 0", "codeblock(cpp) [\n"
"#define MACRO 0\n"
"]",
}, },
{ {
R"cpp(// Macro R"cpp(// Macro
@ -1085,8 +1143,10 @@ TEST(Hover, All) {
} }
int main() ^MACRO int main() ^MACRO
)cpp", )cpp",
"#define MACRO " R"cpp(codeblock(cpp) [
" \\\n { return 0; }", #define MACRO \
{ return 0; }
])cpp",
}, },
{ {
R"cpp(// Forward class declaration R"cpp(// Forward class declaration
@ -1094,7 +1154,10 @@ TEST(Hover, All) {
class Foo {}; class Foo {};
F^oo* foo(); F^oo* foo();
)cpp", )cpp",
"Declared in global namespace\n\nclass Foo {}", "text[Declared in]code[global namespace]\n"
"codeblock(cpp) [\n"
"class Foo {}\n"
"]",
}, },
{ {
R"cpp(// Function declaration R"cpp(// Function declaration
@ -1102,7 +1165,10 @@ TEST(Hover, All) {
void g() { f^oo(); } void g() { f^oo(); }
void foo() {} void foo() {}
)cpp", )cpp",
"Declared in global namespace\n\nvoid foo()", "text[Declared in]code[global namespace]\n"
"codeblock(cpp) [\n"
"void foo()\n"
"]",
}, },
{ {
R"cpp(// Enum declaration R"cpp(// Enum declaration
@ -1113,7 +1179,10 @@ TEST(Hover, All) {
Hel^lo hello = ONE; Hel^lo hello = ONE;
} }
)cpp", )cpp",
"Declared in global namespace\n\nenum Hello {}", "text[Declared in]code[global namespace]\n"
"codeblock(cpp) [\n"
"enum Hello {}\n"
"]",
}, },
{ {
R"cpp(// Enumerator R"cpp(// Enumerator
@ -1124,7 +1193,10 @@ TEST(Hover, All) {
Hello hello = O^NE; Hello hello = O^NE;
} }
)cpp", )cpp",
"Declared in Hello\n\nONE", "text[Declared in]code[Hello]\n"
"codeblock(cpp) [\n"
"ONE\n"
"]",
}, },
{ {
R"cpp(// Enumerator in anonymous enum R"cpp(// Enumerator in anonymous enum
@ -1135,7 +1207,10 @@ TEST(Hover, All) {
int hello = O^NE; int hello = O^NE;
} }
)cpp", )cpp",
"Declared in global namespace\n\nONE", "text[Declared in]code[global namespace]\n"
"codeblock(cpp) [\n"
"ONE\n"
"]",
}, },
{ {
R"cpp(// Global variable R"cpp(// Global variable
@ -1144,7 +1219,10 @@ TEST(Hover, All) {
he^y++; he^y++;
} }
)cpp", )cpp",
"Declared in global namespace\n\nstatic int hey = 10", "text[Declared in]code[global namespace]\n"
"codeblock(cpp) [\n"
"static int hey = 10\n"
"]",
}, },
{ {
R"cpp(// Global variable in namespace R"cpp(// Global variable in namespace
@ -1155,7 +1233,10 @@ TEST(Hover, All) {
ns1::he^y++; ns1::he^y++;
} }
)cpp", )cpp",
"Declared in ns1\n\nstatic int hey = 10", "text[Declared in]code[ns1]\n"
"codeblock(cpp) [\n"
"static int hey = 10\n"
"]",
}, },
{ {
R"cpp(// Field in anonymous struct R"cpp(// Field in anonymous struct
@ -1166,7 +1247,10 @@ TEST(Hover, All) {
s.he^llo++; s.he^llo++;
} }
)cpp", )cpp",
"Declared in (anonymous struct)\n\nint hello", "text[Declared in]code[(anonymous struct)]\n"
"codeblock(cpp) [\n"
"int hello\n"
"]",
}, },
{ {
R"cpp(// Templated function R"cpp(// Templated function
@ -1176,7 +1260,10 @@ TEST(Hover, All) {
} }
void g() { auto x = f^oo<int>(); } void g() { auto x = f^oo<int>(); }
)cpp", )cpp",
"Declared in global namespace\n\ntemplate <typename T> T foo()", "text[Declared in]code[global namespace]\n"
"codeblock(cpp) [\n"
"template <typename T> T foo()\n"
"]",
}, },
{ {
R"cpp(// Anonymous union R"cpp(// Anonymous union
@ -1187,7 +1274,10 @@ TEST(Hover, All) {
}; };
void g() { struct outer o; o.v.d^ef++; } void g() { struct outer o; o.v.d^ef++; }
)cpp", )cpp",
"Declared in outer::(anonymous union)\n\nint def", "text[Declared in]code[outer::(anonymous union)]\n"
"codeblock(cpp) [\n"
"int def\n"
"]",
}, },
{ {
R"cpp(// Nothing R"cpp(// Nothing
@ -1203,7 +1293,9 @@ TEST(Hover, All) {
^auto i = 1; ^auto i = 1;
} }
)cpp", )cpp",
"int", "codeblock(cpp) [\n"
"int\n"
"]",
}, },
{ {
R"cpp(// Simple initialization with const auto R"cpp(// Simple initialization with const auto
@ -1211,7 +1303,9 @@ TEST(Hover, All) {
const ^auto i = 1; const ^auto i = 1;
} }
)cpp", )cpp",
"int", "codeblock(cpp) [\n"
"int\n"
"]",
}, },
{ {
R"cpp(// Simple initialization with const auto& R"cpp(// Simple initialization with const auto&
@ -1219,7 +1313,9 @@ TEST(Hover, All) {
const ^auto& i = 1; const ^auto& i = 1;
} }
)cpp", )cpp",
"int", "codeblock(cpp) [\n"
"int\n"
"]",
}, },
{ {
R"cpp(// Simple initialization with auto& R"cpp(// Simple initialization with auto&
@ -1227,7 +1323,9 @@ TEST(Hover, All) {
^auto& i = 1; ^auto& i = 1;
} }
)cpp", )cpp",
"int", "codeblock(cpp) [\n"
"int\n"
"]",
}, },
{ {
R"cpp(// Simple initialization with auto* R"cpp(// Simple initialization with auto*
@ -1236,7 +1334,9 @@ TEST(Hover, All) {
^auto* i = &a; ^auto* i = &a;
} }
)cpp", )cpp",
"int", "codeblock(cpp) [\n"
"int\n"
"]",
}, },
{ {
R"cpp(// Auto with initializer list. R"cpp(// Auto with initializer list.
@ -1249,7 +1349,9 @@ TEST(Hover, All) {
^auto i = {1,2}; ^auto i = {1,2};
} }
)cpp", )cpp",
"class std::initializer_list<int>", "codeblock(cpp) [\n"
"class std::initializer_list<int>\n"
"]",
}, },
{ {
R"cpp(// User defined conversion to auto R"cpp(// User defined conversion to auto
@ -1257,7 +1359,9 @@ TEST(Hover, All) {
operator ^auto() const { return 10; } operator ^auto() const { return 10; }
}; };
)cpp", )cpp",
"int", "codeblock(cpp) [\n"
"int\n"
"]",
}, },
{ {
R"cpp(// Simple initialization with decltype(auto) R"cpp(// Simple initialization with decltype(auto)
@ -1265,7 +1369,9 @@ TEST(Hover, All) {
^decltype(auto) i = 1; ^decltype(auto) i = 1;
} }
)cpp", )cpp",
"int", "codeblock(cpp) [\n"
"int\n"
"]",
}, },
{ {
R"cpp(// Simple initialization with const decltype(auto) R"cpp(// Simple initialization with const decltype(auto)
@ -1274,7 +1380,9 @@ TEST(Hover, All) {
^decltype(auto) i = j; ^decltype(auto) i = j;
} }
)cpp", )cpp",
"const int", "codeblock(cpp) [\n"
"const int\n"
"]",
}, },
{ {
R"cpp(// Simple initialization with const& decltype(auto) R"cpp(// Simple initialization with const& decltype(auto)
@ -1284,7 +1392,9 @@ TEST(Hover, All) {
^decltype(auto) i = j; ^decltype(auto) i = j;
} }
)cpp", )cpp",
"const int &", "codeblock(cpp) [\n"
"const int &\n"
"]",
}, },
{ {
R"cpp(// Simple initialization with & decltype(auto) R"cpp(// Simple initialization with & decltype(auto)
@ -1294,7 +1404,9 @@ TEST(Hover, All) {
^decltype(auto) i = j; ^decltype(auto) i = j;
} }
)cpp", )cpp",
"int &", "codeblock(cpp) [\n"
"int &\n"
"]",
}, },
{ {
R"cpp(// decltype with initializer list: nothing R"cpp(// decltype with initializer list: nothing
@ -1315,7 +1427,9 @@ TEST(Hover, All) {
return 0; return 0;
} }
)cpp", )cpp",
"int", "codeblock(cpp) [\n"
"int\n"
"]",
}, },
{ {
R"cpp(// auto function return with trailing type R"cpp(// auto function return with trailing type
@ -1324,7 +1438,9 @@ TEST(Hover, All) {
return Bar(); return Bar();
} }
)cpp", )cpp",
"struct Bar", "codeblock(cpp) [\n"
"struct Bar\n"
"]",
}, },
{ {
R"cpp(// trailing return type R"cpp(// trailing return type
@ -1333,7 +1449,9 @@ TEST(Hover, All) {
return Bar(); return Bar();
} }
)cpp", )cpp",
"struct Bar", "codeblock(cpp) [\n"
"struct Bar\n"
"]",
}, },
{ {
R"cpp(// auto in function return R"cpp(// auto in function return
@ -1342,7 +1460,9 @@ TEST(Hover, All) {
return Bar(); return Bar();
} }
)cpp", )cpp",
"struct Bar", "codeblock(cpp) [\n"
"struct Bar\n"
"]",
}, },
{ {
R"cpp(// auto& in function return R"cpp(// auto& in function return
@ -1351,7 +1471,9 @@ TEST(Hover, All) {
return Bar(); return Bar();
} }
)cpp", )cpp",
"struct Bar", "codeblock(cpp) [\n"
"struct Bar\n"
"]",
}, },
{ {
R"cpp(// auto* in function return R"cpp(// auto* in function return
@ -1361,7 +1483,9 @@ TEST(Hover, All) {
return bar; return bar;
} }
)cpp", )cpp",
"struct Bar", "codeblock(cpp) [\n"
"struct Bar\n"
"]",
}, },
{ {
R"cpp(// const auto& in function return R"cpp(// const auto& in function return
@ -1370,7 +1494,9 @@ TEST(Hover, All) {
return Bar(); return Bar();
} }
)cpp", )cpp",
"struct Bar", "codeblock(cpp) [\n"
"struct Bar\n"
"]",
}, },
{ {
R"cpp(// decltype(auto) in function return R"cpp(// decltype(auto) in function return
@ -1379,7 +1505,9 @@ TEST(Hover, All) {
return Bar(); return Bar();
} }
)cpp", )cpp",
"struct Bar", "codeblock(cpp) [\n"
"struct Bar\n"
"]",
}, },
{ {
R"cpp(// decltype(auto) reference in function return R"cpp(// decltype(auto) reference in function return
@ -1389,7 +1517,9 @@ TEST(Hover, All) {
return (a); return (a);
} }
)cpp", )cpp",
"int &", "codeblock(cpp) [\n"
"int &\n"
"]",
}, },
{ {
R"cpp(// decltype lvalue reference R"cpp(// decltype lvalue reference
@ -1398,7 +1528,9 @@ TEST(Hover, All) {
^decltype(I) J = I; ^decltype(I) J = I;
} }
)cpp", )cpp",
"int", "codeblock(cpp) [\n"
"int\n"
"]",
}, },
{ {
R"cpp(// decltype lvalue reference R"cpp(// decltype lvalue reference
@ -1408,7 +1540,9 @@ TEST(Hover, All) {
^decltype(K) J = I; ^decltype(K) J = I;
} }
)cpp", )cpp",
"int &", "codeblock(cpp) [\n"
"int &\n"
"]",
}, },
{ {
R"cpp(// decltype lvalue reference parenthesis R"cpp(// decltype lvalue reference parenthesis
@ -1417,7 +1551,9 @@ TEST(Hover, All) {
^decltype((I)) J = I; ^decltype((I)) J = I;
} }
)cpp", )cpp",
"int &", "codeblock(cpp) [\n"
"int &\n"
"]",
}, },
{ {
R"cpp(// decltype rvalue reference R"cpp(// decltype rvalue reference
@ -1426,7 +1562,9 @@ TEST(Hover, All) {
^decltype(static_cast<int&&>(I)) J = static_cast<int&&>(I); ^decltype(static_cast<int&&>(I)) J = static_cast<int&&>(I);
} }
)cpp", )cpp",
"int &&", "codeblock(cpp) [\n"
"int &&\n"
"]",
}, },
{ {
R"cpp(// decltype rvalue reference function call R"cpp(// decltype rvalue reference function call
@ -1436,7 +1574,9 @@ TEST(Hover, All) {
^decltype(bar()) J = bar(); ^decltype(bar()) J = bar();
} }
)cpp", )cpp",
"int &&", "codeblock(cpp) [\n"
"int &&\n"
"]",
}, },
{ {
R"cpp(// decltype of function with trailing return type. R"cpp(// decltype of function with trailing return type.
@ -1448,7 +1588,9 @@ TEST(Hover, All) {
^decltype(test()) i = test(); ^decltype(test()) i = test();
} }
)cpp", )cpp",
"struct Bar", "codeblock(cpp) [\n"
"struct Bar\n"
"]",
}, },
{ {
R"cpp(// decltype of var with decltype. R"cpp(// decltype of var with decltype.
@ -1458,7 +1600,9 @@ TEST(Hover, All) {
^decltype(J) K = J; ^decltype(J) K = J;
} }
)cpp", )cpp",
"int", "codeblock(cpp) [\n"
"int\n"
"]",
}, },
{ {
R"cpp(// structured binding. Not supported yet R"cpp(// structured binding. Not supported yet
@ -1486,7 +1630,9 @@ TEST(Hover, All) {
int bar(); int bar();
^auto (*foo)() = bar; ^auto (*foo)() = bar;
)cpp", )cpp",
"int", "codeblock(cpp) [\n"
"int\n"
"]",
}, },
}; };
@ -1497,7 +1643,8 @@ TEST(Hover, All) {
auto AST = TU.build(); auto AST = TU.build();
if (auto H = getHover(AST, T.point(), format::getLLVMStyle())) { if (auto H = getHover(AST, T.point(), format::getLLVMStyle())) {
EXPECT_NE("", Test.ExpectedHover) << Test.Input; EXPECT_NE("", Test.ExpectedHover) << Test.Input;
EXPECT_EQ(H->render().value, Test.ExpectedHover.str()) << Test.Input; EXPECT_EQ(H->present().renderForTests(), Test.ExpectedHover.str())
<< Test.Input;
} else } else
EXPECT_EQ("", Test.ExpectedHover.str()) << Test.Input; EXPECT_EQ("", Test.ExpectedHover.str()) << Test.Input;
} }