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) && (kind.empty() || kind.startswith(
"."));
261 std::vector<CodeAction> actions;
264 reply(std::move(actions));
270 void LSPServer::onConvertFromBytecode(
273 reply(server.convertFromBytecode(params.
uri));
278 reply(server.convertToBytecode(params.
uri));
287 LSPServer lspServer(server);
291 messageHandler.
method(
"initialize", &lspServer, &LSPServer::onInitialize);
293 &LSPServer::onInitialized);
294 messageHandler.
method(
"shutdown", &lspServer, &LSPServer::onShutdown);
297 messageHandler.
notification(
"textDocument/didOpen", &lspServer,
298 &LSPServer::onDocumentDidOpen);
299 messageHandler.
notification(
"textDocument/didClose", &lspServer,
300 &LSPServer::onDocumentDidClose);
301 messageHandler.
notification(
"textDocument/didChange", &lspServer,
302 &LSPServer::onDocumentDidChange);
305 messageHandler.
method(
"textDocument/definition", &lspServer,
306 &LSPServer::onGoToDefinition);
307 messageHandler.
method(
"textDocument/references", &lspServer,
308 &LSPServer::onReference);
311 messageHandler.
method(
"textDocument/hover", &lspServer, &LSPServer::onHover);
314 messageHandler.
method(
"textDocument/documentSymbol", &lspServer,
315 &LSPServer::onDocumentSymbol);
318 messageHandler.
method(
"textDocument/completion", &lspServer,
319 &LSPServer::onCompletion);
322 messageHandler.
method(
"textDocument/codeAction", &lspServer,
323 &LSPServer::onCodeAction);
326 messageHandler.
method(
"mlir/convertFromBytecode", &lspServer,
327 &LSPServer::onConvertFromBytecode);
328 messageHandler.
method(
"mlir/convertToBytecode", &lspServer,
329 &LSPServer::onConvertToBytecode);
332 lspServer.publishDiagnostics =
334 "textDocument/publishDiagnostics");
338 if (llvm::Error error = transport.
run(messageHandler)) {
340 llvm::consumeError(std::move(error));
343 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.
LogicalResult runMlirLSPServer(MLIRServer &server, JSONTransport &transport)
Run the main loop of the LSP server using the given MLIR server and transport.
This header declares functions that assist transformations in the MemRef dialect.
LogicalResult failure(bool isFailure=true)
Utility function to generate a LogicalResult.
LogicalResult success(bool isSuccess=true)
Utility function to generate a LogicalResult.
This class represents an efficient way to signal success or failure.
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.