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-02-07 18:28:20 +08:00
|
|
|
using namespace clang;
|
|
|
|
using namespace clangd;
|
|
|
|
|
2017-05-16 22:40:30 +08:00
|
|
|
namespace {
|
2017-02-07 18:28:20 +08:00
|
|
|
|
2017-05-16 22:40:30 +08:00
|
|
|
struct InitializeHandler : Handler {
|
|
|
|
InitializeHandler(JSONOutput &Output, ProtocolCallbacks &Callbacks)
|
|
|
|
: Handler(Output), Callbacks(Callbacks) {}
|
|
|
|
|
|
|
|
void handleMethod(llvm::yaml::MappingNode *Params, StringRef ID) override {
|
|
|
|
Callbacks.onInitialize(ID, Output);
|
2017-04-10 21:31:39 +08:00
|
|
|
}
|
|
|
|
|
2017-05-16 22:40:30 +08:00
|
|
|
private:
|
|
|
|
ProtocolCallbacks &Callbacks;
|
|
|
|
};
|
2017-04-10 21:31:39 +08:00
|
|
|
|
2017-05-16 22:40:30 +08:00
|
|
|
struct ShutdownHandler : Handler {
|
|
|
|
ShutdownHandler(JSONOutput &Output, ProtocolCallbacks &Callbacks)
|
|
|
|
: Handler(Output), Callbacks(Callbacks) {}
|
|
|
|
|
|
|
|
void handleMethod(llvm::yaml::MappingNode *Params, StringRef ID) override {
|
|
|
|
Callbacks.onShutdown(Output);
|
2017-02-07 18:28:20 +08:00
|
|
|
}
|
|
|
|
|
2017-05-16 22:40:30 +08:00
|
|
|
private:
|
|
|
|
ProtocolCallbacks &Callbacks;
|
|
|
|
};
|
|
|
|
|
|
|
|
struct TextDocumentDidOpenHandler : Handler {
|
|
|
|
TextDocumentDidOpenHandler(JSONOutput &Output, ProtocolCallbacks &Callbacks)
|
|
|
|
: Handler(Output), Callbacks(Callbacks) {}
|
|
|
|
|
|
|
|
void handleNotification(llvm::yaml::MappingNode *Params) override {
|
2017-09-18 23:02:59 +08:00
|
|
|
auto DOTDP = DidOpenTextDocumentParams::parse(Params, Output);
|
2017-05-16 22:40:30 +08:00
|
|
|
if (!DOTDP) {
|
|
|
|
Output.log("Failed to decode DidOpenTextDocumentParams!\n");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
Callbacks.onDocumentDidOpen(*DOTDP, Output);
|
2017-02-07 18:28:20 +08:00
|
|
|
}
|
|
|
|
|
2017-05-16 22:40:30 +08:00
|
|
|
private:
|
|
|
|
ProtocolCallbacks &Callbacks;
|
|
|
|
};
|
|
|
|
|
|
|
|
struct TextDocumentDidChangeHandler : Handler {
|
|
|
|
TextDocumentDidChangeHandler(JSONOutput &Output, ProtocolCallbacks &Callbacks)
|
|
|
|
: Handler(Output), Callbacks(Callbacks) {}
|
|
|
|
|
|
|
|
void handleNotification(llvm::yaml::MappingNode *Params) override {
|
2017-09-18 23:02:59 +08:00
|
|
|
auto DCTDP = DidChangeTextDocumentParams::parse(Params, Output);
|
2017-05-16 22:40:30 +08:00
|
|
|
if (!DCTDP || DCTDP->contentChanges.size() != 1) {
|
|
|
|
Output.log("Failed to decode DidChangeTextDocumentParams!\n");
|
|
|
|
return;
|
|
|
|
}
|
2017-02-07 18:28:20 +08:00
|
|
|
|
2017-05-16 22:40:30 +08:00
|
|
|
Callbacks.onDocumentDidChange(*DCTDP, Output);
|
2017-02-07 18:28:20 +08:00
|
|
|
}
|
|
|
|
|
2017-05-16 22:40:30 +08:00
|
|
|
private:
|
|
|
|
ProtocolCallbacks &Callbacks;
|
|
|
|
};
|
2017-03-02 00:16:29 +08:00
|
|
|
|
2017-05-16 22:40:30 +08:00
|
|
|
struct TextDocumentDidCloseHandler : Handler {
|
|
|
|
TextDocumentDidCloseHandler(JSONOutput &Output, ProtocolCallbacks &Callbacks)
|
|
|
|
: Handler(Output), Callbacks(Callbacks) {}
|
|
|
|
|
|
|
|
void handleNotification(llvm::yaml::MappingNode *Params) override {
|
2017-09-18 23:02:59 +08:00
|
|
|
auto DCTDP = DidCloseTextDocumentParams::parse(Params, Output);
|
2017-05-16 22:40:30 +08:00
|
|
|
if (!DCTDP) {
|
|
|
|
Output.log("Failed to decode DidCloseTextDocumentParams!\n");
|
|
|
|
return;
|
|
|
|
}
|
2017-02-07 18:28:20 +08:00
|
|
|
|
2017-05-16 22:40:30 +08:00
|
|
|
Callbacks.onDocumentDidClose(*DCTDP, Output);
|
2017-02-07 18:28:20 +08:00
|
|
|
}
|
|
|
|
|
2017-05-16 22:40:30 +08:00
|
|
|
private:
|
|
|
|
ProtocolCallbacks &Callbacks;
|
|
|
|
};
|
2017-02-07 18:28:20 +08:00
|
|
|
|
2017-05-16 22:40:30 +08:00
|
|
|
struct TextDocumentOnTypeFormattingHandler : Handler {
|
|
|
|
TextDocumentOnTypeFormattingHandler(JSONOutput &Output,
|
|
|
|
ProtocolCallbacks &Callbacks)
|
|
|
|
: Handler(Output), Callbacks(Callbacks) {}
|
2017-02-07 18:28:20 +08:00
|
|
|
|
2017-05-16 22:40:30 +08:00
|
|
|
void handleMethod(llvm::yaml::MappingNode *Params, StringRef ID) override {
|
2017-09-18 23:02:59 +08:00
|
|
|
auto DOTFP = DocumentOnTypeFormattingParams::parse(Params, Output);
|
2017-05-16 22:40:30 +08:00
|
|
|
if (!DOTFP) {
|
|
|
|
Output.log("Failed to decode DocumentOnTypeFormattingParams!\n");
|
|
|
|
return;
|
|
|
|
}
|
2017-02-07 18:28:20 +08:00
|
|
|
|
2017-05-16 22:40:30 +08:00
|
|
|
Callbacks.onDocumentOnTypeFormatting(*DOTFP, ID, Output);
|
2017-02-16 18:49:46 +08:00
|
|
|
}
|
|
|
|
|
2017-05-16 22:40:30 +08:00
|
|
|
private:
|
|
|
|
ProtocolCallbacks &Callbacks;
|
|
|
|
};
|
2017-02-16 18:49:46 +08:00
|
|
|
|
2017-05-16 22:40:30 +08:00
|
|
|
struct TextDocumentRangeFormattingHandler : Handler {
|
|
|
|
TextDocumentRangeFormattingHandler(JSONOutput &Output,
|
|
|
|
ProtocolCallbacks &Callbacks)
|
|
|
|
: Handler(Output), Callbacks(Callbacks) {}
|
2017-02-07 18:28:20 +08:00
|
|
|
|
2017-05-16 22:40:30 +08:00
|
|
|
void handleMethod(llvm::yaml::MappingNode *Params, StringRef ID) override {
|
2017-09-18 23:02:59 +08:00
|
|
|
auto DRFP = DocumentRangeFormattingParams::parse(Params, Output);
|
2017-05-16 22:40:30 +08:00
|
|
|
if (!DRFP) {
|
|
|
|
Output.log("Failed to decode DocumentRangeFormattingParams!\n");
|
|
|
|
return;
|
|
|
|
}
|
2017-03-02 00:16:29 +08:00
|
|
|
|
2017-05-16 22:40:30 +08:00
|
|
|
Callbacks.onDocumentRangeFormatting(*DRFP, ID, Output);
|
2017-03-02 00:16:29 +08:00
|
|
|
}
|
|
|
|
|
2017-05-16 22:40:30 +08:00
|
|
|
private:
|
|
|
|
ProtocolCallbacks &Callbacks;
|
|
|
|
};
|
|
|
|
|
|
|
|
struct TextDocumentFormattingHandler : Handler {
|
|
|
|
TextDocumentFormattingHandler(JSONOutput &Output,
|
|
|
|
ProtocolCallbacks &Callbacks)
|
|
|
|
: Handler(Output), Callbacks(Callbacks) {}
|
|
|
|
|
|
|
|
void handleMethod(llvm::yaml::MappingNode *Params, StringRef ID) override {
|
2017-09-18 23:02:59 +08:00
|
|
|
auto DFP = DocumentFormattingParams::parse(Params, Output);
|
2017-05-16 22:40:30 +08:00
|
|
|
if (!DFP) {
|
|
|
|
Output.log("Failed to decode DocumentFormattingParams!\n");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
Callbacks.onDocumentFormatting(*DFP, ID, Output);
|
2017-03-02 00:16:29 +08:00
|
|
|
}
|
|
|
|
|
2017-05-16 22:40:30 +08:00
|
|
|
private:
|
|
|
|
ProtocolCallbacks &Callbacks;
|
|
|
|
};
|
|
|
|
|
|
|
|
struct CodeActionHandler : Handler {
|
|
|
|
CodeActionHandler(JSONOutput &Output, ProtocolCallbacks &Callbacks)
|
|
|
|
: Handler(Output), Callbacks(Callbacks) {}
|
2017-04-04 17:46:39 +08:00
|
|
|
|
2017-05-16 22:40:30 +08:00
|
|
|
void handleMethod(llvm::yaml::MappingNode *Params, StringRef ID) override {
|
2017-09-18 23:02:59 +08:00
|
|
|
auto CAP = CodeActionParams::parse(Params, Output);
|
2017-05-16 22:40:30 +08:00
|
|
|
if (!CAP) {
|
|
|
|
Output.log("Failed to decode CodeActionParams!\n");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
Callbacks.onCodeAction(*CAP, ID, Output);
|
2017-04-04 17:46:39 +08:00
|
|
|
}
|
|
|
|
|
2017-05-16 22:40:30 +08:00
|
|
|
private:
|
|
|
|
ProtocolCallbacks &Callbacks;
|
|
|
|
};
|
|
|
|
|
|
|
|
struct CompletionHandler : Handler {
|
|
|
|
CompletionHandler(JSONOutput &Output, ProtocolCallbacks &Callbacks)
|
|
|
|
: Handler(Output), Callbacks(Callbacks) {}
|
|
|
|
|
|
|
|
void handleMethod(llvm::yaml::MappingNode *Params, StringRef ID) override {
|
2017-09-18 23:02:59 +08:00
|
|
|
auto TDPP = TextDocumentPositionParams::parse(Params, Output);
|
2017-05-16 22:40:30 +08:00
|
|
|
if (!TDPP) {
|
|
|
|
Output.log("Failed to decode TextDocumentPositionParams!\n");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
Callbacks.onCompletion(*TDPP, ID, Output);
|
2017-04-04 17:46:39 +08:00
|
|
|
}
|
2017-05-16 22:40:30 +08:00
|
|
|
|
|
|
|
private:
|
|
|
|
ProtocolCallbacks &Callbacks;
|
|
|
|
};
|
|
|
|
|
2017-06-29 00:12:10 +08:00
|
|
|
struct GotoDefinitionHandler : Handler {
|
|
|
|
GotoDefinitionHandler(JSONOutput &Output, ProtocolCallbacks &Callbacks)
|
|
|
|
: Handler(Output), Callbacks(Callbacks) {}
|
|
|
|
|
|
|
|
void handleMethod(llvm::yaml::MappingNode *Params, StringRef ID) override {
|
2017-09-18 23:02:59 +08:00
|
|
|
auto TDPP = TextDocumentPositionParams::parse(Params, Output);
|
2017-06-29 00:12:10 +08:00
|
|
|
if (!TDPP) {
|
|
|
|
Output.log("Failed to decode TextDocumentPositionParams!\n");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
Callbacks.onGoToDefinition(*TDPP, ID, Output);
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
ProtocolCallbacks &Callbacks;
|
|
|
|
};
|
|
|
|
|
2017-05-16 22:40:30 +08:00
|
|
|
} // namespace
|
|
|
|
|
|
|
|
void clangd::regiterCallbackHandlers(JSONRPCDispatcher &Dispatcher,
|
|
|
|
JSONOutput &Out,
|
|
|
|
ProtocolCallbacks &Callbacks) {
|
|
|
|
Dispatcher.registerHandler(
|
|
|
|
"initialize", llvm::make_unique<InitializeHandler>(Out, Callbacks));
|
|
|
|
Dispatcher.registerHandler(
|
|
|
|
"shutdown", llvm::make_unique<ShutdownHandler>(Out, Callbacks));
|
|
|
|
Dispatcher.registerHandler(
|
|
|
|
"textDocument/didOpen",
|
|
|
|
llvm::make_unique<TextDocumentDidOpenHandler>(Out, Callbacks));
|
|
|
|
Dispatcher.registerHandler(
|
|
|
|
"textDocument/didClose",
|
|
|
|
llvm::make_unique<TextDocumentDidCloseHandler>(Out, Callbacks));
|
|
|
|
Dispatcher.registerHandler(
|
|
|
|
"textDocument/didChange",
|
|
|
|
llvm::make_unique<TextDocumentDidChangeHandler>(Out, Callbacks));
|
|
|
|
Dispatcher.registerHandler(
|
|
|
|
"textDocument/rangeFormatting",
|
|
|
|
llvm::make_unique<TextDocumentRangeFormattingHandler>(Out, Callbacks));
|
|
|
|
Dispatcher.registerHandler(
|
|
|
|
"textDocument/onTypeFormatting",
|
|
|
|
llvm::make_unique<TextDocumentOnTypeFormattingHandler>(Out, Callbacks));
|
|
|
|
Dispatcher.registerHandler(
|
|
|
|
"textDocument/formatting",
|
|
|
|
llvm::make_unique<TextDocumentFormattingHandler>(Out, Callbacks));
|
|
|
|
Dispatcher.registerHandler(
|
|
|
|
"textDocument/codeAction",
|
|
|
|
llvm::make_unique<CodeActionHandler>(Out, Callbacks));
|
|
|
|
Dispatcher.registerHandler(
|
|
|
|
"textDocument/completion",
|
|
|
|
llvm::make_unique<CompletionHandler>(Out, Callbacks));
|
2017-08-02 17:08:39 +08:00
|
|
|
Dispatcher.registerHandler(
|
|
|
|
"textDocument/definition",
|
2017-06-29 00:12:10 +08:00
|
|
|
llvm::make_unique<GotoDefinitionHandler>(Out, Callbacks));
|
2017-04-04 17:46:39 +08:00
|
|
|
}
|