mirror of
https://github.com/ThePrimeagen/harpoon.git
synced 2025-07-14 10:00:29 +00:00
feat: changing buffers updates list positions
This commit is contained in:
parent
eafaec8a2e
commit
90bcfebca9
@ -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
|
||||
|
@ -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)
|
||||
end
|
||||
self.data:sync()
|
||||
|
||||
if not seen then
|
||||
return
|
||||
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")
|
||||
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
|
||||
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
|
||||
|
||||
function Harpoon:info()
|
||||
|
@ -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)
|
||||
|
@ -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
|
||||
|
Loading…
x
Reference in New Issue
Block a user