fix: window switch (#1856)

This commit is contained in:
yetone
2025-04-11 11:42:55 +08:00
committed by GitHub
parent 9952ad381c
commit 51bad68087
3 changed files with 15 additions and 18 deletions

View File

@@ -377,6 +377,7 @@ M._defaults = {
enable_cursor_planning_mode = false, enable_cursor_planning_mode = false,
enable_claude_text_editor_tool_mode = false, enable_claude_text_editor_tool_mode = false,
use_cwd_as_project_root = false, use_cwd_as_project_root = false,
auto_focus_on_diff_view = false,
}, },
history = { history = {
max_tokens = 4096, max_tokens = 4096,

View File

@@ -167,10 +167,10 @@ end
---Highlight each part of a git conflict i.e. the incoming changes vs the current/HEAD changes ---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 ---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 ---them when a buffer changes since otherwise we have to reparse the whole buffer constantly
---@param bufnr integer
---@param positions table ---@param positions table
---@param lines string[] ---@param lines string[]
local function highlight_conflicts(positions, lines) local function highlight_conflicts(bufnr, positions, lines)
local bufnr = api.nvim_get_current_buf()
M.clear(bufnr) M.clear(bufnr)
for _, position in ipairs(positions) do for _, position in ipairs(positions) do
@@ -326,7 +326,7 @@ local function parse_buffer(bufnr, range_start, range_end)
update_visited_buffers(bufnr, positions) update_visited_buffers(bufnr, positions)
if has_conflict then if has_conflict then
register_cursor_move_events(bufnr) register_cursor_move_events(bufnr)
highlight_conflicts(positions, lines) highlight_conflicts(bufnr, positions, lines)
else else
M.clear(bufnr) M.clear(bufnr)
end end
@@ -337,11 +337,11 @@ local function parse_buffer(bufnr, range_start, range_end)
end end
---Process a buffer if the changed tick has changed ---Process a buffer if the changed tick has changed
---@param bufnr integer? ---@param bufnr integer
---@param range_start integer? ---@param range_start integer?
---@param range_end integer? ---@param range_end integer?
function M.process(bufnr, range_start, range_end) 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 if visited_buffers[bufnr] and visited_buffers[bufnr].tick == vim.b[bufnr].changedtick then return end
parse_buffer(bufnr, range_start, range_end) parse_buffer(bufnr, range_start, range_end)
end end

View File

@@ -3,6 +3,7 @@ local Utils = require("avante.utils")
local Base = require("avante.llm_tools.base") local Base = require("avante.llm_tools.base")
local Helpers = require("avante.llm_tools.helpers") local Helpers = require("avante.llm_tools.helpers")
local Diff = require("avante.diff") local Diff = require("avante.diff")
local Config = require("avante.config")
---@class AvanteLLMTool ---@class AvanteLLMTool
local M = setmetatable({}, Base) 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)) vim.list_extend(patched_new_lines, vim.list_slice(old_lines, current_start_a, #old_lines))
end end
vim.api.nvim_buf_set_lines(bufnr, start_line - 1, end_line, false, patched_new_lines) 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.add_visited_buffer(bufnr)
Diff.process(bufnr) Diff.process(bufnr)
if #patch > 0 then if #patch > 0 then
vim.api.nvim_win_set_cursor(sidebar.code.winid, { math.max(patch[1][1] + start_line - 1, 1), 0 }) vim.api.nvim_win_set_cursor(sidebar.code.winid, { math.max(patch[1][1] + start_line - 1, 1), 0 })
end end
vim.cmd("normal! zz") vim.api.nvim_win_call(sidebar.code.winid, function() vim.cmd("normal! zz") end)
vim.api.nvim_set_current_win(current_winid)
local augroup = vim.api.nvim_create_augroup("avante_str_replace_editor", { clear = true }) 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 local confirm
vim.api.nvim_create_autocmd({ "TextChangedI", "TextChanged" }, { vim.api.nvim_create_autocmd({ "TextChangedI", "TextChanged" }, {
group = augroup, 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 if current_lines_content:find(patch_end_line_content) then return end
pcall(vim.api.nvim_del_augroup_by_id, augroup) pcall(vim.api.nvim_del_augroup_by_id, augroup)
if confirm then confirm:close() end 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 if lines_content == current_lines_content then
on_complete(false, "User canceled") on_complete(false, "User canceled")
return 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) confirm = Helpers.confirm("Are you sure you want to apply this modification?", function(ok, reason)
pcall(vim.api.nvim_del_augroup_by_id, augroup) pcall(vim.api.nvim_del_augroup_by_id, augroup)
vim.api.nvim_set_current_win(sidebar.code.winid) vim.api.nvim_win_call(sidebar.code.winid, function()
vim.cmd("noautocmd stopinsert") vim.cmd("noautocmd stopinsert")
vim.cmd("noautocmd undo") vim.cmd("noautocmd undo")
end)
if not ok then if not ok then
vim.api.nvim_set_current_win(current_winid)
on_complete(false, "User declined, reason: " .. (reason or "unknown")) on_complete(false, "User declined, reason: " .. (reason or "unknown"))
return return
end end
vim.api.nvim_buf_set_lines(bufnr, start_line - 1, end_line, false, new_lines) vim.api.nvim_buf_set_lines(bufnr, start_line - 1, end_line, false, new_lines)
vim.cmd("noautocmd write") vim.api.nvim_buf_call(bufnr, function() vim.cmd("noautocmd write") end)
vim.api.nvim_set_current_win(current_winid)
on_complete(true, nil) on_complete(true, nil)
end, { focus = false }, session_ctx) end, { focus = not Config.behaviour.auto_focus_on_diff_view }, session_ctx)
end end
return M return M