perf(sidebar): do not replace all content in the code buffer (#433)

This commit is contained in:
yetone 2024-09-01 02:45:32 +08:00 committed by GitHub
parent 5f06638d37
commit 2e2c1ba486
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

View File

@ -420,34 +420,34 @@ local function ensure_snippets_no_overlap(original_content, snippets)
return result return result
end end
local function get_conflict_content(content, snippets) local function insert_conflict_contents(bufnr, snippets)
-- sort snippets by start_line -- sort snippets by start_line
table.sort(snippets, function(a, b) table.sort(snippets, function(a, b)
return a.range[1] < b.range[1] return a.range[1] < b.range[1]
end) end)
local content = table.concat(Utils.get_buf_lines(0, -1, bufnr), "\n")
local lines = vim.split(content, "\n") local lines = vim.split(content, "\n")
local result = {}
local current_line = 1 local offset = 0
for _, snippet in ipairs(snippets) do for _, snippet in ipairs(snippets) do
local start_line, end_line = unpack(snippet.range) local start_line, end_line = unpack(snippet.range)
while current_line < start_line do
table.insert(result, lines[current_line])
current_line = current_line + 1
end
local need_prepend_indentation = false local need_prepend_indentation = false
local original_start_line_indentation = Utils.get_indentation(lines[start_line] or "") local original_start_line_indentation = Utils.get_indentation(lines[start_line] or "")
local result = {}
table.insert(result, "<<<<<<< HEAD") table.insert(result, "<<<<<<< HEAD")
for i = start_line, end_line do for i = start_line, end_line do
table.insert(result, lines[i]) table.insert(result, lines[i])
end end
table.insert(result, "=======") table.insert(result, "=======")
for idx, line in ipairs(vim.split(snippet.content, "\n")) do local snippet_lines = vim.split(snippet.content, "\n")
for idx, line in ipairs(snippet_lines) do
line = line:gsub("^L%d+: ", "") line = line:gsub("^L%d+: ", "")
if idx == 1 then if idx == 1 then
local indentation = Utils.get_indentation(line) local indentation = Utils.get_indentation(line)
@ -461,15 +461,9 @@ local function get_conflict_content(content, snippets)
table.insert(result, ">>>>>>> Snippet") table.insert(result, ">>>>>>> Snippet")
current_line = end_line + 1 api.nvim_buf_set_lines(bufnr, offset + start_line - 1, offset + end_line, false, result)
offset = offset + #snippet_lines + 3
end end
while current_line <= #lines do
table.insert(result, lines[current_line])
current_line = current_line + 1
end
return result
end end
---@param codeblocks table<integer, any> ---@param codeblocks table<integer, any>
@ -539,10 +533,8 @@ function Sidebar:apply(current_cursor)
end end
end end
local conflict_content = get_conflict_content(content, snippets)
vim.defer_fn(function() vim.defer_fn(function()
api.nvim_buf_set_lines(self.code.bufnr, 0, -1, false, conflict_content) insert_conflict_contents(self.code.bufnr, snippets)
api.nvim_set_current_win(self.code.winid) api.nvim_set_current_win(self.code.winid)
api.nvim_feedkeys(api.nvim_replace_termcodes("<Esc>", true, false, true), "n", true) api.nvim_feedkeys(api.nvim_replace_termcodes("<Esc>", true, false, true), "n", true)