From 6755eaab4449c1266ebd760e8b8214ffdd8c0601 Mon Sep 17 00:00:00 2001 From: CJ van den Berg Date: Wed, 23 Oct 2024 12:20:40 +0200 Subject: [PATCH] fix: convert column to byte position in inspector_view.inspect_location fixes #54 --- src/buffer/Buffer.zig | 18 ++++++++++++++++++ src/tui/inspector_view.zig | 4 +++- 2 files changed, 21 insertions(+), 1 deletion(-) diff --git a/src/buffer/Buffer.zig b/src/buffer/Buffer.zig index 1f19d75..31ec237 100644 --- a/src/buffer/Buffer.zig +++ b/src/buffer/Buffer.zig @@ -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, diff --git a/src/tui/inspector_view.zig b/src/tui/inspector_view.zig index 0287a2d..720056a 100644 --- a/src/tui/inspector_view.zig +++ b/src/tui/inspector_view.zig @@ -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 {