13 #include "llvm/Support/LSP/Logging.h"
14 #include "llvm/Support/LSP/Protocol.h"
15 #include "llvm/Support/LSP/Transport.h"
18 #define DEBUG_TYPE "pdll-lsp-server"
23 using llvm::lsp::Callback;
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::DocumentLinkParams;
30 using llvm::lsp::DocumentSymbol;
31 using llvm::lsp::DocumentSymbolParams;
32 using llvm::lsp::Hover;
33 using llvm::lsp::InitializedParams;
34 using llvm::lsp::InitializeParams;
35 using llvm::lsp::InlayHintsParams;
36 using llvm::lsp::JSONTransport;
37 using llvm::lsp::Location;
38 using llvm::lsp::Logger;
39 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;
53 LSPServer(
PDLLServer &server, JSONTransport &transport)
54 : server(server), transport(transport) {}
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 onDocumentLink(
const DocumentLinkParams ¶ms,
83 Callback<std::vector<DocumentLink>> reply);
88 void onHover(
const TextDocumentPositionParams ¶ms,
89 Callback<std::optional<Hover>> reply);
94 void onDocumentSymbol(
const DocumentSymbolParams ¶ms,
95 Callback<std::vector<DocumentSymbol>> reply);
100 void onCompletion(
const CompletionParams ¶ms,
101 Callback<CompletionList> reply);
106 void onSignatureHelp(
const TextDocumentPositionParams ¶ms,
107 Callback<SignatureHelp> reply);
112 void onInlayHint(
const InlayHintsParams ¶ms,
113 Callback<std::vector<InlayHint>> reply);
119 Callback<std::optional<PDLLViewOutputResult>> reply);
126 JSONTransport &transport;
130 OutgoingNotification<PublishDiagnosticsParams> publishDiagnostics;
134 bool shutdownRequestReceived =
false;
142 void LSPServer::onInitialize(
const InitializeParams ¶ms,
143 Callback<llvm::json::Value> reply) {
145 llvm::json::Object serverCaps{
149 {
"change", (int)TextDocumentSyncKind::Incremental},
152 {
"completionProvider",
154 {
"allCommitCharacters",
155 {
"\t",
"(",
")",
"[",
"]",
"{",
"}",
"<",
">",
156 ":",
";",
",",
"+",
"-",
"/",
"*",
"%",
"^",
157 "&",
"#",
"?",
".",
"=",
"\"",
"'",
"|"}},
158 {
"resolveProvider",
false},
159 {
"triggerCharacters",
160 {
".",
">",
"(",
"{",
",",
"<",
":",
"[",
" ",
"\"",
"/"}},
162 {
"signatureHelpProvider",
164 {
"triggerCharacters", {
"(",
","}},
166 {
"definitionProvider",
true},
167 {
"referencesProvider",
true},
168 {
"documentLinkProvider",
170 {
"resolveProvider",
false},
172 {
"hoverProvider",
true},
173 {
"documentSymbolProvider",
true},
174 {
"inlayHintProvider",
true},
177 llvm::json::Object result{
178 {{
"serverInfo", llvm::json::Object{{
"name",
"mlir-pdll-lsp-server"},
179 {
"version",
"0.0.1"}}},
180 {
"capabilities", std::move(serverCaps)}}};
181 reply(std::move(result));
183 void LSPServer::onInitialized(
const InitializedParams &) {}
184 void LSPServer::onShutdown(
const NoParams &, Callback<std::nullptr_t> reply) {
185 shutdownRequestReceived =
true;
193 void LSPServer::onDocumentDidOpen(
const DidOpenTextDocumentParams ¶ms) {
194 PublishDiagnosticsParams diagParams(params.textDocument.uri,
195 params.textDocument.version);
196 server.addDocument(params.textDocument.uri, params.textDocument.text,
197 params.textDocument.version, diagParams.diagnostics);
200 publishDiagnostics(diagParams);
202 void LSPServer::onDocumentDidClose(
const DidCloseTextDocumentParams ¶ms) {
203 std::optional<int64_t> version =
204 server.removeDocument(params.textDocument.uri);
212 PublishDiagnosticsParams(params.textDocument.uri, *version));
214 void LSPServer::onDocumentDidChange(
const DidChangeTextDocumentParams ¶ms) {
215 PublishDiagnosticsParams diagParams(params.textDocument.uri,
216 params.textDocument.version);
217 server.updateDocument(params.textDocument.uri, params.contentChanges,
218 params.textDocument.version, diagParams.diagnostics);
221 publishDiagnostics(diagParams);
228 void LSPServer::onGoToDefinition(
const TextDocumentPositionParams ¶ms,
229 Callback<std::vector<Location>> reply) {
230 std::vector<Location> locations;
231 server.getLocationsOf(params.textDocument.uri, params.position, locations);
232 reply(std::move(locations));
235 void LSPServer::onReference(
const ReferenceParams ¶ms,
236 Callback<std::vector<Location>> reply) {
237 std::vector<Location> locations;
238 server.findReferencesOf(params.textDocument.uri, params.position, locations);
239 reply(std::move(locations));
246 void LSPServer::onDocumentLink(
const DocumentLinkParams ¶ms,
247 Callback<std::vector<DocumentLink>> reply) {
248 std::vector<DocumentLink> links;
249 server.getDocumentLinks(params.textDocument.uri, links);
250 reply(std::move(links));
257 void LSPServer::onHover(
const TextDocumentPositionParams ¶ms,
258 Callback<std::optional<Hover>> reply) {
259 reply(server.findHover(params.textDocument.uri, params.position));
266 void LSPServer::onDocumentSymbol(
const DocumentSymbolParams ¶ms,
267 Callback<std::vector<DocumentSymbol>> reply) {
268 std::vector<DocumentSymbol> symbols;
269 server.findDocumentSymbols(params.textDocument.uri, symbols);
270 reply(std::move(symbols));
277 void LSPServer::onCompletion(
const CompletionParams ¶ms,
278 Callback<CompletionList> reply) {
279 reply(server.getCodeCompletion(params.textDocument.uri, params.position));
286 void LSPServer::onSignatureHelp(
const TextDocumentPositionParams ¶ms,
287 Callback<SignatureHelp> reply) {
288 reply(server.getSignatureHelp(params.textDocument.uri, params.position));
295 void LSPServer::onInlayHint(
const InlayHintsParams ¶ms,
296 Callback<std::vector<InlayHint>> reply) {
297 std::vector<InlayHint> hints;
298 server.getInlayHints(params.textDocument.uri, params.range, hints);
299 reply(std::move(hints));
306 void LSPServer::onPDLLViewOutput(
308 Callback<std::optional<PDLLViewOutputResult>> reply) {
309 reply(server.getPDLLViewOutput(params.
uri, params.
kind));
317 JSONTransport &transport) {
318 LSPServer lspServer(server, transport);
319 MessageHandler messageHandler(transport);
322 messageHandler.method(
"initialize", &lspServer, &LSPServer::onInitialize);
323 messageHandler.notification(
"initialized", &lspServer,
324 &LSPServer::onInitialized);
325 messageHandler.method(
"shutdown", &lspServer, &LSPServer::onShutdown);
328 messageHandler.notification(
"textDocument/didOpen", &lspServer,
329 &LSPServer::onDocumentDidOpen);
330 messageHandler.notification(
"textDocument/didClose", &lspServer,
331 &LSPServer::onDocumentDidClose);
332 messageHandler.notification(
"textDocument/didChange", &lspServer,
333 &LSPServer::onDocumentDidChange);
336 messageHandler.method(
"textDocument/definition", &lspServer,
337 &LSPServer::onGoToDefinition);
338 messageHandler.method(
"textDocument/references", &lspServer,
339 &LSPServer::onReference);
342 messageHandler.method(
"textDocument/documentLink", &lspServer,
343 &LSPServer::onDocumentLink);
346 messageHandler.method(
"textDocument/hover", &lspServer, &LSPServer::onHover);
349 messageHandler.method(
"textDocument/documentSymbol", &lspServer,
350 &LSPServer::onDocumentSymbol);
353 messageHandler.method(
"textDocument/completion", &lspServer,
354 &LSPServer::onCompletion);
357 messageHandler.method(
"textDocument/signatureHelp", &lspServer,
358 &LSPServer::onSignatureHelp);
361 messageHandler.method(
"textDocument/inlayHint", &lspServer,
362 &LSPServer::onInlayHint);
365 messageHandler.method(
"pdll/viewOutput", &lspServer,
366 &LSPServer::onPDLLViewOutput);
369 lspServer.publishDiagnostics =
370 messageHandler.outgoingNotification<PublishDiagnosticsParams>(
371 "textDocument/publishDiagnostics");
374 if (llvm::Error error = transport.run(messageHandler)) {
375 Logger::error(
"Transport error: {0}", error);
376 llvm::consumeError(std::move(error));
379 return success(lspServer.shutdownRequestReceived);
This class implements all of the PDLL related functionality necessary for a language server.
llvm::LogicalResult runPdllLSPServer(PDLLServer &server, llvm::lsp::JSONTransport &transport)
Run the main loop of the LSP server using the given PDLL server and transport.
Include the generated interface declarations.
Represents the parameters used when viewing the output of a PDLL file.
URIForFile uri
The URI of the document to view the output of.
PDLLViewOutputKind kind
The kind of output to generate.