refactor(Buffer): remove Plane dependency in Buffer

This commit is contained in:
CJ van den Berg 2024-07-05 00:33:31 +02:00
parent a3a298f721
commit 128182a448
7 changed files with 242 additions and 216 deletions

View file

@ -1,9 +1,9 @@
const std = @import("std");
const cbor = @import("cbor");
const Plane = @import("renderer").Plane;
const Buffer = @import("Buffer.zig");
const View = @import("View.zig");
const Selection = @import("Selection.zig");
const Metrix = Buffer.Metrix;
row: usize = 0,
col: usize = 0,
@ -27,20 +27,20 @@ pub inline fn right_of(self: Self, other: Self) bool {
return if (self.row > other.row) true else if (self.row == other.row and self.col > other.col) true else false;
}
pub fn clamp_to_buffer(self: *Self, root: Buffer.Root, plane: Plane) void {
pub fn clamp_to_buffer(self: *Self, root: Buffer.Root, mtrx: Metrix) void {
self.row = @min(self.row, root.lines() - 1);
self.col = @min(self.col, root.line_width(self.row, plane) catch 0);
self.col = @min(self.col, root.line_width(self.row, mtrx) catch 0);
}
fn follow_target(self: *Self, root: Buffer.Root, plane: Plane) void {
self.col = @min(self.target, root.line_width(self.row, plane) catch 0);
fn follow_target(self: *Self, root: Buffer.Root, mtrx: Metrix) void {
self.col = @min(self.target, root.line_width(self.row, mtrx) catch 0);
}
fn move_right_no_target(self: *Self, root: Buffer.Root, plane: Plane) !void {
fn move_right_no_target(self: *Self, root: Buffer.Root, mtrx: Metrix) !void {
const lines = root.lines();
if (lines <= self.row) return error.Stop;
if (self.col < root.line_width(self.row, plane) catch 0) {
_, const wcwidth, const offset = root.ecg_at(self.row, self.col, plane) catch return error.Stop;
if (self.col < root.line_width(self.row, mtrx) catch 0) {
_, const wcwidth, const offset = root.ecg_at(self.row, self.col, mtrx) catch return error.Stop;
self.col += wcwidth - offset;
} else if (self.row < lines - 1) {
self.col = 0;
@ -48,73 +48,73 @@ fn move_right_no_target(self: *Self, root: Buffer.Root, plane: Plane) !void {
} else return error.Stop;
}
pub fn move_right(self: *Self, root: Buffer.Root, plane: Plane) !void {
try self.move_right_no_target(root, plane);
pub fn move_right(self: *Self, root: Buffer.Root, mtrx: Metrix) !void {
try self.move_right_no_target(root, mtrx);
self.target = self.col;
}
fn move_left_no_target(self: *Self, root: Buffer.Root, plane: Plane) !void {
fn move_left_no_target(self: *Self, root: Buffer.Root, mtrx: Metrix) !void {
if (self.col == 0) {
if (self.row == 0) return error.Stop;
self.row -= 1;
self.col = root.line_width(self.row, plane) catch 0;
self.col = root.line_width(self.row, mtrx) catch 0;
} else {
_, const wcwidth, _ = root.ecg_at(self.row, self.col - 1, plane) catch return error.Stop;
_, const wcwidth, _ = root.ecg_at(self.row, self.col - 1, mtrx) catch return error.Stop;
if (self.col > wcwidth) self.col -= wcwidth else self.col = 0;
}
}
pub fn move_left(self: *Self, root: Buffer.Root, plane: Plane) !void {
try self.move_left_no_target(root, plane);
pub fn move_left(self: *Self, root: Buffer.Root, mtrx: Metrix) !void {
try self.move_left_no_target(root, mtrx);
self.target = self.col;
}
pub fn move_up(self: *Self, root: Buffer.Root, plane: Plane) !void {
pub fn move_up(self: *Self, root: Buffer.Root, mtrx: Metrix) !void {
if (self.row > 0) {
self.row -= 1;
self.follow_target(root, plane);
self.move_left_no_target(root, plane) catch return;
try self.move_right_no_target(root, plane);
self.follow_target(root, mtrx);
self.move_left_no_target(root, mtrx) catch return;
try self.move_right_no_target(root, mtrx);
} else return error.Stop;
}
pub fn move_down(self: *Self, root: Buffer.Root, plane: Plane) !void {
pub fn move_down(self: *Self, root: Buffer.Root, mtrx: Metrix) !void {
if (self.row < root.lines() - 1) {
self.row += 1;
self.follow_target(root, plane);
self.move_left_no_target(root, plane) catch return;
try self.move_right_no_target(root, plane);
self.follow_target(root, mtrx);
self.move_left_no_target(root, mtrx) catch return;
try self.move_right_no_target(root, mtrx);
} else return error.Stop;
}
pub fn move_page_up(self: *Self, root: Buffer.Root, view: *const View, plane: Plane) void {
pub fn move_page_up(self: *Self, root: Buffer.Root, view: *const View, mtrx: Metrix) void {
self.row = if (self.row > view.rows) self.row - view.rows else 0;
self.follow_target(root, plane);
self.move_left_no_target(root, plane) catch return;
self.move_right_no_target(root, plane) catch return;
self.follow_target(root, mtrx);
self.move_left_no_target(root, mtrx) catch return;
self.move_right_no_target(root, mtrx) catch return;
}
pub fn move_page_down(self: *Self, root: Buffer.Root, view: *const View, plane: Plane) void {
pub fn move_page_down(self: *Self, root: Buffer.Root, view: *const View, mtrx: Metrix) void {
if (root.lines() > self.row + view.rows) {
self.row += view.rows;
} else self.move_buffer_last(root, plane);
self.follow_target(root, plane);
self.move_left_no_target(root, plane) catch return;
self.move_right_no_target(root, plane) catch return;
} else self.move_buffer_last(root, mtrx);
self.follow_target(root, mtrx);
self.move_left_no_target(root, mtrx) catch return;
self.move_right_no_target(root, mtrx) catch return;
}
pub fn move_to(self: *Self, root: Buffer.Root, row: usize, col: usize, plane: Plane) !void {
pub fn move_to(self: *Self, root: Buffer.Root, row: usize, col: usize, mtrx: Metrix) !void {
if (row < root.lines()) {
self.row = row;
self.col = @min(col, root.line_width(self.row, plane) catch return error.Stop);
self.col = @min(col, root.line_width(self.row, mtrx) catch return error.Stop);
self.target = self.col;
} else return error.Stop;
}
pub fn move_abs(self: *Self, root: Buffer.Root, v: *View, y: usize, x: usize, plane: Plane) !void {
pub fn move_abs(self: *Self, root: Buffer.Root, v: *View, y: usize, x: usize, mtrx: Metrix) !void {
self.row = v.row + y;
self.col = v.col + x;
self.clamp_to_buffer(root, plane);
self.clamp_to_buffer(root, mtrx);
self.target = self.col;
}
@ -123,8 +123,8 @@ pub fn move_begin(self: *Self) void {
self.target = self.col;
}
pub fn move_end(self: *Self, root: Buffer.Root, plane: Plane) void {
if (self.row < root.lines()) self.col = root.line_width(self.row, plane) catch 0;
pub fn move_end(self: *Self, root: Buffer.Root, mtrx: Metrix) void {
if (self.row < root.lines()) self.col = root.line_width(self.row, mtrx) catch 0;
self.target = std.math.maxInt(u32);
}
@ -134,32 +134,32 @@ pub fn move_buffer_begin(self: *Self) void {
self.target = 0;
}
pub fn move_buffer_end(self: *Self, root: Buffer.Root, plane: Plane) void {
pub fn move_buffer_end(self: *Self, root: Buffer.Root, mtrx: Metrix) void {
self.row = root.lines() - 1;
self.move_end(root, plane);
self.move_end(root, mtrx);
if (self.col == 0) self.target = 0;
}
fn move_buffer_first(self: *Self, root: Buffer.Root, plane: Plane) void {
fn move_buffer_first(self: *Self, root: Buffer.Root, mtrx: Metrix) void {
self.row = 0;
self.follow_target(root, plane);
self.follow_target(root, mtrx);
}
fn move_buffer_last(self: *Self, root: Buffer.Root, plane: Plane) void {
fn move_buffer_last(self: *Self, root: Buffer.Root, mtrx: Metrix) void {
self.row = root.lines() - 1;
self.follow_target(root, plane);
self.follow_target(root, mtrx);
}
fn is_at_begin(self: *const Self) bool {
return self.col == 0;
}
fn is_at_end(self: *const Self, root: Buffer.Root, plane: Plane) bool {
return if (self.row < root.lines()) self.col == root.line_width(self.row, plane) catch 0 else true;
fn is_at_end(self: *const Self, root: Buffer.Root, mtrx: Metrix) bool {
return if (self.row < root.lines()) self.col == root.line_width(self.row, mtrx) catch 0 else true;
}
pub fn test_at(self: *const Self, root: Buffer.Root, pred: *const fn (c: []const u8) bool, plane: Plane) bool {
return root.test_at(pred, self.row, self.col, plane);
pub fn test_at(self: *const Self, root: Buffer.Root, pred: *const fn (c: []const u8) bool, mtrx: Metrix) bool {
return root.test_at(pred, self.row, self.col, mtrx);
}
pub fn write(self: *const Self, writer: Buffer.MetaWriter) !void {