Switch to Diff Match Patch (diffz) (#982)

* Add tests, note about correctness issue

* Use diffz (DiffMatchPatch)
This commit is contained in:
Auguste Rame
2023-02-11 14:21:10 -05:00
committed by GitHub
parent 73d6264cab
commit bf19ed3ea9
9 changed files with 150 additions and 341 deletions

View File

@@ -5,6 +5,7 @@ comptime {
_ = @import("utility/offsets.zig");
_ = @import("utility/position_context.zig");
_ = @import("utility/uri.zig");
_ = @import("utility/diff.zig");
// TODO Lifecycle Messages

30
tests/utility/diff.zig Normal file
View File

@@ -0,0 +1,30 @@
const std = @import("std");
const zls = @import("zls");
const allocator = std.testing.allocator;
fn gen(alloc: std.mem.Allocator, rand: std.rand.Random) ![]const u8 {
var buffer = try alloc.alloc(u8, rand.intRangeAtMost(usize, 16, 1024));
for (buffer) |*b| b.* = rand.intRangeAtMost(u8, 32, 126);
return buffer;
}
test "diff - random" {
var arena = std.heap.ArenaAllocator.init(allocator);
defer arena.deinit();
var rand = std.rand.DefaultPrng.init(0);
var index: usize = 0;
while (index < 100) : (index += 1) {
defer _ = arena.reset(.retain_capacity);
const pre = try gen(arena.allocator(), rand.random());
const post = try gen(arena.allocator(), rand.random());
var edits = try zls.diff.edits(arena.allocator(), pre, post, .@"utf-8");
const applied = try zls.diff.applyTextEdits(arena.allocator(), pre, edits.items, .@"utf-8");
try std.testing.expectEqualStrings(post, applied);
}
}