optimize: streaming diff performance (#2150)

This commit is contained in:
yetone
2025-06-04 23:28:28 +08:00
committed by GitHub
parent 16e041ff9a
commit 3a77a591b3
2 changed files with 27 additions and 0 deletions

View File

@@ -113,11 +113,22 @@ function M.func(opts, on_log, on_complete, session_ctx)
local is_streaming = opts.streaming or false
if is_streaming then
local streaming_diff_lines_count = Utils.count_lines(opts.diff)
session_ctx.streaming_diff_lines_count_history = session_ctx.streaming_diff_lines_count_history or {}
local prev_streaming_diff_lines_count = session_ctx.streaming_diff_lines_count_history[opts.tool_use_id]
if streaming_diff_lines_count == prev_streaming_diff_lines_count then
return false, "Diff lines count hasn't changed"
end
session_ctx.streaming_diff_lines_count_history[opts.tool_use_id] = streaming_diff_lines_count
end
local diff = fix_diff(opts.diff)
if on_log and diff ~= opts.diff then on_log("diff fixed") end
local diff_lines = vim.split(diff, "\n")
local is_searching = false
local is_replacing = false
local current_old_lines = {}

View File

@@ -1646,4 +1646,20 @@ function M.message_to_text(message, messages)
return ""
end
function M.count_lines(str)
if not str or str == "" then return 0 end
local count = 1
local len = #str
local newline_byte = string.byte("\n")
for i = 1, len do
if str:byte(i) == newline_byte then count = count + 1 end
end
if str:byte(len) == newline_byte then count = count - 1 end
return count
end
return M