Skip to content

Commit

Permalink
[clangd] Introduce a structured hover response
Browse files Browse the repository at this point in the history
Summary:
Change ClangdServer layer to output a structured response for Hover,
which can be rendered by client according to their needs.

Reviewers: sammccall, ilya-biryukov

Subscribers: ilya-biryukov, MaskRay, jkorous, arphaman, cfe-commits

Tags: #clang

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

llvm-svn: 361803
  • Loading branch information
kadircet committed May 28, 2019
1 parent 173a68f commit c6578ee
Show file tree
Hide file tree
Showing 9 changed files with 769 additions and 143 deletions.
15 changes: 14 additions & 1 deletion clang-tools-extra/clangd/ClangdLSPServer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -842,7 +842,20 @@ void ClangdLSPServer::onDocumentHighlight(
void ClangdLSPServer::onHover(const TextDocumentPositionParams &Params,
Callback<llvm::Optional<Hover>> Reply) {
Server->findHover(Params.textDocument.uri.file(), Params.position,
std::move(Reply));
Bind(
[](decltype(Reply) Reply,
llvm::Expected<llvm::Optional<HoverInfo>> HIorErr) {
if (!HIorErr)
return Reply(HIorErr.takeError());
const auto &HI = HIorErr.get();
if (!HI)
return Reply(llvm::None);
Hover H;
H.range = HI->SymRange;
H.contents = HI->render();
return Reply(H);
},
std::move(Reply)));
}

void ClangdLSPServer::onTypeHierarchy(
Expand Down
11 changes: 7 additions & 4 deletions clang-tools-extra/clangd/ClangdServer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -461,15 +461,18 @@ void ClangdServer::findDocumentHighlights(
}

void ClangdServer::findHover(PathRef File, Position Pos,
Callback<llvm::Optional<Hover>> CB) {
auto Action = [Pos](Callback<llvm::Optional<Hover>> CB,
Callback<llvm::Optional<HoverInfo>> CB) {
auto Action = [Pos](Callback<llvm::Optional<HoverInfo>> CB, Path File,
llvm::Expected<InputsAndAST> InpAST) {
if (!InpAST)
return CB(InpAST.takeError());
CB(clangd::getHover(InpAST->AST, Pos));
format::FormatStyle Style = getFormatStyleForFile(
File, InpAST->Inputs.Contents, InpAST->Inputs.FS.get());
CB(clangd::getHover(InpAST->AST, Pos, std::move(Style)));
};

WorkScheduler.runWithAST("Hover", File, Bind(Action, std::move(CB)));
WorkScheduler.runWithAST("Hover", File,
Bind(Action, std::move(CB), File.str()));
}

void ClangdServer::typeHierarchy(PathRef File, Position Pos, int Resolve,
Expand Down
2 changes: 1 addition & 1 deletion clang-tools-extra/clangd/ClangdServer.h
Original file line number Diff line number Diff line change
Expand Up @@ -186,7 +186,7 @@ class ClangdServer {

/// Get code hover for a given position.
void findHover(PathRef File, Position Pos,
Callback<llvm::Optional<Hover>> CB);
Callback<llvm::Optional<HoverInfo>> CB);

/// Get information about type hierarchy for a given position.
void typeHierarchy(PathRef File, Position Pos, int Resolve,
Expand Down
1 change: 1 addition & 0 deletions clang-tools-extra/clangd/Protocol.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
#include "llvm/ADT/Hashing.h"
#include "llvm/ADT/SmallString.h"
#include "llvm/ADT/StringSwitch.h"
#include "llvm/Support/ErrorHandling.h"
#include "llvm/Support/Format.h"
#include "llvm/Support/FormatVariadic.h"
#include "llvm/Support/JSON.h"
Expand Down
Loading

0 comments on commit c6578ee

Please sign in to comment.