1 //===- LSPServer.cpp - MLIR Language Server -------------------------------===// 2 // 3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. 4 // See https://llvm.org/LICENSE.txt for license information. 5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception 6 // 7 //===----------------------------------------------------------------------===// 8 9 #include "LSPServer.h" 10 #include "MLIRServer.h" 11 #include "lsp/Logging.h" 12 #include "lsp/Protocol.h" 13 #include "lsp/Transport.h" 14 #include "llvm/ADT/FunctionExtras.h" 15 #include "llvm/ADT/StringMap.h" 16 17 #define DEBUG_TYPE "mlir-lsp-server" 18 19 using namespace mlir; 20 using namespace mlir::lsp; 21 22 //===----------------------------------------------------------------------===// 23 // LSPServer::Impl 24 //===----------------------------------------------------------------------===// 25 26 struct LSPServer::Impl { 27 Impl(MLIRServer &server, JSONTransport &transport) 28 : server(server), transport(transport) {} 29 30 //===--------------------------------------------------------------------===// 31 // Initialization 32 33 void onInitialize(const InitializeParams ¶ms, 34 Callback<llvm::json::Value> reply); 35 void onInitialized(const InitializedParams ¶ms); 36 void onShutdown(const NoParams ¶ms, Callback<std::nullptr_t> reply); 37 38 //===--------------------------------------------------------------------===// 39 // Document Change 40 41 void onDocumentDidOpen(const DidOpenTextDocumentParams ¶ms); 42 void onDocumentDidClose(const DidCloseTextDocumentParams ¶ms); 43 void onDocumentDidChange(const DidChangeTextDocumentParams ¶ms); 44 45 //===--------------------------------------------------------------------===// 46 // Definitions and References 47 48 void onGoToDefinition(const TextDocumentPositionParams ¶ms, 49 Callback<std::vector<Location>> reply); 50 void onReference(const ReferenceParams ¶ms, 51 Callback<std::vector<Location>> reply); 52 53 //===--------------------------------------------------------------------===// 54 // Hover 55 56 void onHover(const TextDocumentPositionParams ¶ms, 57 Callback<Optional<Hover>> reply); 58 59 MLIRServer &server; 60 JSONTransport &transport; 61 62 /// Used to indicate that the 'shutdown' request was received from the 63 /// Language Server client. 64 bool shutdownRequestReceived = false; 65 }; 66 67 //===----------------------------------------------------------------------===// 68 // Initialization 69 70 void LSPServer::Impl::onInitialize(const InitializeParams ¶ms, 71 Callback<llvm::json::Value> reply) { 72 llvm::json::Object serverCaps{ 73 {"textDocumentSync", 74 llvm::json::Object{ 75 {"openClose", true}, 76 {"change", (int)TextDocumentSyncKind::Full}, 77 {"save", true}, 78 }}, 79 {"definitionProvider", true}, 80 {"referencesProvider", true}, 81 {"hoverProvider", true}, 82 }; 83 84 llvm::json::Object result{ 85 {{"serverInfo", 86 llvm::json::Object{{"name", "mlir-lsp-server"}, {"version", "0.0.0"}}}, 87 {"capabilities", std::move(serverCaps)}}}; 88 reply(std::move(result)); 89 } 90 void LSPServer::Impl::onInitialized(const InitializedParams &) {} 91 void LSPServer::Impl::onShutdown(const NoParams &, 92 Callback<std::nullptr_t> reply) { 93 shutdownRequestReceived = true; 94 reply(nullptr); 95 } 96 97 //===----------------------------------------------------------------------===// 98 // Document Change 99 100 void LSPServer::Impl::onDocumentDidOpen( 101 const DidOpenTextDocumentParams ¶ms) { 102 server.addOrUpdateDocument(params.textDocument.uri, params.textDocument.text); 103 } 104 void LSPServer::Impl::onDocumentDidClose( 105 const DidCloseTextDocumentParams ¶ms) { 106 server.removeDocument(params.textDocument.uri); 107 } 108 void LSPServer::Impl::onDocumentDidChange( 109 const DidChangeTextDocumentParams ¶ms) { 110 // TODO: We currently only support full document updates, we should refactor 111 // to avoid this. 112 if (params.contentChanges.size() != 1) 113 return; 114 server.addOrUpdateDocument(params.textDocument.uri, 115 params.contentChanges.front().text); 116 } 117 118 //===----------------------------------------------------------------------===// 119 // Definitions and References 120 121 void LSPServer::Impl::onGoToDefinition(const TextDocumentPositionParams ¶ms, 122 Callback<std::vector<Location>> reply) { 123 std::vector<Location> locations; 124 server.getLocationsOf(params.textDocument.uri, params.position, locations); 125 reply(std::move(locations)); 126 } 127 128 void LSPServer::Impl::onReference(const ReferenceParams ¶ms, 129 Callback<std::vector<Location>> reply) { 130 std::vector<Location> locations; 131 server.findReferencesOf(params.textDocument.uri, params.position, locations); 132 reply(std::move(locations)); 133 } 134 135 //===----------------------------------------------------------------------===// 136 // Hover 137 138 void LSPServer::Impl::onHover(const TextDocumentPositionParams ¶ms, 139 Callback<Optional<Hover>> reply) { 140 reply(server.findHover(params.textDocument.uri, params.position)); 141 } 142 143 //===----------------------------------------------------------------------===// 144 // LSPServer 145 //===----------------------------------------------------------------------===// 146 147 LSPServer::LSPServer(MLIRServer &server, JSONTransport &transport) 148 : impl(std::make_unique<Impl>(server, transport)) {} 149 LSPServer::~LSPServer() {} 150 151 LogicalResult LSPServer::run() { 152 MessageHandler messageHandler(impl->transport); 153 154 // Initialization 155 messageHandler.method("initialize", impl.get(), &Impl::onInitialize); 156 messageHandler.notification("initialized", impl.get(), &Impl::onInitialized); 157 messageHandler.method("shutdown", impl.get(), &Impl::onShutdown); 158 159 // Document Changes 160 messageHandler.notification("textDocument/didOpen", impl.get(), 161 &Impl::onDocumentDidOpen); 162 messageHandler.notification("textDocument/didClose", impl.get(), 163 &Impl::onDocumentDidClose); 164 messageHandler.notification("textDocument/didChange", impl.get(), 165 &Impl::onDocumentDidChange); 166 167 // Definitions and References 168 messageHandler.method("textDocument/definition", impl.get(), 169 &Impl::onGoToDefinition); 170 messageHandler.method("textDocument/references", impl.get(), 171 &Impl::onReference); 172 173 // Hover 174 messageHandler.method("textDocument/hover", impl.get(), &Impl::onHover); 175 176 // Run the main loop of the transport. 177 LogicalResult result = success(); 178 if (llvm::Error error = impl->transport.run(messageHandler)) { 179 Logger::error("Transport error: {0}", error); 180 llvm::consumeError(std::move(error)); 181 result = failure(); 182 } else { 183 result = success(impl->shutdownRequestReceived); 184 } 185 return result; 186 } 187