Index: clangd/ClangdLSPServer.cpp =================================================================== --- clangd/ClangdLSPServer.cpp +++ clangd/ClangdLSPServer.cpp @@ -365,7 +365,7 @@ void ClangdLSPServer::onHover(TextDocumentPositionParams &Params) { Server.findHover(Params.textDocument.uri.file(), Params.position, - [](llvm::Expected H) { + [](llvm::Expected> H) { if (!H) { replyError(ErrorCode::InternalError, llvm::toString(H.takeError())); Index: clangd/ClangdServer.h =================================================================== --- clangd/ClangdServer.h +++ clangd/ClangdServer.h @@ -158,7 +158,8 @@ Callback> CB); /// Get code hover for a given position. - void findHover(PathRef File, Position Pos, Callback CB); + void findHover(PathRef File, Position Pos, + Callback> CB); /// Retrieve the top symbols from the workspace matching a query. void workspaceSymbols(StringRef Query, int Limit, Index: clangd/ClangdServer.cpp =================================================================== --- clangd/ClangdServer.cpp +++ clangd/ClangdServer.cpp @@ -403,9 +403,10 @@ WorkScheduler.runWithAST("Highlights", File, Bind(Action, std::move(CB))); } -void ClangdServer::findHover(PathRef File, Position Pos, Callback CB) { +void ClangdServer::findHover(PathRef File, Position Pos, + Callback> CB) { auto FS = FSProvider.getFileSystem(); - auto Action = [Pos, FS](Callback CB, + auto Action = [Pos, FS](Callback> CB, llvm::Expected InpAST) { if (!InpAST) return CB(InpAST.takeError()); Index: clangd/JSONExpr.h =================================================================== --- clangd/JSONExpr.h +++ clangd/JSONExpr.h @@ -22,6 +22,8 @@ namespace clang { namespace clangd { namespace json { +class Expr; +template Expr toJSON(const llvm::Optional &Opt); // An Expr is an JSON value of unknown type. // They can be copied, but should generally be moved. @@ -516,6 +518,11 @@ return false; } +template +json::Expr toJSON(const llvm::Optional& Opt) { + return Opt ? json::Expr(*Opt) : json::Expr(nullptr); +} + // Helper for mapping JSON objects onto protocol structs. // See file header for example. class ObjectMapper { Index: clangd/XRefs.h =================================================================== --- clangd/XRefs.h +++ clangd/XRefs.h @@ -16,6 +16,7 @@ #include "ClangdUnit.h" #include "Protocol.h" #include "index/Index.h" +#include "llvm/ADT/Optional.h" #include namespace clang { @@ -30,7 +31,7 @@ Position Pos); /// Get the hover information when hovering at \p Pos. -Hover getHover(ParsedAST &AST, Position Pos); +llvm::Optional getHover(ParsedAST &AST, Position Pos); } // namespace clangd } // namespace clang Index: clangd/XRefs.cpp =================================================================== --- clangd/XRefs.cpp +++ clangd/XRefs.cpp @@ -526,7 +526,7 @@ return H; } -Hover getHover(ParsedAST &AST, Position Pos) { +Optional getHover(ParsedAST &AST, Position Pos) { const SourceManager &SourceMgr = AST.getASTContext().getSourceManager(); SourceLocation SourceLocationBeg = getBeginningOfIdentifier(AST, Pos, SourceMgr.getMainFileID()); @@ -539,7 +539,7 @@ if (!Symbols.Decls.empty()) return getHoverContents(Symbols.Decls[0]); - return Hover(); + return None; } } // namespace clangd Index: test/clangd/hover.test =================================================================== --- test/clangd/hover.test +++ test/clangd/hover.test @@ -14,6 +14,11 @@ # CHECK-NEXT: } # CHECK-NEXT:} --- +{"jsonrpc":"2.0","id":1,"method":"textDocument/hover","params":{"textDocument":{"uri":"test:///main.cpp"},"position":{"line":0,"character":10}}} +# CHECK: "id": 1, +# CHECK-NEXT: "jsonrpc": "2.0", +# CHECK-NEXT: "result": null +--- {"jsonrpc":"2.0","id":3,"method":"shutdown"} --- {"jsonrpc":"2.0","method":"exit"} Index: unittests/clangd/JSONExprTests.cpp =================================================================== --- unittests/clangd/JSONExprTests.cpp +++ unittests/clangd/JSONExprTests.cpp @@ -38,6 +38,8 @@ EXPECT_EQ(R"({"A":{"B":{}}})", s(obj{{"A", obj{{"B", obj{}}}}})); EXPECT_EQ(R"({"A":{"B":{"X":"Y"}}})", s(obj{{"A", obj{{"B", obj{{"X", "Y"}}}}}})); + EXPECT_EQ("null", s(llvm::Optional())); + EXPECT_EQ("2.5", s(llvm::Optional(2.5))); } TEST(JSONExprTests, StringOwnership) { Index: unittests/clangd/XRefsTests.cpp =================================================================== --- unittests/clangd/XRefsTests.cpp +++ unittests/clangd/XRefsTests.cpp @@ -629,14 +629,24 @@ )cpp", "Declared in union outer::(anonymous)\n\nint def", }, + { + R"cpp(// Nothing + void foo() { + ^ + } + )cpp", + "", + }, }; for (const OneTest &Test : Tests) { Annotations T(Test.Input); auto AST = TestTU::withCode(T.code()).build(); - Hover H = getHover(AST, T.point()); - - EXPECT_EQ(H.contents.value, Test.ExpectedHover) << Test.Input; + if (auto H = getHover(AST, T.point())) { + EXPECT_EQ("", Test.ExpectedHover) << Test.Input; + EXPECT_EQ(H->contents.value, Test.ExpectedHover) << Test.Input; + } else + EXPECT_EQ("", Test.ExpectedHover) << Test.Input; } }