feat: make indent size and tab width configurable and add indent guide mode

This commit is contained in:
CJ van den Berg 2024-10-10 23:14:55 +02:00
parent 55fb6d29a0
commit d2238bf847
Signed by: neurocyte
GPG key ID: 8EB1E1BB660E3FB9
15 changed files with 363 additions and 323 deletions

View file

@ -19,8 +19,9 @@ pub const Metrics = struct {
ctx: *const anyopaque,
egc_length: egc_length_func,
egc_chunk_width: egc_chunk_width_func,
pub const egc_length_func = *const fn (ctx: *const anyopaque, egcs: []const u8, colcount: *c_int, abs_col: usize) usize;
pub const egc_chunk_width_func = *const fn (self: *const anyopaque, chunk_: []const u8, abs_col_: usize) usize;
tab_width: usize,
pub const egc_length_func = *const fn (self: Metrics, egcs: []const u8, colcount: *c_int, abs_col: usize) usize;
pub const egc_chunk_width_func = *const fn (self: Metrics, chunk_: []const u8, abs_col_: usize) usize;
};
arena: std.heap.ArenaAllocator,
@ -165,11 +166,11 @@ pub const Leaf = struct {
var buf = self.buf;
while (buf.len > 0 and pos.* > 0) {
if (buf[0] == '\t') {
cols = @intCast(8 - (abs_col % 8));
cols = @intCast(metrics.tab_width - (abs_col % metrics.tab_width));
buf = buf[1..];
pos.* -= 1;
} else {
const bytes = metrics.egc_length(metrics.ctx, buf, &cols, abs_col);
const bytes = metrics.egc_length(metrics, buf, &cols, abs_col);
buf = buf[bytes..];
pos.* -= bytes;
}
@ -192,7 +193,7 @@ pub const Leaf = struct {
return while (buf.len > 0) {
if (col == 0)
break @intFromPtr(buf.ptr) - @intFromPtr(self.buf.ptr);
const bytes = metrics.egc_length(metrics.ctx, buf, &cols, abs_col);
const bytes = metrics.egc_length(metrics, buf, &cols, abs_col);
buf = buf[bytes..];
if (col < cols)
break @intFromPtr(buf.ptr) - @intFromPtr(self.buf.ptr);
@ -225,7 +226,7 @@ pub const Leaf = struct {
buf = buf[1..];
},
else => {
const bytes = metrics.egc_length(metrics.ctx, buf, &cols, 0);
const bytes = metrics.egc_length(metrics, buf, &cols, 0);
var buf_: [4096]u8 = undefined;
try l.appendSlice(try std.fmt.bufPrint(&buf_, "{s}", .{std.fmt.fmtSliceEscapeLower(buf[0..bytes])}));
buf = buf[bytes..];
@ -484,7 +485,7 @@ const Node = union(enum) {
var buf: []const u8 = leaf.buf;
while (buf.len > 0) {
var cols: c_int = undefined;
const bytes = metrics.egc_length(metrics.ctx, buf, &cols, ctx.abs_col);
const bytes = metrics.egc_length(metrics, buf, &cols, ctx.abs_col);
const ret = ctx.walker_f(ctx.walker_ctx, buf[0..bytes], @intCast(cols), metrics);
if (ret.err) |e| return .{ .err = e };
buf = buf[bytes..];
@ -849,7 +850,7 @@ const Node = union(enum) {
line += 1;
col = 0;
} else {
col += metrics_.egc_chunk_width(metrics_.ctx, chunk, col);
col += metrics_.egc_chunk_width(metrics_, chunk, col);
}
}
return .{ line, col, self };
@ -972,7 +973,7 @@ const Node = union(enum) {
if (ctx.abs_col >= ctx.pos.col) return error.Stop;
if (buf[0] == '\n') return error.Stop;
var cols: c_int = undefined;
const egc_bytes = ctx.metrics.egc_length(ctx.metrics.ctx, buf, &cols, ctx.abs_col);
const egc_bytes = ctx.metrics.egc_length(ctx.metrics, buf, &cols, ctx.abs_col);
ctx.abs_col += @intCast(cols);
ctx.byte_pos += egc_bytes;
buf = buf[egc_bytes..];