diff --git a/lua/flash/highlight.lua b/lua/flash/highlight.lua index 2bbf296..eb3b3ed 100644 --- a/lua/flash/highlight.lua +++ b/lua/flash/highlight.lua @@ -113,8 +113,8 @@ function M.update(state) local function label(match, pos, offset, is_after) local buf = vim.api.nvim_win_get_buf(match.win) local cursor = vim.api.nvim_win_get_cursor(match.win) - local row = pos[1] - 1 + offset[1] - local col = math.max(pos[2] + offset[2], 0) + local pos2 = require('flash.util').offset_pos(buf, pos, offset) + local row, col = pos2[1] - 1, pos2[2] -- dont show the label if the cursor is on the same position -- in the same window -- and the label is not a range diff --git a/lua/flash/jump.lua b/lua/flash/jump.lua index 814a487..0a9ef0d 100644 --- a/lua/flash/jump.lua +++ b/lua/flash/jump.lua @@ -190,8 +190,7 @@ function M._jump(match, state, opts) offset = 1 end - pos = pos + Pos({ 0, offset or 0 }) - ---@cast pos Pos + pos = Pos(require('flash.util').offset_pos(vim.api.nvim_win_get_buf(match.win), pos, { 0, offset or 0})) pos[2] = math.max(0, pos[2]) end diff --git a/lua/flash/util.lua b/lua/flash/util.lua index a99914b..360d050 100644 --- a/lua/flash/util.lua +++ b/lua/flash/util.lua @@ -13,6 +13,25 @@ M.BS = M.t("") M.LUA_CALLBACK = "\x80\253g" M.CMD = "\x80\253h" +---@param buf number +---@param pos number[] (1,0)-indexed position +---@param offset number[] +---@return number[] (1,0)-indexed position +function M.offset_pos(buf, pos, offset) + local row = pos[1] + offset[1] + local ok, lines = pcall(vim.api.nvim_buf_get_lines, buf, row - 1, row, true) + if not ok or lines == nil then + -- fallback to old behavior if anything wrong happens + return {row, math.max(pos[2] + offset[2], 0)} + end + + local line = lines[1] + local charidx = vim.fn.charidx(line, pos[2]) + local col = vim.fn.byteidx(line, charidx + offset[2]) + + return {row, math.max(col, 0)} +end + function M.get_char() Hacks.setcursor() vim.cmd.redraw()