blob: 9f6f335c29771dfb3de16eb237a20a10910555aa [file] [log] [blame]
Benjamin Kramerbb1cdb62017-02-07 10:28:20 +00001//===--- ProtocolHandlers.cpp - LSP callbacks -----------------------------===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9
10#include "ProtocolHandlers.h"
Ilya Biryukovafb55542017-05-16 14:40:30 +000011#include "ClangdLSPServer.h"
Ilya Biryukov38d79772017-05-16 09:38:59 +000012#include "ClangdServer.h"
13#include "DraftStore.h"
Sam McCall8567cb32017-11-02 09:21:51 +000014#include "Trace.h"
Sam McCall8a5dded2017-10-12 13:29:58 +000015
Benjamin Kramerbb1cdb62017-02-07 10:28:20 +000016using namespace clang;
Sam McCall8a5dded2017-10-12 13:29:58 +000017using namespace clang::clangd;
Benjamin Kramerbb1cdb62017-02-07 10:28:20 +000018
Ilya Biryukovafb55542017-05-16 14:40:30 +000019namespace {
Benjamin Kramerbb1cdb62017-02-07 10:28:20 +000020
Sam McCall8a5dded2017-10-12 13:29:58 +000021// Helper for attaching ProtocolCallbacks methods to a JSONRPCDispatcher.
22// Invoke like: Registerer("foo", &ProtocolCallbacks::onFoo)
23// onFoo should be: void onFoo(Ctx &C, FooParams &Params)
Sam McCallff8b8742017-11-30 21:32:29 +000024// FooParams should have a fromJSON function.
Sam McCall8a5dded2017-10-12 13:29:58 +000025struct HandlerRegisterer {
26 template <typename Param>
27 void operator()(StringRef Method,
28 void (ProtocolCallbacks::*Handler)(RequestContext, Param)) {
29 // Capture pointers by value, as the lambda will outlive this object.
30 auto *Out = this->Out;
31 auto *Callbacks = this->Callbacks;
32 Dispatcher.registerHandler(
Sam McCallec109022017-11-28 09:37:43 +000033 Method, [=](RequestContext C, const json::Expr &RawParams) {
Sam McCallff8b8742017-11-30 21:32:29 +000034 typename std::remove_reference<Param>::type P;
35 if (fromJSON(RawParams, P)) {
36 (Callbacks->*Handler)(std::move(C), P);
Sam McCall8a5dded2017-10-12 13:29:58 +000037 } else {
Sam McCall318fbeb2017-11-30 23:21:34 +000038 Out->log("Failed to decode " + Method + " request.");
Sam McCall8a5dded2017-10-12 13:29:58 +000039 }
40 });
Krasimir Georgiev561ba5e2017-04-10 13:31:39 +000041 }
42
Sam McCall8a5dded2017-10-12 13:29:58 +000043 JSONRPCDispatcher &Dispatcher;
44 JSONOutput *Out;
45 ProtocolCallbacks *Callbacks;
Marc-Andre Laperlebf114242017-10-02 18:00:37 +000046};
47
Ilya Biryukovafb55542017-05-16 14:40:30 +000048} // namespace
49
Sam McCallef41c342017-09-29 16:41:23 +000050void clangd::registerCallbackHandlers(JSONRPCDispatcher &Dispatcher,
Ilya Biryukov3847be52017-10-10 14:21:04 +000051 JSONOutput &Out,
52 ProtocolCallbacks &Callbacks) {
Sam McCall8a5dded2017-10-12 13:29:58 +000053 HandlerRegisterer Register{Dispatcher, &Out, &Callbacks};
54
55 Register("initialize", &ProtocolCallbacks::onInitialize);
56 Register("shutdown", &ProtocolCallbacks::onShutdown);
Ilya Biryukov0d9b8a32017-10-25 08:45:41 +000057 Register("exit", &ProtocolCallbacks::onExit);
Sam McCall8a5dded2017-10-12 13:29:58 +000058 Register("textDocument/didOpen", &ProtocolCallbacks::onDocumentDidOpen);
59 Register("textDocument/didClose", &ProtocolCallbacks::onDocumentDidClose);
60 Register("textDocument/didChange", &ProtocolCallbacks::onDocumentDidChange);
61 Register("textDocument/rangeFormatting",
62 &ProtocolCallbacks::onDocumentRangeFormatting);
63 Register("textDocument/onTypeFormatting",
64 &ProtocolCallbacks::onDocumentOnTypeFormatting);
65 Register("textDocument/formatting", &ProtocolCallbacks::onDocumentFormatting);
66 Register("textDocument/codeAction", &ProtocolCallbacks::onCodeAction);
67 Register("textDocument/completion", &ProtocolCallbacks::onCompletion);
68 Register("textDocument/signatureHelp", &ProtocolCallbacks::onSignatureHelp);
69 Register("textDocument/definition", &ProtocolCallbacks::onGoToDefinition);
70 Register("textDocument/switchSourceHeader",
71 &ProtocolCallbacks::onSwitchSourceHeader);
Haojian Wu345099c2017-11-09 11:30:04 +000072 Register("textDocument/rename", &ProtocolCallbacks::onRename);
Sam McCall8a5dded2017-10-12 13:29:58 +000073 Register("workspace/didChangeWatchedFiles", &ProtocolCallbacks::onFileEvent);
Marc-Andre Laperlee7ec16a2017-11-03 13:39:15 +000074 Register("workspace/executeCommand", &ProtocolCallbacks::onCommand);
Ilya Biryukov0e6a51f2017-12-12 12:27:47 +000075 Register("textDocument/documentHighlight",
76 &ProtocolCallbacks::onDocumentHighlight);
Krasimir Georgiev6d2131a2017-04-04 09:46:39 +000077}