refactor: move change diffs to editor

This commit is contained in:
CJ van den Berg 2026-01-13 18:40:25 +01:00
parent 6ef5ea1336
commit 53f5096ef5
Signed by: neurocyte
GPG key ID: 8EB1E1BB660E3FB9
2 changed files with 9 additions and 8 deletions

View file

@ -19,6 +19,7 @@ const input = @import("input");
const command = @import("command"); const command = @import("command");
const EventHandler = @import("EventHandler"); const EventHandler = @import("EventHandler");
const snippet = @import("snippet"); const snippet = @import("snippet");
const Diff = @import("diff").LineDiff;
const scrollbar_v = @import("scrollbar_v.zig"); const scrollbar_v = @import("scrollbar_v.zig");
const editor_gutter = @import("editor_gutter.zig"); const editor_gutter = @import("editor_gutter.zig");
@ -421,6 +422,8 @@ pub const Editor = struct {
completion_col: usize = 0, completion_col: usize = 0,
completion_is_complete: bool = true, completion_is_complete: bool = true,
changes: std.ArrayList(Diff) = .empty,
checked_formatter: bool = false, checked_formatter: bool = false,
formatter: ?[]const []const u8 = null, formatter: ?[]const []const u8 = null,
enable_format_on_save: bool, enable_format_on_save: bool,
@ -613,6 +616,7 @@ pub const Editor = struct {
for (self.diagnostics.items) |*d| d.deinit(self.allocator); for (self.diagnostics.items) |*d| d.deinit(self.allocator);
self.diagnostics.deinit(self.allocator); self.diagnostics.deinit(self.allocator);
self.completions.deinit(self.allocator); self.completions.deinit(self.allocator);
self.changes.deinit(self.allocator);
self.clear_event_triggers(); self.clear_event_triggers();
if (self.syntax) |syn| syn.destroy(tui.query_cache()); if (self.syntax) |syn| syn.destroy(tui.query_cache());
self.cancel_all_tabstops(); self.cancel_all_tabstops();

View file

@ -38,7 +38,6 @@ width: usize = 4,
editor: *ed.Editor, editor: *ed.Editor,
editor_widget: ?*const Widget = null, editor_widget: ?*const Widget = null,
differ: diffz.AsyncDiffer, differ: diffz.AsyncDiffer,
diff_symbols: std.ArrayList(Diff),
const Self = @This(); const Self = @This();
@ -55,7 +54,6 @@ pub fn create(allocator: Allocator, parent: Widget, event_source: Widget, editor
.symbols = tui.config().gutter_symbols, .symbols = tui.config().gutter_symbols,
.editor = editor, .editor = editor,
.differ = try diffz.create(), .differ = try diffz.create(),
.diff_symbols = .empty,
}; };
try tui.message_filters().add(MessageFilter.bind(self, filter_receive)); try tui.message_filters().add(MessageFilter.bind(self, filter_receive));
try event_source.subscribe(EventHandler.bind(self, handle_event)); try event_source.subscribe(EventHandler.bind(self, handle_event));
@ -68,14 +66,13 @@ pub fn widget(self: *Self) Widget {
pub fn deinit(self: *Self, allocator: Allocator) void { pub fn deinit(self: *Self, allocator: Allocator) void {
self.diff_symbols_clear(); self.diff_symbols_clear();
self.diff_symbols.deinit(self.allocator);
tui.message_filters().remove_ptr(self); tui.message_filters().remove_ptr(self);
self.plane.deinit(); self.plane.deinit();
allocator.destroy(self); allocator.destroy(self);
} }
fn diff_symbols_clear(self: *Self) void { fn diff_symbols_clear(self: *Self) void {
self.diff_symbols.clearRetainingCapacity(); self.editor.changes.clearRetainingCapacity();
} }
pub fn handle_event(self: *Self, _: tp.pid_ref, m: tp.message) tp.result { pub fn handle_event(self: *Self, _: tp.pid_ref, m: tp.message) tp.result {
@ -174,7 +171,7 @@ pub fn render_none(self: *Self, theme: *const Widget.Theme) void {
var pos: usize = 0; var pos: usize = 0;
var linenum = self.view_top + 1; var linenum = self.view_top + 1;
var rows = self.view_rows; var rows = self.view_rows;
var diff_symbols = self.diff_symbols.items; var diff_symbols = self.editor.changes.items;
while (rows > 0) : (rows -= 1) { while (rows > 0) : (rows -= 1) {
if (linenum > self.lines) return; if (linenum > self.lines) return;
if (self.highlight and linenum == self.line + 1) if (self.highlight and linenum == self.line + 1)
@ -189,7 +186,7 @@ pub fn render_linear(self: *Self, theme: *const Widget.Theme) void {
var pos: usize = 0; var pos: usize = 0;
var linenum = self.view_top + 1; var linenum = self.view_top + 1;
var rows = self.view_rows; var rows = self.view_rows;
var diff_symbols = self.diff_symbols.items; var diff_symbols = self.editor.changes.items;
while (rows > 0) : (rows -= 1) { while (rows > 0) : (rows -= 1) {
if (linenum > self.lines) return; if (linenum > self.lines) return;
if (linenum == self.line + 1) { if (linenum == self.line + 1) {
@ -216,7 +213,7 @@ pub fn render_relative(self: *Self, theme: *const Widget.Theme) void {
var linenum: isize = row - line; var linenum: isize = row - line;
var abs_linenum = self.view_top + 1; var abs_linenum = self.view_top + 1;
var rows = self.view_rows; var rows = self.view_rows;
var diff_symbols = self.diff_symbols.items; var diff_symbols = self.editor.changes.items;
while (rows > 0) : (rows -= 1) { while (rows > 0) : (rows -= 1) {
if (self.lines > @as(u32, @intCast(row)) and pos > self.lines - @as(u32, @intCast(row))) return; if (self.lines > @as(u32, @intCast(row)) and pos > self.lines - @as(u32, @intCast(row))) return;
self.plane.set_style(if (linenum == 0) theme.editor_gutter_active else theme.editor_gutter); self.plane.set_style(if (linenum == 0) theme.editor_gutter_active else theme.editor_gutter);
@ -419,7 +416,7 @@ pub fn process_diff(self: *Self, cb: []const u8) MessageFilter.Error!void {
fn process_edit(self: *Self, kind: Kind, line: usize, lines: usize) !void { fn process_edit(self: *Self, kind: Kind, line: usize, lines: usize) !void {
std.log.debug("edit: {} l:{d} n:{}", .{ kind, line, lines }); std.log.debug("edit: {} l:{d} n:{}", .{ kind, line, lines });
(try self.diff_symbols.addOne(self.allocator)).* = .{ .kind = kind, .line = line, .lines = lines }; (try self.editor.changes.addOne(self.allocator)).* = .{ .kind = kind, .line = line, .lines = lines };
} }
pub fn filter_receive(self: *Self, _: tp.pid_ref, m: tp.message) MessageFilter.Error!bool { pub fn filter_receive(self: *Self, _: tp.pid_ref, m: tp.message) MessageFilter.Error!bool {