[mlir:LSP][NFC] Make the LSPServer class private

There is no benefit to making it public, and the code is much
cleaner and easier to follow when inlined. This also matches
the pattern within the PDLL lsp server.
This commit is contained in:
River Riddle 2022-07-19 20:39:09 -07:00
parent 79660d339e
commit ee7ccbeaa7
3 changed files with 53 additions and 73 deletions

View File

@ -20,12 +20,12 @@ using namespace mlir;
using namespace mlir::lsp; using namespace mlir::lsp;
//===----------------------------------------------------------------------===// //===----------------------------------------------------------------------===//
// LSPServer::Impl // LSPServer
//===----------------------------------------------------------------------===// //===----------------------------------------------------------------------===//
struct LSPServer::Impl { namespace {
Impl(MLIRServer &server, JSONTransport &transport) struct LSPServer {
: server(server), transport(transport) {} LSPServer(MLIRServer &server) : server(server) {}
//===--------------------------------------------------------------------===// //===--------------------------------------------------------------------===//
// Initialization // Initialization
@ -73,7 +73,6 @@ struct LSPServer::Impl {
//===--------------------------------------------------------------------===// //===--------------------------------------------------------------------===//
MLIRServer &server; MLIRServer &server;
JSONTransport &transport;
/// An outgoing notification used to send diagnostics to the client when they /// An outgoing notification used to send diagnostics to the client when they
/// are ready to be processed. /// are ready to be processed.
@ -83,12 +82,13 @@ struct LSPServer::Impl {
/// Language Server client. /// Language Server client.
bool shutdownRequestReceived = false; bool shutdownRequestReceived = false;
}; };
} // namespace
//===----------------------------------------------------------------------===// //===----------------------------------------------------------------------===//
// Initialization // Initialization
void LSPServer::Impl::onInitialize(const InitializeParams &params, void LSPServer::onInitialize(const InitializeParams &params,
Callback<llvm::json::Value> reply) { Callback<llvm::json::Value> reply) {
// Send a response with the capabilities of this server. // Send a response with the capabilities of this server.
llvm::json::Object serverCaps{ llvm::json::Object serverCaps{
{"textDocumentSync", {"textDocumentSync",
@ -121,9 +121,8 @@ void LSPServer::Impl::onInitialize(const InitializeParams &params,
{"capabilities", std::move(serverCaps)}}}; {"capabilities", std::move(serverCaps)}}};
reply(std::move(result)); reply(std::move(result));
} }
void LSPServer::Impl::onInitialized(const InitializedParams &) {} void LSPServer::onInitialized(const InitializedParams &) {}
void LSPServer::Impl::onShutdown(const NoParams &, void LSPServer::onShutdown(const NoParams &, Callback<std::nullptr_t> reply) {
Callback<std::nullptr_t> reply) {
shutdownRequestReceived = true; shutdownRequestReceived = true;
reply(nullptr); reply(nullptr);
} }
@ -131,8 +130,7 @@ void LSPServer::Impl::onShutdown(const NoParams &,
//===----------------------------------------------------------------------===// //===----------------------------------------------------------------------===//
// Document Change // Document Change
void LSPServer::Impl::onDocumentDidOpen( void LSPServer::onDocumentDidOpen(const DidOpenTextDocumentParams &params) {
const DidOpenTextDocumentParams &params) {
PublishDiagnosticsParams diagParams(params.textDocument.uri, PublishDiagnosticsParams diagParams(params.textDocument.uri,
params.textDocument.version); params.textDocument.version);
server.addOrUpdateDocument(params.textDocument.uri, params.textDocument.text, server.addOrUpdateDocument(params.textDocument.uri, params.textDocument.text,
@ -142,8 +140,7 @@ void LSPServer::Impl::onDocumentDidOpen(
// Publish any recorded diagnostics. // Publish any recorded diagnostics.
publishDiagnostics(diagParams); publishDiagnostics(diagParams);
} }
void LSPServer::Impl::onDocumentDidClose( void LSPServer::onDocumentDidClose(const DidCloseTextDocumentParams &params) {
const DidCloseTextDocumentParams &params) {
Optional<int64_t> version = server.removeDocument(params.textDocument.uri); Optional<int64_t> version = server.removeDocument(params.textDocument.uri);
if (!version) if (!version)
return; return;
@ -154,8 +151,7 @@ void LSPServer::Impl::onDocumentDidClose(
publishDiagnostics( publishDiagnostics(
PublishDiagnosticsParams(params.textDocument.uri, *version)); PublishDiagnosticsParams(params.textDocument.uri, *version));
} }
void LSPServer::Impl::onDocumentDidChange( void LSPServer::onDocumentDidChange(const DidChangeTextDocumentParams &params) {
const DidChangeTextDocumentParams &params) {
// TODO: We currently only support full document updates, we should refactor // TODO: We currently only support full document updates, we should refactor
// to avoid this. // to avoid this.
if (params.contentChanges.size() != 1) if (params.contentChanges.size() != 1)
@ -173,15 +169,15 @@ void LSPServer::Impl::onDocumentDidChange(
//===----------------------------------------------------------------------===// //===----------------------------------------------------------------------===//
// Definitions and References // Definitions and References
void LSPServer::Impl::onGoToDefinition(const TextDocumentPositionParams &params, void LSPServer::onGoToDefinition(const TextDocumentPositionParams &params,
Callback<std::vector<Location>> reply) { Callback<std::vector<Location>> reply) {
std::vector<Location> locations; std::vector<Location> locations;
server.getLocationsOf(params.textDocument.uri, params.position, locations); server.getLocationsOf(params.textDocument.uri, params.position, locations);
reply(std::move(locations)); reply(std::move(locations));
} }
void LSPServer::Impl::onReference(const ReferenceParams &params, void LSPServer::onReference(const ReferenceParams &params,
Callback<std::vector<Location>> reply) { Callback<std::vector<Location>> reply) {
std::vector<Location> locations; std::vector<Location> locations;
server.findReferencesOf(params.textDocument.uri, params.position, locations); server.findReferencesOf(params.textDocument.uri, params.position, locations);
reply(std::move(locations)); reply(std::move(locations));
@ -190,17 +186,16 @@ void LSPServer::Impl::onReference(const ReferenceParams &params,
//===----------------------------------------------------------------------===// //===----------------------------------------------------------------------===//
// Hover // Hover
void LSPServer::Impl::onHover(const TextDocumentPositionParams &params, void LSPServer::onHover(const TextDocumentPositionParams &params,
Callback<Optional<Hover>> reply) { Callback<Optional<Hover>> reply) {
reply(server.findHover(params.textDocument.uri, params.position)); reply(server.findHover(params.textDocument.uri, params.position));
} }
//===----------------------------------------------------------------------===// //===----------------------------------------------------------------------===//
// Document Symbols // Document Symbols
void LSPServer::Impl::onDocumentSymbol( void LSPServer::onDocumentSymbol(const DocumentSymbolParams &params,
const DocumentSymbolParams &params, Callback<std::vector<DocumentSymbol>> reply) {
Callback<std::vector<DocumentSymbol>> reply) {
std::vector<DocumentSymbol> symbols; std::vector<DocumentSymbol> symbols;
server.findDocumentSymbols(params.textDocument.uri, symbols); server.findDocumentSymbols(params.textDocument.uri, symbols);
reply(std::move(symbols)); reply(std::move(symbols));
@ -209,65 +204,64 @@ void LSPServer::Impl::onDocumentSymbol(
//===----------------------------------------------------------------------===// //===----------------------------------------------------------------------===//
// Code Completion // Code Completion
void LSPServer::Impl::onCompletion(const CompletionParams &params, void LSPServer::onCompletion(const CompletionParams &params,
Callback<CompletionList> reply) { Callback<CompletionList> reply) {
reply(server.getCodeCompletion(params.textDocument.uri, params.position)); reply(server.getCodeCompletion(params.textDocument.uri, params.position));
} }
//===----------------------------------------------------------------------===// //===----------------------------------------------------------------------===//
// LSPServer // Entry point
//===----------------------------------------------------------------------===// //===----------------------------------------------------------------------===//
LSPServer::LSPServer(MLIRServer &server, JSONTransport &transport) LogicalResult lsp::runMlirLSPServer(MLIRServer &server,
: impl(std::make_unique<Impl>(server, transport)) {} JSONTransport &transport) {
LSPServer::~LSPServer() = default; LSPServer lspServer(server);
MessageHandler messageHandler(transport);
LogicalResult LSPServer::run() {
MessageHandler messageHandler(impl->transport);
// Initialization // Initialization
messageHandler.method("initialize", impl.get(), &Impl::onInitialize); messageHandler.method("initialize", &lspServer, &LSPServer::onInitialize);
messageHandler.notification("initialized", impl.get(), &Impl::onInitialized); messageHandler.notification("initialized", &lspServer,
messageHandler.method("shutdown", impl.get(), &Impl::onShutdown); &LSPServer::onInitialized);
messageHandler.method("shutdown", &lspServer, &LSPServer::onShutdown);
// Document Changes // Document Changes
messageHandler.notification("textDocument/didOpen", impl.get(), messageHandler.notification("textDocument/didOpen", &lspServer,
&Impl::onDocumentDidOpen); &LSPServer::onDocumentDidOpen);
messageHandler.notification("textDocument/didClose", impl.get(), messageHandler.notification("textDocument/didClose", &lspServer,
&Impl::onDocumentDidClose); &LSPServer::onDocumentDidClose);
messageHandler.notification("textDocument/didChange", impl.get(), messageHandler.notification("textDocument/didChange", &lspServer,
&Impl::onDocumentDidChange); &LSPServer::onDocumentDidChange);
// Definitions and References // Definitions and References
messageHandler.method("textDocument/definition", impl.get(), messageHandler.method("textDocument/definition", &lspServer,
&Impl::onGoToDefinition); &LSPServer::onGoToDefinition);
messageHandler.method("textDocument/references", impl.get(), messageHandler.method("textDocument/references", &lspServer,
&Impl::onReference); &LSPServer::onReference);
// Hover // Hover
messageHandler.method("textDocument/hover", impl.get(), &Impl::onHover); messageHandler.method("textDocument/hover", &lspServer, &LSPServer::onHover);
// Document Symbols // Document Symbols
messageHandler.method("textDocument/documentSymbol", impl.get(), messageHandler.method("textDocument/documentSymbol", &lspServer,
&Impl::onDocumentSymbol); &LSPServer::onDocumentSymbol);
// Code Completion // Code Completion
messageHandler.method("textDocument/completion", impl.get(), messageHandler.method("textDocument/completion", &lspServer,
&Impl::onCompletion); &LSPServer::onCompletion);
// Diagnostics // Diagnostics
impl->publishDiagnostics = lspServer.publishDiagnostics =
messageHandler.outgoingNotification<PublishDiagnosticsParams>( messageHandler.outgoingNotification<PublishDiagnosticsParams>(
"textDocument/publishDiagnostics"); "textDocument/publishDiagnostics");
// Run the main loop of the transport. // Run the main loop of the transport.
LogicalResult result = success(); LogicalResult result = success();
if (llvm::Error error = impl->transport.run(messageHandler)) { if (llvm::Error error = transport.run(messageHandler)) {
Logger::error("Transport error: {0}", error); Logger::error("Transport error: {0}", error);
llvm::consumeError(std::move(error)); llvm::consumeError(std::move(error));
result = failure(); result = failure();
} else { } else {
result = success(impl->shutdownRequestReceived); result = success(lspServer.shutdownRequestReceived);
} }
return result; return result;
} }

View File

@ -18,22 +18,9 @@ namespace lsp {
class JSONTransport; class JSONTransport;
class MLIRServer; class MLIRServer;
/// This class represents the main LSP server, and handles communication with /// Run the main loop of the LSP server using the given MLIR server and
/// the LSP client. /// transport.
class LSPServer { LogicalResult runMlirLSPServer(MLIRServer &server, JSONTransport &transport);
public:
/// Construct a new language server with the given MLIR server.
LSPServer(MLIRServer &server, JSONTransport &transport);
~LSPServer();
/// Run the main loop of the server.
LogicalResult run();
private:
struct Impl;
std::unique_ptr<Impl> impl;
};
} // namespace lsp } // namespace lsp
} // namespace mlir } // namespace mlir

View File

@ -70,6 +70,5 @@ LogicalResult mlir::MlirLspServerMain(int argc, char **argv,
// Configure the servers and start the main language server. // Configure the servers and start the main language server.
MLIRServer server(registry); MLIRServer server(registry);
LSPServer lspServer(server, transport); return runMlirLSPServer(server, transport);
return lspServer.run();
} }