Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(encode): use a for-loop, instead of next() or ipairs(), to prevent loss of data #573

Open
wants to merge 3 commits into
base: harpoon2
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 13 additions & 4 deletions lua/harpoon/list.lua
Original file line number Diff line number Diff line change
Expand Up @@ -344,8 +344,12 @@ end
--- @return string[]
function HarpoonList:encode()
local out = {}
for _, v in ipairs(self.items) do
table.insert(out, self.config.encode(v))
local items = self.items
for i = 1, self._length do
local item = items[i]
if item then
out[i] = self.config.encode(item)
end
end

return out
Expand All @@ -358,8 +362,13 @@ end
function HarpoonList.decode(list_config, name, items)
local list_items = {}

for _, item in ipairs(items) do
table.insert(list_items, list_config.decode(item))
for i = 1, #items do
local item = items[i]
if item == vim.NIL then
list_items[i] = nil -- allow nil-values
else
list_items[i] = list_config.decode(item)
end
end

return HarpoonList:new(list_config, name, list_items)
Expand Down
25 changes: 25 additions & 0 deletions lua/harpoon/test/harpoon_spec.lua
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,31 @@ describe("harpoon", function()
})
end)

it("should ignore removed items", function()
local list = harpoon:list()

utils.create_file("/tmp/harpoon-test1", {}, 1, 0)
list:add()

utils.create_file("/tmp/harpoon-test2", {}, 1, 0)
list:add()

utils.create_file("/tmp/harpoon-test3", {}, 1, 0)
list:add()

list:remove_at(2) -- remove the second item

local count_items = 0
local encoded_items = list:encode()
local i, _ = next(encoded_items, nil)
while i do
count_items = count_items + 1
i, _ = next(encoded_items, i)
end

eq(2, count_items, "expecting two items in the list")
end)

it("out of bounds test: row over", function()
out_of_bounds_test({
row = 5,
Expand Down