From 7837e127b86f0e6a434b13642a5e3e4730a3d7b3 Mon Sep 17 00:00:00 2001 From: luozhiya Date: Thu, 27 Jun 2024 08:52:47 +0800 Subject: [PATCH] Focus the chat window --- lua/fittencode/config.lua | 3 +++ lua/fittencode/engines/actions/init.lua | 8 +++++++- lua/fittencode/engines/inline/init.lua | 2 +- lua/fittencode/views/chat.lua | 9 +++++++++ 4 files changed, 20 insertions(+), 2 deletions(-) diff --git a/lua/fittencode/config.lua b/lua/fittencode/config.lua index c2918be2..c37f00a4 100644 --- a/lua/fittencode/config.lua +++ b/lua/fittencode/config.lua @@ -48,6 +48,7 @@ local M = {} ---@class FittenCodeChatSidebarOptions ---@field width integer ---@field position 'left' | 'right' +---@field focus boolean ---@class FittenCodeChatFloatingOptions ---@field border 'rounded' | 'none' @@ -168,6 +169,8 @@ local defaults = { -- * `left` -- * `right` position = 'left', + -- Focus the chat window when there is a new action is performed. + focus = false, }, floating = { -- Border style of the floating window. diff --git a/lua/fittencode/engines/actions/init.lua b/lua/fittencode/engines/actions/init.lua index cd598bb9..0cc73f9d 100644 --- a/lua/fittencode/engines/actions/init.lua +++ b/lua/fittencode/engines/actions/init.lua @@ -511,6 +511,10 @@ local function _start_action_wrap(window, buffer, action, action_name, headless, end end + if not opts.silence then + chat:try_focus() + end + chain_actions({ start = true, prompt_ctx = prompt_ctx, @@ -546,6 +550,7 @@ function ActionsEngine.start_action(action, opts) local headless = opts.headless == true if headless then + opts.silence = true _start_action_wrap(window, buffer, action, action_name, true, opts) return end @@ -768,7 +773,8 @@ function ActionsEngine.show_chat() if not chat:is_created() then chat:create() end - chat:show() + chat:show(api.nvim_get_current_win()) + chat:try_focus() end function ActionsEngine.toggle_chat() diff --git a/lua/fittencode/engines/inline/init.lua b/lua/fittencode/engines/inline/init.lua index 6e92db75..fbf49352 100644 --- a/lua/fittencode/engines/inline/init.lua +++ b/lua/fittencode/engines/inline/init.lua @@ -187,7 +187,7 @@ local function _generate_one_stage(row, col, on_success, on_error) local ctx = PromptProviders.get_current_prompt_ctx(row, col) Sessions.request_generate_one_stage(task_id, ctx, function(id, _, suggestions) local lines = preprocessing(ctx, id, suggestions) - Log.debug('InlineEngine<{}> Preprocessed: {}', string.format('%x', id), lines) + Log.debug('InlineEngine<{}> Preprocessed: {}, Generated: {}', string.format('%x', id), lines, suggestions) if lines and #lines > 0 then status:update(SC.SUGGESTIONS_READY) apply_new_suggestions(task_id, row, col, lines) diff --git a/lua/fittencode/views/chat.lua b/lua/fittencode/views/chat.lua index 7e51ab43..aed4248a 100644 --- a/lua/fittencode/views/chat.lua +++ b/lua/fittencode/views/chat.lua @@ -211,6 +211,15 @@ local function set_autocmds_win(window) }) end +function M:try_focus() + if self.window and api.nvim_win_is_valid(self.window) and Config.options.chat.sidebar.focus and Config.options.chat.style == 'sidebar' then + fn.win_gotoid(self.window) + if Base.vmode() then + api.nvim_win_call(self.window, function() api.nvim_feedkeys(api.nvim_replace_termcodes('', true, true, true), 'nx', false) end) + end + end +end + function M:show(parent) if not self.buffer or not api.nvim_buf_is_valid(self.buffer) then return