nixd
Loading...
Searching...
No Matches
TextDocumentSync.cpp
Go to the documentation of this file.
1/// \file
2/// \brief Implementation of the [text document
3/// sync](https://microsoft.github.io/language-server-protocol/specifications/lsp/3.17/specification/#textDocument_synchronization).
5
7
8namespace nixd {
9
10using namespace llvm::json;
11using namespace lspserver;
12
13void Controller::onDocumentDidOpen(
15 PathRef File = Params.textDocument.uri.file();
16 const std::string &Contents = Params.textDocument.text;
17 std::optional<int64_t> Version = Params.textDocument.version;
18 Store.addDraft(File, DraftStore::encodeVersion(Version), Contents);
19 actOnDocumentAdd(File, Version);
20}
21
22void Controller::onDocumentDidChange(
24 PathRef File = Params.textDocument.uri.file();
25 auto Code = Store.getDraft(File);
26 if (!Code) {
27 log("Trying to incrementally change non-added document: {0}", File);
28 return;
29 }
30 std::string NewCode(*Code->Contents);
31 for (const auto &Change : Params.contentChanges) {
32 if (auto Err = applyChange(NewCode, Change)) {
33 // If this fails, we are most likely going to be not in sync anymore
34 // with the client. It is better to remove the draft and let further
35 // operations fail rather than giving wrong results.
36 removeDocument(File);
37 elog("Failed to update {0}: {1}", File, std::move(Err));
38 return;
39 }
40 }
41 std::optional<int64_t> Version = Params.textDocument.version;
43 actOnDocumentAdd(File, Version);
44}
45
46void Controller::onDocumentDidClose(const DidCloseTextDocumentParams &Params) {
47 PathRef File = Params.textDocument.uri.file();
48 removeDocument(File);
49}
50
51} // namespace nixd
static std::string encodeVersion(std::optional< int64_t > LSPVersion)
std::optional< Draft > getDraft(PathRef File) const
std::string addDraft(PathRef File, llvm::StringRef Version, llvm::StringRef Contents)
Whether current platform treats paths case insensitively.
Definition Connection.h:11
llvm::Error applyChange(std::string &Contents, const TextDocumentContentChangeEvent &Change)
Apply an incremental update to a text document.
void elog(const char *Fmt, Ts &&...Vals)
Definition Logger.h:52
llvm::StringRef PathRef
Definition Path.h:27
void log(const char *Fmt, Ts &&...Vals)
Definition Logger.h:58
bool fromJSON(const llvm::json::Value &Params, Configuration::Diagnostic &R, llvm::json::Path P)