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});
|
||||
|
||||
Reference in new issue
Block a user