diff --git a/README.md b/README.md index d48ceb8..05ebdbe 100644 --- a/README.md +++ b/README.md @@ -170,7 +170,8 @@ Settings can alter the experience of harpoon **Definition** ```lua ---@class HarpoonSettings ----@field save_on_toggle boolean defaults to true +---@field save_on_toggle boolean defaults to false +---@field sync_on_ui_close boolean defaults to false ---@field key (fun(): string) ``` @@ -184,6 +185,7 @@ Settings can alter the experience of harpoon ```lua settings = { save_on_toggle = false, + sync_on_ui_close = false, border_chars = { "─", "│", "─", "│", "╭", "╮", "╯", "╰" }, key = function() return vim.loop.cwd() diff --git a/lua/harpoon/buffer.lua b/lua/harpoon/buffer.lua index fd0e05c..494001f 100644 --- a/lua/harpoon/buffer.lua +++ b/lua/harpoon/buffer.lua @@ -24,7 +24,7 @@ end function M.run_toggle_command(key) local harpoon = require("harpoon") harpoon.logger:log("toggle by keymap '" .. key .. "'") - harpoon.ui:select_menu_item() + harpoon.ui:toggle_quick_menu() end ---TODO: I don't know how to do what i want to do, but i want to be able to @@ -63,7 +63,7 @@ function M.setup_autocmds_and_keymaps(bufnr) bufnr, "n", "", - "lua require('harpoon.buffer').run_toggle_command('')", + "lua require('harpoon.buffer').run_toggle_command('Esc')", { silent = true } ) vim.api.nvim_buf_set_keymap( @@ -86,16 +86,17 @@ function M.setup_autocmds_and_keymaps(bufnr) ) end --]] - vim.cmd( - string.format( - "autocmd BufModifiedSet set nomodified", - bufnr - ) - ) + vim.api.nvim_create_autocmd("BufModifiedSet", { + buffer = bufnr, + group = HarpoonGroup, + callback = function() + vim.api.nvim_buf_set_option(bufnr, "modified", false) + end, + }) vim.api.nvim_create_autocmd({ "BufWriteCmd" }, { group = HarpoonGroup, - pattern = "__harpoon*", + buffer = bufnr, callback = function() require("harpoon").ui:save() vim.schedule(function() @@ -107,7 +108,7 @@ function M.setup_autocmds_and_keymaps(bufnr) vim.api.nvim_create_autocmd({ "BufLeave" }, { group = HarpoonGroup, - pattern = "__harpoon*", + buffer = bufnr, callback = function() require("harpoon").logger:log("toggle by BufLeave") require("harpoon").ui:toggle_quick_menu() diff --git a/lua/harpoon/config.lua b/lua/harpoon/config.lua index 260f004..10af4d2 100644 --- a/lua/harpoon/config.lua +++ b/lua/harpoon/config.lua @@ -27,12 +27,14 @@ M.DEFAULT_LIST = DEFAULT_LIST ---@class HarpoonSettings ---@field border_chars string[] defaults to { "─", "│", "─", "│", "╭", "╮", "╯", "╰" } ---@field save_on_toggle boolean defaults to true +---@field sync_on_ui_close? boolean ---@field ui_fallback_width number defaults 69, nice ---@field ui_width_ratio number defaults to 0.62569 ---@field key (fun(): string) ---@class HarpoonPartialSettings ---@field save_on_toggle? boolean +---@field sync_on_ui_close? boolean ---@field key? (fun(): string) ---@class HarpoonConfig @@ -56,6 +58,7 @@ function M.get_default_config() settings = { save_on_toggle = false, + sync_on_ui_close = false, border_chars = { "─", "│", @@ -117,6 +120,10 @@ function M.get_default_config() set_position = true bufnr = vim.fn.bufnr(list_item.value, true) end + if not vim.api.nvim_buf_is_loaded(bufnr) then + vim.fn.bufload(bufnr) + vim.api.nvim_buf_set_option(bufnr, "buflisted", true) + end if options.vsplit then vim.cmd("vsplit") diff --git a/lua/harpoon/init.lua b/lua/harpoon/init.lua index f0edf64..72d9ab1 100644 --- a/lua/harpoon/init.lua +++ b/lua/harpoon/init.lua @@ -51,6 +51,10 @@ function Harpoon:list(name) local existing_list = lists[name] if existing_list then + if not self.data.seen[key] then + self.data.seen[key] = {} + end + self.data.seen[key][name] = true return existing_list end diff --git a/lua/harpoon/list.lua b/lua/harpoon/list.lua index 20a44e3..2e0130d 100644 --- a/lua/harpoon/list.lua +++ b/lua/harpoon/list.lua @@ -99,12 +99,14 @@ end ---@return HarpoonList function HarpoonList:removeAt(index) - Listeners.listeners:emit( - Listeners.event_names.REMOVE, - { list = self, item = self.items[index], idx = index } - ) - Logger:log("HarpoonList:removeAt", { item = self.items[index], index = index }) - table.remove(self.items, index) + if self.items[index] then + Logger:log("HarpoonList:removeAt", { item = self.items[index], index = index }) + Listeners.listeners:emit( + Listeners.event_names.REMOVE, + { list = self, item = self.items[index], idx = index } + ) + table.remove(self.items, index) + end return self end @@ -129,11 +131,11 @@ function HarpoonList:resolve_displayed(displayed) local list_displayed = self:display() for i, v in ipairs(list_displayed) do - local index = index_of(list_displayed, v) + local index = index_of(displayed, v) if index == -1 then Listeners.listeners:emit( Listeners.event_names.REMOVE, - { list = self, item = v, idx = i } + { list = self, item = self.items[i], idx = i } ) end end @@ -141,11 +143,11 @@ function HarpoonList:resolve_displayed(displayed) for i, v in ipairs(displayed) do local index = index_of(list_displayed, v) if index == -1 then + new_list[i] = self.config.create_list_item(self.config, v) Listeners.listeners:emit( Listeners.event_names.ADD, - { list = self, item = v, idx = i } + { list = self, item = new_list[i], idx = i } ) - new_list[i] = self.config.create_list_item(self.config, v) else local index_in_new_list = index_of(new_list, self.items[index], self.config) diff --git a/lua/harpoon/ui.lua b/lua/harpoon/ui.lua index 4cd598e..65cdff4 100644 --- a/lua/harpoon/ui.lua +++ b/lua/harpoon/ui.lua @@ -107,7 +107,6 @@ end ---@param list? HarpoonList function HarpoonUI:toggle_quick_menu(list) - if list == nil or self.win_id ~= nil then Logger:log("ui#toggle_quick_menu#closing", list and list.name) if self.settings.save_on_toggle then @@ -154,6 +153,9 @@ function HarpoonUI:save() local list = Buffer.get_contents(self.bufnr) Logger:log("ui#save", list) self.active_list:resolve_displayed(list) + if self.settings.sync_on_ui_close then + require("harpoon"):sync() + end end ---@param settings HarpoonSettings