diff --git a/lua/harpoonline/init.lua b/lua/harpoonline/init.lua index 6914765..3cd7c32 100644 --- a/lua/harpoonline/init.lua +++ b/lua/harpoonline/init.lua @@ -167,50 +167,48 @@ H.create_autocommands = function() }) end +-- Update the data when the user adds to or removes from a list. +-- Needed because those actions can be done without leaving the buffer. +-- All other update scenarios are covered by listening to the BufEnter event. +H.create_extensions = function(Extensions) + H.harpoon_plugin:extend({ [Extensions.event_names.ADD] = H.update }) + H.harpoon_plugin:extend({ [Extensions.event_names.REMOVE] = H.update }) +end + ---@return HarpoonList H.get_list = function() return H.harpoon_plugin:list(H.data.list_name) end -- If the current buffer is harpooned, return the index of the harpoon mark -- Otherwise, return nil +---@param list HarpoonList ---@return number|nil -H.buffer_idx = function() +H.buffer_idx = function(list) if vim.bo.buftype ~= '' then return end -- not a normal buffer + if list:length() == 0 then return end -- no items in the list local current_file = vim.fn.expand('%:p:.') - local marks = H.get_list().items - for idx, item in ipairs(marks) do + for idx, item in ipairs(list.items) do if item.value == current_file then return idx end end end --- Update the data when the user adds to or removes from a list. --- Needed because those actions can be done without leaving the buffer. --- All other update scenarios are covered by listening to the BufEnter event. -H.create_extensions = function(Extensions) - H.harpoon_plugin:extend({ - [Extensions.event_names.ADD] = H.update, - }) - H.harpoon_plugin:extend({ - [Extensions.event_names.REMOVE] = H.update, - }) -end - -H.update_data = function() - H.data.list_length = H.get_list():length() - H.data.buffer_idx = H.buffer_idx() +---@param list HarpoonList +H.update_data = function(list) + H.data.list_length = list:length() + H.data.buffer_idx = H.buffer_idx(list) end -- To be invoked on any harpoon-related event -- Performs action on_update if present H.update = function() - H.update_data() + H.update_data(H.get_list()) H.cached_result = nil -- the format function should recompute local on_update = H.get_config().on_update if on_update then on_update() end end -H.initialize = function() H.update_data() end +H.initialize = function() H.update_data(H.get_list()) end -- Return either the icon or an empty string ---@return string diff --git a/tests/test_harpoonline.lua b/tests/test_harpoonline.lua index 1d8021c..1a27732 100644 --- a/tests/test_harpoonline.lua +++ b/tests/test_harpoonline.lua @@ -1,7 +1,6 @@ local MiniTest = require('mini.test') local new_set = MiniTest.new_set local eq = MiniTest.expect.equality --- local expect = MiniTest.expect ---@class MiniTestChildNeovim ---@field stop function @@ -9,7 +8,6 @@ local eq = MiniTest.expect.equality ---@field lua function ---@field lua_get function ---@field cmd function ----@field api function ---@type MiniTestChildNeovim local child = MiniTest.new_child_neovim() -- Create (but not start) child Neovim object @@ -139,6 +137,15 @@ T['format()']['extended']['remove item'] = function() child.lua([[ require("harpoon"):list():remove_at(3) ]]) eq(child.lua_get([[ M.format() ]]), icon .. ' 1 2 ') end +T['format()']['extended']['remove all items'] = function() + child.lua([[M.setup()]]) + add_files_to_list({ '1', '2' }) + child.lua([[ require("harpoon"):list():remove_at(2) ]]) + child.lua([[ require("harpoon"):list():remove_at(1) ]]) + eq(child.lua_get([[ M.format() ]]), icon .. ' 1 ') -- should be empty + + MiniTest.add_note('Incorrect, not empty! See harpoon issue #555') +end T['format()']['extended']['switch list'] = function() child.lua([[M.setup()]]) add_files_to_list({ '1', '2' }, 'dev')