Skip to content

Commit 14cbed6

Browse files
author
git apple-llvm automerger
committed
Merge commit 'f9ea3ebef228' from llvm.org/main into apple/main
2 parents 0e0782e + f9ea3eb commit 14cbed6

File tree

8 files changed

+88
-22
lines changed

8 files changed

+88
-22
lines changed

mlir/lib/Tools/mlir-lsp-server/LSPServer.cpp

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -103,32 +103,38 @@ void LSPServer::Impl::onShutdown(const NoParams &,
103103

104104
void LSPServer::Impl::onDocumentDidOpen(
105105
const DidOpenTextDocumentParams &params) {
106-
PublishDiagnosticsParams diagParams(params.textDocument.uri);
106+
PublishDiagnosticsParams diagParams(params.textDocument.uri,
107+
params.textDocument.version);
107108
server.addOrUpdateDocument(params.textDocument.uri, params.textDocument.text,
109+
params.textDocument.version,
108110
diagParams.diagnostics);
109111

110112
// Publish any recorded diagnostics.
111113
publishDiagnostics(diagParams);
112114
}
113115
void LSPServer::Impl::onDocumentDidClose(
114116
const DidCloseTextDocumentParams &params) {
115-
server.removeDocument(params.textDocument.uri);
117+
Optional<int64_t> version = server.removeDocument(params.textDocument.uri);
118+
if (!version)
119+
return;
116120

117121
// Empty out the diagnostics shown for this document. This will clear out
118122
// anything currently displayed by the client for this document (e.g. in the
119123
// "Problems" pane of VSCode).
120-
publishDiagnostics(PublishDiagnosticsParams(params.textDocument.uri));
124+
publishDiagnostics(
125+
PublishDiagnosticsParams(params.textDocument.uri, *version));
121126
}
122127
void LSPServer::Impl::onDocumentDidChange(
123128
const DidChangeTextDocumentParams &params) {
124129
// TODO: We currently only support full document updates, we should refactor
125130
// to avoid this.
126131
if (params.contentChanges.size() != 1)
127132
return;
128-
PublishDiagnosticsParams diagParams(params.textDocument.uri);
129-
server.addOrUpdateDocument(params.textDocument.uri,
130-
params.contentChanges.front().text,
131-
diagParams.diagnostics);
133+
PublishDiagnosticsParams diagParams(params.textDocument.uri,
134+
params.textDocument.version);
135+
server.addOrUpdateDocument(
136+
params.textDocument.uri, params.contentChanges.front().text,
137+
params.textDocument.version, diagParams.diagnostics);
132138

133139
// Publish any recorded diagnostics.
134140
publishDiagnostics(diagParams);

mlir/lib/Tools/mlir-lsp-server/MLIRServer.cpp

Lines changed: 16 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -252,7 +252,7 @@ namespace {
252252
/// This class represents all of the information pertaining to a specific MLIR
253253
/// document.
254254
struct MLIRDocument {
255-
MLIRDocument(const lsp::URIForFile &uri, StringRef contents,
255+
MLIRDocument(const lsp::URIForFile &uri, StringRef contents, int64_t version,
256256
DialectRegistry &registry,
257257
std::vector<lsp::Diagnostic> &diagnostics);
258258

@@ -283,6 +283,9 @@ struct MLIRDocument {
283283
buildHoverForBlockArgument(llvm::SMRange hoverRange, BlockArgument arg,
284284
const AsmParserState::BlockDefinition &block);
285285

286+
/// The version of this document.
287+
int64_t version;
288+
286289
/// The context used to hold the state contained by the parsed document.
287290
MLIRContext context;
288291

@@ -299,9 +302,9 @@ struct MLIRDocument {
299302
} // namespace
300303

301304
MLIRDocument::MLIRDocument(const lsp::URIForFile &uri, StringRef contents,
302-
DialectRegistry &registry,
305+
int64_t version, DialectRegistry &registry,
303306
std::vector<lsp::Diagnostic> &diagnostics)
304-
: context(registry) {
307+
: version(version), context(registry) {
305308
context.allowUnregisteredDialects();
306309
ScopedDiagnosticHandler handler(&context, [&](Diagnostic &diag) {
307310
diagnostics.push_back(getLspDiagnoticFromDiag(diag, uri));
@@ -569,14 +572,20 @@ lsp::MLIRServer::MLIRServer(DialectRegistry &registry)
569572
lsp::MLIRServer::~MLIRServer() {}
570573

571574
void lsp::MLIRServer::addOrUpdateDocument(
572-
const URIForFile &uri, StringRef contents,
575+
const URIForFile &uri, StringRef contents, int64_t version,
573576
std::vector<Diagnostic> &diagnostics) {
574577
impl->documents[uri.file()] = std::make_unique<MLIRDocument>(
575-
uri, contents, impl->registry, diagnostics);
578+
uri, contents, version, impl->registry, diagnostics);
576579
}
577580

578-
void lsp::MLIRServer::removeDocument(const URIForFile &uri) {
579-
impl->documents.erase(uri.file());
581+
Optional<int64_t> lsp::MLIRServer::removeDocument(const URIForFile &uri) {
582+
auto it = impl->documents.find(uri.file());
583+
if (it == impl->documents.end())
584+
return llvm::None;
585+
586+
int64_t version = it->second->version;
587+
impl->documents.erase(it);
588+
return version;
580589
}
581590

582591
void lsp::MLIRServer::getLocationsOf(const URIForFile &uri,

mlir/lib/Tools/mlir-lsp-server/MLIRServer.h

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -31,13 +31,17 @@ class MLIRServer {
3131
MLIRServer(DialectRegistry &registry);
3232
~MLIRServer();
3333

34-
/// Add or update the document at the given URI. Any diagnostics emitted for
35-
/// this document should be added to `diagnostics`
34+
/// Add or update the document, with the provided `version`, at the given URI.
35+
/// Any diagnostics emitted for this document should be added to
36+
/// `diagnostics`.
3637
void addOrUpdateDocument(const URIForFile &uri, StringRef contents,
38+
int64_t version,
3739
std::vector<Diagnostic> &diagnostics);
3840

39-
/// Remove the document with the given uri.
40-
void removeDocument(const URIForFile &uri);
41+
/// Remove the document with the given uri. Returns the version of the removed
42+
/// document, or None if the uri did not have a corresponding document within
43+
/// the server.
44+
Optional<int64_t> removeDocument(const URIForFile &uri);
4145

4246
/// Return the locations of the object pointed at by the given position.
4347
void getLocationsOf(const URIForFile &uri, const Position &defPos,

mlir/lib/Tools/mlir-lsp-server/lsp/Protocol.cpp

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -287,7 +287,8 @@ bool mlir::lsp::fromJSON(const llvm::json::Value &value,
287287
TextDocumentItem &result, llvm::json::Path path) {
288288
llvm::json::ObjectMapper o(value, path);
289289
return o && o.map("uri", result.uri) &&
290-
o.map("languageId", result.languageId) && o.map("text", result.text);
290+
o.map("languageId", result.languageId) && o.map("text", result.text) &&
291+
o.map("version", result.version);
291292
}
292293

293294
//===----------------------------------------------------------------------===//
@@ -305,6 +306,25 @@ bool mlir::lsp::fromJSON(const llvm::json::Value &value,
305306
return o && o.map("uri", result.uri);
306307
}
307308

309+
//===----------------------------------------------------------------------===//
310+
// VersionedTextDocumentIdentifier
311+
//===----------------------------------------------------------------------===//
312+
313+
llvm::json::Value
314+
mlir::lsp::toJSON(const VersionedTextDocumentIdentifier &value) {
315+
return llvm::json::Object{
316+
{"uri", value.uri},
317+
{"version", value.version},
318+
};
319+
}
320+
321+
bool mlir::lsp::fromJSON(const llvm::json::Value &value,
322+
VersionedTextDocumentIdentifier &result,
323+
llvm::json::Path path) {
324+
llvm::json::ObjectMapper o(value, path);
325+
return o && o.map("uri", result.uri) && o.map("version", result.version);
326+
}
327+
308328
//===----------------------------------------------------------------------===//
309329
// Position
310330
//===----------------------------------------------------------------------===//
@@ -512,5 +532,6 @@ llvm::json::Value mlir::lsp::toJSON(const PublishDiagnosticsParams &params) {
512532
return llvm::json::Object{
513533
{"uri", params.uri},
514534
{"diagnostics", params.diagnostics},
535+
{"version", params.version},
515536
};
516537
}

mlir/lib/Tools/mlir-lsp-server/lsp/Protocol.h

Lines changed: 24 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -180,6 +180,9 @@ struct TextDocumentItem {
180180

181181
/// The content of the opened text document.
182182
std::string text;
183+
184+
/// The version number of this document.
185+
int64_t version;
183186
};
184187

185188
/// Add support for JSON serialization.
@@ -200,6 +203,22 @@ llvm::json::Value toJSON(const TextDocumentIdentifier &value);
200203
bool fromJSON(const llvm::json::Value &value, TextDocumentIdentifier &result,
201204
llvm::json::Path path);
202205

206+
//===----------------------------------------------------------------------===//
207+
// VersionedTextDocumentIdentifier
208+
//===----------------------------------------------------------------------===//
209+
210+
struct VersionedTextDocumentIdentifier {
211+
/// The text document's URI.
212+
URIForFile uri;
213+
/// The version number of this document.
214+
int64_t version;
215+
};
216+
217+
/// Add support for JSON serialization.
218+
llvm::json::Value toJSON(const VersionedTextDocumentIdentifier &value);
219+
bool fromJSON(const llvm::json::Value &value,
220+
VersionedTextDocumentIdentifier &result, llvm::json::Path path);
221+
203222
//===----------------------------------------------------------------------===//
204223
// Position
205224
//===----------------------------------------------------------------------===//
@@ -381,7 +400,7 @@ bool fromJSON(const llvm::json::Value &value,
381400

382401
struct DidChangeTextDocumentParams {
383402
/// The document that changed.
384-
TextDocumentIdentifier textDocument;
403+
VersionedTextDocumentIdentifier textDocument;
385404

386405
/// The actual content changes.
387406
std::vector<TextDocumentContentChangeEvent> contentChanges;
@@ -498,12 +517,15 @@ llvm::json::Value toJSON(const Diagnostic &diag);
498517
//===----------------------------------------------------------------------===//
499518

500519
struct PublishDiagnosticsParams {
501-
PublishDiagnosticsParams(URIForFile uri) : uri(uri) {}
520+
PublishDiagnosticsParams(URIForFile uri, int64_t version)
521+
: uri(uri), version(version) {}
502522

503523
/// The URI for which diagnostic information is reported.
504524
URIForFile uri;
505525
/// The list of reported diagnostics.
506526
std::vector<Diagnostic> diagnostics;
527+
/// The version number of the document the diagnostics are published for.
528+
int64_t version;
507529
};
508530

509531
/// Add support for JSON serialization.

mlir/lib/Tools/mlir-lsp-server/lsp/Transport.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -217,6 +217,7 @@ void JSONTransport::sendMessage(llvm::json::Value msg) {
217217
out << "Content-Length: " << outputBuffer.size() << "\r\n\r\n"
218218
<< outputBuffer;
219219
out.flush();
220+
Logger::debug(">>> {0}\n", outputBuffer);
220221
}
221222

222223
bool JSONTransport::handleMessage(llvm::json::Value msg,

mlir/lib/Tools/mlir-lsp-server/lsp/Transport.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
#ifndef LIB_MLIR_TOOLS_MLIRLSPSERVER_LSP_TRANSPORT_H_
1616
#define LIB_MLIR_TOOLS_MLIRLSPSERVER_LSP_TRANSPORT_H_
1717

18+
#include "Logging.h"
1819
#include "Protocol.h"
1920
#include "mlir/Support/LLVM.h"
2021
#include "mlir/Support/LogicalResult.h"
@@ -157,6 +158,7 @@ class MessageHandler {
157158
template <typename T>
158159
OutgoingNotification<T> outgoingNotification(llvm::StringLiteral method) {
159160
return [&, method](const T &params) {
161+
Logger::info("--> {0}", method);
160162
transport.notify(method, llvm::json::Value(params));
161163
};
162164
}

mlir/test/mlir-lsp-server/diagnostics.test

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,8 @@
2727
// CHECK-NEXT: "source": "mlir"
2828
// CHECK-NEXT: }
2929
// CHECK-NEXT: ],
30-
// CHECK-NEXT: "uri": "test:///foo.mlir"
30+
// CHECK-NEXT: "uri": "test:///foo.mlir",
31+
// CHECK-NEXT: "version": 1
3132
// CHECK-NEXT: }
3233
// -----
3334
{"jsonrpc":"2.0","id":3,"method":"shutdown"}

0 commit comments

Comments
 (0)