Skip to content

Commit

Permalink
feat: resolve_display now works
Browse files Browse the repository at this point in the history
  • Loading branch information
ThePrimeagen committed Nov 4, 2023
1 parent ab45ad2 commit 362b79f
Show file tree
Hide file tree
Showing 3 changed files with 125 additions and 22 deletions.
15 changes: 11 additions & 4 deletions lua/harpoon2/config.lua
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ local M = {}
---@field display? (fun(list_item: HarpoonListItem): string)
---@field select? (fun(list_item: HarpoonListItem): nil)
---@field equals? (fun(list_line_a: HarpoonListItem, list_line_b: HarpoonListItem): boolean)
---@field add? fun(): HarpoonListItem
---@field add? fun(item: any?): HarpoonListItem

---@class HarpoonSettings
---@field save_on_toggle boolean defaults to true
Expand Down Expand Up @@ -90,10 +90,17 @@ function M.get_default_config()
return list_item_a.value == list_item_b.value
end,

---@param value any
---@return HarpoonListItem
add = function()
local name = vim.api.nvim_buf_get_name(vim.api.nvim_get_current_buf())
local pos = vim.api.nvim_win_get_cursor(0)
add = function(name)
name = name or vim.api.nvim_buf_get_name(vim.api.nvim_get_current_buf())
local bufnr = vim.fn.bufnr(name, false)

local pos = {1, 0}
if bufnr ~= -1 then
pos = vim.api.nvim_win_get_cursor(0)
end

return {
value = name,
context = {
Expand Down
35 changes: 17 additions & 18 deletions lua/harpoon2/list.lua
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
local function index_of(config, items, element)
local function index_of(items, element, config)
local equals = config and config.equals or function(a, b) return a == b end
local index = -1
for i, item in ipairs(items) do
if config.equals(element, item) then
if equals(element, item) then
index = i
break
end
Expand Down Expand Up @@ -33,7 +34,7 @@ end
function HarpoonList:append(item)
item = item or self.config.add()

local index = index_of(self.config, self.items, item)
local index = index_of(self.items, item, self.config)
if index == -1 then
table.insert(self.items, item)
end
Expand All @@ -44,7 +45,7 @@ end
---@return HarpoonList
function HarpoonList:prepend(item)
item = item or self.config.add()
local index = index_of(self.config, self.items, item)
local index = index_of(self.items, item, self.config)
if index == -1 then
table.insert(self.items, 1, item)
end
Expand Down Expand Up @@ -76,25 +77,23 @@ end
--- much inefficiencies. dun care
---@param displayed string[]
function HarpoonList:resolve_displayed(displayed)
local not_found = {}

for _, v in ipairs(displayed) do
local found = false
for _, in_table in ipairs(self.items) do
if self.config.display(in_table) == v then
found = true
break
local new_list = {}

local list_displayed = self:display()
for i, v in ipairs(displayed) do
local index = index_of(list_displayed, v)
if index == -1 then
table.insert(new_list, self.config.add(v))
else
local index_in_new_list = index_of(new_list, self.items[index], self.config)
if index_in_new_list == -1 then
table.insert(new_list, self.items[index])
end
end

if not found then
table.insert(not_found, v)
end
end

for _, v in ipairs(not_found) do
self:remove(v)
end
self.items = new_list
end

--- @return string[]
Expand Down
97 changes: 97 additions & 0 deletions lua/harpoon2/test/harpoon_spec.lua
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,103 @@ describe("harpoon", function()
})

end)

it("ui - display resolve", function()
harpoon:setup({
default = {
display = function(item)
-- split string on /
local parts = vim.split(item.value, "/")
return parts[#parts]
end
}
})

local file_names = {
"/tmp/harpoon-test-1",
"/tmp/harpoon-test-2",
"/tmp/harpoon-test-3",
"/tmp/harpoon-test-4",
}

local contents = { "foo", "bar", "baz", "qux" }

local bufnrs = {}
local list = harpoon:list()
for _, v in ipairs(file_names) do
table.insert(bufnrs, utils.create_file(v, contents))
harpoon:list():append()
end

local displayed = list:display()
eq(displayed, {
"harpoon-test-1",
"harpoon-test-2",
"harpoon-test-3",
"harpoon-test-4",
})

table.remove(displayed, 3)
table.remove(displayed, 2)

list:resolve_displayed(displayed)

eq(list.items, {
{value = file_names[1], context = {row = 4, col = 2}},
{value = file_names[4], context = {row = 4, col = 2}},
})
end)

it("ui - display resolve", function()
local file_names = {
"/tmp/harpoon-test-1",
"/tmp/harpoon-test-2",
"/tmp/harpoon-test-3",
"/tmp/harpoon-test-4",
}

local contents = { "foo", "bar", "baz", "qux" }

local bufnrs = {}
local list = harpoon:list()
for _, v in ipairs(file_names) do
table.insert(bufnrs, utils.create_file(v, contents))
harpoon:list():append()
end

local displayed = list:display()
eq(displayed, {
"/tmp/harpoon-test-1",
"/tmp/harpoon-test-2",
"/tmp/harpoon-test-3",
"/tmp/harpoon-test-4",
})

table.remove(displayed, 3)
table.remove(displayed, 2)

table.insert(displayed, "/tmp/harpoon-test-other-file-1")
table.insert(displayed, "/tmp/harpoon-test-other-file-2")

list:resolve_displayed(displayed)

eq(list.items, {
{value = file_names[1], context = {row = 4, col = 2}},
{value = file_names[4], context = {row = 4, col = 2}},
{value = "/tmp/harpoon-test-other-file-1", context = {row = 1, col = 0}},
{value = "/tmp/harpoon-test-other-file-2", context = {row = 1, col = 0}},
})

table.remove(displayed, 3)
table.insert(displayed, "/tmp/harpoon-test-4")
list:resolve_displayed(displayed)

eq(list.items, {
{value = file_names[1], context = {row = 4, col = 2}},
{value = file_names[4], context = {row = 4, col = 2}},
{value = "/tmp/harpoon-test-other-file-2", context = {row = 1, col = 0}},
})
end)
end)


1 comment on commit 362b79f

@JordanShurmer
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

shoutout for this functionality. resolving the ui back to the data behind the curtain was a great idea

Please sign in to comment.