From 2fc5ee0fed571b9e0b4c7d0b59119eca9bf07571 Mon Sep 17 00:00:00 2001 From: luozhiya Date: Sun, 2 Jun 2024 20:06:41 +0800 Subject: [PATCH] Toggle chat --- lua/fittencode/api.lua | 3 +++ lua/fittencode/bindings.lua | 2 ++ lua/fittencode/engines/actions/init.lua | 8 ++++++++ lua/fittencode/views/chat.lua | 5 +++++ 4 files changed, 18 insertions(+) diff --git a/lua/fittencode/api.lua b/lua/fittencode/api.lua index bc390a6..d84ebba 100644 --- a/lua/fittencode/api.lua +++ b/lua/fittencode/api.lua @@ -134,6 +134,9 @@ M.api = { show_chat = function() return ActionsEngine.show_chat() end, + toggle_chat = function() + return ActionsEngine.toggle_chat() + end, } return M diff --git a/lua/fittencode/bindings.lua b/lua/fittencode/bindings.lua index 0d2e684..246cd1c 100644 --- a/lua/fittencode/bindings.lua +++ b/lua/fittencode/bindings.lua @@ -208,6 +208,8 @@ function M.setup_commands() stop_eval = API.stop_eval, -- Arguments: Nop show_chat = API.show_chat, + -- Arguments: Nop + toggle_chat = API.toggle_chat, } Base.command('Fitten', function(line) ---@type string[] diff --git a/lua/fittencode/engines/actions/init.lua b/lua/fittencode/engines/actions/init.lua index b1f7caf..5e96322 100644 --- a/lua/fittencode/engines/actions/init.lua +++ b/lua/fittencode/engines/actions/init.lua @@ -690,4 +690,12 @@ function ActionsEngine.show_chat() chat:show() end +function ActionsEngine.toggle_chat() + if chat:is_visible() then + chat:close() + else + chat:show() + end +end + return ActionsEngine diff --git a/lua/fittencode/views/chat.lua b/lua/fittencode/views/chat.lua index ec5869f..f6412cd 100644 --- a/lua/fittencode/views/chat.lua +++ b/lua/fittencode/views/chat.lua @@ -12,6 +12,7 @@ local Log = require('fittencode.log') ---@field create function ---@field last_cursor? table ---@field callbacks table +---@field is_visible function local M = {} function M:new(callbacks) @@ -158,4 +159,8 @@ function M:commit(lines) return _commit(self.window, self.buffer, lines) end +function M:is_visible() + return self.window and api.nvim_win_is_valid(self.window) +end + return M