feat: perform gutter diffing against git HEAD if available
This commit is contained in:
parent
18983c00b4
commit
1190c99212
2 changed files with 19 additions and 19 deletions
10
src/diff.zig
10
src/diff.zig
|
|
@ -47,10 +47,14 @@ pub const AsyncDiffer = struct {
|
||||||
|
|
||||||
pub const CallBack = fn (from: tp.pid_ref, edits: []Diff) void;
|
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 {
|
pub fn diff_buffer(self: @This(), cb: *const CallBack, buffer: *const Buffer) tp.result {
|
||||||
const text_dst = text_from_root(root_dst, eol_mode) catch |e| return tp.exit_error(e, @errorReturnTrace());
|
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);
|
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);
|
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_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;
|
const text_src_ptr: usize = if (text_src.len > 0) @intFromPtr(text_src.ptr) else 0;
|
||||||
|
|
|
||||||
|
|
@ -338,11 +338,7 @@ fn diff_update(self: *Self) !void {
|
||||||
self.diff_symbols_clear();
|
self.diff_symbols_clear();
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
const editor = self.editor;
|
return self.diff_.diff_buffer(diff_result, self.editor.buffer orelse return);
|
||||||
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);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
fn diff_result(from: tp.pid_ref, edits: []diff.Diff) void {
|
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 {
|
fn diff_result_send(from: tp.pid_ref, edits: []diff.Diff) !void {
|
||||||
var buf: [tp.max_message_size]u8 = undefined;
|
var buf: std.Io.Writer.Allocating = .init(std.heap.c_allocator);
|
||||||
var writer: std.Io.Writer = .fixed(&buf);
|
defer buf.deinit();
|
||||||
try cbor.writeArrayHeader(&writer, 2);
|
try cbor.writeArrayHeader(&buf.writer, 2);
|
||||||
try cbor.writeValue(&writer, "DIFF");
|
try cbor.writeValue(&buf.writer, "DIFF");
|
||||||
try cbor.writeArrayHeader(&writer, edits.len);
|
try cbor.writeArrayHeader(&buf.writer, edits.len);
|
||||||
for (edits) |edit| {
|
for (edits) |edit| {
|
||||||
try cbor.writeArrayHeader(&writer, 4);
|
try cbor.writeArrayHeader(&buf.writer, 4);
|
||||||
try cbor.writeValue(&writer, switch (edit.kind) {
|
try cbor.writeValue(&buf.writer, switch (edit.kind) {
|
||||||
.insert => "I",
|
.insert => "I",
|
||||||
.delete => "D",
|
.delete => "D",
|
||||||
});
|
});
|
||||||
try cbor.writeValue(&writer, edit.line);
|
try cbor.writeValue(&buf.writer, edit.line);
|
||||||
try cbor.writeValue(&writer, edit.offset);
|
try cbor.writeValue(&buf.writer, edit.offset);
|
||||||
try cbor.writeValue(&writer, edit.bytes);
|
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 {
|
pub fn process_diff(self: *Self, cb: []const u8) MessageFilter.Error!void {
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue