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 equals? (fun(list_line_a: HarpoonListItem, list_line_b: HarpoonListItem): boolean)
---@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
---@field save_on_toggle boolean defaults to true
---@field jump_to_file_location boolean defaults to true
@ -124,6 +127,20 @@ function M.get_default_config()
}
}
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

View File

@ -14,6 +14,7 @@ local DEFAULT_LIST = "__harpoon_files"
---@field config HarpoonConfig
---@field data HarpoonData
---@field lists {[string]: {[string]: HarpoonList}}
---@field hooks_setup boolean
local Harpoon = {}
Harpoon.__index = Harpoon
@ -26,6 +27,7 @@ function Harpoon:new()
config = config,
data = Data.Data:new(),
lists = {},
hooks_setup = false,
}, self)
end
@ -33,6 +35,32 @@ end
---@return Harpoon
function Harpoon:setup(partial_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
end
@ -64,22 +92,29 @@ function Harpoon:list(name)
return list
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 seen = self.data.seen[key]
local lists = self.lists[key]
for list_name, _ in pairs(seen) do
local encoded = lists[list_name]:encode()
self.data:update(key, list_name, encoded)
if not seen then
return
end
for list_name, _ in pairs(seen) do
local list_config = Config.get_config(self.config, list_name)
cb(lists[list_name], list_config, list_name)
end
self.data:sync()
end
function Harpoon:setup_hooks()
-- setup the autocommands
-- vim exits sync data
-- buf exit setup the cursor location
error("I haven't implemented this yet")
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
function Harpoon:info()

View File

@ -74,6 +74,16 @@ function HarpoonList:get(index)
return self.items[index]
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
---@param displayed string[]
function HarpoonList:resolve_displayed(displayed)

View File

@ -6,6 +6,7 @@ local eq = assert.are.same
describe("harpoon", function()
before_each(function()
Data.set_data_path("/tmp/harpoon2.json")
Data.__dangerously_clear_data()
@ -25,6 +26,37 @@ describe("harpoon", function()
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()
local file_name = "/tmp/harpoon-test"
local row = 3