From 7bea73eb8021cbb328f62551b48d75869220864f Mon Sep 17 00:00:00 2001 From: yetone Date: Sat, 24 Aug 2024 00:14:20 +0800 Subject: [PATCH] fix: check if is sidebar buf (#179) --- lua/avante/init.lua | 4 ++-- lua/avante/selection.lua | 9 +++++---- lua/avante/sidebar.lua | 6 ++++++ lua/avante/utils/init.lua | 16 +++++++++++++++- 4 files changed, 28 insertions(+), 7 deletions(-) diff --git a/lua/avante/init.lua b/lua/avante/init.lua index ecb3e20..1f5d864 100644 --- a/lua/avante/init.lua +++ b/lua/avante/init.lua @@ -1,5 +1,6 @@ local api = vim.api +local Utils = require("avante.utils") local Sidebar = require("avante.sidebar") local Selection = require("avante.selection") local Config = require("avante.config") @@ -168,10 +169,9 @@ M.refresh = function() return end - local ft = vim.api.nvim_get_option_value("filetype", { buf = curbuf }) local listed = vim.api.nvim_get_option_value("buflisted", { buf = curbuf }) - if ft == "Avante" or not listed then + if Utils.is_sidebar_buffer(curbuf) or not listed then return end diff --git a/lua/avante/selection.lua b/lua/avante/selection.lua index 7e87ef8..1fa0bc1 100644 --- a/lua/avante/selection.lua +++ b/lua/avante/selection.lua @@ -1,3 +1,4 @@ +local Utils = require("avante.utils") local Config = require("avante.config") local api = vim.api @@ -66,7 +67,7 @@ function Selection:setup_autocmds() group = self.augroup, pattern = { "n:v", "n:V", "n:" }, -- Entering Visual mode from Normal mode callback = function(ev) - if vim.bo[ev.buf].filetype ~= "Avante" then + if not Utils.is_sidebar_buffer(ev.buf) then self:show_hints_popup() end end, @@ -75,8 +76,8 @@ function Selection:setup_autocmds() api.nvim_create_autocmd({ "CursorMoved", "CursorMovedI" }, { group = self.augroup, callback = function(ev) - if vim.bo[ev.buf].filetype ~= "Avante" then - if vim.fn.mode() == "v" or vim.fn.mode() == "V" or vim.fn.mode() == "" then + if not Utils.is_sidebar_buffer(ev.buf) then + if Utils.in_visual_mode() then self:show_hints_popup() else self:close_hints_popup() @@ -89,7 +90,7 @@ function Selection:setup_autocmds() group = self.augroup, pattern = { "v:n", "v:i", "v:c" }, -- Switching from visual mode back to normal, insert, or other modes callback = function(ev) - if vim.bo[ev.buf].filetype ~= "Avante" then + if not Utils.is_sidebar_buffer(ev.buf) then self:close_hints_popup() end end, diff --git a/lua/avante/sidebar.lua b/lua/avante/sidebar.lua index 8fab4e9..e02a4a1 100644 --- a/lua/avante/sidebar.lua +++ b/lua/avante/sidebar.lua @@ -799,6 +799,12 @@ function Sidebar:on_mount() end end, }) + + for _, comp in pairs(self) do + if comp and type(comp) == "table" and comp.mount and comp.bufnr and api.nvim_buf_is_valid(comp.bufnr) then + Utils.mark_as_sidebar_buffer(comp.bufnr) + end + end end function Sidebar:refresh_winids() diff --git a/lua/avante/utils/init.lua b/lua/avante/utils/init.lua index 7bf606f..d5506cc 100644 --- a/lua/avante/utils/init.lua +++ b/lua/avante/utils/init.lua @@ -39,7 +39,7 @@ end function M.in_visual_mode() local current_mode = vim.fn.mode() - return current_mode == "v" or current_mode == "V" or current_mode == "" + return current_mode == "v" or current_mode == "V" or current_mode == "" end ---Get the selected content and range in Visual mode @@ -302,4 +302,18 @@ M.buf_list_wins = function(bufnr) return wins end +local sidebar_buffer_var_name = "is_avante_sidebar_buffer" + +function M.mark_as_sidebar_buffer(bufnr) + api.nvim_buf_set_var(bufnr, sidebar_buffer_var_name, true) +end + +function M.is_sidebar_buffer(bufnr) + local ok, v = pcall(api.nvim_buf_get_var, bufnr, sidebar_buffer_var_name) + if not ok then + return false + end + return v == true +end + return M