1//===- LSPServer.cpp - TableGen 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
11#include "TableGenServer.h"
12#include "llvm/Support/LSP/Logging.h"
13#include "llvm/Support/LSP/Protocol.h"
14#include "llvm/Support/LSP/Transport.h"
15#include <optional>
16
17using namespace mlir;
18using namespace mlir::lsp;
19
20using llvm::lsp::Callback;
21using llvm::lsp::DidChangeTextDocumentParams;
22using llvm::lsp::DidCloseTextDocumentParams;
23using llvm::lsp::DidOpenTextDocumentParams;
24using llvm::lsp::DocumentLinkParams;
25using llvm::lsp::Hover;
26using llvm::lsp::InitializedParams;
27using llvm::lsp::InitializeParams;
28using llvm::lsp::JSONTransport;
29using llvm::lsp::Location;
30using llvm::lsp::Logger;
31using llvm::lsp::MessageHandler;
32using llvm::lsp::NoParams;
33using llvm::lsp::OutgoingNotification;
34using llvm::lsp::PublishDiagnosticsParams;
35using llvm::lsp::ReferenceParams;
36using llvm::lsp::TextDocumentPositionParams;
37using llvm::lsp::TextDocumentSyncKind;
38
39//===----------------------------------------------------------------------===//
40// LSPServer
41//===----------------------------------------------------------------------===//
42
43namespace {
44struct LSPServer {
45 LSPServer(TableGenServer &server, JSONTransport &transport)
46 : server(server), transport(transport) {}
47
48 //===--------------------------------------------------------------------===//
49 // Initialization
50
51 void onInitialize(const InitializeParams &params,
52 Callback<llvm::json::Value> reply);
53 void onInitialized(const InitializedParams &params);
54 void onShutdown(const NoParams &params, Callback<std::nullptr_t> reply);
55
56 //===--------------------------------------------------------------------===//
57 // Document Change
58
59 void onDocumentDidOpen(const DidOpenTextDocumentParams &params);
60 void onDocumentDidClose(const DidCloseTextDocumentParams &params);
61 void onDocumentDidChange(const DidChangeTextDocumentParams &params);
62
63 //===--------------------------------------------------------------------===//
64 // Definitions and References
65
66 void onGoToDefinition(const TextDocumentPositionParams &params,
67 Callback<std::vector<Location>> reply);
68 void onReference(const ReferenceParams &params,
69 Callback<std::vector<Location>> reply);
70
71 //===----------------------------------------------------------------------===//
72 // DocumentLink
73
74 void onDocumentLink(const DocumentLinkParams &params,
75 Callback<std::vector<DocumentLink>> reply);
76
77 //===--------------------------------------------------------------------===//
78 // Hover
79
80 void onHover(const TextDocumentPositionParams &params,
81 Callback<std::optional<Hover>> reply);
82
83 //===--------------------------------------------------------------------===//
84 // Fields
85 //===--------------------------------------------------------------------===//
86
87 TableGenServer &server;
88 JSONTransport &transport;
89
90 /// An outgoing notification used to send diagnostics to the client when they
91 /// are ready to be processed.
92 OutgoingNotification<PublishDiagnosticsParams> publishDiagnostics;
93
94 /// Used to indicate that the 'shutdown' request was received from the
95 /// Language Server client.
96 bool shutdownRequestReceived = false;
97};
98} // namespace
99
100//===----------------------------------------------------------------------===//
101// Initialization
102//===----------------------------------------------------------------------===//
103
104void LSPServer::onInitialize(const InitializeParams &params,
105 Callback<llvm::json::Value> reply) {
106 // Send a response with the capabilities of this server.
107 llvm::json::Object serverCaps{
108 {.K: "textDocumentSync",
109 .V: llvm::json::Object{
110 {.K: "openClose", .V: true},
111 {.K: "change", .V: (int)TextDocumentSyncKind::Incremental},
112 {.K: "save", .V: true},
113 }},
114 {.K: "definitionProvider", .V: true},
115 {.K: "referencesProvider", .V: true},
116 {.K: "documentLinkProvider",
117 .V: llvm::json::Object{
118 {.K: "resolveProvider", .V: false},
119 }},
120 {.K: "hoverProvider", .V: true},
121 };
122
123 llvm::json::Object result{
124 {{.K: "serverInfo", .V: llvm::json::Object{{.K: "name", .V: "tblgen-lsp-server"},
125 {.K: "version", .V: "0.0.1"}}},
126 {.K: "capabilities", .V: std::move(serverCaps)}}};
127 reply(std::move(result));
128}
129void LSPServer::onInitialized(const InitializedParams &) {}
130void LSPServer::onShutdown(const NoParams &, Callback<std::nullptr_t> reply) {
131 shutdownRequestReceived = true;
132 reply(nullptr);
133}
134
135//===----------------------------------------------------------------------===//
136// Document Change
137//===----------------------------------------------------------------------===//
138
139void LSPServer::onDocumentDidOpen(const DidOpenTextDocumentParams &params) {
140 PublishDiagnosticsParams diagParams(params.textDocument.uri,
141 params.textDocument.version);
142 server.addDocument(uri: params.textDocument.uri, contents: params.textDocument.text,
143 version: params.textDocument.version, diagnostics&: diagParams.diagnostics);
144
145 // Publish any recorded diagnostics.
146 publishDiagnostics(diagParams);
147}
148void LSPServer::onDocumentDidClose(const DidCloseTextDocumentParams &params) {
149 std::optional<int64_t> version =
150 server.removeDocument(uri: params.textDocument.uri);
151 if (!version)
152 return;
153
154 // Empty out the diagnostics shown for this document. This will clear out
155 // anything currently displayed by the client for this document (e.g. in the
156 // "Problems" pane of VSCode).
157 publishDiagnostics(
158 PublishDiagnosticsParams(params.textDocument.uri, *version));
159}
160void LSPServer::onDocumentDidChange(const DidChangeTextDocumentParams &params) {
161 PublishDiagnosticsParams diagParams(params.textDocument.uri,
162 params.textDocument.version);
163 server.updateDocument(uri: params.textDocument.uri, changes: params.contentChanges,
164 version: params.textDocument.version, diagnostics&: diagParams.diagnostics);
165
166 // Publish any recorded diagnostics.
167 publishDiagnostics(diagParams);
168}
169
170//===----------------------------------------------------------------------===//
171// Definitions and References
172//===----------------------------------------------------------------------===//
173
174void LSPServer::onGoToDefinition(const TextDocumentPositionParams &params,
175 Callback<std::vector<Location>> reply) {
176 std::vector<Location> locations;
177 server.getLocationsOf(uri: params.textDocument.uri, defPos: params.position, locations);
178 reply(std::move(locations));
179}
180
181void LSPServer::onReference(const ReferenceParams &params,
182 Callback<std::vector<Location>> reply) {
183 std::vector<Location> locations;
184 server.findReferencesOf(uri: params.textDocument.uri, pos: params.position, references&: locations);
185 reply(std::move(locations));
186}
187
188//===----------------------------------------------------------------------===//
189// DocumentLink
190//===----------------------------------------------------------------------===//
191
192void LSPServer::onDocumentLink(const DocumentLinkParams &params,
193 Callback<std::vector<DocumentLink>> reply) {
194 std::vector<DocumentLink> links;
195 server.getDocumentLinks(uri: params.textDocument.uri, documentLinks&: links);
196 reply(std::move(links));
197}
198
199//===----------------------------------------------------------------------===//
200// Hover
201//===----------------------------------------------------------------------===//
202
203void LSPServer::onHover(const TextDocumentPositionParams &params,
204 Callback<std::optional<Hover>> reply) {
205 reply(server.findHover(uri: params.textDocument.uri, hoverPos: params.position));
206}
207
208//===----------------------------------------------------------------------===//
209// Entry Point
210//===----------------------------------------------------------------------===//
211
212LogicalResult mlir::lsp::runTableGenLSPServer(TableGenServer &server,
213 JSONTransport &transport) {
214 LSPServer lspServer(server, transport);
215 MessageHandler messageHandler(transport);
216
217 // Initialization
218 messageHandler.method(Method: "initialize", ThisPtr: &lspServer, Handler: &LSPServer::onInitialize);
219 messageHandler.notification(Method: "initialized", ThisPtr: &lspServer,
220 Handler: &LSPServer::onInitialized);
221 messageHandler.method(Method: "shutdown", ThisPtr: &lspServer, Handler: &LSPServer::onShutdown);
222
223 // Document Changes
224 messageHandler.notification(Method: "textDocument/didOpen", ThisPtr: &lspServer,
225 Handler: &LSPServer::onDocumentDidOpen);
226 messageHandler.notification(Method: "textDocument/didClose", ThisPtr: &lspServer,
227 Handler: &LSPServer::onDocumentDidClose);
228 messageHandler.notification(Method: "textDocument/didChange", ThisPtr: &lspServer,
229 Handler: &LSPServer::onDocumentDidChange);
230
231 // Definitions and References
232 messageHandler.method(Method: "textDocument/definition", ThisPtr: &lspServer,
233 Handler: &LSPServer::onGoToDefinition);
234 messageHandler.method(Method: "textDocument/references", ThisPtr: &lspServer,
235 Handler: &LSPServer::onReference);
236
237 // Document Link
238 messageHandler.method(Method: "textDocument/documentLink", ThisPtr: &lspServer,
239 Handler: &LSPServer::onDocumentLink);
240
241 // Hover
242 messageHandler.method(Method: "textDocument/hover", ThisPtr: &lspServer, Handler: &LSPServer::onHover);
243
244 // Diagnostics
245 lspServer.publishDiagnostics =
246 messageHandler.outgoingNotification<PublishDiagnosticsParams>(
247 Method: "textDocument/publishDiagnostics");
248
249 // Run the main loop of the transport.
250 if (llvm::Error error = transport.run(Handler&: messageHandler)) {
251 Logger::error(Fmt: "Transport error: {0}", Vals&: error);
252 llvm::consumeError(Err: std::move(error));
253 return failure();
254 }
255 return success(IsSuccess: lspServer.shutdownRequestReceived);
256}
257