fix: release the cursor when generating (#1109)

This commit is contained in:
yetone 2025-01-21 00:04:48 +08:00 committed by GitHub
parent b6d4180bb6
commit 76f60794c3
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 16 additions and 9 deletions

View File

@ -114,13 +114,16 @@ M._defaults = {
timeout = 60000, -- Timeout in milliseconds timeout = 60000, -- Timeout in milliseconds
}, },
---Specify the behaviour of avante.nvim ---Specify the behaviour of avante.nvim
---1. auto_apply_diff_after_generation: Whether to automatically apply diff after LLM response. ---1. auto_focus_sidebar : Whether to automatically focus the sidebar when opening avante.nvim. Default to true.
---2. auto_suggestions = false, -- Whether to enable auto suggestions. Default to false.
---3. auto_apply_diff_after_generation: Whether to automatically apply diff after LLM response.
--- This would simulate similar behaviour to cursor. Default to false. --- This would simulate similar behaviour to cursor. Default to false.
---2. auto_set_keymaps : Whether to automatically set the keymap for the current line. Default to true. ---4. auto_set_keymaps : Whether to automatically set the keymap for the current line. Default to true.
--- Note that avante will safely set these keymap. See https://github.com/yetone/avante.nvim/wiki#keymaps-and-api-i-guess for more details. --- Note that avante will safely set these keymap. See https://github.com/yetone/avante.nvim/wiki#keymaps-and-api-i-guess for more details.
---3. auto_set_highlight_group : Whether to automatically set the highlight group for the current line. Default to true. ---5. auto_set_highlight_group : Whether to automatically set the highlight group for the current line. Default to true.
---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. ---6. jump_to_result_buffer_on_finish = false, -- Whether to automatically jump to the result buffer after generation
---5. minimize_diff : Whether to remove unchanged lines when applying a code block ---7. support_paste_from_clipboard : Whether to support pasting image from clipboard. This will be determined automatically based whether img-clip is available or not.
---8. minimize_diff : Whether to remove unchanged lines when applying a code block
behaviour = { behaviour = {
auto_focus_sidebar = true, auto_focus_sidebar = true,
auto_suggestions = false, -- Experimental stage auto_suggestions = false, -- Experimental stage
@ -128,6 +131,7 @@ M._defaults = {
auto_set_highlight_group = true, auto_set_highlight_group = true,
auto_set_keymaps = true, auto_set_keymaps = true,
auto_apply_diff_after_generation = false, auto_apply_diff_after_generation = false,
jump_result_buffer_on_finish = false,
support_paste_from_clipboard = false, support_paste_from_clipboard = false,
minimize_diff = true, minimize_diff = true,
}, },

View File

@ -86,6 +86,7 @@ function Sidebar:open(opts)
self:initialize() self:initialize()
if opts.selection then self.code.selection = opts.selection end if opts.selection then self.code.selection = opts.selection end
self:render(opts) self:render(opts)
self:focus()
else else
if in_visual_mode or opts.selection then if in_visual_mode or opts.selection then
self:close() self:close()
@ -1049,7 +1050,9 @@ function Sidebar:on_mount(opts)
and api.nvim_win_is_valid(self.input_container.winid) and api.nvim_win_is_valid(self.input_container.winid)
then then
api.nvim_set_current_win(self.input_container.winid) api.nvim_set_current_win(self.input_container.winid)
if Config.windows.ask.start_insert then vim.cmd("startinsert") end vim.schedule(function()
if Config.windows.ask.start_insert then vim.cmd("startinsert") end
end)
end end
end end
return true return true
@ -1202,7 +1205,7 @@ end
---@param opts? {focus?: boolean, scroll?: boolean, backspace?: integer, ignore_history?: boolean, callback?: fun(): nil} whether to focus the result view ---@param opts? {focus?: boolean, scroll?: boolean, backspace?: integer, ignore_history?: boolean, callback?: fun(): nil} whether to focus the result view
function Sidebar:update_content(content, opts) function Sidebar:update_content(content, opts)
if not self.result_container or not self.result_container.bufnr then return end if not self.result_container or not self.result_container.bufnr then return end
opts = vim.tbl_deep_extend("force", { focus = true, scroll = true, stream = false, callback = nil }, opts or {}) opts = vim.tbl_deep_extend("force", { focus = false, scroll = true, stream = false, callback = nil }, opts or {})
if not opts.ignore_history then if not opts.ignore_history then
local chat_history = Path.history.load(self.code.bufnr) local chat_history = Path.history.load(self.code.bufnr)
content = self:render_history_content(chat_history) .. "---\n\n" .. content content = self:render_history_content(chat_history) .. "---\n\n" .. content
@ -1257,7 +1260,7 @@ 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 Config.behaviour.auto_focus_sidebar and not self:is_focused_on_result() then if opts.focus and not self:is_focused_on_result() then
--- 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) xpcall(function() api.nvim_set_current_win(self.result_container.winid) end, function(err) return err end)
end end
@ -1719,7 +1722,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 and Config.behaviour.jump_result_buffer_on_finish
then then
api.nvim_set_current_win(self.result_container.winid) api.nvim_set_current_win(self.result_container.winid)
end end