Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

list: save and restore cursor positions #533

Open
wants to merge 3 commits into
base: harpoon2
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
69 changes: 32 additions & 37 deletions lua/harpoon/config.lua
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
local Extensions = require("harpoon.extensions")
local Logger = require("harpoon.logger")
local Path = require("plenary.path")
local function normalize_path(buf_name, root)
return Path:new(buf_name):make_relative(root)
end
local List = require("harpoon.list")
local Utils = require("harpoon.utils")

local function to_exact_name(value)
return "^" .. value .. "$"
end
Expand Down Expand Up @@ -104,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 @@ -130,38 +127,36 @@ 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

Extensions.extensions:emit(Extensions.event_names.NAVIGATE, {
Expand Down Expand Up @@ -190,7 +185,7 @@ function M.get_default_config()
---@return HarpoonListItem
create_list_item = function(config, name)
name = name
or normalize_path(
or Utils.normalize_path(
vim.api.nvim_buf_get_name(
vim.api.nvim_get_current_buf()
),
Expand Down Expand Up @@ -219,14 +214,14 @@ function M.get_default_config()
---@param list HarpoonList
BufLeave = function(arg, list)
local bufnr = arg.buf
local bufname = normalize_path(
local bufname = Utils.normalize_path(
vim.api.nvim_buf_get_name(bufnr),
list.config.get_root_dir()
)
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
24 changes: 22 additions & 2 deletions lua/harpoon/list.lua
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
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 @@ -259,7 +259,7 @@ function HarpoonList:resolve_displayed(displayed, length)
for i = 1, length do
local v = displayed[i]
local index = index_of(list_displayed, self._length, v)
if utils.is_white_space(v) then
if Utils.is_white_space(v) then
new_list[i] = nil
elseif index == -1 then
new_list[i] = self.config.create_list_item(self.config, v)
Expand All @@ -285,6 +285,17 @@ function HarpoonList:resolve_displayed(displayed, length)
end
end

function HarpoonList:sync_cursor()
local current_display = Utils.normalize_path(
davvid marked this conversation as resolved.
Show resolved Hide resolved
vim.api.nvim_buf_get_name(vim.api.nvim_get_current_buf()),
self.config.get_root_dir()
)
local item = self:get_by_value(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 +376,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
6 changes: 3 additions & 3 deletions lua/harpoon/test/harpoon_spec.lua
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,7 @@ describe("harpoon", function()
col = 3,
}, {
row = 4,
col = 3,
col = 2,
})
end)

Expand All @@ -138,7 +138,7 @@ describe("harpoon", function()
col = 4,
}, {
row = 4,
col = 3,
col = 2,
})
end)

Expand All @@ -148,7 +148,7 @@ describe("harpoon", function()
col = 4,
}, {
row = 4,
col = 3,
col = 2,
})
end)

Expand Down
6 changes: 6 additions & 0 deletions lua/harpoon/utils.lua
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
local Path = require("plenary.path")

local M = {}

function M.trim(str)
Expand All @@ -22,4 +24,8 @@ function M.is_white_space(str)
return str:gsub("%s", "") == ""
end

function M.normalize_path(buf_name, root)
return Path:new(buf_name):make_relative(root)
end

return M
Loading