refactor: move write_range to Buffer

This commit is contained in:
CJ van den Berg 2025-09-26 14:55:22 +02:00
parent 06b9d2384e
commit dfdb82ca20
Signed by: neurocyte
GPG key ID: 8EB1E1BB660E3FB9
2 changed files with 45 additions and 45 deletions

View file

@ -1119,6 +1119,49 @@ const Node = union(enum) {
break :blk l.toOwnedSlice();
} else error.NotFound;
}
pub fn write_range(
self: *const Node,
sel: Selection,
writer: *std.Io.Writer,
wcwidth_: ?*usize,
metrics: Metrics,
) std.Io.Writer.Error!void {
const Ctx = struct {
col: usize = 0,
sel: Selection,
writer: *std.Io.Writer,
wcwidth: usize = 0,
fn walker(ctx_: *anyopaque, egc: []const u8, wcwidth: usize, _: Metrics) Walker {
const ctx = @as(*@This(), @ptrCast(@alignCast(ctx_)));
if (ctx.col < ctx.sel.begin.col) {
ctx.col += wcwidth;
return Walker.keep_walking;
}
_ = ctx.writer.write(egc) catch |e| return Walker{ .err = e };
ctx.wcwidth += wcwidth;
if (egc[0] == '\n') {
ctx.col = 0;
ctx.sel.begin.col = 0;
ctx.sel.begin.row += 1;
} else {
ctx.col += wcwidth;
ctx.sel.begin.col += wcwidth;
}
return if (ctx.sel.begin.eql(ctx.sel.end))
Walker.stop
else
Walker.keep_walking;
}
};
var ctx: Ctx = .{ .sel = sel, .writer = writer };
ctx.sel.normalize();
if (sel.begin.eql(sel.end))
return;
self.walk_egc_forward(sel.begin.row, Ctx.walker, &ctx, metrics) catch return error.WriteFailed;
if (wcwidth_) |p| p.* = ctx.wcwidth;
}
};
pub fn create(allocator: Allocator) error{OutOfMemory}!*Self {