feat: changing buffers updates list positions

This commit is contained in:
mpaulson 2023-11-20 21:01:15 -07:00
parent eafaec8a2e
commit 90bcfebca9
4 changed files with 104 additions and 10 deletions

View File

@ -10,7 +10,10 @@ local M = {}
---@field select? (fun(list_item: HarpoonListItem, options: any?): nil) ---@field select? (fun(list_item: HarpoonListItem, options: any?): nil)
---@field equals? (fun(list_line_a: HarpoonListItem, list_line_b: HarpoonListItem): boolean) ---@field equals? (fun(list_line_a: HarpoonListItem, list_line_b: HarpoonListItem): boolean)
---@field add? fun(item: any?): HarpoonListItem ---@field add? fun(item: any?): HarpoonListItem
---@field BufLeave? fun(evt: any, list: HarpoonList): nil
---@field VimLeavePre? fun(evt: any, list: HarpoonList): nil
---notehunthoeunthoeunthoeunthoeunthoeunth
---@class HarpoonSettings ---@class HarpoonSettings
---@field save_on_toggle boolean defaults to true ---@field save_on_toggle boolean defaults to true
---@field jump_to_file_location boolean defaults to true ---@field jump_to_file_location boolean defaults to true
@ -124,6 +127,20 @@ function M.get_default_config()
} }
} }
end, end,
BufLeave = function(arg, list)
local bufnr = arg.buf;
local bufname = vim.api.nvim_buf_get_name(bufnr);
local item = list:get_by_display(bufname)
if item then
local pos = vim.api.nvim_win_get_cursor(0)
item.context.row = pos[1]
item.context.col = pos[2]
end
end,
autocmds = {"BufLeave"},
} }
} }
end end

View File

@ -14,6 +14,7 @@ local DEFAULT_LIST = "__harpoon_files"
---@field config HarpoonConfig ---@field config HarpoonConfig
---@field data HarpoonData ---@field data HarpoonData
---@field lists {[string]: {[string]: HarpoonList}} ---@field lists {[string]: {[string]: HarpoonList}}
---@field hooks_setup boolean
local Harpoon = {} local Harpoon = {}
Harpoon.__index = Harpoon Harpoon.__index = Harpoon
@ -26,6 +27,7 @@ function Harpoon:new()
config = config, config = config,
data = Data.Data:new(), data = Data.Data:new(),
lists = {}, lists = {},
hooks_setup = false,
}, self) }, self)
end end
@ -33,6 +35,32 @@ end
---@return Harpoon ---@return Harpoon
function Harpoon:setup(partial_config) function Harpoon:setup(partial_config)
self.config = Config.merge_config(partial_config, self.config) self.config = Config.merge_config(partial_config, self.config)
if self.hooks_setup == false then
local augroup = vim.api.nvim_create_augroup
local HarpoonGroup = augroup('Harpoon', {})
vim.api.nvim_create_autocmd({"BufLeave", "VimLeavePre"}, {
group = HarpoonGroup,
pattern = '*',
callback = function(ev)
self:_for_each_list(function(list, config)
local fn = config[ev.event]
if fn ~= nil then
fn(ev, list)
end
if ev.event == "VimLeavePre" then
self:sync()
end
end)
end,
})
self.hooks_setup = true
end
return self return self
end end
@ -64,22 +92,29 @@ function Harpoon:list(name)
return list return list
end end
function Harpoon:sync() ---@param cb fun(list: HarpoonList, config: HarpoonPartialConfigItem, name: string)
function Harpoon:_for_each_list(cb)
local key = self.config.settings.key() local key = self.config.settings.key()
local seen = self.data.seen[key] local seen = self.data.seen[key]
local lists = self.lists[key] local lists = self.lists[key]
for list_name, _ in pairs(seen) do
local encoded = lists[list_name]:encode() if not seen then
self.data:update(key, list_name, encoded) return
end
self.data:sync()
end end
function Harpoon:setup_hooks() for list_name, _ in pairs(seen) do
-- setup the autocommands local list_config = Config.get_config(self.config, list_name)
-- vim exits sync data cb(lists[list_name], list_config, list_name)
-- buf exit setup the cursor location end
error("I haven't implemented this yet") end
function Harpoon:sync()
local key = self.config.settings.key()
self:_for_each_list(function(list, _, list_name)
local encoded = list:encode()
self.data:update(key, list_name, encoded)
end)
self.data:sync()
end end
function Harpoon:info() function Harpoon:info()

View File

@ -74,6 +74,16 @@ function HarpoonList:get(index)
return self.items[index] return self.items[index]
end end
function HarpoonList:get_by_display(name)
local displayed = self:display()
local index = index_of(displayed, name)
if index == -1 then
return nil
end
return self.items[index]
end
--- much inefficiencies. dun care --- much inefficiencies. dun care
---@param displayed string[] ---@param displayed string[]
function HarpoonList:resolve_displayed(displayed) function HarpoonList:resolve_displayed(displayed)

View File

@ -6,6 +6,7 @@ local eq = assert.are.same
describe("harpoon", function() describe("harpoon", function()
before_each(function() before_each(function()
Data.set_data_path("/tmp/harpoon2.json") Data.set_data_path("/tmp/harpoon2.json")
Data.__dangerously_clear_data() Data.__dangerously_clear_data()
@ -25,6 +26,37 @@ describe("harpoon", function()
end) end)
it("when we change buffers we update the row and column", function()
local file_name = "/tmp/harpoon-test"
local row = 1
local col = 0
local target_buf = utils.create_file(file_name, {
"foo",
"bar",
"baz",
"qux"
}, row, col)
local list = harpoon:list():append()
local other_buf = utils.create_file("other-file", {
"foo",
"bar",
"baz",
"qux"
}, row, col)
vim.api.nvim_set_current_buf(target_buf)
vim.api.nvim_win_set_cursor(0, {row + 1, col})
vim.api.nvim_set_current_buf(other_buf)
local expected = {
{value = file_name, context = {row = row + 1, col = col}},
}
eq(list.items, expected)
end)
it("full harpoon add sync cycle", function() it("full harpoon add sync cycle", function()
local file_name = "/tmp/harpoon-test" local file_name = "/tmp/harpoon-test"
local row = 3 local row = 3