perf(sidebar): update the buffer more efficiently (#724)
This commit is contained in:
@@ -682,4 +682,46 @@ function M.create_new_buffer_with_file(filepath)
|
||||
return buf
|
||||
end
|
||||
|
||||
---@param bufnr integer
|
||||
---@param new_lines string[]
|
||||
---@return { start_line: integer, end_line: integer, content: string[] }[]
|
||||
local function get_buffer_content_diffs(bufnr, new_lines)
|
||||
local old_lines = api.nvim_buf_get_lines(bufnr, 0, -1, false)
|
||||
local diffs = {}
|
||||
local prev_diff_idx = nil
|
||||
for i, line in ipairs(new_lines) do
|
||||
if line ~= old_lines[i] then
|
||||
if prev_diff_idx == nil then prev_diff_idx = i end
|
||||
else
|
||||
if prev_diff_idx ~= nil then
|
||||
local content = vim.list_slice(new_lines, prev_diff_idx, i - 1)
|
||||
table.insert(diffs, { start_line = prev_diff_idx, end_line = i, content = content })
|
||||
prev_diff_idx = nil
|
||||
end
|
||||
end
|
||||
end
|
||||
if prev_diff_idx ~= nil then
|
||||
table.insert(
|
||||
diffs,
|
||||
{ start_line = prev_diff_idx, end_line = #new_lines + 1, content = vim.list_slice(new_lines, prev_diff_idx) }
|
||||
)
|
||||
end
|
||||
if #new_lines < #old_lines then
|
||||
table.insert(diffs, { start_line = #new_lines + 1, end_line = #old_lines + 1, content = {} })
|
||||
end
|
||||
table.sort(diffs, function(a, b) return a.start_line > b.start_line end)
|
||||
return diffs
|
||||
end
|
||||
|
||||
--- Update the buffer content more efficiently by only updating the changed lines
|
||||
---@param bufnr integer
|
||||
---@param new_lines string[]
|
||||
function M.update_buffer_content(bufnr, new_lines)
|
||||
local diffs = get_buffer_content_diffs(bufnr, new_lines)
|
||||
if #diffs == 0 then return end
|
||||
for _, diff in ipairs(diffs) do
|
||||
api.nvim_buf_set_lines(bufnr, diff.start_line - 1, diff.end_line - 1, false, diff.content)
|
||||
end
|
||||
end
|
||||
|
||||
return M
|
||||
|
||||
Reference in New Issue
Block a user