From b158c04d1fc0e4023ba51a804ba296d06d5030fc Mon Sep 17 00:00:00 2001 From: Marco Kellershoff <1384938+gorillamoe@users.noreply.github.com> Date: Wed, 14 Aug 2024 22:05:03 +0200 Subject: [PATCH] fix(winbar): keymappings not working anymore (#137) --- lua/kulala/globals/init.lua | 2 +- lua/kulala/ui/callbacks.lua | 16 ++++++++++++++++ lua/kulala/ui/init.lua | 7 ++++++- lua/kulala/ui/winbar.lua | 8 +++++--- 4 files changed, 28 insertions(+), 5 deletions(-) create mode 100644 lua/kulala/ui/callbacks.lua diff --git a/lua/kulala/globals/init.lua b/lua/kulala/globals/init.lua index 6ce3746..06b4dcf 100644 --- a/lua/kulala/globals/init.lua +++ b/lua/kulala/globals/init.lua @@ -2,7 +2,7 @@ local FS = require("kulala.utils.fs") local M = {} -M.VERSION = "3.1.1" +M.VERSION = "3.1.2" M.UI_ID = "kulala://ui" M.SCRATCHPAD_ID = "kulala://scratchpad" M.HEADERS_FILE = FS.get_plugin_tmp_dir() .. "/headers.txt" diff --git a/lua/kulala/ui/callbacks.lua b/lua/kulala/ui/callbacks.lua new file mode 100644 index 0000000..948c903 --- /dev/null +++ b/lua/kulala/ui/callbacks.lua @@ -0,0 +1,16 @@ +local M = {} + +M.callbacks = {} + +M.add = function(name, callback) + if M.callbacks[name] == nil then + M.callbacks[name] = {} + end + table.insert(M.callbacks[name], callback) +end + +M.get = function(name) + return M.callbacks[name] or {} +end + +return M diff --git a/lua/kulala/ui/init.lua b/lua/kulala/ui/init.lua index 95f170f..2871fc6 100644 --- a/lua/kulala/ui/init.lua +++ b/lua/kulala/ui/init.lua @@ -1,3 +1,4 @@ +local UICallbacks = require("kulala.ui.callbacks") local WINBAR = require("kulala.ui.winbar") local GLOBALS = require("kulala.globals") local CONFIG = require("kulala.config") @@ -42,6 +43,7 @@ end ---This is necessary to prevent bugs like this: ---https://github.com/mistweaverco/kulala.nvim/issues/128 local replace_buffer = function() + local callbacks = UICallbacks.get("on_replace_buffer") local old_bufnr = get_buffer() local new_bufnr = vim.api.nvim_create_buf(true, false) @@ -60,6 +62,9 @@ local replace_buffer = function() -- Set the buffer name to the UI_ID after we have deleted the old buffer vim.api.nvim_buf_set_name(new_bufnr, GLOBALS.UI_ID) + for _, callback in ipairs(callbacks) do + callback(old_bufnr, new_bufnr) + end return new_bufnr end @@ -68,7 +73,7 @@ local open_buffer = function() local sd = CONFIG.get().split_direction == "vertical" and "vsplit" or "split" vim.cmd(sd .. " " .. GLOBALS.UI_ID) if CONFIG.get().winbar then - WINBAR.create_winbar(get_win(), get_buffer()) + WINBAR.create_winbar(get_win()) end vim.api.nvim_set_current_win(prev_win) end diff --git a/lua/kulala/ui/winbar.lua b/lua/kulala/ui/winbar.lua index 16eadd4..3216f54 100644 --- a/lua/kulala/ui/winbar.lua +++ b/lua/kulala/ui/winbar.lua @@ -1,3 +1,4 @@ +local UICallbacks = require("kulala.ui.callbacks") local CONFIG = require("kulala.config") local M = {} @@ -41,13 +42,14 @@ M.toggle_winbar_tab = function(win_id, view) end ---@param win_id integer|nil Window id ----@param buf integer|nil Buffer -M.create_winbar = function(win_id, buf) +M.create_winbar = function(win_id) if win_id then local default_view = CONFIG.get().default_view M.winbar_sethl() M.toggle_winbar_tab(win_id, default_view) - M.winbar_set_key_mapping(buf) + UICallbacks.add("on_replace_buffer", function(_, new_buffer) + M.winbar_set_key_mapping(new_buffer) + end) end end