Skip to content

Commit

Permalink
list: save and restore cursor positions
Browse files Browse the repository at this point in the history
Harpoon was not remembering the cursor position after quitting nvim.
Ensure that the cursor information is always saved and applied so that
we get back to exactly where we last were in the harpooned file.

Closes: #441
  • Loading branch information
davvid committed Mar 11, 2024
1 parent 335f547 commit 3d45f6f
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 13 deletions.
22 changes: 9 additions & 13 deletions lua/harpoon/config.lua
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
local Extensions = require("harpoon.extensions")
local Logger = require("harpoon.logger")
local List = require("harpoon.list")
local Utils = require("harpoon.utils")

local M = {}
Expand Down Expand Up @@ -98,10 +99,10 @@ function M.get_default_config()
return
end

list:sync_cursor()

local bufnr = vim.fn.bufnr(list_item.value)
local set_position = false
if bufnr == -1 then
set_position = true
bufnr = vim.fn.bufnr(list_item.value, true)
end
if not vim.api.nvim_buf_is_loaded(bufnr) then
Expand All @@ -121,12 +122,11 @@ function M.get_default_config()

vim.api.nvim_set_current_buf(bufnr)

if set_position then
vim.api.nvim_win_set_cursor(0, {
list_item.context.row or 1,
list_item.context.col or 0,
})
end
-- The stored cursor position could be outside of the file bounds.
pcall(vim.api.nvim_win_set_cursor, 0, {
list_item.context.row or 1,
list_item.context.col or 0,
})

Extensions.extensions:emit(Extensions.event_names.NAVIGATE, {
buffer = bufnr,
Expand Down Expand Up @@ -184,8 +184,7 @@ function M.get_default_config()
local item = list:get_by_display(bufname)

if item then
local pos = vim.api.nvim_win_get_cursor(0)

local pos = List._sync_cursor(item)
Logger:log(
"config_default#BufLeave updating position",
bufnr,
Expand All @@ -194,9 +193,6 @@ function M.get_default_config()
"to position",
pos
)

item.context.row = pos[1]
item.context.col = pos[2]
end
end,

Expand Down
1 change: 1 addition & 0 deletions lua/harpoon/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,7 @@ function Harpoon:sync()
return
end

list:sync_cursor()
local encoded = list:encode()
self.data:update(key, list_name, encoded)
end)
Expand Down
21 changes: 21 additions & 0 deletions lua/harpoon/list.lua
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
local Logger = require("harpoon.logger")
local Extensions = require("harpoon.extensions")
local Utils = require("harpoon.utils")

--- @class HarpoonNavOptions
--- @field ui_nav_wrap? boolean
Expand Down Expand Up @@ -172,6 +173,17 @@ function HarpoonList:resolve_displayed(displayed)
self.items = new_list
end

function HarpoonList:sync_cursor()
local current_display = Utils.normalize_path(
vim.api.nvim_buf_get_name(vim.api.nvim_get_current_buf()),
self.config.get_root_dir()
)
local item = self:get_by_display(current_display)
if item then
HarpoonList._sync_cursor(item)
end
end

function HarpoonList:select(index, options)
local item = self.items[index]
if item or self.config.select_with_nil then
Expand Down Expand Up @@ -251,4 +263,13 @@ function HarpoonList.decode(list_config, name, items)
return HarpoonList:new(list_config, name, list_items)
end

--- @return integer[]
--- @param item HarpoonItem
function HarpoonList._sync_cursor(item)
local pos = vim.api.nvim_win_get_cursor(0)
item.context.row = pos[1]
item.context.col = pos[2]
return pos
end

return HarpoonList

0 comments on commit 3d45f6f

Please sign in to comment.