fix: convert column to byte position in inspector_view.inspect_location

fixes #54
This commit is contained in:
CJ van den Berg 2024-10-23 12:20:40 +02:00
parent a46c75da63
commit 6755eaab44
Signed by: neurocyte
GPG key ID: 8EB1E1BB660E3FB9
2 changed files with 21 additions and 1 deletions

View file

@ -553,6 +553,24 @@ const Node = union(enum) {
};
}
pub fn get_line_width_to_pos(self: *const Node, line: usize, col: usize, metrics: Metrics) error{Stop}!usize {
const Ctx = struct {
col: usize,
wcwidth: usize = 0,
pos: usize = 0,
fn walker(ctx_: *anyopaque, egc: []const u8, wcwidth: usize, _: Metrics) Walker {
const ctx = @as(*@This(), @ptrCast(@alignCast(ctx_)));
if (ctx.wcwidth >= ctx.col) return Walker.stop;
ctx.pos += egc.len;
ctx.wcwidth += wcwidth;
return if (egc[0] == '\n') Walker.stop else Walker.keep_walking;
}
};
var ctx: Ctx = .{ .col = col };
self.walk_egc_forward(line, Ctx.walker, &ctx, metrics) catch return error.Stop;
return ctx.pos;
}
pub fn get_range(self: *const Node, sel: Selection, copy_buf: ?[]u8, size: ?*usize, wcwidth_: ?*usize, metrics_: Metrics) error{ Stop, NoSpaceLeft }!?[]u8 {
const Ctx = struct {
col: usize = 0,

View file

@ -73,7 +73,9 @@ fn clear(self: *Self) void {
fn inspect_location(self: *Self, row: usize, col: usize) void {
const syn = self.editor.syntax orelse return;
syn.highlights_at_point(self, dump_highlight, .{ .row = @intCast(row), .column = @intCast(col) });
const root = (self.editor.buffer orelse return).root;
const col_pos = root.get_line_width_to_pos(row, col,self.editor.metrics) catch return;
syn.highlights_at_point(self, dump_highlight, .{ .row = @intCast(row), .column = @intCast(col_pos) });
}
fn get_buffer_text(self: *Self, buf: []u8, sel: Buffer.Selection) ?[]const u8 {