feat: auto focus sidebar (#982)

This commit is contained in:
Alban Kurti 2025-01-08 18:40:19 +01:00 committed by GitHub
parent 794c643033
commit 225e6f9572
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 15 additions and 9 deletions

View File

@ -122,6 +122,7 @@ M._defaults = {
---4. support_paste_from_clipboard : Whether to support pasting image from clipboard. This will be determined automatically based whether img-clip is available or not. ---4. support_paste_from_clipboard : Whether to support pasting image from clipboard. This will be determined automatically based whether img-clip is available or not.
---5. minimize_diff : Whether to remove unchanged lines when applying a code block ---5. minimize_diff : Whether to remove unchanged lines when applying a code block
behaviour = { behaviour = {
auto_focus_sidebar = true,
auto_suggestions = false, -- Experimental stage auto_suggestions = false, -- Experimental stage
auto_set_highlight_group = true, auto_set_highlight_group = true,
auto_set_keymaps = true, auto_set_keymaps = true,

View File

@ -1041,10 +1041,16 @@ function Sidebar:on_mount(opts)
group = self.augroup, group = self.augroup,
buffer = self.result_container.bufnr, buffer = self.result_container.bufnr,
callback = function() callback = function()
self:focus() if Config.behaviour.auto_focus_sidebar then
if self.input_container and self.input_container.winid and api.nvim_win_is_valid(self.input_container.winid) then self:focus()
api.nvim_set_current_win(self.input_container.winid) if
if Config.windows.ask.start_insert then vim.cmd("startinsert") end self.input_container
and self.input_container.winid
and api.nvim_win_is_valid(self.input_container.winid)
then
api.nvim_set_current_win(self.input_container.winid)
if Config.windows.ask.start_insert then vim.cmd("startinsert") end
end
end end
return true return true
end, end,
@ -1246,11 +1252,9 @@ function Sidebar:update_content(content, opts)
Utils.update_buffer_content(self.result_container.bufnr, lines) Utils.update_buffer_content(self.result_container.bufnr, lines)
Utils.lock_buf(self.result_container.bufnr) Utils.lock_buf(self.result_container.bufnr)
api.nvim_set_option_value("filetype", "Avante", { buf = self.result_container.bufnr }) api.nvim_set_option_value("filetype", "Avante", { buf = self.result_container.bufnr })
if opts.focus and not self:is_focused_on_result() then if opts.focus and Config.behaviour.auto_focus_sidebar and not self:is_focused_on_result() then
xpcall(function() --- set cursor to bottom of result view
--- set cursor to bottom of result view xpcall(function() api.nvim_set_current_win(self.result_container.winid) end, function(err) return err end)
api.nvim_set_current_win(self.result_container.winid)
end, function(err) return err end)
end end
if opts.scroll then Utils.buf_scroll_to_end(self.result_container.bufnr) end if opts.scroll then Utils.buf_scroll_to_end(self.result_container.bufnr) end
@ -1710,6 +1714,7 @@ function Sidebar:create_input_container(opts)
self.result_container self.result_container
and self.result_container.winid and self.result_container.winid
and api.nvim_win_is_valid(self.result_container.winid) and api.nvim_win_is_valid(self.result_container.winid)
and Config.behaviour.auto_focus_sidebar
then then
api.nvim_set_current_win(self.result_container.winid) api.nvim_set_current_win(self.result_container.winid)
end end