Skip to content

Commit

Permalink
fix: take into count of multi-width characters on offset of highlight…
Browse files Browse the repository at this point in the history
…s and jump (#125)

* feat: take into count of multi-width characters on offset of highlights and jump

* fix: wrong jump position on operator pending mode

This also let offset_pos() receive and return (1,0)-indexed position.
Formerly it received (1,0)-based index and returned (0,0)-based index

* refactor: simplify util.offset_pos

* refactor: let offset_pos use (1,0)-indexed positions with internal variables

to be consistent with parameter and return values
  • Loading branch information
atusy committed Jul 5, 2023
1 parent b4017be commit 41c09fa
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 4 deletions.
4 changes: 2 additions & 2 deletions lua/flash/highlight.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
3 changes: 1 addition & 2 deletions lua/flash/jump.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
19 changes: 19 additions & 0 deletions lua/flash/util.lua
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,25 @@ M.BS = M.t("<bs>")
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()
Expand Down

0 comments on commit 41c09fa

Please sign in to comment.