Refine buffer updates and tool call metadata (#2784)
This commit is contained in:
@@ -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
|
||||||
|
|||||||
@@ -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
|
|
||||||
diff_start_idx = i
|
|
||||||
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
|
|
||||||
local line_1b = skip_line_count + i
|
|
||||||
pcall(old_line.unbind_events, old_line, bufnr, line_1b)
|
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|
||||||
local changed_lines = vim.list_slice(new_lines, diff_start_idx)
|
-- Unbind events from existing lines before rewriting the buffer section.
|
||||||
local text_lines = vim.tbl_map(function(line) return tostring(line) end, changed_lines)
|
for i, old_line in ipairs(old_lines) do
|
||||||
local cleaned_text_lines = {}
|
if old_line and type(old_line.unbind_events) == "function" then
|
||||||
for _, line in ipairs(text_lines) do
|
local line_1b = skip_line_count + i
|
||||||
local lines_ = vim.split(line, "\n")
|
pcall(old_line.unbind_events, old_line, bufnr, line_1b)
|
||||||
cleaned_text_lines = vim.list_extend(cleaned_text_lines, lines_)
|
|
||||||
end
|
|
||||||
vim.api.nvim_buf_set_lines(
|
|
||||||
bufnr,
|
|
||||||
skip_line_count + diff_start_idx - 1,
|
|
||||||
skip_line_count + diff_start_idx + #changed_lines,
|
|
||||||
false,
|
|
||||||
cleaned_text_lines
|
|
||||||
)
|
|
||||||
for i, line in ipairs(changed_lines) do
|
|
||||||
-- 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
|
|
||||||
local line_1b = skip_line_count + diff_start_idx + i - 1
|
|
||||||
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
|
-- Collect the text representation of each line and track their positions.
|
||||||
for i = #new_lines + 1, #old_lines do
|
local cleaned_text_lines = {}
|
||||||
local old_line = old_lines[i]
|
local line_positions = {}
|
||||||
if old_line and type(old_line.unbind_events) == "function" then
|
local current_line_0b = skip_line_count
|
||||||
local line_1b = skip_line_count + i
|
|
||||||
pcall(old_line.unbind_events, old_line, bufnr, line_1b)
|
for idx, line in ipairs(new_lines) do
|
||||||
end
|
local pieces = vim.split(tostring(line), "\n")
|
||||||
end
|
line_positions[idx] = current_line_0b
|
||||||
vim.api.nvim_buf_set_lines(bufnr, skip_line_count + #new_lines, skip_line_count + #old_lines, false, {})
|
vim.list_extend(cleaned_text_lines, pieces)
|
||||||
|
current_line_0b = current_line_0b + #pieces
|
||||||
end
|
end
|
||||||
|
|
||||||
|
-- Replace the entire dynamic portion of the buffer.
|
||||||
|
vim.api.nvim_buf_set_lines(bufnr, skip_line_count, -1, false, cleaned_text_lines)
|
||||||
|
|
||||||
|
-- Re-apply highlights and bind events for the new lines.
|
||||||
|
for i, line in ipairs(new_lines) do
|
||||||
|
local line_pos_0b = line_positions[i] or (skip_line_count + i - 1)
|
||||||
|
if type(line.set_highlights) == "function" then line:set_highlights(ns_id, bufnr, line_pos_0b) end
|
||||||
|
if type(line.bind_events) == "function" then
|
||||||
|
local line_1b = line_pos_0b + 1
|
||||||
|
pcall(line.bind_events, line, ns_id, bufnr, line_1b)
|
||||||
|
end
|
||||||
|
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
|
||||||
|
|||||||
Reference in New Issue
Block a user