Skip to content

Commit

Permalink
fix: move jump target should offset in row first, then in cell
Browse files Browse the repository at this point in the history
  • Loading branch information
yehuohan committed Nov 7, 2023
1 parent 80adcd5 commit 3ff8d5c
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 19 deletions.
2 changes: 1 addition & 1 deletion lua/hop-yank/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ M.paste_char1 = function(opts)
return
end

jump_target.move_jump_target(target, opts.hint_offset)
jump_target.move_jump_target(target, 0, opts.hint_offset)

require('hop-yank.yank').paste_from(target, opts.yank_register)
end)
Expand Down
2 changes: 1 addition & 1 deletion lua/hop/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -212,7 +212,7 @@ function M.move_cursor_to(jt, opts)
jt.cursor.col = jt.cursor.col + 1
end

jump_target.move_jump_target(jt, opts.hint_offset)
jump_target.move_jump_target(jt, 0, opts.hint_offset)

-- Update the jump list
-- There is bug with set extmark neovim/neovim#17861
Expand Down
34 changes: 17 additions & 17 deletions lua/hop/jump_target.lua
Original file line number Diff line number Diff line change
Expand Up @@ -215,26 +215,13 @@ function M.sort_indirect_jump_targets(indirect_jump_targets, opts)
end

-- Apply an offset on jump target
-- Always offset in cell first, then in line
-- Always offset in row first, then in cell
---@param jt JumpTarget
---@param offset_cell WindowCell|nil
---@param offset_row WindowRow|nil
function M.move_jump_target(jt, offset_cell, offset_row)
local dcell = offset_cell or 0
---@param offset_cell WindowCell|nil
function M.move_jump_target(jt, offset_row, offset_cell)
local drow = offset_row or 0

if dcell ~= 0 then
local line = vim.api.nvim_buf_get_lines(jt.buffer, jt.cursor.row - 1, jt.cursor.row, false)[1]
local line_cells = vim.fn.strdisplaywidth(line)
---@type WindowCell
local new_cell = vim.fn.strdisplaywidth(line:sub(1, jt.cursor.col)) + dcell
if new_cell >= line_cells then
new_cell = line_cells
elseif new_cell < 0 then
new_cell = 0
end
jt.cursor.col = vim.fn.byteidx(line, window.cell2char(line, new_cell))
end
local dcell = offset_cell or 0

if drow ~= 0 then
---@type WindowRow
Expand All @@ -248,6 +235,19 @@ function M.move_jump_target(jt, offset_cell, offset_row)
jt.cursor.row = new_row
end
end

if dcell ~= 0 then
local line = vim.api.nvim_buf_get_lines(jt.buffer, jt.cursor.row - 1, jt.cursor.row, false)[1]
local line_cells = vim.fn.strdisplaywidth(line)
---@type WindowCell
local new_cell = vim.fn.strdisplaywidth(line:sub(1, jt.cursor.col)) + dcell
if new_cell >= line_cells then
new_cell = line_cells
elseif new_cell < 0 then
new_cell = 0
end
jt.cursor.col = vim.fn.byteidx(line, window.cell2char(line, new_cell))
end
end

return M

0 comments on commit 3ff8d5c

Please sign in to comment.