fix: restore cursor to correct position on picker close

This commit is contained in:
James Trew
2024-01-10 23:11:20 +01:00
committed by Simon Hauser
parent 4367e05c06
commit 6a7dd9b399
4 changed files with 40 additions and 27 deletions
+33
View File
@@ -603,4 +603,37 @@ utils.__separate_file_path_location = function(path)
return path, 0, 0
end
---@param winid integer
---@param row integer
---@param col integer
local function protected_win_set_cursor(winid, row, col)
vim.schedule(function()
local ok, err_msg = pcall(vim.api.nvim_win_set_cursor, winid, { row, col })
if not ok then
log.debug("Failed to move to cursor:", err_msg, row, col)
end
end)
end
--- set the cursor of a window to its original, or provided position
---@param winid integer winid of original window
---@param row? integer possible new row position
---@param col? integer possible new col position. must pass row position for col to be used.
utils.set_window_cursor = function(winid, row, col)
if row and col then
protected_win_set_cursor(winid, row, col - 1) -- entry.col is 1-index but nvim api uses 0-index
return
end
if row then
protected_win_set_cursor(winid, row, 0)
else
local cursor_valid, cursor_pos = pcall(vim.api.nvim_win_get_cursor, winid)
if not cursor_valid then
return
end
protected_win_set_cursor(winid, cursor_pos[1], cursor_pos[2])
end
end
return utils