Skip to content

Commit

Permalink
feat: auto focus sidebar (#982)
Browse files Browse the repository at this point in the history
  • Loading branch information
albankurti authored Jan 8, 2025
1 parent 794c643 commit 225e6f9
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 9 deletions.
1 change: 1 addition & 0 deletions lua/avante/config.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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.
---5. minimize_diff : Whether to remove unchanged lines when applying a code block
behaviour = {
auto_focus_sidebar = true,
auto_suggestions = false, -- Experimental stage
auto_set_highlight_group = true,
auto_set_keymaps = true,
Expand Down
23 changes: 14 additions & 9 deletions lua/avante/sidebar.lua
Original file line number Diff line number Diff line change
Expand Up @@ -1041,10 +1041,16 @@ function Sidebar:on_mount(opts)
group = self.augroup,
buffer = self.result_container.bufnr,
callback = function()
self:focus()
if 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
if Config.behaviour.auto_focus_sidebar then
self:focus()
if
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
return true
end,
Expand Down Expand Up @@ -1246,11 +1252,9 @@ function Sidebar:update_content(content, opts)
Utils.update_buffer_content(self.result_container.bufnr, lines)
Utils.lock_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
xpcall(function()
--- set cursor to bottom of result view
api.nvim_set_current_win(self.result_container.winid)
end, function(err) return err end)
if opts.focus and Config.behaviour.auto_focus_sidebar and not self:is_focused_on_result() then
--- set cursor to bottom of result view
xpcall(function() api.nvim_set_current_win(self.result_container.winid) end, function(err) return err end)
end

if opts.scroll then Utils.buf_scroll_to_end(self.result_container.bufnr) end
Expand Down Expand Up @@ -1710,6 +1714,7 @@ function Sidebar:create_input_container(opts)
self.result_container
and self.result_container.winid
and api.nvim_win_is_valid(self.result_container.winid)
and Config.behaviour.auto_focus_sidebar
then
api.nvim_set_current_win(self.result_container.winid)
end
Expand Down

0 comments on commit 225e6f9

Please sign in to comment.