From d02f7dc282e674731356616d6e17cdc5baed812c Mon Sep 17 00:00:00 2001 From: A-Lamia Date: Sat, 12 Aug 2023 17:26:57 +1000 Subject: [PATCH] fix: stop autocmds from duplicating on each SSR open SSR creates instances when you Ui:Open but the autocmds are not treated like an instance they are sharing the same autocmds, these autocmds how ever are being duplicated on each run of Ui:Open causing bad side effects like data invalidation changes in this commit creates a unique augroup and allows only one autocmd per instance then removes them once the instance is closed. --- lua/ssr.lua | 19 +++++++++++++++++-- 1 file changed, 17 insertions(+), 2 deletions(-) diff --git a/lua/ssr.lua b/lua/ssr.lua index 18253ef..92df957 100644 --- a/lua/ssr.lua +++ b/lua/ssr.lua @@ -78,6 +78,8 @@ function Ui:open() self.ns = api.nvim_create_namespace "" self.cur_match_ns = api.nvim_create_namespace "" + local augroup_name = "ssr_" .. tonumber(math.random(1000)) + local augroup = vim.api.nvim_create_augroup(augroup_name, { clear = true }) local function set_extmark(row, text) return api.nvim_buf_set_extmark(self.ui_buf, self.ns, row, 0, { virt_text = text, virt_text_pos = "overlay" }) end @@ -139,8 +141,10 @@ function Ui:open() }) api.nvim_win_set_cursor(win, { 3, 0 }) fn.matchadd("Title", [[$\w\+]]) + api.nvim_create_autocmd({ "TextChanged", "TextChangedI" }, { buffer = self.ui_buf, + group = augroup, callback = function() local lines = api.nvim_buf_get_lines(self.ui_buf, 0, -1, true) if #lines ~= api.nvim_win_get_height(win) and #lines >= config.min_height and #lines <= config.max_height then @@ -153,13 +157,24 @@ function Ui:open() self:search() end, }) + api.nvim_create_autocmd({ "WinClosed" }, { buffer = self.ui_buf, + group = augroup, callback = function() api.nvim_buf_clear_namespace(self.origin_buf, self.ns, 0, -1) api.nvim_buf_clear_namespace(self.origin_buf, self.cur_match_ns, 0, -1) - keymap.del("n", "n", { buffer = self.origin_buf }) - keymap.del("n", "N", { buffer = self.origin_buf }) + + -- Cleanup autocmds and augroup + api.nvim_clear_autocmds { group = augroup } + api.nvim_exec2("augroup! " .. augroup_name, {}) + + -- Prevents an error if two instances are created on + -- a singgle buffer. + pcall(function() + keymap.del("n", "n", { buffer = self.origin_buf }) + keymap.del("n", "N", { buffer = self.origin_buf }) + end) end, }) end