Refine buffer updates and tool call metadata (#2784)

This commit is contained in:
yetone
2025-10-20 13:58:34 +08:00
committed by GitHub
parent 5da78a7521
commit fb28520067
2 changed files with 34 additions and 51 deletions

View File

@@ -983,6 +983,8 @@ function M._stream_acp(opts)
id = update.toolCallId, id = update.toolCallId,
name = update.kind, name = update.kind,
input = update.rawInput or {}, input = update.rawInput or {},
}, {
uuid = update.toolCallId,
}) })
last_tool_call_message = message last_tool_call_message = message
message.acp_tool_call = update message.acp_tool_call = update

View File

@@ -1281,61 +1281,42 @@ end
---@param skip_line_count? integer ---@param skip_line_count? integer
function M.update_buffer_lines(ns_id, bufnr, old_lines, new_lines, skip_line_count) function M.update_buffer_lines(ns_id, bufnr, old_lines, new_lines, skip_line_count)
skip_line_count = skip_line_count or 0 skip_line_count = skip_line_count or 0
local diff_start_idx = 0 old_lines = old_lines or {}
for i, line in ipairs(new_lines) do new_lines = new_lines or {}
local old_line = old_lines[i]
if not old_line or old_line ~= line then -- Unbind events from existing lines before rewriting the buffer section.
diff_start_idx = i for i, old_line in ipairs(old_lines) do
break
end
end
if diff_start_idx > 0 then
-- Unbind events on old lines that will be replaced/moved
for i = diff_start_idx, #old_lines do
local old_line = old_lines[i]
if old_line and type(old_line.unbind_events) == "function" then if old_line and type(old_line.unbind_events) == "function" then
local line_1b = skip_line_count + i local line_1b = skip_line_count + i
pcall(old_line.unbind_events, old_line, bufnr, line_1b) pcall(old_line.unbind_events, old_line, bufnr, line_1b)
end end
end end
local changed_lines = vim.list_slice(new_lines, diff_start_idx) -- Collect the text representation of each line and track their positions.
local text_lines = vim.tbl_map(function(line) return tostring(line) end, changed_lines)
local cleaned_text_lines = {} local cleaned_text_lines = {}
for _, line in ipairs(text_lines) do local line_positions = {}
local lines_ = vim.split(line, "\n") local current_line_0b = skip_line_count
cleaned_text_lines = vim.list_extend(cleaned_text_lines, lines_)
for idx, line in ipairs(new_lines) do
local pieces = vim.split(tostring(line), "\n")
line_positions[idx] = current_line_0b
vim.list_extend(cleaned_text_lines, pieces)
current_line_0b = current_line_0b + #pieces
end end
vim.api.nvim_buf_set_lines(
bufnr, -- Replace the entire dynamic portion of the buffer.
skip_line_count + diff_start_idx - 1, vim.api.nvim_buf_set_lines(bufnr, skip_line_count, -1, false, cleaned_text_lines)
skip_line_count + diff_start_idx + #changed_lines,
false, -- Re-apply highlights and bind events for the new lines.
cleaned_text_lines for i, line in ipairs(new_lines) do
) local line_pos_0b = line_positions[i] or (skip_line_count + i - 1)
for i, line in ipairs(changed_lines) do if type(line.set_highlights) == "function" then line:set_highlights(ns_id, bufnr, line_pos_0b) end
-- Apply highlights
if type(line.set_highlights) == "function" then
line:set_highlights(ns_id, bufnr, skip_line_count + diff_start_idx + i - 2)
end
-- Bind events if provided by the line
if type(line.bind_events) == "function" then if type(line.bind_events) == "function" then
local line_1b = skip_line_count + diff_start_idx + i - 1 local line_1b = line_pos_0b + 1
pcall(line.bind_events, line, ns_id, bufnr, line_1b) pcall(line.bind_events, line, ns_id, bufnr, line_1b)
end end
end end
end
if #old_lines > #new_lines then
-- Unbind events on removed trailing lines
for i = #new_lines + 1, #old_lines do
local old_line = old_lines[i]
if old_line and type(old_line.unbind_events) == "function" then
local line_1b = skip_line_count + i
pcall(old_line.unbind_events, old_line, bufnr, line_1b)
end
end
vim.api.nvim_buf_set_lines(bufnr, skip_line_count + #new_lines, skip_line_count + #old_lines, false, {})
end
vim.cmd("redraw") vim.cmd("redraw")
-- local diffs = get_lines_diff(old_lines, new_lines) -- local diffs = get_lines_diff(old_lines, new_lines)
-- if #diffs == 0 then return end -- if #diffs == 0 then return end