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 Apr 13, 2024
1 parent 812c198 commit 761d4d4
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 31 deletions.
64 changes: 33 additions & 31 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 function to_exact_name(value)
Expand Down Expand Up @@ -102,13 +103,11 @@ function M.get_default_config()
return
end

list:sync_cursor()
options = options or {}

local bufnr = vim.fn.bufnr(to_exact_name(list_item.value))
local set_position = false
if bufnr == -1 then -- must create a buffer!
set_position = true
-- bufnr = vim.fn.bufnr(list_item.value, true)
bufnr = vim.fn.bufadd(list_item.value)
end
if not vim.api.nvim_buf_is_loaded(bufnr) then
Expand All @@ -128,39 +127,42 @@ function M.get_default_config()

vim.api.nvim_set_current_buf(bufnr)

if set_position then
local lines = vim.api.nvim_buf_line_count(bufnr)
local lines = vim.api.nvim_buf_line_count(bufnr)

local edited = false
if list_item.context.row > lines then
list_item.context.row = lines
edited = true
end
local edited = false
if list_item.context.row > lines then
list_item.context.row = lines
edited = true
end

local row = list_item.context.row
local row_text =
vim.api.nvim_buf_get_lines(0, row - 1, row, false)
local col = #row_text[1]
local row = list_item.context.row
local row_text =
vim.api.nvim_buf_get_lines(0, row - 1, row, false)
local col = #row_text[1]

if list_item.context.col > col then
list_item.context.col = col
edited = true
end
if list_item.context.col > col then
list_item.context.col = col
edited = true
end

vim.api.nvim_win_set_cursor(0, {
list_item.context.row or 1,
list_item.context.col or 0,
})
vim.api.nvim_win_set_cursor(0, {
list_item.context.row or 1,
list_item.context.col or 0,
})

if edited then
Extensions.extensions:emit(
Extensions.event_names.POSITION_UPDATED,
{
list_item = list_item,
}
)
end
if edited then
Extensions.extensions:emit(
Extensions.event_names.POSITION_UPDATED,
{
list_item = list_item,
}
)
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 @@ -224,7 +226,7 @@ function M.get_default_config()
local item = list:get_by_value(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",
Expand Down
1 change: 1 addition & 0 deletions lua/harpoon/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,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,6 +1,7 @@
local Logger = require("harpoon.logger")
local utils = require("harpoon.utils")
local Extensions = require("harpoon.extensions")
local Utils = require("harpoon.utils")

local function guess_length(arr)
local last_known = #arr
Expand Down Expand Up @@ -285,6 +286,17 @@ function HarpoonList:resolve_displayed(displayed, length)
end
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 @@ -365,4 +377,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 761d4d4

Please sign in to comment.