Handle workspace file-operation notifications
The server advertises didCreate/didRename/didDelete file operations with file-scheme filters. A bounded core converts notifications into canonical filesystem changes (rename as old-gone/new-present, non-file URIs ignored, invalid URIs rejected) with registered tests, and EditorDiagnostics invalidates every dependent index context for changed or removed paths before re-queueing open documents. A real stdio session observed the index become incomplete after a deleted header, and a malformed notification left the server answering.
This commit is contained in:
1 parent
130cb57a7f
commit
158125ace5
12 files changed
+248
-12
No files matched your search
+47
-4
@@ -21,6 +21,13 @@ using Protocol::as_object;
|
||||
using Protocol::as_string;
|
||||
using Protocol::find_member;
|
||||
|
||||
Json file_operation_registration() {
|
||||
return Json{Json::Object{
|
||||
{"filters", Json{Json::Array{Json{Json::Object{
|
||||
{"scheme", Json{std::string{"file"}}},
|
||||
{"pattern", Json{Json::Object{{"glob", Json{std::string{"**/*"}}}}}}}}}}}}};
|
||||
}
|
||||
|
||||
const Json *member(const Json *value, std::string_view key) {
|
||||
const auto *object = as_object(value);
|
||||
return object ? find_member(*object, key) : nullptr;
|
||||
@@ -213,6 +220,39 @@ DispatchResult Server::handle(std::string_view body) {
|
||||
.diagnostic = updated.error().substr(0, 256)};
|
||||
return {};
|
||||
}
|
||||
if (*method == "workspace/didCreateFiles" || *method == "workspace/didRenameFiles" ||
|
||||
*method == "workspace/didDeleteFiles") {
|
||||
if (!diagnostics_)
|
||||
return {};
|
||||
const auto *files = as_array(member(params, "files"));
|
||||
if (!files)
|
||||
return {.response = std::nullopt,
|
||||
.exit_code = std::nullopt,
|
||||
.diagnostic = "invalid file operation notification"};
|
||||
const auto kind = *method == "workspace/didCreateFiles"
|
||||
? features::file_operations::Kind::create
|
||||
: *method == "workspace/didRenameFiles"
|
||||
? features::file_operations::Kind::rename
|
||||
: features::file_operations::Kind::delete_;
|
||||
std::vector<features::file_operations::Input> inputs;
|
||||
for (const auto &item : *files) {
|
||||
const auto *fields = as_object(&item);
|
||||
const auto *uri = fields ? as_string(find_member(*fields, "uri")) : nullptr;
|
||||
const auto *old_uri =
|
||||
fields ? as_string(find_member(*fields, "oldUri")) : nullptr;
|
||||
inputs.push_back(
|
||||
{kind, uri ? *uri : std::string{}, old_uri ? *old_uri : std::string{}});
|
||||
}
|
||||
const auto parsed = features::file_operations::parse(inputs);
|
||||
if (!parsed)
|
||||
return {.response = std::nullopt,
|
||||
.exit_code = std::nullopt,
|
||||
.diagnostic = parsed.error()};
|
||||
diagnostics_->begin_update();
|
||||
diagnostics_->files_changed(parsed->changes);
|
||||
diagnostics_->finish_update(true);
|
||||
return {};
|
||||
}
|
||||
const bool change = *method == "textDocument/didOpen" ||
|
||||
*method == "textDocument/didChange" ||
|
||||
*method == "textDocument/didClose";
|
||||
@@ -253,14 +293,17 @@ DispatchResult Server::handle(std::string_view body) {
|
||||
Protocol::as_bool(member(edit, "documentChanges")).value_or(false));
|
||||
}
|
||||
Json::Object sync{{"openClose", Json{true}}, {"change", Json{std::int64_t{2}}}};
|
||||
Json::Object folders{{"supported", Json{true}}, {"changeNotifications", Json{true}}};
|
||||
Json::Object workspace{
|
||||
{"workspaceFolders", Json{std::move(folders)}},
|
||||
{"fileOperations", Json{Json::Object{{"didCreate", file_operation_registration()},
|
||||
{"didRename", file_operation_registration()},
|
||||
{"didDelete", file_operation_registration()}}}}};
|
||||
Json::Object capabilities{
|
||||
{"positionEncoding",
|
||||
Json{std::string{Void::LanguageServer::Documents::encoding_name(encoding)}}},
|
||||
{"textDocumentSync", Json{std::move(sync)}},
|
||||
{"workspace",
|
||||
Json{Json::Object{
|
||||
{"workspaceFolders", Json{Json::Object{{"supported", Json{true}},
|
||||
{"changeNotifications", Json{true}}}}}}}}};
|
||||
{"workspace", Json{std::move(workspace)}}};
|
||||
if (diagnostics_) {
|
||||
capabilities.emplace("documentSymbolProvider", Json{true});
|
||||
capabilities.emplace("selectionRangeProvider", Json{true});
|
||||
|
||||
@@ -0,0 +1,45 @@
|
||||
#include <vls/features/FileOperations.hpp>
|
||||
|
||||
#include <Void/LanguageServer/Documents/Uri.hpp>
|
||||
|
||||
namespace vls::features::file_operations {
|
||||
|
||||
std::expected<Result, std::string> parse(std::span<const Input> inputs, const Limits &limits) {
|
||||
if (inputs.size() > limits.files)
|
||||
return std::unexpected("too many file operations");
|
||||
Result result;
|
||||
const auto add = [&](const std::string &uri, bool gone) -> std::expected<void, std::string> {
|
||||
if (uri.size() > limits.pathBytes)
|
||||
return std::unexpected("file operation URI exceeds its bound");
|
||||
const auto identity = Void::LanguageServer::Documents::normalize_uri(uri);
|
||||
if (!identity)
|
||||
return std::unexpected("file operation URI is invalid");
|
||||
if (!identity->is_file_uri) {
|
||||
++result.ignored;
|
||||
return {};
|
||||
}
|
||||
result.changes.push_back({identity->canonical_path.native(), gone});
|
||||
return {};
|
||||
};
|
||||
for (const auto &input : inputs) {
|
||||
switch (input.kind) {
|
||||
case Kind::create:
|
||||
if (auto added = add(input.uri, false); !added)
|
||||
return std::unexpected(added.error());
|
||||
break;
|
||||
case Kind::delete_:
|
||||
if (auto added = add(input.uri, true); !added)
|
||||
return std::unexpected(added.error());
|
||||
break;
|
||||
case Kind::rename:
|
||||
if (auto added = add(input.old_uri, true); !added)
|
||||
return std::unexpected(added.error());
|
||||
if (auto added = add(input.uri, false); !added)
|
||||
return std::unexpected(added.error());
|
||||
break;
|
||||
}
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
} // namespace vls::features::file_operations
|
||||
@@ -696,6 +696,18 @@ void EditorDiagnostics::begin_update() {
|
||||
s.module_work.cancel(key);
|
||||
}
|
||||
}
|
||||
void EditorDiagnostics::files_changed(std::span<const features::file_operations::Change> changes) {
|
||||
auto &s = *state_;
|
||||
std::lock_guard lock{s.mutex};
|
||||
for (const auto &change : changes) {
|
||||
if (change.gone)
|
||||
s.symbols.clear_overlay(change.path);
|
||||
// The new content is unknown, so the path is treated as changed: every
|
||||
// context that consumed it is marked incomplete, never silently kept.
|
||||
s.symbols.invalidate(change.path, std::nullopt);
|
||||
}
|
||||
}
|
||||
|
||||
void EditorDiagnostics::finish_update(bool workspace_changed) {
|
||||
auto &s = *state_;
|
||||
auto overlays = s.documents.list_overlays();
|
||||
|
||||
Reference in new issue
Block a user