12 #include "llvm/Support/LSP/Logging.h"
13 #include "llvm/Support/LSP/Transport.h"
16 #define DEBUG_TYPE "mlir-lsp-server"
21 using llvm::lsp::Callback;
22 using llvm::lsp::CodeAction;
23 using llvm::lsp::CodeActionParams;
24 using llvm::lsp::CompletionList;
25 using llvm::lsp::CompletionParams;
26 using llvm::lsp::DidChangeTextDocumentParams;
27 using llvm::lsp::DidCloseTextDocumentParams;
28 using llvm::lsp::DidOpenTextDocumentParams;
29 using llvm::lsp::DocumentSymbol;
30 using llvm::lsp::DocumentSymbolParams;
31 using llvm::lsp::Hover;
32 using llvm::lsp::InitializedParams;
33 using llvm::lsp::InitializeParams;
34 using llvm::lsp::JSONTransport;
35 using llvm::lsp::Location;
36 using llvm::lsp::Logger;
37 using llvm::lsp::MessageHandler;
40 using llvm::lsp::NoParams;
41 using llvm::lsp::OutgoingNotification;
42 using llvm::lsp::PublishDiagnosticsParams;
43 using llvm::lsp::ReferenceParams;
44 using llvm::lsp::TextDocumentPositionParams;
45 using llvm::lsp::TextDocumentSyncKind;
46 using llvm::lsp::URIForFile;
54 LSPServer(
MLIRServer &server) : server(server) {}
59 void onInitialize(
const InitializeParams ¶ms,
60 Callback<llvm::json::Value> reply);
61 void onInitialized(
const InitializedParams ¶ms);
62 void onShutdown(
const NoParams ¶ms, Callback<std::nullptr_t> reply);
67 void onDocumentDidOpen(
const DidOpenTextDocumentParams ¶ms);
68 void onDocumentDidClose(
const DidCloseTextDocumentParams ¶ms);
69 void onDocumentDidChange(
const DidChangeTextDocumentParams ¶ms);
74 void onGoToDefinition(
const TextDocumentPositionParams ¶ms,
75 Callback<std::vector<Location>> reply);
76 void onReference(
const ReferenceParams ¶ms,
77 Callback<std::vector<Location>> reply);
82 void onHover(
const TextDocumentPositionParams ¶ms,
83 Callback<std::optional<Hover>> reply);
88 void onDocumentSymbol(
const DocumentSymbolParams ¶ms,
89 Callback<std::vector<DocumentSymbol>> reply);
94 void onCompletion(
const CompletionParams ¶ms,
95 Callback<CompletionList> reply);
100 void onCodeAction(
const CodeActionParams ¶ms,
101 Callback<llvm::json::Value> reply);
107 Callback<MLIRConvertBytecodeResult> reply);
109 Callback<MLIRConvertBytecodeResult> reply);
119 OutgoingNotification<PublishDiagnosticsParams> publishDiagnostics;
123 bool shutdownRequestReceived =
false;
131 void LSPServer::onInitialize(
const InitializeParams ¶ms,
132 Callback<llvm::json::Value> reply) {
134 llvm::json::Object serverCaps{
138 {
"change", (int)TextDocumentSyncKind::Full},
141 {
"completionProvider",
143 {
"allCommitCharacters",
151 {
"resolveProvider",
false},
152 {
"triggerCharacters",
153 {
".",
"%",
"^",
"!",
"#",
"(",
",",
"<",
":",
"[",
" ",
"\"",
"/"}},
155 {
"definitionProvider",
true},
156 {
"referencesProvider",
true},
157 {
"hoverProvider",
true},
161 {
"documentSymbolProvider",
162 params.capabilities.hierarchicalDocumentSymbol},
168 serverCaps[
"codeActionProvider"] =
169 params.capabilities.codeActionStructure
170 ? llvm::json::Object{{
"codeActionKinds",
171 {CodeAction::kQuickFix, CodeAction::kRefactor,
173 : llvm::json::Value(
true);
175 llvm::json::Object result{
177 llvm::json::Object{{
"name",
"mlir-lsp-server"}, {
"version",
"0.0.0"}}},
178 {
"capabilities", std::move(serverCaps)}}};
179 reply(std::move(result));
181 void LSPServer::onInitialized(
const InitializedParams &) {}
182 void LSPServer::onShutdown(
const NoParams &, Callback<std::nullptr_t> reply) {
183 shutdownRequestReceived =
true;
191 void LSPServer::onDocumentDidOpen(
const DidOpenTextDocumentParams ¶ms) {
192 PublishDiagnosticsParams diagParams(params.textDocument.uri,
193 params.textDocument.version);
194 server.addOrUpdateDocument(params.textDocument.uri, params.textDocument.text,
195 params.textDocument.version,
196 diagParams.diagnostics);
199 publishDiagnostics(diagParams);
201 void LSPServer::onDocumentDidClose(
const DidCloseTextDocumentParams ¶ms) {
202 std::optional<int64_t> version =
203 server.removeDocument(params.textDocument.uri);
211 PublishDiagnosticsParams(params.textDocument.uri, *version));
213 void LSPServer::onDocumentDidChange(
const DidChangeTextDocumentParams ¶ms) {
216 if (params.contentChanges.size() != 1)
218 PublishDiagnosticsParams diagParams(params.textDocument.uri,
219 params.textDocument.version);
220 server.addOrUpdateDocument(
221 params.textDocument.uri, params.contentChanges.front().text,
222 params.textDocument.version, diagParams.diagnostics);
225 publishDiagnostics(diagParams);
232 void LSPServer::onGoToDefinition(
const TextDocumentPositionParams ¶ms,
233 Callback<std::vector<Location>> reply) {
234 std::vector<Location> locations;
235 server.getLocationsOf(params.textDocument.uri, params.position, locations);
236 reply(std::move(locations));
239 void LSPServer::onReference(
const ReferenceParams ¶ms,
240 Callback<std::vector<Location>> reply) {
241 std::vector<Location> locations;
242 server.findReferencesOf(params.textDocument.uri, params.position, locations);
243 reply(std::move(locations));
250 void LSPServer::onHover(
const TextDocumentPositionParams ¶ms,
251 Callback<std::optional<Hover>> reply) {
252 reply(server.findHover(params.textDocument.uri, params.position));
259 void LSPServer::onDocumentSymbol(
const DocumentSymbolParams ¶ms,
260 Callback<std::vector<DocumentSymbol>> reply) {
261 std::vector<DocumentSymbol> symbols;
262 server.findDocumentSymbols(params.textDocument.uri, symbols);
263 reply(std::move(symbols));
270 void LSPServer::onCompletion(
const CompletionParams ¶ms,
271 Callback<CompletionList> reply) {
272 reply(server.getCodeCompletion(params.textDocument.uri, params.position));
279 void LSPServer::onCodeAction(
const CodeActionParams ¶ms,
280 Callback<llvm::json::Value> reply) {
281 URIForFile uri = params.textDocument.uri;
284 auto isKindAllowed = [only(params.context.only)](StringRef
kind) {
287 return llvm::any_of(only, [&](StringRef base) {
288 return kind.consume_front(base) &&
289 (
kind.empty() ||
kind.starts_with(
"."));
294 std::vector<CodeAction> actions;
295 if (isKindAllowed(CodeAction::kQuickFix))
296 server.getCodeActions(uri, params.range.start, params.context, actions);
297 reply(std::move(actions));
304 void LSPServer::onConvertFromBytecode(
306 Callback<MLIRConvertBytecodeResult> reply) {
307 reply(server.convertFromBytecode(params.
uri));
311 Callback<MLIRConvertBytecodeResult> reply) {
312 reply(server.convertToBytecode(params.
uri));
320 JSONTransport &transport) {
321 LSPServer lspServer(server);
322 MessageHandler messageHandler(transport);
325 messageHandler.method(
"initialize", &lspServer, &LSPServer::onInitialize);
326 messageHandler.notification(
"initialized", &lspServer,
327 &LSPServer::onInitialized);
328 messageHandler.method(
"shutdown", &lspServer, &LSPServer::onShutdown);
331 messageHandler.notification(
"textDocument/didOpen", &lspServer,
332 &LSPServer::onDocumentDidOpen);
333 messageHandler.notification(
"textDocument/didClose", &lspServer,
334 &LSPServer::onDocumentDidClose);
335 messageHandler.notification(
"textDocument/didChange", &lspServer,
336 &LSPServer::onDocumentDidChange);
339 messageHandler.method(
"textDocument/definition", &lspServer,
340 &LSPServer::onGoToDefinition);
341 messageHandler.method(
"textDocument/references", &lspServer,
342 &LSPServer::onReference);
345 messageHandler.method(
"textDocument/hover", &lspServer, &LSPServer::onHover);
348 messageHandler.method(
"textDocument/documentSymbol", &lspServer,
349 &LSPServer::onDocumentSymbol);
352 messageHandler.method(
"textDocument/completion", &lspServer,
353 &LSPServer::onCompletion);
356 messageHandler.method(
"textDocument/codeAction", &lspServer,
357 &LSPServer::onCodeAction);
360 messageHandler.method(
"mlir/convertFromBytecode", &lspServer,
361 &LSPServer::onConvertFromBytecode);
362 messageHandler.method(
"mlir/convertToBytecode", &lspServer,
363 &LSPServer::onConvertToBytecode);
366 lspServer.publishDiagnostics =
367 messageHandler.outgoingNotification<PublishDiagnosticsParams>(
368 "textDocument/publishDiagnostics");
371 LogicalResult result = success();
372 if (llvm::Error error = transport.run(messageHandler)) {
373 Logger::error(
"Transport error: {0}", error);
374 llvm::consumeError(std::move(error));
377 result = success(lspServer.shutdownRequestReceived);
union mlir::linalg::@1245::ArityGroupAndKind::Kind kind
This class implements all of the MLIR related functionality necessary for a language server.
llvm::LogicalResult runMlirLSPServer(MLIRServer &server, llvm::lsp::JSONTransport &transport)
Run the main loop of the LSP server using the given MLIR server and transport.
Include the generated interface declarations.
This class represents the parameters used when converting between MLIR's bytecode and textual format.
URIForFile uri
The input file containing the bytecode or textual format.
This class represents the result of converting between MLIR's bytecode and textual format.