#include #include #include #include #include #include #include #include #include #include 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{2, 3}); expect( index.range_to_offsets(text, {{0, line_bytes - 3}, {0, kMaxPositionValue}}, encoding) == std::pair{line_bytes - 3, line_bytes}); expect(index.range_to_offsets(text, {{0, line_bytes + 1}, {1, 0}}, encoding) == std::pair{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{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); } }