2017-02-07 18:28:20 +08:00
|
|
|
//===--- ProtocolHandlers.cpp - LSP callbacks -----------------------------===//
|
|
|
|
//
|
|
|
|
// The LLVM Compiler Infrastructure
|
|
|
|
//
|
|
|
|
// This file is distributed under the University of Illinois Open Source
|
|
|
|
// License. See LICENSE.TXT for details.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
#include "ProtocolHandlers.h"
|
2017-05-16 22:40:30 +08:00
|
|
|
#include "ClangdLSPServer.h"
|
2017-05-16 17:38:59 +08:00
|
|
|
#include "ClangdServer.h"
|
|
|
|
#include "DraftStore.h"
|
2017-11-02 17:21:51 +08:00
|
|
|
#include "Trace.h"
|
2017-10-12 21:29:58 +08:00
|
|
|
|
2017-02-07 18:28:20 +08:00
|
|
|
using namespace clang;
|
2017-10-12 21:29:58 +08:00
|
|
|
using namespace clang::clangd;
|
2017-02-07 18:28:20 +08:00
|
|
|
|
2017-05-16 22:40:30 +08:00
|
|
|
namespace {
|
2017-02-07 18:28:20 +08:00
|
|
|
|
2017-10-12 21:29:58 +08:00
|
|
|
// Helper for attaching ProtocolCallbacks methods to a JSONRPCDispatcher.
|
|
|
|
// Invoke like: Registerer("foo", &ProtocolCallbacks::onFoo)
|
|
|
|
// onFoo should be: void onFoo(Ctx &C, FooParams &Params)
|
2017-11-28 17:37:43 +08:00
|
|
|
// FooParams should have a static factory method: parse(const json::Expr&).
|
2017-10-12 21:29:58 +08:00
|
|
|
struct HandlerRegisterer {
|
|
|
|
template <typename Param>
|
|
|
|
void operator()(StringRef Method,
|
|
|
|
void (ProtocolCallbacks::*Handler)(RequestContext, Param)) {
|
|
|
|
// Capture pointers by value, as the lambda will outlive this object.
|
|
|
|
auto *Out = this->Out;
|
|
|
|
auto *Callbacks = this->Callbacks;
|
|
|
|
Dispatcher.registerHandler(
|
2017-11-28 17:37:43 +08:00
|
|
|
Method, [=](RequestContext C, const json::Expr &RawParams) {
|
2017-11-02 17:21:51 +08:00
|
|
|
if (auto P = [&] {
|
|
|
|
trace::Span Tracer("Parse");
|
2017-11-28 17:37:43 +08:00
|
|
|
return std::decay<Param>::type::parse(RawParams);
|
2017-11-02 17:21:51 +08:00
|
|
|
}()) {
|
2017-10-12 21:29:58 +08:00
|
|
|
(Callbacks->*Handler)(std::move(C), *P);
|
|
|
|
} else {
|
|
|
|
Out->log("Failed to decode " + Method + " request.\n");
|
|
|
|
}
|
|
|
|
});
|
2017-10-03 02:00:37 +08:00
|
|
|
}
|
|
|
|
|
2017-10-12 21:29:58 +08:00
|
|
|
JSONRPCDispatcher &Dispatcher;
|
|
|
|
JSONOutput *Out;
|
|
|
|
ProtocolCallbacks *Callbacks;
|
2017-10-03 02:00:37 +08:00
|
|
|
};
|
|
|
|
|
2017-05-16 22:40:30 +08:00
|
|
|
} // namespace
|
|
|
|
|
2017-09-30 00:41:23 +08:00
|
|
|
void clangd::registerCallbackHandlers(JSONRPCDispatcher &Dispatcher,
|
2017-10-10 22:21:04 +08:00
|
|
|
JSONOutput &Out,
|
|
|
|
ProtocolCallbacks &Callbacks) {
|
2017-10-12 21:29:58 +08:00
|
|
|
HandlerRegisterer Register{Dispatcher, &Out, &Callbacks};
|
|
|
|
|
|
|
|
Register("initialize", &ProtocolCallbacks::onInitialize);
|
|
|
|
Register("shutdown", &ProtocolCallbacks::onShutdown);
|
2017-10-25 16:45:41 +08:00
|
|
|
Register("exit", &ProtocolCallbacks::onExit);
|
2017-10-12 21:29:58 +08:00
|
|
|
Register("textDocument/didOpen", &ProtocolCallbacks::onDocumentDidOpen);
|
|
|
|
Register("textDocument/didClose", &ProtocolCallbacks::onDocumentDidClose);
|
|
|
|
Register("textDocument/didChange", &ProtocolCallbacks::onDocumentDidChange);
|
|
|
|
Register("textDocument/rangeFormatting",
|
|
|
|
&ProtocolCallbacks::onDocumentRangeFormatting);
|
|
|
|
Register("textDocument/onTypeFormatting",
|
|
|
|
&ProtocolCallbacks::onDocumentOnTypeFormatting);
|
|
|
|
Register("textDocument/formatting", &ProtocolCallbacks::onDocumentFormatting);
|
|
|
|
Register("textDocument/codeAction", &ProtocolCallbacks::onCodeAction);
|
|
|
|
Register("textDocument/completion", &ProtocolCallbacks::onCompletion);
|
|
|
|
Register("textDocument/signatureHelp", &ProtocolCallbacks::onSignatureHelp);
|
|
|
|
Register("textDocument/definition", &ProtocolCallbacks::onGoToDefinition);
|
|
|
|
Register("textDocument/switchSourceHeader",
|
|
|
|
&ProtocolCallbacks::onSwitchSourceHeader);
|
2017-11-09 19:30:04 +08:00
|
|
|
Register("textDocument/rename", &ProtocolCallbacks::onRename);
|
2017-10-12 21:29:58 +08:00
|
|
|
Register("workspace/didChangeWatchedFiles", &ProtocolCallbacks::onFileEvent);
|
[clangd] Handle clangd.applyFix server-side
Summary:
When the user selects a fix-it (or any code action with commands), it is
possible to let the client forward the selected command to the server.
When the clangd.applyFix command is handled on the server, it can send a
workspace/applyEdit request to the client. This has the advantage that
the client doesn't explicitly have to know how to handle
clangd.applyFix. Therefore, the code to handle clangd.applyFix in the VS
Code extension (and any other Clangd client) is not required anymore.
Reviewers: ilya-biryukov, sammccall, Nebiroth, hokein
Reviewed By: hokein
Subscribers: ioeric, hokein, rwols, puremourning, bkramer, ilya-biryukov
Tags: #clang-tools-extra
Differential Revision: https://reviews.llvm.org/D39276
llvm-svn: 317322
2017-11-03 21:39:15 +08:00
|
|
|
Register("workspace/executeCommand", &ProtocolCallbacks::onCommand);
|
2017-04-04 17:46:39 +08:00
|
|
|
}
|