Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Focus the chat window #97

Merged
merged 1 commit into from
Jun 28, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions lua/fittencode/config.lua
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ local M = {}
---@class FittenCodeChatSidebarOptions
---@field width integer
---@field position 'left' | 'right'
---@field focus boolean

---@class FittenCodeChatFloatingOptions
---@field border 'rounded' | 'none'
Expand Down Expand Up @@ -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.
Expand Down
8 changes: 7 additions & 1 deletion lua/fittencode/engines/actions/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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()
Expand Down
2 changes: 1 addition & 1 deletion lua/fittencode/engines/inline/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
9 changes: 9 additions & 0 deletions lua/fittencode/views/chat.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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('<ESC>', 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
Expand Down