Implement the VLS roadmap on the native VBS build
Native VBS and CMake builds of the server and every registered suite: protocol and document sync, VBS/compdb providers, stock GCC diagnostics (including in-process default profile preparation), the stock occurrence observer and hover/definition, the bounded Scheduler, C++ module graph, scan and artifact cache with real CMI production, the context-aware symbol index with schema-2 snapshot migration, navigation and guarded rename, reflection provenance with splice-origin extraction, index-backed completion and signature help, live GCC worker control with a registered DAP suite, and the Neovim and VS Code clients. VoidLibrary follows main. The private GCC 16.2.0 frontend is reproduced from the versioned patches and verified stepping reflection/template code with projected values 17/34 and 41/82.
This commit is contained in:
1 parent
ca04975d54
commit
da8003f6d4
310 files changed
+47552
-480
No files matched your search
@@ -0,0 +1,452 @@
|
||||
#include <Void/LanguageServer/Documents/DocumentStore.hpp>
|
||||
|
||||
#include <Void/System/TempDir.hpp>
|
||||
#include <Void/Testing/Assert.hpp>
|
||||
#include <Void/Testing/Test.hpp>
|
||||
|
||||
#include <algorithm>
|
||||
#include <array>
|
||||
#include <filesystem>
|
||||
#include <fstream>
|
||||
#include <source_location>
|
||||
#include <string>
|
||||
#include <utility>
|
||||
|
||||
namespace {
|
||||
using namespace Void::LanguageServer::Documents;
|
||||
using Void::Testing::expect;
|
||||
using Void::Testing::require;
|
||||
using Kind = DocumentError::Kind;
|
||||
namespace fs = std::filesystem;
|
||||
|
||||
struct Files {
|
||||
Void::FileSystem::TempDir directory{"vls_bound_inputs_"};
|
||||
fs::path root = fs::canonical(directory.path());
|
||||
|
||||
fs::path path(std::string_view name) const { return root / name; }
|
||||
};
|
||||
|
||||
void write(const fs::path &path, std::string_view text) {
|
||||
std::ofstream output{path, std::ios::binary};
|
||||
output << text;
|
||||
output.close();
|
||||
require(static_cast<bool>(output));
|
||||
}
|
||||
|
||||
template <class T>
|
||||
T get(std::expected<T, DocumentError> result,
|
||||
std::source_location loc = std::source_location::current()) {
|
||||
require(result.has_value(), loc);
|
||||
return std::move(*result);
|
||||
}
|
||||
|
||||
template <class T>
|
||||
void expect_error(const std::expected<T, DocumentError> &result, Kind kind,
|
||||
std::source_location loc = std::source_location::current()) {
|
||||
require(!result.has_value(), loc);
|
||||
expect(result.error().kind == kind, loc);
|
||||
}
|
||||
|
||||
auto observe(const DocumentStore &store, const FileOverlayInventory &inventory,
|
||||
const fs::path &path) {
|
||||
return store.observe_bound_source(inventory, path_to_file_uri(path), path);
|
||||
}
|
||||
|
||||
auto replace(DocumentStore &store, std::string_view uri, std::int32_t version, std::string text) {
|
||||
return store.apply_changes(uri, version, std::array{TextEdit{std::nullopt, std::move(text)}});
|
||||
}
|
||||
|
||||
const FileOverlayBinding &binding(const FileOverlayInventory &inventory, std::string_view uri) {
|
||||
const auto entries = inventory.entries();
|
||||
const auto found = std::ranges::find_if(
|
||||
entries, [&](const auto &entry) { return entry.identity.canonical_uri == uri; });
|
||||
require(found != entries.end());
|
||||
return *found;
|
||||
}
|
||||
} // namespace
|
||||
|
||||
VOID_TEST(BoundInputs, disk_snapshot_uses_caller_bytes_and_exact_bound_names) {
|
||||
Files files;
|
||||
const auto physical = files.path("one.cpp");
|
||||
const auto logical = files.path("other.cpp");
|
||||
write(physical, "ambient physical");
|
||||
write(logical, "ambient logical");
|
||||
const auto uri = path_to_file_uri(logical);
|
||||
const auto display = "FILE://LOCALHOST" + uri.substr(7);
|
||||
const std::string bytes = "a\U0001f600\r\nb";
|
||||
for (const auto encoding : {PositionEncoding::utf8, PositionEncoding::utf16}) {
|
||||
unsigned ambient_reads = 0;
|
||||
DocumentStore store{encoding, {}, [&] { ++ambient_reads; }};
|
||||
auto inventory = get(store.file_overlay_inventory(0));
|
||||
const auto observation = get(store.observe_bound_source(inventory, display, physical));
|
||||
expect(!observation.overlay());
|
||||
expect(observation.identity().canonical_uri == uri);
|
||||
expect(observation.identity().display_uri == display);
|
||||
expect(observation.identity().canonical_path == physical);
|
||||
auto disk = get(store.materialize_bound_disk(observation, bytes));
|
||||
expect(disk->identity == observation.identity() && disk->text == bytes);
|
||||
expect(disk->origin == DocumentOrigin::disk && !disk->version &&
|
||||
disk->encoding == encoding);
|
||||
const auto column = encoding == PositionEncoding::utf8 ? 5u : 3u;
|
||||
expect(disk->offset_to_position(5) == Position{0, column});
|
||||
expect(disk->position_to_offset({1, 0}) == 7);
|
||||
auto again = get(store.materialize_bound_disk(observation, bytes));
|
||||
expect(again->revision_id != disk->revision_id && again->same_input_as(*disk));
|
||||
expect(!get(store.materialize_bound_disk(observation, "different"))->same_input_as(*disk));
|
||||
expect(get(store.materialize_bound_disk(observation, ""))->text.empty());
|
||||
expect(store.validate_bound_sources(inventory, std::array{observation}).has_value());
|
||||
expect(ambient_reads == 0);
|
||||
expect(get(store.resolve_content(physical))->text == "ambient physical");
|
||||
expect(ambient_reads == 1);
|
||||
}
|
||||
}
|
||||
|
||||
VOID_TEST(BoundInputs, empty_inventory_detects_a_new_unsaved_header) {
|
||||
Files files;
|
||||
const auto tu = files.path("main.cpp");
|
||||
const auto header = files.path("optional.hpp");
|
||||
const auto uri = path_to_file_uri(header);
|
||||
DocumentStore store;
|
||||
require(store.open("untitled:scratch", "cpp", 1, "virtual").has_value());
|
||||
const auto empty = get(store.file_overlay_inventory(0));
|
||||
require(empty.entries().empty());
|
||||
const auto disk = get(observe(store, empty, tu));
|
||||
const auto new_header = get(store.open(uri, "cpp", 1, ""));
|
||||
expect(!fs::exists(header));
|
||||
expect_error(store.validate_bound_sources(empty), Kind::stale_read);
|
||||
expect_error(observe(store, empty, tu), Kind::stale_read);
|
||||
expect_error(store.materialize_bound_disk(disk, "old"), Kind::stale_read);
|
||||
|
||||
const auto current = get(store.file_overlay_inventory(1));
|
||||
require(current.entries().size() == 1);
|
||||
const auto &entry = binding(current, uri);
|
||||
expect(entry.identity.canonical_path == header && entry.content == new_header);
|
||||
const auto selected = get(observe(store, current, header));
|
||||
expect(selected.overlay() == new_header && selected.overlay()->text.empty());
|
||||
expect_error(store.materialize_bound_disk(selected, ""), Kind::already_open);
|
||||
const auto current_tu = get(observe(store, current, tu));
|
||||
expect_error(store.validate_bound_sources(current, std::array{disk}), Kind::stale_read);
|
||||
expect_error(store.validate_bound_sources(empty, std::array{current_tu}), Kind::stale_read);
|
||||
expect(store.validate_bound_sources(current, std::array{current_tu, selected}).has_value());
|
||||
}
|
||||
|
||||
VOID_TEST(BoundInputs, selection_uses_current_membership_and_per_document_revisions) {
|
||||
Files files;
|
||||
const auto a = files.path("early.cpp");
|
||||
const auto b = files.path("other.hpp");
|
||||
const auto early = path_to_file_uri(a);
|
||||
const auto other = path_to_file_uri(b);
|
||||
DocumentStore store;
|
||||
const auto first = get(store.open(early, "cpp", 1, "early"));
|
||||
const auto initial = get(store.file_overlay_inventory());
|
||||
const auto old_a = get(observe(store, initial, a));
|
||||
const auto second = get(store.open(other, "cpp", 1, "previous"));
|
||||
require(first->overlay_epoch != second->overlay_epoch);
|
||||
const auto census = get(store.file_overlay_inventory());
|
||||
const auto kept_a = get(observe(store, census, a));
|
||||
const auto kept_b = get(observe(store, census, b));
|
||||
expect(kept_a.overlay() == first);
|
||||
expect(store.validate_bound_sources(census, std::array{kept_a, kept_b}).has_value());
|
||||
expect_error(store.validate_bound_sources(census, std::array{old_a}), Kind::stale_read);
|
||||
|
||||
const auto updated = get(replace(store, other, 2, "previous"));
|
||||
expect(store.validate_bound_sources(census, std::array{kept_a}).has_value());
|
||||
expect_error(store.validate_bound_sources(census, std::array{kept_a, kept_b}),
|
||||
Kind::stale_read);
|
||||
expect(binding(census, other).content == second);
|
||||
const auto fresh_b = get(observe(store, census, b));
|
||||
expect(fresh_b.overlay() == updated && !updated->same_input_as(*second));
|
||||
expect(store.validate_bound_sources(census, std::array{kept_a, fresh_b}).has_value());
|
||||
}
|
||||
|
||||
VOID_TEST(BoundInputs, open_edit_close_rejects_late_bytes_for_an_earlier_absence) {
|
||||
Files files;
|
||||
const auto source = files.path("main.cpp");
|
||||
const auto uri = path_to_file_uri(source);
|
||||
DocumentStore store;
|
||||
const auto initial = get(store.file_overlay_inventory());
|
||||
const auto absent = get(observe(store, initial, source));
|
||||
const auto before = get(store.materialize_bound_disk(absent, "same"));
|
||||
require(store.open(uri, "cpp", 1, "open").has_value());
|
||||
expect_error(store.materialize_bound_disk(absent, "same"), Kind::stale_read);
|
||||
require(replace(store, uri, 2, "changed").has_value());
|
||||
require(store.close(uri).has_value());
|
||||
expect_error(store.materialize_bound_disk(absent, "same"), Kind::stale_read);
|
||||
const auto latest = get(store.file_overlay_inventory());
|
||||
expect_error(store.validate_bound_sources(latest, std::array{absent}), Kind::stale_read);
|
||||
const auto now_absent = get(observe(store, latest, source));
|
||||
const auto after = get(store.materialize_bound_disk(now_absent, "same"));
|
||||
expect(!after->same_input_as(*before));
|
||||
}
|
||||
|
||||
VOID_TEST(BoundInputs, desynchronization_recovery_and_reopen_recheck_retained_overlays) {
|
||||
Files files;
|
||||
const auto path = files.path("edit.cpp");
|
||||
const auto uri = path_to_file_uri(path);
|
||||
DocumentStore store;
|
||||
const auto text = get(store.open(uri, "cpp", 7, "initial"));
|
||||
const auto census = get(store.file_overlay_inventory());
|
||||
const auto original = get(observe(store, census, path));
|
||||
require(store.mark_desynchronized(uri, std::nullopt, {Kind::malformed_change, "bad range"})
|
||||
.has_value());
|
||||
expect_error(store.validate_bound_sources(census, std::array{original}), Kind::desynchronized);
|
||||
expect_error(store.materialize_bound_disk(original, "disk"), Kind::desynchronized);
|
||||
expect_error(observe(store, census, path), Kind::desynchronized);
|
||||
const auto blocked = get(store.file_overlay_inventory());
|
||||
expect_error(binding(blocked, uri).content, Kind::desynchronized);
|
||||
const auto recovered = get(replace(store, uri, 8, "initial"));
|
||||
const auto fresh = get(observe(store, blocked, path));
|
||||
expect(fresh.overlay() == recovered && !recovered->same_input_as(*text));
|
||||
expect_error(store.validate_bound_sources(census, std::array{original}), Kind::stale_read);
|
||||
require(store.close(uri).has_value());
|
||||
const auto reopened = get(store.open(uri, "cpp", 7, "initial"));
|
||||
expect_error(store.validate_bound_sources(blocked, std::array{fresh}), Kind::stale_read);
|
||||
expect(original.overlay() == text && original.overlay()->text == reopened->text);
|
||||
expect(!original.overlay()->same_input_as(*reopened));
|
||||
}
|
||||
|
||||
VOID_TEST(BoundInputs, refused_file_is_inventoried_and_cannot_be_materialized_from_disk) {
|
||||
Files files;
|
||||
const auto a = files.path("kept.cpp");
|
||||
const auto b = files.path("refused.cpp");
|
||||
const auto kept = path_to_file_uri(a);
|
||||
const auto refused = path_to_file_uri(b);
|
||||
DocumentStore store{PositionEncoding::utf16,
|
||||
{.max_open_documents = 1, .max_unavailable_documents = 1}};
|
||||
const auto snapshot = get(store.open(kept, "cpp", 1, "editor"));
|
||||
const auto before = get(store.file_overlay_inventory());
|
||||
const auto absent = get(observe(store, before, b));
|
||||
expect_error(store.open(refused, "cpp", 1, "refused bytes"), Kind::resource_limit);
|
||||
expect_error(store.materialize_bound_disk(absent, "disk"), Kind::stale_read);
|
||||
expect_error(store.file_overlay_inventory(1), Kind::resource_limit);
|
||||
const auto census = get(store.file_overlay_inventory(2));
|
||||
require(census.entries().size() == 2);
|
||||
expect(binding(census, kept).content == snapshot);
|
||||
expect_error(binding(census, refused).content, Kind::unavailable_overlay);
|
||||
expect_error(observe(store, census, b), Kind::unavailable_overlay);
|
||||
require(store.mark_desynchronized(kept, 0, {Kind::invalid_range, "bad"}).has_value());
|
||||
const auto at_quota = get(store.file_overlay_inventory());
|
||||
expect_error(binding(at_quota, kept).content, Kind::desynchronized);
|
||||
expect_error(binding(at_quota, refused).content, Kind::unavailable_overlay);
|
||||
expect_error(replace(store, refused, 2, "still refused"), Kind::resource_limit);
|
||||
require(store.close(kept).has_value());
|
||||
const auto room = get(store.file_overlay_inventory(1));
|
||||
expect_error(binding(room, refused).content, Kind::unavailable_overlay);
|
||||
const auto restored = get(replace(store, refused, 3, "restored"));
|
||||
expect(get(observe(store, room, b)).overlay() == restored);
|
||||
expect(get(store.resolve_content(b)) == restored);
|
||||
}
|
||||
|
||||
VOID_TEST(BoundInputs, aliases_keep_their_client_keys_and_block_ambiguous_source_bytes) {
|
||||
Files files;
|
||||
fs::create_directories(files.path("real/sub"));
|
||||
fs::create_directory_symlink(files.path("real/sub"), files.path("link"));
|
||||
const auto physical = files.path("real/x.cpp");
|
||||
write(physical, "disk");
|
||||
const auto indirect = path_to_file_uri(files.path("link/../x.cpp"));
|
||||
const auto direct = path_to_file_uri(physical);
|
||||
DocumentStore store;
|
||||
const auto first = get(store.open(indirect, "cpp", 1, "first"));
|
||||
const auto old = get(store.file_overlay_inventory());
|
||||
const auto one = get(observe(store, old, physical));
|
||||
expect(one.identity().canonical_uri == direct && one.overlay() == first);
|
||||
const auto indirect_view = get(store.observe_bound_source(old, indirect, physical));
|
||||
expect(indirect_view.identity().canonical_uri == indirect && indirect.contains("/link/../"));
|
||||
const auto second = get(store.open(direct, "cpp", 1, "second"));
|
||||
const auto both = get(store.file_overlay_inventory());
|
||||
require(both.entries().size() == 2);
|
||||
for (const auto &key : {direct, indirect}) {
|
||||
const auto &entry = binding(both, key);
|
||||
expect(entry.identity.canonical_path == physical);
|
||||
expect_error(entry.content, Kind::ambiguous_overlay);
|
||||
expect_error(store.observe_bound_source(both, key, physical), Kind::ambiguous_overlay);
|
||||
}
|
||||
expect_error(store.validate_bound_sources(both, std::array{one}), Kind::stale_read);
|
||||
expect(store.snapshot(indirect) == first && store.snapshot(direct) == second);
|
||||
require(store.close(direct).has_value());
|
||||
expect(get(observe(store, get(store.file_overlay_inventory()), physical)).overlay() == first);
|
||||
}
|
||||
|
||||
VOID_TEST(BoundInputs, retargeted_lexical_key_cannot_supply_a_different_physical_source) {
|
||||
Files files;
|
||||
const auto a = files.path("a.cpp");
|
||||
const auto b = files.path("b.cpp");
|
||||
const auto link = files.path("alias.cpp");
|
||||
const auto uri = path_to_file_uri(link);
|
||||
write(a, "disk a");
|
||||
write(b, "disk b");
|
||||
fs::create_symlink(a, link);
|
||||
DocumentStore store;
|
||||
const auto original = get(store.open(uri, "cpp", 1, "owned a"));
|
||||
const auto census = get(store.file_overlay_inventory());
|
||||
const auto selected = get(store.observe_bound_source(census, uri, a));
|
||||
require(fs::remove(link));
|
||||
fs::create_symlink(b, link);
|
||||
expect_error(store.observe_bound_source(census, uri, b), Kind::stale_read);
|
||||
expect(get(observe(store, census, a)).overlay() == original);
|
||||
expect(!get(observe(store, census, b)).overlay());
|
||||
// The store checks the supplied binding. The source catalog checks the changed disk name.
|
||||
expect(get(store.observe_bound_source(census, uri, a)).overlay() == original);
|
||||
expect(store.validate_bound_sources(census, std::array{selected}).has_value());
|
||||
require(store.close(uri).has_value());
|
||||
const auto next = get(store.file_overlay_inventory());
|
||||
expect(!get(store.observe_bound_source(next, uri, b)).overlay());
|
||||
}
|
||||
|
||||
VOID_TEST(BoundInputs, virtual_marker_overflow_preserves_file_authority) {
|
||||
Files files;
|
||||
const auto a = files.path("open.cpp");
|
||||
const auto b = files.path("disk.cpp");
|
||||
const auto uri = path_to_file_uri(a);
|
||||
DocumentStore store{PositionEncoding::utf16,
|
||||
{.max_open_documents = 1, .max_unavailable_documents = 0}};
|
||||
const auto doc = get(store.open(uri, "cpp", 1, "overlay"));
|
||||
const auto census = get(store.file_overlay_inventory());
|
||||
const auto active = get(observe(store, census, a));
|
||||
const auto absent = get(observe(store, census, b));
|
||||
expect_error(store.open("UnTiTled:Scratch", "cpp", 1, "virtual"), Kind::resource_limit);
|
||||
expect(store.validate_bound_sources(census, std::array{active, absent}).has_value());
|
||||
const auto fresh = get(store.file_overlay_inventory(1));
|
||||
expect(binding(fresh, uri).content == doc);
|
||||
expect(get(store.resolve_content(a)) == doc);
|
||||
expect(get(store.materialize_bound_disk(absent, "disk"))->text == "disk");
|
||||
expect_error(store.close("untitled:Scratch"), Kind::not_open);
|
||||
require(store.close(uri).has_value());
|
||||
expect(get(store.file_overlay_inventory(0)).entries().empty());
|
||||
expect(!store.set_position_encoding(PositionEncoding::utf8));
|
||||
const auto virtual_doc = get(store.open("untitled:Other", "cpp", 1, "virtual"));
|
||||
expect(store.snapshot("untitled:Other") == virtual_doc);
|
||||
require(store.close("untitled:Other").has_value());
|
||||
}
|
||||
|
||||
VOID_TEST(BoundInputs, unrecorded_file_or_unclassifiable_open_withholds_file_authority) {
|
||||
Files files;
|
||||
const auto a = files.path("open.cpp");
|
||||
const auto b = files.path("future.hpp");
|
||||
const auto uri = path_to_file_uri(a);
|
||||
for (const auto &[refused, reason] :
|
||||
std::array{std::pair{path_to_file_uri(b), Kind::resource_limit},
|
||||
std::pair{std::string(kMaxDocumentUriBytes + 1, 'x'), Kind::resource_limit},
|
||||
std::pair{std::string{"no-scheme"}, Kind::invalid_uri}}) {
|
||||
DocumentStore store{PositionEncoding::utf16,
|
||||
{.max_open_documents = 1, .max_unavailable_documents = 0}};
|
||||
const auto doc = get(store.open(uri, "cpp", 1, "overlay"));
|
||||
const auto census = get(store.file_overlay_inventory());
|
||||
const auto selected = get(observe(store, census, a));
|
||||
expect_error(store.open(refused, "cpp", 1, ""), reason);
|
||||
expect_error(store.file_overlay_inventory(), Kind::unavailable_overlay);
|
||||
expect_error(store.validate_bound_sources(census, std::array{selected}), Kind::stale_read);
|
||||
expect(store.snapshot(uri) == doc);
|
||||
require(store.close(uri).has_value());
|
||||
expect_error(store.file_overlay_inventory(), Kind::unavailable_overlay);
|
||||
expect_error(store.resolve_content(b), Kind::unavailable_overlay);
|
||||
}
|
||||
}
|
||||
|
||||
VOID_TEST(BoundInputs, unbound_file_blocks_inventory_until_its_client_key_closes) {
|
||||
Files files;
|
||||
const auto bad = path_to_file_uri(files.path("missing/../hidden.cpp"));
|
||||
DocumentStore store;
|
||||
const auto before = get(store.file_overlay_inventory());
|
||||
const auto open = get(store.open(bad, "cpp", 1, "unbound"));
|
||||
require(open->identity.is_file_uri && open->identity.canonical_path.empty());
|
||||
expect_error(store.file_overlay_inventory(), Kind::unavailable_overlay);
|
||||
expect_error(store.validate_bound_sources(before), Kind::stale_read);
|
||||
require(store.close(bad).has_value());
|
||||
const auto restored = get(store.file_overlay_inventory(0));
|
||||
expect(!get(observe(store, restored, files.path("hidden.cpp"))).overlay());
|
||||
}
|
||||
|
||||
VOID_TEST(BoundInputs, retained_inputs_reject_another_store_at_the_same_address) {
|
||||
Files files;
|
||||
const auto file = files.path("open.cpp");
|
||||
const auto disk = files.path("disk.cpp");
|
||||
const auto uri = path_to_file_uri(file);
|
||||
std::optional<DocumentStore> store{std::in_place};
|
||||
const auto *address = &*store;
|
||||
require(store->open(uri, "cpp", 1, "kept").has_value());
|
||||
const auto old = get(store->file_overlay_inventory());
|
||||
const auto active = get(observe(*store, old, file));
|
||||
const auto absent = get(observe(*store, old, disk));
|
||||
const auto snapshot = get(store->materialize_bound_disk(absent, "disk\nkept"));
|
||||
store.reset();
|
||||
expect(active.overlay()->text == "kept" && binding(old, uri).content == active.overlay());
|
||||
expect(snapshot->line_index.line_count() == 2 && snapshot->text == "disk\nkept");
|
||||
store.emplace();
|
||||
require(&*store == address);
|
||||
require(store->open(uri, "cpp", 1, "kept").has_value());
|
||||
const auto fresh = get(store->file_overlay_inventory());
|
||||
const auto new_active = get(observe(*store, fresh, file));
|
||||
require(new_active.overlay()->overlay_epoch == active.overlay()->overlay_epoch);
|
||||
expect_error(store->validate_bound_sources(old), Kind::stale_read);
|
||||
expect_error(observe(*store, old, disk), Kind::stale_read);
|
||||
expect_error(store->materialize_bound_disk(absent, "disk"), Kind::stale_read);
|
||||
expect_error(store->validate_bound_sources(fresh, std::array{active}), Kind::stale_read);
|
||||
expect_error(store->validate_bound_sources(fresh, std::array{absent}), Kind::stale_read);
|
||||
expect(store->validate_bound_sources(fresh, std::array{new_active}).has_value());
|
||||
}
|
||||
|
||||
VOID_TEST(BoundInputs, disk_bytes_line_indexes_and_retained_snapshots_share_the_store_budget) {
|
||||
Files files;
|
||||
const auto path = files.path("large.cpp");
|
||||
const auto uri = path_to_file_uri(path);
|
||||
DocumentStore store{PositionEncoding::utf8,
|
||||
{.max_document_bytes = 6500, .max_snapshot_bytes = 8192}};
|
||||
{
|
||||
const auto empty = get(store.file_overlay_inventory());
|
||||
const auto absent = get(observe(store, empty, path));
|
||||
expect_error(store.materialize_bound_disk(absent, std::string(6501, 'x')),
|
||||
Kind::resource_limit);
|
||||
expect_error(store.materialize_bound_disk(absent, "\xF0\x9F"), Kind::invalid_utf8);
|
||||
expect_error(store.materialize_bound_disk(absent, std::string(2048, '\n')),
|
||||
Kind::resource_limit);
|
||||
std::string reserved = "tiny";
|
||||
reserved.reserve(8192);
|
||||
expect_error(store.materialize_bound_disk(absent, std::move(reserved)),
|
||||
Kind::resource_limit);
|
||||
const auto kept = get(store.materialize_bound_disk(absent, std::string(6000, 'a')));
|
||||
expect_error(store.materialize_bound_disk(absent, std::string(6000, 'b')),
|
||||
Kind::resource_limit);
|
||||
expect_error(store.open(uri, "cpp", 1, std::string(6000, 'c')), Kind::resource_limit);
|
||||
const auto refused = get(store.file_overlay_inventory());
|
||||
expect_error(binding(refused, uri).content, Kind::unavailable_overlay);
|
||||
expect(kept->text.size() == 6000);
|
||||
}
|
||||
{
|
||||
require(replace(store, uri, 2, std::string(6000, 'c')).has_value());
|
||||
const auto kept = get(store.file_overlay_inventory());
|
||||
require(store.close(uri).has_value());
|
||||
const auto empty = get(store.file_overlay_inventory());
|
||||
const auto absent = get(observe(store, empty, path));
|
||||
expect_error(store.materialize_bound_disk(absent, std::string(6000, 'a')),
|
||||
Kind::resource_limit);
|
||||
expect(binding(kept, uri).content.value()->text.size() == 6000);
|
||||
}
|
||||
const auto absent = get(observe(store, get(store.file_overlay_inventory()), path));
|
||||
expect(get(store.materialize_bound_disk(absent, std::string(6000, 'd')))->text.size() == 6000);
|
||||
}
|
||||
|
||||
VOID_TEST(BoundInputs, invalid_bound_names_and_encoding_changes_have_typed_outcomes) {
|
||||
Files files;
|
||||
const auto path = files.path("main.cpp");
|
||||
const auto uri = path_to_file_uri(path);
|
||||
DocumentStore store;
|
||||
const auto empty = get(store.file_overlay_inventory(0));
|
||||
for (const auto *bad : {"untitled:scratch", "broken", "file://remote/x.cpp", "file:///x%2Fy",
|
||||
"file:///dir/", "file:///dir/.", "file:///dir/.."}) {
|
||||
expect_error(store.observe_bound_source(empty, bad, path), Kind::invalid_uri);
|
||||
}
|
||||
for (const auto &bad :
|
||||
{fs::path{}, fs::path{"relative"}, files.path("x/../main.cpp"), files.path("dir/"),
|
||||
files.root / std::string{"a\0b", 3}, files.root / std::string{"bad\xFF"}}) {
|
||||
expect_error(store.observe_bound_source(empty, uri, bad), Kind::invalid_uri);
|
||||
}
|
||||
expect_error(
|
||||
store.observe_bound_source(empty, uri, files.root / std::string(kMaxDocumentUriBytes, 'x')),
|
||||
Kind::resource_limit);
|
||||
const auto absent = get(observe(store, empty, path));
|
||||
require(store.set_position_encoding(PositionEncoding::utf8));
|
||||
require(store.set_position_encoding(PositionEncoding::utf16));
|
||||
expect_error(store.validate_bound_sources(empty), Kind::stale_read);
|
||||
expect_error(store.materialize_bound_disk(absent, ""), Kind::stale_read);
|
||||
}
|
||||
@@ -0,0 +1,629 @@
|
||||
#include <Void/LanguageServer/Documents/DocumentStore.hpp>
|
||||
|
||||
#include <Void/Testing/Assert.hpp>
|
||||
#include <Void/Testing/Test.hpp>
|
||||
|
||||
#include <array>
|
||||
#include <chrono>
|
||||
#include <cstdlib>
|
||||
#include <filesystem>
|
||||
#include <fstream>
|
||||
#include <limits>
|
||||
#include <source_location>
|
||||
#include <string>
|
||||
#include <string_view>
|
||||
#include <system_error>
|
||||
#include <utility>
|
||||
|
||||
#ifndef _WIN32
|
||||
#include <Void/LanguageServer/Support/RegularFile.hpp>
|
||||
#include <sys/stat.h>
|
||||
#endif
|
||||
|
||||
namespace {
|
||||
|
||||
using namespace Void::LanguageServer::Documents;
|
||||
using Void::Testing::expect;
|
||||
using Void::Testing::expect_eq;
|
||||
using Void::Testing::require;
|
||||
|
||||
using ErrorKind = DocumentError::Kind;
|
||||
constexpr auto utf8 = PositionEncoding::utf8;
|
||||
constexpr auto utf16 = PositionEncoding::utf16;
|
||||
constexpr std::string_view virtual_uri = "untitled:document.cpp";
|
||||
|
||||
template <class T>
|
||||
void expect_error(const std::expected<T, DocumentError> &result, ErrorKind kind,
|
||||
std::source_location location = std::source_location::current()) {
|
||||
require(!result.has_value(), location);
|
||||
expect_eq(result.error().kind, kind, location);
|
||||
}
|
||||
|
||||
template <class T>
|
||||
T get(std::expected<T, DocumentError> result,
|
||||
std::source_location location = std::source_location::current()) {
|
||||
require(result.has_value(), location);
|
||||
return std::move(*result);
|
||||
}
|
||||
|
||||
TextEdit full(std::string text) { return TextEdit{std::nullopt, std::move(text)}; }
|
||||
|
||||
TextEdit edit(Position start, Position end, std::string text) {
|
||||
return TextEdit{Range{start, end}, std::move(text)};
|
||||
}
|
||||
|
||||
#ifndef _WIN32
|
||||
class TestDirectory {
|
||||
public:
|
||||
TestDirectory() {
|
||||
auto pattern = (std::filesystem::temp_directory_path() / "vls-documents-XXXXXX").string();
|
||||
const auto *created = ::mkdtemp(pattern.data());
|
||||
require(created != nullptr);
|
||||
path_ = created;
|
||||
}
|
||||
|
||||
TestDirectory(const TestDirectory &) = delete;
|
||||
TestDirectory &operator=(const TestDirectory &) = delete;
|
||||
|
||||
~TestDirectory() {
|
||||
std::error_code error;
|
||||
std::filesystem::remove_all(path_, error);
|
||||
}
|
||||
|
||||
std::filesystem::path path(std::string_view name) const { return path_ / name; }
|
||||
|
||||
private:
|
||||
std::filesystem::path path_;
|
||||
};
|
||||
|
||||
void write_file(const std::filesystem::path &path, std::string_view text) {
|
||||
std::ofstream file{path, std::ios::binary};
|
||||
file.write(text.data(), static_cast<std::streamsize>(text.size()));
|
||||
file.close();
|
||||
require(static_cast<bool>(file));
|
||||
}
|
||||
#endif
|
||||
|
||||
bool matches_fresh_index(const DocumentSnapshot &snapshot) {
|
||||
const LineIndex fresh{snapshot.text};
|
||||
if (fresh.line_count() != snapshot.line_index.line_count()) {
|
||||
return false;
|
||||
}
|
||||
for (std::uint32_t line = 0; line < fresh.line_count(); ++line) {
|
||||
if (fresh.line_start_offset(line) != snapshot.line_index.line_start_offset(line)) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
for (std::size_t offset = 0; offset <= snapshot.text.size(); ++offset) {
|
||||
if (fresh.offset_to_position(snapshot.text, offset, snapshot.encoding) !=
|
||||
snapshot.offset_to_position(offset)) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
} // namespace
|
||||
|
||||
VOID_TEST(Documents, positions_round_trip_and_clamp_in_both_encodings) {
|
||||
constexpr std::string_view text = "a\u0301\U0001F600\r\n\u03B2\rz\n";
|
||||
const LineIndex index{text};
|
||||
struct Boundary {
|
||||
std::size_t byte;
|
||||
Position utf8_position;
|
||||
Position utf16_position;
|
||||
};
|
||||
constexpr std::array boundaries{
|
||||
Boundary{0, {0, 0}, {0, 0}}, Boundary{1, {0, 1}, {0, 1}}, Boundary{3, {0, 3}, {0, 2}},
|
||||
Boundary{7, {0, 7}, {0, 4}}, Boundary{9, {1, 0}, {1, 0}}, Boundary{11, {1, 2}, {1, 1}},
|
||||
Boundary{12, {2, 0}, {2, 0}}, Boundary{13, {2, 1}, {2, 1}}, Boundary{14, {3, 0}, {3, 0}},
|
||||
};
|
||||
for (const auto &point : boundaries) {
|
||||
expect(index.position_to_offset(text, point.utf8_position, utf8) == point.byte);
|
||||
expect(index.position_to_offset(text, point.utf16_position, utf16) == point.byte);
|
||||
expect(index.offset_to_position(text, point.byte, utf8) == point.utf8_position);
|
||||
expect(index.offset_to_position(text, point.byte, utf16) == point.utf16_position);
|
||||
}
|
||||
for (const auto encoding : {utf8, utf16}) {
|
||||
expect(index.position_to_offset(text, {0, kMaxPositionValue}, encoding) == 7);
|
||||
expect(index.position_to_offset(text, {1, 800}, encoding) == 11);
|
||||
expect(index.position_to_offset(text, {3, 800}, encoding) == text.size());
|
||||
expect(!index.position_to_offset(text, {4, 0}, encoding));
|
||||
expect(!index.position_to_offset(text, {0, kMaxPositionValue + 1}, encoding));
|
||||
expect(!index.offset_to_position(text, text.size() + 1, encoding));
|
||||
expect(!index.offset_to_position(text, 8, encoding));
|
||||
expect(index.range_to_offsets(text, {{0, 99}, {1, 0}}, encoding) ==
|
||||
std::pair<std::size_t, std::size_t>{7, 9});
|
||||
expect(!index.range_to_offsets(text, {{1, 0}, {0, 99}}, encoding));
|
||||
for (const auto split : {2U, 4U, 5U, 6U, 10U}) {
|
||||
expect(!index.offset_to_position(text, split, encoding));
|
||||
}
|
||||
}
|
||||
expect(!index.position_to_offset(text, {0, 2}, utf8));
|
||||
expect(!index.position_to_offset(text, {0, 5}, utf8));
|
||||
expect(!index.position_to_offset(text, {0, 3}, utf16));
|
||||
expect(!index.line_start_offset(4));
|
||||
expect(!index.line_content_end_offset(text, 4));
|
||||
expect(!index.line_content_end_offset("different", 0));
|
||||
}
|
||||
|
||||
VOID_TEST(Documents, empty_text_and_truncated_utf8_have_safe_boundaries) {
|
||||
const LineIndex empty{""};
|
||||
expect_eq(empty.line_count(), std::size_t{1});
|
||||
expect(empty.position_to_offset("", {0, kMaxPositionValue}, utf16) == 0);
|
||||
expect(empty.offset_to_position("", 0, utf8) == Position{0, 0});
|
||||
expect(!empty.offset_to_position("", 1, utf8));
|
||||
|
||||
const std::string truncated{"\xF0\x9F"};
|
||||
const LineIndex broken{truncated};
|
||||
expect(!broken.position_to_offset(truncated, {0, 900}, utf8));
|
||||
expect(!broken.position_to_offset(truncated, {0, 900}, utf16));
|
||||
expect(!broken.offset_to_position(truncated, truncated.size(), utf16));
|
||||
|
||||
DocumentStore store;
|
||||
expect_error(store.open(virtual_uri, "cpp", 1, truncated), ErrorKind::invalid_utf8);
|
||||
expect(!store.snapshot(virtual_uri));
|
||||
auto restored = get(store.apply_changes(virtual_uri, 2, std::array{full("")}));
|
||||
expect(restored->position_to_offset({0, kMaxPositionValue}) == 0);
|
||||
expect(restored->version == 2);
|
||||
}
|
||||
|
||||
VOID_TEST(Documents, virtual_and_file_uri_spellings_are_validated) {
|
||||
const auto item = normalize_uri("VoId-GeNeRaTeD://host/unit%2Fpart#range");
|
||||
require(item.has_value());
|
||||
expect_eq(item->canonical_uri, std::string{"void-generated://host/unit%2Fpart#range"});
|
||||
expect_eq(item->display_uri, std::string{"VoId-GeNeRaTeD://host/unit%2Fpart#range"});
|
||||
expect(!item->is_file_uri && item->canonical_path.empty());
|
||||
expect(client_uri_key("void-generated://host/unit/part#range") != item->canonical_uri);
|
||||
for (const auto invalid :
|
||||
{"no-scheme", "3untitled:x", "untitled:", "untitled:a b", "untitled:x%00y",
|
||||
"untitled:x%ZZ", "file:relative", "file://remote/path", "file:////unc/path",
|
||||
"file:///x%2Fy", "file:///x%5cy", "file:///x%00y", "file:///x%FF", "file:///trailing%",
|
||||
"file:///x?query", "file:///x#fragment", "file:///x\\y"}) {
|
||||
expect(!normalize_uri(invalid));
|
||||
}
|
||||
expect(!normalize_uri(std::string_view{"untitled:a\0b", 12}));
|
||||
expect(!identity_from_path(std::filesystem::path{std::string{"/a\0b", 4}}));
|
||||
|
||||
DocumentStore store;
|
||||
auto snapshot = get(store.open("UNTITLED:Scratch", "cpp", -3, "virtual"));
|
||||
expect_eq(snapshot->identity.display_uri, std::string{"UNTITLED:Scratch"});
|
||||
expect(store.snapshot("untitled:Scratch") == snapshot);
|
||||
expect(!store.snapshot("untitled:scratch"));
|
||||
require(store.close("untitled:Scratch").has_value());
|
||||
}
|
||||
|
||||
VOID_TEST(Documents, ordered_edits_preserve_prior_snapshot_text_and_positions) {
|
||||
DocumentStore store;
|
||||
auto original = get(store.open(virtual_uri, "cpp", 5, "a\r\nb\n"));
|
||||
const std::array edits{
|
||||
edit({0, 1}, {0, 1}, "\u0301\U0001F600"),
|
||||
edit({0, 4}, {1, 0}, "\r"),
|
||||
edit({1, 0}, {1, 0}, "\n"),
|
||||
edit({1, 400}, {1, 400}, "!"),
|
||||
};
|
||||
auto changed = get(store.apply_changes(virtual_uri, 6, edits));
|
||||
expect_eq(changed->text, std::string{"a\u0301\U0001F600\r\nb!\n"});
|
||||
expect(changed->version == 6 && changed->revision_id > original->revision_id);
|
||||
expect(changed->origin == DocumentOrigin::overlay && !changed->same_input_as(*original));
|
||||
expect(matches_fresh_index(*changed));
|
||||
expect(store.snapshot(virtual_uri) == changed);
|
||||
|
||||
expect_eq(original->text, std::string{"a\r\nb\n"});
|
||||
expect(original->version == 5 && original->position_to_offset({1, 0}) == 3);
|
||||
require(store.close(virtual_uri).has_value());
|
||||
expect(!store.snapshot(virtual_uri));
|
||||
expect_eq(changed->text, std::string{"a\u0301\U0001F600\r\nb!\n"});
|
||||
}
|
||||
|
||||
VOID_TEST(Documents, edited_line_index_matches_rebuild_across_crlf_splices) {
|
||||
DocumentStore store{utf8};
|
||||
constexpr std::array inputs{
|
||||
"", "x", "\r", "\n", "\r\n", "\r\r\n", "x\r\ny\nz\rt", "x\r\U0001F600\n", "\rxx\n"};
|
||||
constexpr std::array replacements{"", "y", "\r", "\n",
|
||||
"\r\n", "\n\r", "\rhello\n", "\U0001F600"};
|
||||
for (const std::string_view input : inputs) {
|
||||
const LineIndex base{input};
|
||||
for (std::size_t begin = 0; begin <= input.size(); ++begin) {
|
||||
const auto start = base.offset_to_position(input, begin, utf8);
|
||||
if (!start) {
|
||||
continue;
|
||||
}
|
||||
for (std::size_t end = begin; end <= input.size(); ++end) {
|
||||
const auto stop = base.offset_to_position(input, end, utf8);
|
||||
if (!stop) {
|
||||
continue;
|
||||
}
|
||||
for (const std::string_view replacement : replacements) {
|
||||
require(store.open(virtual_uri, "cpp", 0, std::string{input}).has_value());
|
||||
const std::array edits{edit(*start, *stop, std::string{replacement})};
|
||||
const auto changed = get(store.apply_changes(virtual_uri, 1, edits));
|
||||
auto expected_text = std::string{input};
|
||||
expected_text.replace(begin, end - begin, replacement);
|
||||
expect_eq(changed->text, expected_text);
|
||||
expect(matches_fresh_index(*changed));
|
||||
require(store.close(virtual_uri).has_value());
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
VOID_TEST(Documents, failed_batch_has_no_partial_publication_and_requires_a_newer_replacement) {
|
||||
DocumentStore store;
|
||||
auto original = get(store.open(virtual_uri, "cpp", 8, "base\n"));
|
||||
const std::array invalid{full("unpublished"), edit({3, 0}, {3, 0}, "!")};
|
||||
expect_error(store.apply_changes(virtual_uri, 12, invalid), ErrorKind::invalid_range);
|
||||
expect(!store.snapshot(virtual_uri) && store.list_overlays().empty());
|
||||
expect_eq(original->text, std::string{"base\n"});
|
||||
expect(original->version == 8 && original->position_to_offset({1, 0}) == 5);
|
||||
expect_error(store.apply_changes(virtual_uri, 10, std::array{full("stale")}),
|
||||
ErrorKind::nonmonotonic_version);
|
||||
expect(!store.snapshot(virtual_uri));
|
||||
expect_error(store.open(virtual_uri, "cpp", 13, "duplicate"), ErrorKind::already_open);
|
||||
|
||||
const std::array recovery{full("a\U0001F600"), edit({0, 3}, {0, 3}, "!")};
|
||||
auto restored = get(store.apply_changes(virtual_uri, 13, recovery));
|
||||
expect(restored->version == 13);
|
||||
expect_eq(restored->text, std::string{"a\U0001F600!"});
|
||||
expect(restored->revision_id > original->revision_id);
|
||||
require(store.close(virtual_uri).has_value());
|
||||
|
||||
auto reopened = get(store.open(virtual_uri, "cpp", -20, "base\n"));
|
||||
expect(reopened->version == -20 && reopened->revision_id > restored->revision_id);
|
||||
expect(!reopened->same_input_as(*original));
|
||||
expect(reopened->content_hash == original->content_hash);
|
||||
}
|
||||
|
||||
VOID_TEST(Documents, malformed_and_stale_notifications_discard_readiness) {
|
||||
DocumentStore store;
|
||||
const auto min_version = std::numeric_limits<std::int32_t>::min();
|
||||
auto original = get(store.open(virtual_uri, "cpp", min_version, "initial"));
|
||||
expect(original->version == min_version);
|
||||
require(store
|
||||
.mark_desynchronized(virtual_uri, std::nullopt,
|
||||
{ErrorKind::malformed_change, "missing version"})
|
||||
.has_value());
|
||||
expect(!store.snapshot(virtual_uri));
|
||||
require(store.apply_changes(virtual_uri, 8, std::array{full("current")}).has_value());
|
||||
require(store
|
||||
.mark_desynchronized("UNTITLED:document.cpp", 6,
|
||||
{ErrorKind::malformed_change, "malformed range"})
|
||||
.has_value());
|
||||
expect(!store.snapshot(virtual_uri));
|
||||
expect_error(store.apply_changes(virtual_uri, 7, std::array{full("late")}),
|
||||
ErrorKind::nonmonotonic_version);
|
||||
require(
|
||||
store.mark_desynchronized(virtual_uri, 12, {ErrorKind::malformed_change, "invalid event"})
|
||||
.has_value());
|
||||
expect_error(store.apply_changes(virtual_uri, 10, std::array{full("late")}),
|
||||
ErrorKind::nonmonotonic_version);
|
||||
require(store.apply_changes(virtual_uri, 13, std::array{full("ready")}).has_value());
|
||||
|
||||
constexpr auto invalid_version = std::int64_t{std::numeric_limits<std::int32_t>::max()} + 1;
|
||||
expect_error(store.apply_changes(virtual_uri, invalid_version, std::array{full("invalid")}),
|
||||
ErrorKind::invalid_version);
|
||||
expect(!store.snapshot(virtual_uri));
|
||||
require(store.apply_changes(virtual_uri, 14, std::array{full("ready again")}).has_value());
|
||||
expect_error(store.open("untitled:bad-version", "cpp", invalid_version, ""),
|
||||
ErrorKind::invalid_version);
|
||||
expect_error(
|
||||
store.mark_desynchronized("untitled:unknown", 15, {ErrorKind::malformed_change, "unknown"}),
|
||||
ErrorKind::not_open);
|
||||
}
|
||||
|
||||
VOID_TEST(Documents, change_count_aggregate_text_and_document_size_are_bounded) {
|
||||
DocumentStore store{utf16, {.max_document_bytes = 8, .max_changes = 2, .max_change_bytes = 11}};
|
||||
expect_error(store.open(virtual_uri, "cpp", 1, "123456789"), ErrorKind::resource_limit);
|
||||
expect(!store.snapshot(virtual_uri));
|
||||
auto original = get(store.apply_changes(virtual_uri, 2, std::array{full("12345678")}));
|
||||
const std::array too_many{full("a"), full("b"), full("c")};
|
||||
expect_error(store.apply_changes(virtual_uri, 3, too_many), ErrorKind::resource_limit);
|
||||
const std::array too_many_bytes{full("12345678"), full("1234")};
|
||||
expect_error(store.apply_changes(virtual_uri, 4, too_many_bytes), ErrorKind::resource_limit);
|
||||
const std::array too_large{full("12345678"), edit({0, 8}, {0, 8}, "9")};
|
||||
expect_error(store.apply_changes(virtual_uri, 5, too_large), ErrorKind::resource_limit);
|
||||
expect(!store.snapshot(virtual_uri));
|
||||
expect_eq(original->text, std::string{"12345678"});
|
||||
require(store.apply_changes(virtual_uri, 6, std::array{full("recovery")}).has_value());
|
||||
}
|
||||
|
||||
#ifndef _WIN32
|
||||
VOID_TEST(Documents, prospective_names_preserve_the_actual_parent_traversal) {
|
||||
namespace fs = std::filesystem;
|
||||
TestDirectory temp;
|
||||
write_file(temp.path("x.cpp"), "int actual;\n");
|
||||
fs::create_symlink("absent", temp.path("dangling"));
|
||||
fs::create_directories(temp.path("real/sub"));
|
||||
fs::create_directory_symlink("real/sub", temp.path("link"));
|
||||
for (const auto *name :
|
||||
{"absent/../x.cpp", "dangling/../x.cpp", "x.cpp/../x.cpp", "dangling"}) {
|
||||
const auto uri = path_to_file_uri(temp.path(name));
|
||||
auto identity = normalize_uri(uri);
|
||||
require(identity.has_value());
|
||||
expect(identity->is_file_uri && identity->canonical_path.empty());
|
||||
expect(client_file_path(uri) == temp.path(name));
|
||||
}
|
||||
const auto name = temp.path("link/../new/child.cpp");
|
||||
auto future = identity_from_path(name);
|
||||
require(future.has_value());
|
||||
expect(future->canonical_path == temp.path("real/new/child.cpp"));
|
||||
}
|
||||
|
||||
VOID_TEST(Documents, file_aliases_preserve_client_binding_and_reject_ambiguous_lookup) {
|
||||
TestDirectory temp;
|
||||
const auto a = temp.path("first file#.cpp");
|
||||
const auto b = temp.path("other.cpp");
|
||||
const auto alias = temp.path("link.cpp");
|
||||
write_file(a, "first disk");
|
||||
write_file(b, "other disk");
|
||||
std::filesystem::create_symlink(a, alias);
|
||||
const auto a_uri = path_to_file_uri(a);
|
||||
const auto a_alt = "FILE://LOCALHOST" + a_uri.substr(7);
|
||||
const auto alias_uri = path_to_file_uri(alias);
|
||||
const auto a_identity = normalize_uri(a_alt);
|
||||
const auto alias_identity = normalize_uri(alias_uri);
|
||||
require(a_identity.has_value() && alias_identity.has_value());
|
||||
expect_eq(a_identity->canonical_uri, a_uri);
|
||||
expect(a_identity->canonical_uri.contains("first%20file%23.cpp"));
|
||||
expect(a_identity->canonical_path == alias_identity->canonical_path);
|
||||
expect(a_identity->canonical_uri != alias_identity->canonical_uri);
|
||||
expect(client_uri_key("file:" + a_uri.substr(7)) == a_uri);
|
||||
|
||||
DocumentStore store;
|
||||
auto first = get(store.open(a_alt, "cpp", 1, "first overlay"));
|
||||
expect_eq(first->identity.display_uri, a_alt);
|
||||
expect(store.snapshot(a_uri) == first);
|
||||
expect(get(store.resolve_content(alias, 1)) == first);
|
||||
expect_error(store.open(a_uri, "cpp", 2, "same lexical document"), ErrorKind::already_open);
|
||||
auto second = get(store.open(alias_uri, "cpp", 1, "second overlay"));
|
||||
expect(store.snapshot(alias_uri) == second);
|
||||
expect_error(store.resolve_content(a), ErrorKind::ambiguous_overlay);
|
||||
expect(!store.snapshot_by_path(a));
|
||||
|
||||
require(std::filesystem::remove(alias));
|
||||
std::filesystem::create_symlink(b, alias);
|
||||
expect(store.snapshot(alias_uri) == second);
|
||||
expect_error(store.resolve_content(alias), ErrorKind::stale_read);
|
||||
expect_eq(get(store.resolve_content(b))->text, std::string{"other disk"});
|
||||
auto updated = get(store.apply_changes(alias_uri, 2, std::array{full("bound to original")}));
|
||||
expect(updated->identity.canonical_path == first->identity.canonical_path);
|
||||
require(store.close(alias_uri).has_value());
|
||||
expect(get(store.resolve_content(a)) == first);
|
||||
expect_eq(get(store.resolve_content(alias))->text, std::string{"other disk"});
|
||||
}
|
||||
|
||||
VOID_TEST(Documents, dot_segments_are_resolved_after_symlinks) {
|
||||
TestDirectory temp;
|
||||
const auto a = temp.path("a");
|
||||
const auto b = temp.path("b");
|
||||
std::filesystem::create_directory(a);
|
||||
std::filesystem::create_directories(b / "inner");
|
||||
write_file(a / "test.cpp", "a");
|
||||
write_file(b / "test.cpp", "b");
|
||||
std::filesystem::create_symlink(b / "inner", a / "link");
|
||||
const auto indirect = a / "link" / ".." / "test.cpp";
|
||||
const auto indirect_uri = path_to_file_uri(indirect);
|
||||
const auto direct_uri = path_to_file_uri(a / "test.cpp");
|
||||
DocumentStore store;
|
||||
auto opened = get(store.open(indirect_uri, "cpp", 1, "open b"));
|
||||
expect(opened->identity.canonical_uri.contains("/link/../"));
|
||||
expect(get(store.resolve_content(b / "test.cpp")) == opened);
|
||||
expect_eq(get(store.resolve_content(a / "test.cpp"))->text, std::string{"a"});
|
||||
expect(!store.snapshot(direct_uri));
|
||||
}
|
||||
|
||||
VOID_TEST(Documents, disk_reads_are_typed_bounded_and_have_stable_input_identity) {
|
||||
TestDirectory temp;
|
||||
const auto source = temp.path("disk.cpp");
|
||||
const auto missing = temp.path("missing.cpp");
|
||||
const auto invalid = temp.path("invalid.cpp");
|
||||
const auto fifo = temp.path("named-pipe");
|
||||
write_file(source, "disk");
|
||||
write_file(invalid, "\xF0\x9F");
|
||||
require(::mkfifo(fifo.c_str(), 0600) == 0);
|
||||
DocumentStore store{utf16, {.max_document_bytes = 4}};
|
||||
expect_error(store.resolve_content(missing), ErrorKind::disk_read_failed);
|
||||
expect_error(store.resolve_content(temp.path("")), ErrorKind::disk_read_failed);
|
||||
expect_error(store.resolve_content(fifo), ErrorKind::disk_read_failed);
|
||||
expect_error(store.resolve_content(invalid), ErrorKind::invalid_utf8);
|
||||
expect_error(store.resolve_content(source, 3), ErrorKind::resource_limit);
|
||||
auto first = get(store.resolve_content(source));
|
||||
auto second = get(store.resolve_content(source));
|
||||
expect(first->origin == DocumentOrigin::disk && !first->version);
|
||||
expect(second->revision_id != first->revision_id && second->same_input_as(*first));
|
||||
expect(first->same_input_as(*second));
|
||||
|
||||
const auto alias = temp.path("disk-link.cpp");
|
||||
std::filesystem::create_symlink(source, alias);
|
||||
expect(get(store.resolve_content(alias))->same_input_as(*first));
|
||||
write_file(source, "edit");
|
||||
expect(!get(store.resolve_content(source))->same_input_as(*first));
|
||||
write_file(source, "disks");
|
||||
expect_error(store.resolve_content(source, 1000), ErrorKind::resource_limit);
|
||||
write_file(source, "disk");
|
||||
const auto uri = path_to_file_uri(source);
|
||||
auto overlay = get(store.open(uri, "cpp", 1, "disk"));
|
||||
expect(!overlay->same_input_as(*first));
|
||||
require(store.close(uri).has_value());
|
||||
auto after_close = get(store.resolve_content(source));
|
||||
expect(!after_close->same_input_as(*first) &&
|
||||
after_close->overlay_epoch > first->overlay_epoch);
|
||||
}
|
||||
|
||||
VOID_TEST(Documents, snapshot_budget_counts_retained_references_and_saves_desync_state) {
|
||||
TestDirectory temp;
|
||||
const auto first = temp.path("first.cpp");
|
||||
const auto second = temp.path("second.cpp");
|
||||
write_file(first, "first disk");
|
||||
write_file(second, "second disk");
|
||||
const auto first_uri = path_to_file_uri(first);
|
||||
const auto second_uri = path_to_file_uri(second);
|
||||
DocumentStore store{utf16, {.max_snapshot_bytes = 8 * 1024}};
|
||||
const std::string large{std::string(6 * 1024, 'x')};
|
||||
auto kept = get(store.open(first_uri, "cpp", 1, large));
|
||||
require(store.mark_desynchronized(first_uri, 12, {ErrorKind::malformed_change, "bad late edit"})
|
||||
.has_value());
|
||||
expect_error(store.resolve_content(first), ErrorKind::desynchronized);
|
||||
expect_error(store.apply_changes(first_uri, 10, std::array{full("stale")}),
|
||||
ErrorKind::nonmonotonic_version);
|
||||
require(store.close(first_uri).has_value());
|
||||
expect_error(store.open(second_uri, "cpp", 1, large), ErrorKind::resource_limit);
|
||||
expect_error(store.resolve_content(second), ErrorKind::unavailable_overlay);
|
||||
expect_eq(kept->text, large);
|
||||
expect(kept->position_to_offset({0, 9000}) == large.size());
|
||||
|
||||
kept.reset();
|
||||
auto restored = get(store.apply_changes(second_uri, 2, std::array{full(large)}));
|
||||
expect(store.snapshot_by_path(second) == restored);
|
||||
expect(restored->version == 2);
|
||||
require(store.close(second_uri).has_value());
|
||||
restored.reset();
|
||||
expect_error(store.open(first_uri, "cpp", 1, std::string(2048, '\n')),
|
||||
ErrorKind::resource_limit);
|
||||
expect_error(store.resolve_content(first), ErrorKind::unavailable_overlay);
|
||||
}
|
||||
|
||||
VOID_TEST(Documents, refused_open_owns_its_content_and_marker_overflow_fails_closed) {
|
||||
TestDirectory temp;
|
||||
const auto a = temp.path("a.cpp");
|
||||
const auto b = temp.path("b.cpp");
|
||||
const auto c = temp.path("c.cpp");
|
||||
write_file(a, "disk a");
|
||||
write_file(b, "disk b");
|
||||
write_file(c, "disk c");
|
||||
const auto a_uri = path_to_file_uri(a);
|
||||
const auto b_uri = path_to_file_uri(b);
|
||||
const auto c_uri = path_to_file_uri(c);
|
||||
DocumentStore store{utf16, {.max_open_documents = 1, .max_unavailable_documents = 1}};
|
||||
auto a_snapshot = get(store.open(a_uri, "cpp", 1, "overlay a"));
|
||||
expect_error(store.open(b_uri, "cpp", 1, "overlay b"), ErrorKind::resource_limit);
|
||||
expect_error(store.resolve_content(b), ErrorKind::unavailable_overlay);
|
||||
expect_error(store.open(c_uri, "cpp", 1, "overlay c"), ErrorKind::resource_limit);
|
||||
expect_error(store.resolve_content(c), ErrorKind::unavailable_overlay);
|
||||
expect(store.snapshot(a_uri) == a_snapshot);
|
||||
require(store.close(b_uri).has_value());
|
||||
require(store.close(a_uri).has_value());
|
||||
expect_error(store.resolve_content(c), ErrorKind::unavailable_overlay);
|
||||
expect_error(store.resolve_content(a), ErrorKind::unavailable_overlay);
|
||||
expect_eq(get(DocumentStore{}.resolve_content(c))->text, std::string{"disk c"});
|
||||
}
|
||||
|
||||
VOID_TEST(Documents, late_open_wins_over_successful_and_failed_disk_reads) {
|
||||
TestDirectory temp;
|
||||
const auto existing = temp.path("existing.cpp");
|
||||
const auto missing = temp.path("missing.cpp");
|
||||
const auto invalid = temp.path("invalid.cpp");
|
||||
write_file(existing, "disk input");
|
||||
write_file(invalid, "\xF0\x9F");
|
||||
for (const auto &path : {existing, missing, invalid}) {
|
||||
const auto uri = path_to_file_uri(path);
|
||||
unsigned callbacks = 0;
|
||||
DocumentStore store{utf16, {}, [&] {
|
||||
++callbacks;
|
||||
require(store.open(uri, "cpp", 1, "new overlay").has_value());
|
||||
}};
|
||||
auto result = get(store.resolve_content(path));
|
||||
expect_eq(callbacks, 1U);
|
||||
expect_eq(result->text, std::string{"new overlay"});
|
||||
expect(result == store.snapshot(uri));
|
||||
}
|
||||
}
|
||||
|
||||
VOID_TEST(Documents, late_unavailable_and_desynchronized_opens_preempt_disk) {
|
||||
TestDirectory temp;
|
||||
const auto path = temp.path("real.cpp");
|
||||
write_file(path, "old text");
|
||||
const auto uri = path_to_file_uri(path);
|
||||
DocumentStore refused{utf16, {.max_open_documents = 0}, [&] {
|
||||
expect_error(refused.open(uri, "cpp", 1, "overlay"),
|
||||
ErrorKind::resource_limit);
|
||||
}};
|
||||
expect_error(refused.resolve_content(path), ErrorKind::unavailable_overlay);
|
||||
|
||||
DocumentStore broken{
|
||||
utf16, {}, [&] {
|
||||
require(broken.open(uri, "cpp", 1, "overlay").has_value());
|
||||
require(broken.mark_desynchronized(uri, 2, {ErrorKind::malformed_change, "bad field"})
|
||||
.has_value());
|
||||
}};
|
||||
expect_error(broken.resolve_content(path), ErrorKind::desynchronized);
|
||||
}
|
||||
|
||||
VOID_TEST(Documents, open_close_aba_rejects_old_success_and_error) {
|
||||
TestDirectory temp;
|
||||
const auto existing = temp.path("exists.cpp");
|
||||
const auto missing = temp.path("missing.cpp");
|
||||
write_file(existing, "disk text");
|
||||
for (const auto &path : {existing, missing}) {
|
||||
const auto uri = path_to_file_uri(path);
|
||||
unsigned callbacks = 0;
|
||||
DocumentStore store{utf16, {}, [&] {
|
||||
++callbacks;
|
||||
require(store.open(uri, "cpp", 1, "transient").has_value());
|
||||
require(store.close(uri).has_value());
|
||||
}};
|
||||
expect_error(store.resolve_content(path), ErrorKind::stale_read);
|
||||
expect_eq(callbacks, 1U);
|
||||
expect(!store.snapshot(uri));
|
||||
}
|
||||
}
|
||||
|
||||
VOID_TEST(Documents, mid_read_modification_and_late_replacement_are_stale) {
|
||||
namespace Files = Void::LanguageServer::Detail;
|
||||
TestDirectory temp;
|
||||
const auto file = temp.path("edited.cpp");
|
||||
const std::string original(160 * 1024, 'a');
|
||||
const std::string changed(original.size(), 'b');
|
||||
write_file(file, original);
|
||||
bool replaced = false;
|
||||
const auto read = Files::read_regular(file, original.size(), false, [&](auto chunk) {
|
||||
if (!replaced) {
|
||||
require(chunk.size() < original.size());
|
||||
write_file(file, changed);
|
||||
// Force a distinct timestamp even on a filesystem with coarse resolution.
|
||||
std::filesystem::last_write_time(file, std::filesystem::last_write_time(file) -
|
||||
std::chrono::seconds{5});
|
||||
replaced = true;
|
||||
}
|
||||
return true;
|
||||
});
|
||||
require(replaced);
|
||||
expect(!read && read.error() == Files::FileError::changed);
|
||||
|
||||
DocumentStore store{utf16, {}, [&] {
|
||||
const auto next = temp.path("replacement.cpp");
|
||||
write_file(next, original);
|
||||
std::filesystem::rename(next, file);
|
||||
}};
|
||||
expect_error(store.resolve_content(file), ErrorKind::stale_read);
|
||||
expect(get(DocumentStore{}.resolve_content(file))->text == original);
|
||||
}
|
||||
|
||||
VOID_TEST(Documents, disk_reads_revalidate_path_and_encoding_before_return) {
|
||||
TestDirectory temp;
|
||||
const auto before = temp.path("before.cpp");
|
||||
const auto after = temp.path("after.cpp");
|
||||
const auto link = temp.path("link.cpp");
|
||||
write_file(before, "before");
|
||||
write_file(after, "after");
|
||||
std::filesystem::create_symlink(before, link);
|
||||
DocumentStore moved{utf16, {}, [&] {
|
||||
require(std::filesystem::remove(link));
|
||||
std::filesystem::create_symlink(after, link);
|
||||
}};
|
||||
expect_error(moved.resolve_content(link), ErrorKind::stale_read);
|
||||
|
||||
unsigned callbacks = 0;
|
||||
DocumentStore negotiation{utf16, {}, [&] {
|
||||
if (++callbacks == 1) {
|
||||
require(negotiation.set_position_encoding(utf8));
|
||||
}
|
||||
}};
|
||||
expect_error(negotiation.resolve_content(after), ErrorKind::stale_read);
|
||||
expect(get(negotiation.resolve_content(after))->encoding == utf8);
|
||||
auto open = get(negotiation.open(path_to_file_uri(after), "cpp", 1, "text"));
|
||||
expect(!negotiation.set_position_encoding(utf16));
|
||||
expect(negotiation.position_encoding() == utf8 && open->encoding == utf8);
|
||||
}
|
||||
#endif
|
||||
@@ -0,0 +1,139 @@
|
||||
#include <Void/LanguageServer/Documents/DocumentStore.hpp>
|
||||
|
||||
#include <Void/Testing/Assert.hpp>
|
||||
#include <Void/Testing/Test.hpp>
|
||||
|
||||
#include <array>
|
||||
#include <cstddef>
|
||||
#include <cstdint>
|
||||
#include <optional>
|
||||
#include <string>
|
||||
#include <string_view>
|
||||
#include <utility>
|
||||
|
||||
namespace {
|
||||
|
||||
using namespace Void::LanguageServer::Documents;
|
||||
using Void::Testing::expect;
|
||||
using Void::Testing::expect_eq;
|
||||
using Void::Testing::require;
|
||||
|
||||
constexpr auto utf8 = PositionEncoding::utf8;
|
||||
constexpr auto utf16 = PositionEncoding::utf16;
|
||||
constexpr std::uint32_t line_bytes = 64 * 1024;
|
||||
constexpr std::uint32_t pivot = line_bytes / 2 - 1;
|
||||
constexpr std::string_view uri = "untitled:long-line.cpp";
|
||||
constexpr auto astral = "\U0001F600";
|
||||
|
||||
} // namespace
|
||||
|
||||
VOID_TEST(LongLines, ascii_endpoints_clamp_and_text_size_stays_checked) {
|
||||
const auto text = std::string(line_bytes, 'a') + "\r\nz";
|
||||
const auto shorter = std::string_view{text}.substr(0, text.size() - 1);
|
||||
const LineIndex index{text};
|
||||
for (const auto encoding : {utf8, utf16}) {
|
||||
expect(index.range_to_offsets(text, {{0, 2}, {0, 3}}, encoding) ==
|
||||
std::pair<std::size_t, std::size_t>{2, 3});
|
||||
expect(
|
||||
index.range_to_offsets(text, {{0, line_bytes - 3}, {0, kMaxPositionValue}}, encoding) ==
|
||||
std::pair<std::size_t, std::size_t>{line_bytes - 3, line_bytes});
|
||||
expect(index.range_to_offsets(text, {{0, line_bytes + 1}, {1, 0}}, encoding) ==
|
||||
std::pair<std::size_t, std::size_t>{line_bytes, line_bytes + 2});
|
||||
expect(index.offset_to_position(text, line_bytes, encoding) == Position{0, line_bytes});
|
||||
expect(!index.offset_to_position(text, line_bytes + 1, encoding));
|
||||
expect(!index.offset_to_position(text, text.size() + 1, encoding));
|
||||
expect(!index.position_to_offset(text, {kMaxPositionValue + 1, 0}, encoding));
|
||||
expect(!index.position_to_offset(text, {0, kMaxPositionValue + 1}, encoding));
|
||||
expect(
|
||||
!index.range_to_offsets(text, {{0, line_bytes}, {0, kMaxPositionValue + 1}}, encoding));
|
||||
expect(!index.range_to_offsets(text, {{0, line_bytes}, {0, 2}}, encoding));
|
||||
expect(!index.position_to_offset(shorter, {0, 2}, encoding));
|
||||
expect(!index.offset_to_position(shorter, 2, encoding));
|
||||
expect(!index.range_to_offsets(shorter, {{0, 2}, {0, 3}}, encoding));
|
||||
}
|
||||
}
|
||||
|
||||
VOID_TEST(LongLines, mixed_ascii_and_unicode_ranges_check_both_endpoints) {
|
||||
auto text = std::string(pivot, 'a') + "\u03B2" + astral;
|
||||
text.append(line_bytes - pivot, 'b');
|
||||
const LineIndex index{text};
|
||||
for (const auto encoding : {utf8, utf16}) {
|
||||
const auto astral_start = pivot + (encoding == utf8 ? 2 : 1);
|
||||
const auto end = line_bytes + (encoding == utf8 ? 6 : 3);
|
||||
expect(index.range_to_offsets(text, {{0, astral_start}, {0, end}}, encoding) ==
|
||||
std::pair<std::size_t, std::size_t>{pivot + 2, text.size()});
|
||||
expect(index.position_to_offset(text, {0, kMaxPositionValue}, encoding) == text.size());
|
||||
expect(index.offset_to_position(text, text.size(), encoding) == Position{0, end});
|
||||
|
||||
const auto split = astral_start + 1;
|
||||
expect(!index.range_to_offsets(text, {{0, pivot}, {0, split}}, encoding));
|
||||
expect(!index.range_to_offsets(text, {{0, split}, {0, end}}, encoding));
|
||||
expect(!index.offset_to_position(text, pivot + 3, encoding));
|
||||
}
|
||||
}
|
||||
|
||||
VOID_TEST(LongLines, invalid_utf8_after_an_ascii_run_is_still_rejected) {
|
||||
for (const std::string_view suffix : {"\x80", "\xF0\x9F"}) {
|
||||
const auto text = std::string(pivot, 'a') + std::string{suffix};
|
||||
const LineIndex index{text};
|
||||
for (const auto encoding : {utf8, utf16}) {
|
||||
expect(index.position_to_offset(text, {0, pivot}, encoding) == pivot);
|
||||
const auto range =
|
||||
index.range_to_offsets(text, {{0, pivot - 1}, {0, kMaxPositionValue}}, encoding);
|
||||
require(!range.has_value());
|
||||
expect_eq(range.error(), std::string{"invalid UTF-8 encountered on line"});
|
||||
expect(!index.offset_to_position(text, text.size(), encoding));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
VOID_TEST(LongLines, edits_insert_remove_and_rebuild_unicode_positions) {
|
||||
const std::string ascii(line_bytes, 'a');
|
||||
for (const auto encoding : {utf8, utf16}) {
|
||||
DocumentStore store{encoding};
|
||||
const auto original = store.open(uri, "cpp", 1, ascii);
|
||||
require(original.has_value());
|
||||
|
||||
const auto scalar_units = encoding == utf8 ? 4U : 2U;
|
||||
const auto after_scalar = pivot + scalar_units;
|
||||
const std::array adding{
|
||||
TextEdit{Range{{0, pivot}, {0, pivot + 1}}, astral},
|
||||
TextEdit{Range{{0, after_scalar}, {0, after_scalar + 1}}, "x"},
|
||||
};
|
||||
const auto mixed = store.apply_changes(uri, 2, adding);
|
||||
require(mixed.has_value());
|
||||
auto expected_mixed = ascii;
|
||||
expected_mixed.replace(pivot, 2, std::string{astral} + "x");
|
||||
expect_eq((*mixed)->text, expected_mixed);
|
||||
expect((*mixed)->position_to_offset({0, after_scalar + 1}) == pivot + 5);
|
||||
|
||||
const std::array removing{
|
||||
TextEdit{Range{{0, pivot}, {0, after_scalar}}, "a"},
|
||||
TextEdit{Range{{0, pivot + 1}, {0, pivot + 2}}, "a"},
|
||||
};
|
||||
const auto restored = store.apply_changes(uri, 3, removing);
|
||||
require(restored.has_value());
|
||||
expect_eq((*restored)->text, ascii);
|
||||
expect((*restored)->position_to_offset({0, kMaxPositionValue}) == line_bytes);
|
||||
|
||||
const std::array rebuilding{
|
||||
TextEdit{std::nullopt, ascii + "\r" + astral + "a"},
|
||||
TextEdit{Range{{1, 0}, {1, 0}}, "\n"},
|
||||
TextEdit{Range{{1, scalar_units}, {1, scalar_units + 1}}, "!"},
|
||||
};
|
||||
const auto rebuilt = store.apply_changes(uri, 4, rebuilding);
|
||||
require(rebuilt.has_value());
|
||||
expect_eq((*rebuilt)->text, ascii + "\r\n" + astral + "!");
|
||||
expect((*rebuilt)->position_to_offset({1, scalar_units}) == line_bytes + 6);
|
||||
expect(!(*rebuilt)->offset_to_position(line_bytes + 1));
|
||||
|
||||
const auto replaced =
|
||||
store.apply_changes(uri, 5, std::array{TextEdit{std::nullopt, ascii}});
|
||||
require(replaced.has_value());
|
||||
expect((*replaced)->offset_to_position(line_bytes) == Position{0, line_bytes});
|
||||
expect(!(*replaced)->position_to_offset({1, 0}));
|
||||
expect((*original)->position_to_offset({0, pivot + 1}) == pivot + 1);
|
||||
expect(!(*mixed)->position_to_offset({0, pivot + 1}));
|
||||
expect_eq((*mixed)->text, expected_mixed);
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user