diff --git a/lua/avante/config.lua b/lua/avante/config.lua index 7719188..43b38b9 100644 --- a/lua/avante/config.lua +++ b/lua/avante/config.lua @@ -377,6 +377,7 @@ M._defaults = { enable_cursor_planning_mode = false, enable_claude_text_editor_tool_mode = false, use_cwd_as_project_root = false, + auto_focus_on_diff_view = false, }, history = { max_tokens = 4096, diff --git a/lua/avante/diff.lua b/lua/avante/diff.lua index 3aa7709..47284fa 100644 --- a/lua/avante/diff.lua +++ b/lua/avante/diff.lua @@ -167,10 +167,10 @@ end ---Highlight each part of a git conflict i.e. the incoming changes vs the current/HEAD changes ---TODO: should extmarks be ephemeral? or is it less expensive to save them and only re-apply ---them when a buffer changes since otherwise we have to reparse the whole buffer constantly +---@param bufnr integer ---@param positions table ---@param lines string[] -local function highlight_conflicts(positions, lines) - local bufnr = api.nvim_get_current_buf() +local function highlight_conflicts(bufnr, positions, lines) M.clear(bufnr) for _, position in ipairs(positions) do @@ -326,7 +326,7 @@ local function parse_buffer(bufnr, range_start, range_end) update_visited_buffers(bufnr, positions) if has_conflict then register_cursor_move_events(bufnr) - highlight_conflicts(positions, lines) + highlight_conflicts(bufnr, positions, lines) else M.clear(bufnr) end @@ -337,11 +337,11 @@ local function parse_buffer(bufnr, range_start, range_end) end ---Process a buffer if the changed tick has changed ----@param bufnr integer? +---@param bufnr integer ---@param range_start integer? ---@param range_end integer? function M.process(bufnr, range_start, range_end) - bufnr = bufnr or api.nvim_get_current_buf() + bufnr = bufnr if visited_buffers[bufnr] and visited_buffers[bufnr].tick == vim.b[bufnr].changedtick then return end parse_buffer(bufnr, range_start, range_end) end diff --git a/lua/avante/llm_tools/str_replace.lua b/lua/avante/llm_tools/str_replace.lua index 432503f..924eb5c 100644 --- a/lua/avante/llm_tools/str_replace.lua +++ b/lua/avante/llm_tools/str_replace.lua @@ -3,6 +3,7 @@ local Utils = require("avante.utils") local Base = require("avante.llm_tools.base") local Helpers = require("avante.llm_tools.helpers") local Diff = require("avante.diff") +local Config = require("avante.config") ---@class AvanteLLMTool local M = setmetatable({}, Base) @@ -125,17 +126,14 @@ function M.func(opts, on_log, on_complete, session_ctx) vim.list_extend(patched_new_lines, vim.list_slice(old_lines, current_start_a, #old_lines)) end vim.api.nvim_buf_set_lines(bufnr, start_line - 1, end_line, false, patched_new_lines) - local current_winid = vim.api.nvim_get_current_win() - vim.api.nvim_set_current_win(sidebar.code.winid) Diff.add_visited_buffer(bufnr) Diff.process(bufnr) if #patch > 0 then vim.api.nvim_win_set_cursor(sidebar.code.winid, { math.max(patch[1][1] + start_line - 1, 1), 0 }) end - vim.cmd("normal! zz") - vim.api.nvim_set_current_win(current_winid) + vim.api.nvim_win_call(sidebar.code.winid, function() vim.cmd("normal! zz") end) local augroup = vim.api.nvim_create_augroup("avante_str_replace_editor", { clear = true }) - vim.api.nvim_set_current_win(sidebar.code.winid) + if Config.behaviour.auto_focus_on_diff_view then vim.api.nvim_set_current_win(sidebar.code.winid) end local confirm vim.api.nvim_create_autocmd({ "TextChangedI", "TextChanged" }, { group = augroup, @@ -146,7 +144,6 @@ function M.func(opts, on_log, on_complete, session_ctx) if current_lines_content:find(patch_end_line_content) then return end pcall(vim.api.nvim_del_augroup_by_id, augroup) if confirm then confirm:close() end - if vim.api.nvim_win_is_valid(current_winid) then vim.api.nvim_set_current_win(current_winid) end if lines_content == current_lines_content then on_complete(false, "User canceled") return @@ -156,19 +153,18 @@ function M.func(opts, on_log, on_complete, session_ctx) }) confirm = Helpers.confirm("Are you sure you want to apply this modification?", function(ok, reason) pcall(vim.api.nvim_del_augroup_by_id, augroup) - vim.api.nvim_set_current_win(sidebar.code.winid) - vim.cmd("noautocmd stopinsert") - vim.cmd("noautocmd undo") + vim.api.nvim_win_call(sidebar.code.winid, function() + vim.cmd("noautocmd stopinsert") + vim.cmd("noautocmd undo") + end) if not ok then - vim.api.nvim_set_current_win(current_winid) on_complete(false, "User declined, reason: " .. (reason or "unknown")) return end vim.api.nvim_buf_set_lines(bufnr, start_line - 1, end_line, false, new_lines) - vim.cmd("noautocmd write") - vim.api.nvim_set_current_win(current_winid) + vim.api.nvim_buf_call(bufnr, function() vim.cmd("noautocmd write") end) on_complete(true, nil) - end, { focus = false }, session_ctx) + end, { focus = not Config.behaviour.auto_focus_on_diff_view }, session_ctx) end return M