From 515bdd00580a85df921fc1d8e08c27037f196a93 Mon Sep 17 00:00:00 2001 From: Will Hopkins Date: Thu, 7 Dec 2023 14:35:59 -0800 Subject: [PATCH 1/6] fix(ui): comprehensive UI fixes - fix: sync on UI save (#377) - fix: ensure buf is loaded on select (#383) - fix: remove angle brackets from Escape mapping rhs (#379, #385) - fix: incorrect function call in run_toggle_command (#379) - refactor: use buf-local autocmds - refactor: use Lua API to create BufModifiedSet autocmd fixes #383, #385, #379, #377, #358 --- lua/harpoon/buffer.lua | 21 +++++++++++---------- lua/harpoon/config.lua | 4 ++++ lua/harpoon/ui.lua | 2 +- 3 files changed, 16 insertions(+), 11 deletions(-) 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..578c785 100644 --- a/lua/harpoon/config.lua +++ b/lua/harpoon/config.lua @@ -117,6 +117,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/ui.lua b/lua/harpoon/ui.lua index 4cd598e..7360d22 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,7 @@ function HarpoonUI:save() local list = Buffer.get_contents(self.bufnr) Logger:log("ui#save", list) self.active_list:resolve_displayed(list) + require("harpoon"):sync() end ---@param settings HarpoonSettings From 10c79d8f4fe0ffe44b12086d5f6cb6de4dbf7334 Mon Sep 17 00:00:00 2001 From: Will Hopkins Date: Fri, 8 Dec 2023 13:05:37 -0800 Subject: [PATCH 2/6] fix(data): mark list as seen if found in `Harpoon:list()` --- lua/harpoon/init.lua | 4 ++++ 1 file changed, 4 insertions(+) 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 From ad621a6656434288316141cb2eccf31a2009d958 Mon Sep 17 00:00:00 2001 From: Will Hopkins Date: Fri, 8 Dec 2023 13:11:09 -0800 Subject: [PATCH 3/6] refactor: move call to `sync` into `resolve_displayed` --- lua/harpoon/list.lua | 2 ++ lua/harpoon/ui.lua | 1 - 2 files changed, 2 insertions(+), 1 deletion(-) diff --git a/lua/harpoon/list.lua b/lua/harpoon/list.lua index 20a44e3..a9a7cf4 100644 --- a/lua/harpoon/list.lua +++ b/lua/harpoon/list.lua @@ -156,6 +156,8 @@ function HarpoonList:resolve_displayed(displayed) end self.items = new_list + + require("harpoon"):sync() end function HarpoonList:select(index, options) diff --git a/lua/harpoon/ui.lua b/lua/harpoon/ui.lua index 7360d22..9d21c7d 100644 --- a/lua/harpoon/ui.lua +++ b/lua/harpoon/ui.lua @@ -153,7 +153,6 @@ function HarpoonUI:save() local list = Buffer.get_contents(self.bufnr) Logger:log("ui#save", list) self.active_list:resolve_displayed(list) - require("harpoon"):sync() end ---@param settings HarpoonSettings From 3d469c19caf5a0689904ed9d8abf59bdbcdf3d2f Mon Sep 17 00:00:00 2001 From: Will Hopkins Date: Sat, 9 Dec 2023 00:07:12 -0800 Subject: [PATCH 4/6] fix: check displayed list for removed values fix: properly pass list item tables to events in `resolve_displayed` --- lua/harpoon/list.lua | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/lua/harpoon/list.lua b/lua/harpoon/list.lua index a9a7cf4..d14ae31 100644 --- a/lua/harpoon/list.lua +++ b/lua/harpoon/list.lua @@ -129,11 +129,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 +141,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) From 61742f45c830afe6ad61e63390143387465c48f3 Mon Sep 17 00:00:00 2001 From: Will Hopkins Date: Sat, 9 Dec 2023 00:13:36 -0800 Subject: [PATCH 5/6] fix: only dispatch remove evt if an item is actually removed by removeAt --- lua/harpoon/list.lua | 14 ++++++++------ 1 file changed, 8 insertions(+), 6 deletions(-) diff --git a/lua/harpoon/list.lua b/lua/harpoon/list.lua index d14ae31..62d7f9b 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 From ac9b93984f222865c827e8677f362d0286b9d188 Mon Sep 17 00:00:00 2001 From: Will Hopkins Date: Sat, 9 Dec 2023 12:14:30 -0800 Subject: [PATCH 6/6] feat: add `sync_on_ui_close` option --- README.md | 4 +++- lua/harpoon/config.lua | 3 +++ lua/harpoon/list.lua | 2 -- lua/harpoon/ui.lua | 3 +++ 4 files changed, 9 insertions(+), 3 deletions(-) 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/config.lua b/lua/harpoon/config.lua index 578c785..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 = { "─", "│", diff --git a/lua/harpoon/list.lua b/lua/harpoon/list.lua index 62d7f9b..2e0130d 100644 --- a/lua/harpoon/list.lua +++ b/lua/harpoon/list.lua @@ -158,8 +158,6 @@ function HarpoonList:resolve_displayed(displayed) end self.items = new_list - - require("harpoon"):sync() end function HarpoonList:select(index, options) diff --git a/lua/harpoon/ui.lua b/lua/harpoon/ui.lua index 9d21c7d..65cdff4 100644 --- a/lua/harpoon/ui.lua +++ b/lua/harpoon/ui.lua @@ -153,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