feat: perform gutter diffing against git HEAD if available

This commit is contained in:
CJ van den Berg 2025-12-17 22:11:52 +01:00
parent 18983c00b4
commit 1190c99212
Signed by: neurocyte
GPG key ID: 8EB1E1BB660E3FB9
2 changed files with 19 additions and 19 deletions

View file

@ -47,10 +47,14 @@ pub const AsyncDiffer = struct {
pub const CallBack = fn (from: tp.pid_ref, edits: []Diff) void;
pub fn diff(self: @This(), cb: *const CallBack, root_dst: Buffer.Root, root_src: Buffer.Root, eol_mode: Buffer.EolMode) tp.result {
const text_dst = text_from_root(root_dst, eol_mode) catch |e| return tp.exit_error(e, @errorReturnTrace());
pub fn diff_buffer(self: @This(), cb: *const CallBack, buffer: *const Buffer) tp.result {
const eol_mode = buffer.file_eol_mode;
const text_dst = text_from_root(buffer.root, eol_mode) catch |e| return tp.exit_error(e, @errorReturnTrace());
errdefer std.heap.c_allocator.free(text_dst);
const text_src = text_from_root(root_src, eol_mode) catch |e| return tp.exit_error(e, @errorReturnTrace());
const text_src = if (buffer.get_vcs_content()) |vcs_content|
std.heap.c_allocator.dupe(u8, vcs_content) catch |e| return tp.exit_error(e, @errorReturnTrace())
else
text_from_root(buffer.last_save orelse return, eol_mode) catch |e| return tp.exit_error(e, @errorReturnTrace());
errdefer std.heap.c_allocator.free(text_src);
const text_dst_ptr: usize = if (text_dst.len > 0) @intFromPtr(text_dst.ptr) else 0;
const text_src_ptr: usize = if (text_src.len > 0) @intFromPtr(text_src.ptr) else 0;

View file

@ -338,11 +338,7 @@ fn diff_update(self: *Self) !void {
self.diff_symbols_clear();
return;
}
const editor = self.editor;
const new = editor.get_current_root() orelse return;
const old = if (editor.buffer) |buffer| buffer.last_save orelse return else return;
const eol_mode = if (editor.buffer) |buffer| buffer.file_eol_mode else return;
return self.diff_.diff(diff_result, new, old, eol_mode);
return self.diff_.diff_buffer(diff_result, self.editor.buffer orelse return);
}
fn diff_result(from: tp.pid_ref, edits: []diff.Diff) void {
@ -350,22 +346,22 @@ fn diff_result(from: tp.pid_ref, edits: []diff.Diff) void {
}
fn diff_result_send(from: tp.pid_ref, edits: []diff.Diff) !void {
var buf: [tp.max_message_size]u8 = undefined;
var writer: std.Io.Writer = .fixed(&buf);
try cbor.writeArrayHeader(&writer, 2);
try cbor.writeValue(&writer, "DIFF");
try cbor.writeArrayHeader(&writer, edits.len);
var buf: std.Io.Writer.Allocating = .init(std.heap.c_allocator);
defer buf.deinit();
try cbor.writeArrayHeader(&buf.writer, 2);
try cbor.writeValue(&buf.writer, "DIFF");
try cbor.writeArrayHeader(&buf.writer, edits.len);
for (edits) |edit| {
try cbor.writeArrayHeader(&writer, 4);
try cbor.writeValue(&writer, switch (edit.kind) {
try cbor.writeArrayHeader(&buf.writer, 4);
try cbor.writeValue(&buf.writer, switch (edit.kind) {
.insert => "I",
.delete => "D",
});
try cbor.writeValue(&writer, edit.line);
try cbor.writeValue(&writer, edit.offset);
try cbor.writeValue(&writer, edit.bytes);
try cbor.writeValue(&buf.writer, edit.line);
try cbor.writeValue(&buf.writer, edit.offset);
try cbor.writeValue(&buf.writer, edit.bytes);
}
from.send_raw(tp.message{ .buf = writer.buffered() }) catch return;
from.send_raw(tp.message{ .buf = buf.written() }) catch return;
}
pub fn process_diff(self: *Self, cb: []const u8) MessageFilter.Error!void {