Skip to content

Commit

Permalink
feat: changing buffers updates list positions
Browse files Browse the repository at this point in the history
  • Loading branch information
ThePrimeagen committed Nov 21, 2023
1 parent eafaec8 commit 90bcfeb
Show file tree
Hide file tree
Showing 4 changed files with 103 additions and 9 deletions.
17 changes: 17 additions & 0 deletions lua/harpoon2/config.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
53 changes: 44 additions & 9 deletions lua/harpoon2/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -26,13 +27,40 @@ function Harpoon:new()
config = config,
data = Data.Data:new(),
lists = {},
hooks_setup = false,
}, self)
end

---@param partial_config HarpoonPartialConfig
---@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

Expand Down Expand Up @@ -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]

if not seen then
return
end

for list_name, _ in pairs(seen) do
local encoded = lists[list_name]:encode()
self.data:update(key, list_name, encoded)
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()
Expand Down
10 changes: 10 additions & 0 deletions lua/harpoon2/list.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
32 changes: 32 additions & 0 deletions lua/harpoon2/test/harpoon_spec.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand All @@ -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
Expand Down

0 comments on commit 90bcfeb

Please sign in to comment.