12 #include "llvm/Support/LSP/Logging.h"
13 #include "llvm/Support/LSP/Protocol.h"
14 #include "llvm/Support/LSP/Transport.h"
20 using llvm::lsp::Callback;
21 using llvm::lsp::DidChangeTextDocumentParams;
22 using llvm::lsp::DidCloseTextDocumentParams;
23 using llvm::lsp::DidOpenTextDocumentParams;
24 using llvm::lsp::DocumentLinkParams;
25 using llvm::lsp::Hover;
26 using llvm::lsp::InitializedParams;
27 using llvm::lsp::InitializeParams;
28 using llvm::lsp::JSONTransport;
29 using llvm::lsp::Location;
30 using llvm::lsp::Logger;
31 using llvm::lsp::MessageHandler;
32 using llvm::lsp::NoParams;
33 using llvm::lsp::OutgoingNotification;
34 using llvm::lsp::PublishDiagnosticsParams;
35 using llvm::lsp::ReferenceParams;
36 using llvm::lsp::TextDocumentPositionParams;
37 using llvm::lsp::TextDocumentSyncKind;
46 : server(server), transport(transport) {}
51 void onInitialize(
const InitializeParams ¶ms,
52 Callback<llvm::json::Value> reply);
53 void onInitialized(
const InitializedParams ¶ms);
54 void onShutdown(
const NoParams ¶ms, Callback<std::nullptr_t> reply);
59 void onDocumentDidOpen(
const DidOpenTextDocumentParams ¶ms);
60 void onDocumentDidClose(
const DidCloseTextDocumentParams ¶ms);
61 void onDocumentDidChange(
const DidChangeTextDocumentParams ¶ms);
66 void onGoToDefinition(
const TextDocumentPositionParams ¶ms,
67 Callback<std::vector<Location>> reply);
68 void onReference(
const ReferenceParams ¶ms,
69 Callback<std::vector<Location>> reply);
74 void onDocumentLink(
const DocumentLinkParams ¶ms,
75 Callback<std::vector<DocumentLink>> reply);
80 void onHover(
const TextDocumentPositionParams ¶ms,
81 Callback<std::optional<Hover>> reply);
88 JSONTransport &transport;
92 OutgoingNotification<PublishDiagnosticsParams> publishDiagnostics;
96 bool shutdownRequestReceived =
false;
104 void LSPServer::onInitialize(
const InitializeParams ¶ms,
105 Callback<llvm::json::Value> reply) {
107 llvm::json::Object serverCaps{
111 {
"change", (int)TextDocumentSyncKind::Incremental},
114 {
"definitionProvider",
true},
115 {
"referencesProvider",
true},
116 {
"documentLinkProvider",
118 {
"resolveProvider",
false},
120 {
"hoverProvider",
true},
123 llvm::json::Object result{
124 {{
"serverInfo", llvm::json::Object{{
"name",
"tblgen-lsp-server"},
125 {
"version",
"0.0.1"}}},
126 {
"capabilities", std::move(serverCaps)}}};
127 reply(std::move(result));
129 void LSPServer::onInitialized(
const InitializedParams &) {}
130 void LSPServer::onShutdown(
const NoParams &, Callback<std::nullptr_t> reply) {
131 shutdownRequestReceived =
true;
139 void LSPServer::onDocumentDidOpen(
const DidOpenTextDocumentParams ¶ms) {
140 PublishDiagnosticsParams diagParams(params.textDocument.uri,
141 params.textDocument.version);
142 server.addDocument(params.textDocument.uri, params.textDocument.text,
143 params.textDocument.version, diagParams.diagnostics);
146 publishDiagnostics(diagParams);
148 void LSPServer::onDocumentDidClose(
const DidCloseTextDocumentParams ¶ms) {
149 std::optional<int64_t> version =
150 server.removeDocument(params.textDocument.uri);
158 PublishDiagnosticsParams(params.textDocument.uri, *version));
160 void LSPServer::onDocumentDidChange(
const DidChangeTextDocumentParams ¶ms) {
161 PublishDiagnosticsParams diagParams(params.textDocument.uri,
162 params.textDocument.version);
163 server.updateDocument(params.textDocument.uri, params.contentChanges,
164 params.textDocument.version, diagParams.diagnostics);
167 publishDiagnostics(diagParams);
174 void LSPServer::onGoToDefinition(
const TextDocumentPositionParams ¶ms,
175 Callback<std::vector<Location>> reply) {
176 std::vector<Location> locations;
177 server.getLocationsOf(params.textDocument.uri, params.position, locations);
178 reply(std::move(locations));
181 void LSPServer::onReference(
const ReferenceParams ¶ms,
182 Callback<std::vector<Location>> reply) {
183 std::vector<Location> locations;
184 server.findReferencesOf(params.textDocument.uri, params.position, locations);
185 reply(std::move(locations));
192 void LSPServer::onDocumentLink(
const DocumentLinkParams ¶ms,
193 Callback<std::vector<DocumentLink>> reply) {
194 std::vector<DocumentLink> links;
195 server.getDocumentLinks(params.textDocument.uri, links);
196 reply(std::move(links));
203 void LSPServer::onHover(
const TextDocumentPositionParams ¶ms,
204 Callback<std::optional<Hover>> reply) {
205 reply(server.findHover(params.textDocument.uri, params.position));
213 JSONTransport &transport) {
214 LSPServer lspServer(server, transport);
215 MessageHandler messageHandler(transport);
218 messageHandler.method(
"initialize", &lspServer, &LSPServer::onInitialize);
219 messageHandler.notification(
"initialized", &lspServer,
220 &LSPServer::onInitialized);
221 messageHandler.method(
"shutdown", &lspServer, &LSPServer::onShutdown);
224 messageHandler.notification(
"textDocument/didOpen", &lspServer,
225 &LSPServer::onDocumentDidOpen);
226 messageHandler.notification(
"textDocument/didClose", &lspServer,
227 &LSPServer::onDocumentDidClose);
228 messageHandler.notification(
"textDocument/didChange", &lspServer,
229 &LSPServer::onDocumentDidChange);
232 messageHandler.method(
"textDocument/definition", &lspServer,
233 &LSPServer::onGoToDefinition);
234 messageHandler.method(
"textDocument/references", &lspServer,
235 &LSPServer::onReference);
238 messageHandler.method(
"textDocument/documentLink", &lspServer,
239 &LSPServer::onDocumentLink);
242 messageHandler.method(
"textDocument/hover", &lspServer, &LSPServer::onHover);
245 lspServer.publishDiagnostics =
246 messageHandler.outgoingNotification<PublishDiagnosticsParams>(
247 "textDocument/publishDiagnostics");
250 if (llvm::Error error = transport.run(messageHandler)) {
251 Logger::error(
"Transport error: {0}", error);
252 llvm::consumeError(std::move(error));
255 return success(lspServer.shutdownRequestReceived);
This class implements all of the TableGen related functionality necessary for a language server.
llvm::LogicalResult runTableGenLSPServer(TableGenServer &server, llvm::lsp::JSONTransport &transport)
Run the main loop of the LSP server using the given TableGen server and transport.
Include the generated interface declarations.