#include "LSPServer.h"
#include "TableGenServer.h"
#include "mlir/Tools/lsp-server-support/Logging.h"
#include "mlir/Tools/lsp-server-support/Protocol.h"
#include "mlir/Tools/lsp-server-support/Transport.h"
#include "llvm/ADT/FunctionExtras.h"
#include "llvm/ADT/StringMap.h"
#include <optional>
using namespace mlir;
using namespace mlir::lsp;
namespace {
struct LSPServer {
LSPServer(TableGenServer &server, JSONTransport &transport)
: server(server), transport(transport) {}
void onInitialize(const InitializeParams ¶ms,
Callback<llvm::json::Value> reply);
void onInitialized(const InitializedParams ¶ms);
void onShutdown(const NoParams ¶ms, Callback<std::nullptr_t> reply);
void onDocumentDidOpen(const DidOpenTextDocumentParams ¶ms);
void onDocumentDidClose(const DidCloseTextDocumentParams ¶ms);
void onDocumentDidChange(const DidChangeTextDocumentParams ¶ms);
void onGoToDefinition(const TextDocumentPositionParams ¶ms,
Callback<std::vector<Location>> reply);
void onReference(const ReferenceParams ¶ms,
Callback<std::vector<Location>> reply);
void onDocumentLink(const DocumentLinkParams ¶ms,
Callback<std::vector<DocumentLink>> reply);
void onHover(const TextDocumentPositionParams ¶ms,
Callback<std::optional<Hover>> reply);
TableGenServer &server;
JSONTransport &transport;
OutgoingNotification<PublishDiagnosticsParams> publishDiagnostics;
bool shutdownRequestReceived = false;
};
}
void LSPServer::onInitialize(const InitializeParams ¶ms,
Callback<llvm::json::Value> reply) {
llvm::json::Object serverCaps{
{"textDocumentSync",
llvm::json::Object{
{"openClose", true},
{"change", (int)TextDocumentSyncKind::Incremental},
{"save", true},
}},
{"definitionProvider", true},
{"referencesProvider", true},
{"documentLinkProvider",
llvm::json::Object{
{"resolveProvider", false},
}},
{"hoverProvider", true},
};
llvm::json::Object result{
{{"serverInfo", llvm::json::Object{{"name", "tblgen-lsp-server"},
{"version", "0.0.1"}}},
{"capabilities", std::move(serverCaps)}}};
reply(std::move(result));
}
void LSPServer::onInitialized(const InitializedParams &) {}
void LSPServer::onShutdown(const NoParams &, Callback<std::nullptr_t> reply) {
shutdownRequestReceived = true;
reply(nullptr);
}
void LSPServer::onDocumentDidOpen(const DidOpenTextDocumentParams ¶ms) {
PublishDiagnosticsParams diagParams(params.textDocument.uri,
params.textDocument.version);
server.addDocument(params.textDocument.uri, params.textDocument.text,
params.textDocument.version, diagParams.diagnostics);
publishDiagnostics(diagParams);
}
void LSPServer::onDocumentDidClose(const DidCloseTextDocumentParams ¶ms) {
std::optional<int64_t> version =
server.removeDocument(params.textDocument.uri);
if (!version)
return;
publishDiagnostics(
PublishDiagnosticsParams(params.textDocument.uri, *version));
}
void LSPServer::onDocumentDidChange(const DidChangeTextDocumentParams ¶ms) {
PublishDiagnosticsParams diagParams(params.textDocument.uri,
params.textDocument.version);
server.updateDocument(params.textDocument.uri, params.contentChanges,
params.textDocument.version, diagParams.diagnostics);
publishDiagnostics(diagParams);
}
void LSPServer::onGoToDefinition(const TextDocumentPositionParams ¶ms,
Callback<std::vector<Location>> reply) {
std::vector<Location> locations;
server.getLocationsOf(params.textDocument.uri, params.position, locations);
reply(std::move(locations));
}
void LSPServer::onReference(const ReferenceParams ¶ms,
Callback<std::vector<Location>> reply) {
std::vector<Location> locations;
server.findReferencesOf(params.textDocument.uri, params.position, locations);
reply(std::move(locations));
}
void LSPServer::onDocumentLink(const DocumentLinkParams ¶ms,
Callback<std::vector<DocumentLink>> reply) {
std::vector<DocumentLink> links;
server.getDocumentLinks(params.textDocument.uri, links);
reply(std::move(links));
}
void LSPServer::onHover(const TextDocumentPositionParams ¶ms,
Callback<std::optional<Hover>> reply) {
reply(server.findHover(params.textDocument.uri, params.position));
}
LogicalResult mlir::lsp::runTableGenLSPServer(TableGenServer &server,
JSONTransport &transport) {
LSPServer lspServer(server, transport);
MessageHandler messageHandler(transport);
messageHandler.method("initialize", &lspServer, &LSPServer::onInitialize);
messageHandler.notification("initialized", &lspServer,
&LSPServer::onInitialized);
messageHandler.method("shutdown", &lspServer, &LSPServer::onShutdown);
messageHandler.notification("textDocument/didOpen", &lspServer,
&LSPServer::onDocumentDidOpen);
messageHandler.notification("textDocument/didClose", &lspServer,
&LSPServer::onDocumentDidClose);
messageHandler.notification("textDocument/didChange", &lspServer,
&LSPServer::onDocumentDidChange);
messageHandler.method("textDocument/definition", &lspServer,
&LSPServer::onGoToDefinition);
messageHandler.method("textDocument/references", &lspServer,
&LSPServer::onReference);
messageHandler.method("textDocument/documentLink", &lspServer,
&LSPServer::onDocumentLink);
messageHandler.method("textDocument/hover", &lspServer, &LSPServer::onHover);
lspServer.publishDiagnostics =
messageHandler.outgoingNotification<PublishDiagnosticsParams>(
"textDocument/publishDiagnostics");
if (llvm::Error error = transport.run(messageHandler)) {
Logger::error("Transport error: {0}", error);
llvm::consumeError(std::move(error));
return failure();
}
return success(lspServer.shutdownRequestReceived);
}