14 #include "llvm/ADT/FunctionExtras.h"
15 #include "llvm/ADT/StringMap.h"
18 #define DEBUG_TYPE "mlir-lsp-server"
29 LSPServer(
MLIRServer &server) : server(server) {}
50 Callback<std::vector<Location>> reply);
52 Callback<std::vector<Location>> reply);
58 Callback<std::optional<Hover>> reply);
64 Callback<std::vector<DocumentSymbol>> reply);
98 bool shutdownRequestReceived =
false;
108 llvm::json::Object serverCaps{
115 {
"completionProvider",
117 {
"allCommitCharacters",
125 {
"resolveProvider",
false},
126 {
"triggerCharacters",
127 {
".",
"%",
"^",
"!",
"#",
"(",
",",
"<",
":",
"[",
" ",
"\"",
"/"}},
129 {
"definitionProvider",
true},
130 {
"referencesProvider",
true},
131 {
"hoverProvider",
true},
135 {
"documentSymbolProvider",
142 serverCaps[
"codeActionProvider"] =
144 ? llvm::json::Object{{
"codeActionKinds",
147 : llvm::json::Value(
true);
149 llvm::json::Object result{
151 llvm::json::Object{{
"name",
"mlir-lsp-server"}, {
"version",
"0.0.0"}}},
152 {
"capabilities", std::move(serverCaps)}}};
153 reply(std::move(result));
157 shutdownRequestReceived =
true;
169 diagParams.diagnostics);
172 publishDiagnostics(diagParams);
175 std::optional<int64_t> version =
193 server.addOrUpdateDocument(
198 publishDiagnostics(diagParams);
205 Callback<std::vector<Location>> reply) {
206 std::vector<Location> locations;
208 reply(std::move(locations));
212 Callback<std::vector<Location>> reply) {
213 std::vector<Location> locations;
215 reply(std::move(locations));
222 Callback<std::optional<Hover>> reply) {
230 Callback<std::vector<DocumentSymbol>> reply) {
231 std::vector<DocumentSymbol> symbols;
233 reply(std::move(symbols));
252 auto isKindAllowed = [only(params.
context.
only)](StringRef kind) {
255 return llvm::any_of(only, [&](StringRef base) {
256 return kind.consume_front(base) &&
257 (kind.empty() || kind.starts_with(
"."));
262 std::vector<CodeAction> actions;
265 reply(std::move(actions));
271 void LSPServer::onConvertFromBytecode(
274 reply(server.convertFromBytecode(params.
uri));
279 reply(server.convertToBytecode(params.
uri));
288 LSPServer lspServer(server);
292 messageHandler.
method(
"initialize", &lspServer, &LSPServer::onInitialize);
294 &LSPServer::onInitialized);
295 messageHandler.
method(
"shutdown", &lspServer, &LSPServer::onShutdown);
298 messageHandler.
notification(
"textDocument/didOpen", &lspServer,
299 &LSPServer::onDocumentDidOpen);
300 messageHandler.
notification(
"textDocument/didClose", &lspServer,
301 &LSPServer::onDocumentDidClose);
302 messageHandler.
notification(
"textDocument/didChange", &lspServer,
303 &LSPServer::onDocumentDidChange);
306 messageHandler.
method(
"textDocument/definition", &lspServer,
307 &LSPServer::onGoToDefinition);
308 messageHandler.
method(
"textDocument/references", &lspServer,
309 &LSPServer::onReference);
312 messageHandler.
method(
"textDocument/hover", &lspServer, &LSPServer::onHover);
315 messageHandler.
method(
"textDocument/documentSymbol", &lspServer,
316 &LSPServer::onDocumentSymbol);
319 messageHandler.
method(
"textDocument/completion", &lspServer,
320 &LSPServer::onCompletion);
323 messageHandler.
method(
"textDocument/codeAction", &lspServer,
324 &LSPServer::onCodeAction);
327 messageHandler.
method(
"mlir/convertFromBytecode", &lspServer,
328 &LSPServer::onConvertFromBytecode);
329 messageHandler.
method(
"mlir/convertToBytecode", &lspServer,
330 &LSPServer::onConvertToBytecode);
333 lspServer.publishDiagnostics =
335 "textDocument/publishDiagnostics");
338 LogicalResult result = success();
339 if (llvm::Error error = transport.
run(messageHandler)) {
341 llvm::consumeError(std::move(error));
344 result = success(lspServer.shutdownRequestReceived);
A transport class that performs the JSON-RPC communication with the LSP client.
llvm::Error run(MessageHandler &handler)
Start executing the JSON-RPC transport.
static void error(const char *fmt, Ts &&...vals)
This class implements all of the MLIR related functionality necessary for a language server.
A handler used to process the incoming transport messages.
void notification(llvm::StringLiteral method, ThisT *thisPtr, void(ThisT::*handler)(const Param &))
void method(llvm::StringLiteral method, ThisT *thisPtr, void(ThisT::*handler)(const Param &, Callback< Result >))
OutgoingNotification< T > outgoingNotification(llvm::StringLiteral method)
Create an OutgoingNotification object used for the given method.
URI in "file" scheme for a file.
llvm::unique_function< void(const T &)> OutgoingNotification
An OutgoingNotification<T> is a function used for outgoing notifications send to the client.
llvm::unique_function< void(llvm::Expected< T >)> Callback
A Callback<T> is a void function that accepts Expected<T>.
@ Full
Documents are synced by always sending the full content of the document.
llvm::LogicalResult runMlirLSPServer(MLIRServer &server, JSONTransport &transport)
Run the main loop of the LSP server using the given MLIR server and transport.
Include the generated interface declarations.
bool codeActionStructure
Client supports CodeAction return value for textDocument/codeAction.
bool hierarchicalDocumentSymbol
Client supports hierarchical document symbols.
std::vector< std::string > only
Requested kind of actions to return.
TextDocumentIdentifier textDocument
The document in which the command was invoked.
Range range
The range for which the command was invoked.
CodeActionContext context
Context carrying additional information.
static const llvm::StringLiteral kRefactor
static const llvm::StringLiteral kInfo
static const llvm::StringLiteral kQuickFix
VersionedTextDocumentIdentifier textDocument
The document that changed.
std::vector< TextDocumentContentChangeEvent > contentChanges
The actual content changes.
TextDocumentIdentifier textDocument
The document that was closed.
TextDocumentItem textDocument
The document that was opened.
TextDocumentIdentifier textDocument
ClientCapabilities capabilities
The capabilities provided by the client (editor or tool).
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.
Position start
The range's start position.
URIForFile uri
The text document's URI.
int64_t version
The version number of this document.
std::string text
The content of the opened text document.
URIForFile uri
The text document's URI.
TextDocumentIdentifier textDocument
The text document.
Position position
The position inside the text document.
int64_t version
The version number of this document.
URIForFile uri
The text document's URI.