1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346
|
//===- LSPServer.cpp - MLIR Language Server -------------------------------===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//
#include "LSPServer.h"
#include "MLIRServer.h"
#include "Protocol.h"
#include "mlir/Tools/lsp-server-support/Logging.h"
#include "mlir/Tools/lsp-server-support/Transport.h"
#include "llvm/ADT/FunctionExtras.h"
#include "llvm/ADT/StringMap.h"
#include <optional>
#define DEBUG_TYPE "mlir-lsp-server"
using namespace mlir;
using namespace mlir::lsp;
//===----------------------------------------------------------------------===//
// LSPServer
//===----------------------------------------------------------------------===//
namespace {
struct LSPServer {
LSPServer(MLIRServer &server) : server(server) {}
//===--------------------------------------------------------------------===//
// Initialization
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);
//===--------------------------------------------------------------------===//
// Document Change
void onDocumentDidOpen(const DidOpenTextDocumentParams ¶ms);
void onDocumentDidClose(const DidCloseTextDocumentParams ¶ms);
void onDocumentDidChange(const DidChangeTextDocumentParams ¶ms);
//===--------------------------------------------------------------------===//
// Definitions and References
void onGoToDefinition(const TextDocumentPositionParams ¶ms,
Callback<std::vector<Location>> reply);
void onReference(const ReferenceParams ¶ms,
Callback<std::vector<Location>> reply);
//===--------------------------------------------------------------------===//
// Hover
void onHover(const TextDocumentPositionParams ¶ms,
Callback<std::optional<Hover>> reply);
//===--------------------------------------------------------------------===//
// Document Symbols
void onDocumentSymbol(const DocumentSymbolParams ¶ms,
Callback<std::vector<DocumentSymbol>> reply);
//===--------------------------------------------------------------------===//
// Code Completion
void onCompletion(const CompletionParams ¶ms,
Callback<CompletionList> reply);
//===--------------------------------------------------------------------===//
// Code Action
void onCodeAction(const CodeActionParams ¶ms,
Callback<llvm::json::Value> reply);
//===--------------------------------------------------------------------===//
// Bytecode
void onConvertFromBytecode(const MLIRConvertBytecodeParams ¶ms,
Callback<MLIRConvertBytecodeResult> reply);
void onConvertToBytecode(const MLIRConvertBytecodeParams ¶ms,
Callback<MLIRConvertBytecodeResult> reply);
//===--------------------------------------------------------------------===//
// Fields
//===--------------------------------------------------------------------===//
MLIRServer &server;
/// An outgoing notification used to send diagnostics to the client when they
/// are ready to be processed.
OutgoingNotification<PublishDiagnosticsParams> publishDiagnostics;
/// Used to indicate that the 'shutdown' request was received from the
/// Language Server client.
bool shutdownRequestReceived = false;
};
} // namespace
//===----------------------------------------------------------------------===//
// Initialization
void LSPServer::onInitialize(const InitializeParams ¶ms,
Callback<llvm::json::Value> reply) {
// Send a response with the capabilities of this server.
llvm::json::Object serverCaps{
{"textDocumentSync",
llvm::json::Object{
{"openClose", true},
{"change", (int)TextDocumentSyncKind::Full},
{"save", true},
}},
{"completionProvider",
llvm::json::Object{
{"allCommitCharacters",
{
"\t",
";",
",",
".",
"=",
}},
{"resolveProvider", false},
{"triggerCharacters",
{".", "%", "^", "!", "#", "(", ",", "<", ":", "[", " ", "\"", "/"}},
}},
{"definitionProvider", true},
{"referencesProvider", true},
{"hoverProvider", true},
// For now we only support documenting symbols when the client supports
// hierarchical symbols.
{"documentSymbolProvider",
params.capabilities.hierarchicalDocumentSymbol},
};
// Per LSP, codeActionProvider can be either boolean or CodeActionOptions.
// CodeActionOptions is only valid if the client supports action literal
// via textDocument.codeAction.codeActionLiteralSupport.
serverCaps["codeActionProvider"] =
params.capabilities.codeActionStructure
? llvm::json::Object{{"codeActionKinds",
{CodeAction::kQuickFix, CodeAction::kRefactor,
CodeAction::kInfo}}}
: llvm::json::Value(true);
llvm::json::Object result{
{{"serverInfo",
llvm::json::Object{{"name", "mlir-lsp-server"}, {"version", "0.0.0"}}},
{"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);
}
//===----------------------------------------------------------------------===//
// Document Change
void LSPServer::onDocumentDidOpen(const DidOpenTextDocumentParams ¶ms) {
PublishDiagnosticsParams diagParams(params.textDocument.uri,
params.textDocument.version);
server.addOrUpdateDocument(params.textDocument.uri, params.textDocument.text,
params.textDocument.version,
diagParams.diagnostics);
// Publish any recorded diagnostics.
publishDiagnostics(diagParams);
}
void LSPServer::onDocumentDidClose(const DidCloseTextDocumentParams ¶ms) {
std::optional<int64_t> version =
server.removeDocument(params.textDocument.uri);
if (!version)
return;
// Empty out the diagnostics shown for this document. This will clear out
// anything currently displayed by the client for this document (e.g. in the
// "Problems" pane of VSCode).
publishDiagnostics(
PublishDiagnosticsParams(params.textDocument.uri, *version));
}
void LSPServer::onDocumentDidChange(const DidChangeTextDocumentParams ¶ms) {
// TODO: We currently only support full document updates, we should refactor
// to avoid this.
if (params.contentChanges.size() != 1)
return;
PublishDiagnosticsParams diagParams(params.textDocument.uri,
params.textDocument.version);
server.addOrUpdateDocument(
params.textDocument.uri, params.contentChanges.front().text,
params.textDocument.version, diagParams.diagnostics);
// Publish any recorded diagnostics.
publishDiagnostics(diagParams);
}
//===----------------------------------------------------------------------===//
// Definitions and References
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));
}
//===----------------------------------------------------------------------===//
// Hover
void LSPServer::onHover(const TextDocumentPositionParams ¶ms,
Callback<std::optional<Hover>> reply) {
reply(server.findHover(params.textDocument.uri, params.position));
}
//===----------------------------------------------------------------------===//
// Document Symbols
void LSPServer::onDocumentSymbol(const DocumentSymbolParams ¶ms,
Callback<std::vector<DocumentSymbol>> reply) {
std::vector<DocumentSymbol> symbols;
server.findDocumentSymbols(params.textDocument.uri, symbols);
reply(std::move(symbols));
}
//===----------------------------------------------------------------------===//
// Code Completion
void LSPServer::onCompletion(const CompletionParams ¶ms,
Callback<CompletionList> reply) {
reply(server.getCodeCompletion(params.textDocument.uri, params.position));
}
//===----------------------------------------------------------------------===//
// Code Action
void LSPServer::onCodeAction(const CodeActionParams ¶ms,
Callback<llvm::json::Value> reply) {
URIForFile uri = params.textDocument.uri;
// Check whether a particular CodeActionKind is included in the response.
auto isKindAllowed = [only(params.context.only)](StringRef kind) {
if (only.empty())
return true;
return llvm::any_of(only, [&](StringRef base) {
return kind.consume_front(base) && (kind.empty() || kind.startswith("."));
});
};
// We provide a code action for fixes on the specified diagnostics.
std::vector<CodeAction> actions;
if (isKindAllowed(CodeAction::kQuickFix))
server.getCodeActions(uri, params.range.start, params.context, actions);
reply(std::move(actions));
}
//===----------------------------------------------------------------------===//
// Bytecode
void LSPServer::onConvertFromBytecode(
const MLIRConvertBytecodeParams ¶ms,
Callback<MLIRConvertBytecodeResult> reply) {
reply(server.convertFromBytecode(params.uri));
}
void LSPServer::onConvertToBytecode(const MLIRConvertBytecodeParams ¶ms,
Callback<MLIRConvertBytecodeResult> reply) {
reply(server.convertToBytecode(params.uri));
}
//===----------------------------------------------------------------------===//
// Entry point
//===----------------------------------------------------------------------===//
LogicalResult lsp::runMlirLSPServer(MLIRServer &server,
JSONTransport &transport) {
LSPServer lspServer(server);
MessageHandler messageHandler(transport);
// Initialization
messageHandler.method("initialize", &lspServer, &LSPServer::onInitialize);
messageHandler.notification("initialized", &lspServer,
&LSPServer::onInitialized);
messageHandler.method("shutdown", &lspServer, &LSPServer::onShutdown);
// Document Changes
messageHandler.notification("textDocument/didOpen", &lspServer,
&LSPServer::onDocumentDidOpen);
messageHandler.notification("textDocument/didClose", &lspServer,
&LSPServer::onDocumentDidClose);
messageHandler.notification("textDocument/didChange", &lspServer,
&LSPServer::onDocumentDidChange);
// Definitions and References
messageHandler.method("textDocument/definition", &lspServer,
&LSPServer::onGoToDefinition);
messageHandler.method("textDocument/references", &lspServer,
&LSPServer::onReference);
// Hover
messageHandler.method("textDocument/hover", &lspServer, &LSPServer::onHover);
// Document Symbols
messageHandler.method("textDocument/documentSymbol", &lspServer,
&LSPServer::onDocumentSymbol);
// Code Completion
messageHandler.method("textDocument/completion", &lspServer,
&LSPServer::onCompletion);
// Code Action
messageHandler.method("textDocument/codeAction", &lspServer,
&LSPServer::onCodeAction);
// Bytecode
messageHandler.method("mlir/convertFromBytecode", &lspServer,
&LSPServer::onConvertFromBytecode);
messageHandler.method("mlir/convertToBytecode", &lspServer,
&LSPServer::onConvertToBytecode);
// Diagnostics
lspServer.publishDiagnostics =
messageHandler.outgoingNotification<PublishDiagnosticsParams>(
"textDocument/publishDiagnostics");
// Run the main loop of the transport.
LogicalResult result = success();
if (llvm::Error error = transport.run(messageHandler)) {
Logger::error("Transport error: {0}", error);
llvm::consumeError(std::move(error));
result = failure();
} else {
result = success(lspServer.shutdownRequestReceived);
}
return result;
}
|