diff --git a/lua/harpoon2/list.lua b/lua/harpoon2/list.lua index 3c22198..269ee35 100644 --- a/lua/harpoon2/list.lua +++ b/lua/harpoon2/list.lua @@ -1,10 +1,19 @@ --- TODO: Define the config object +local function index_of(config, items, element) + local index = -1 + for i, item in ipairs(items) do + if config.equals(element, item) then + index = i + break + end + end + + return index +end --- @class HarpoonItem --- @field value string --- @field context any ---- create a table object to be new'd --- @class HarpoonList --- @field config HarpoonPartialConfigItem --- @field name string @@ -21,14 +30,19 @@ function HarpoonList:new(config, name, items) end ---@return HarpoonList -function HarpoonList:push(item) +function HarpoonList:append(item) item = item or self.config.add() - table.insert(self.items, item) + + local index = index_of(self.config, self.items, item) + if index == -1 then + table.insert(self.items, item) + end + return self end ---@return HarpoonList -function HarpoonList:addToFront(item) +function HarpoonList:prepend(item) item = item or self.config.add() table.insert(self.items, 1, item) return self diff --git a/lua/harpoon2/test/harpoon_spec.lua b/lua/harpoon2/test/harpoon_spec.lua index e409a25..3c80711 100644 --- a/lua/harpoon2/test/harpoon_spec.lua +++ b/lua/harpoon2/test/harpoon_spec.lua @@ -29,7 +29,7 @@ describe("harpoon", function() }) vim.api.nvim_win_set_cursor(0, {row, col}) - local list = harpoon:list():push() + local list = harpoon:list():append() harpoon:sync() eq(harpoon:dump(), { diff --git a/lua/harpoon2/test/list_spec.lua b/lua/harpoon2/test/list_spec.lua index 4ef8627..bf99690 100644 --- a/lua/harpoon2/test/list_spec.lua +++ b/lua/harpoon2/test/list_spec.lua @@ -21,8 +21,9 @@ describe("list", function() end } }) + local list_config = Config.get_config(config, "foo") - local list = List.decode(config, "foo", {"foo:bar", "baz:qux"}) + local list = List.decode(list_config, "foo", {"foo:bar", "baz:qux"}) local displayed = list:display() eq(displayed, {