claude-baseline-1752140136 (startup)
This commit is contained in:
parent
555634f0ec
commit
49c05023e0
|
@ -122,50 +122,33 @@ function M.apply_diff_visualization(bufnr)
|
||||||
-- Track which lines in the current buffer correspond to additions/deletions
|
-- Track which lines in the current buffer correspond to additions/deletions
|
||||||
local additions = {}
|
local additions = {}
|
||||||
local deletions = {}
|
local deletions = {}
|
||||||
local replacements = {} -- Track lines that are replacements (have both deletion and addition)
|
|
||||||
|
|
||||||
-- Start from the beginning of the hunk and track line numbers
|
-- Start from the beginning of the hunk and track line numbers
|
||||||
local new_line_num = hunk.new_start -- 1-indexed line number in new file
|
local new_line_num = hunk.new_start -- 1-indexed line number in new file
|
||||||
local old_line_num = hunk.old_start -- 1-indexed line number in old file
|
local old_line_num = hunk.old_start -- 1-indexed line number in old file
|
||||||
|
|
||||||
-- First pass: identify replacements by looking for adjacent -/+ pairs
|
for _, diff_line in ipairs(hunk.lines) do
|
||||||
local j = 1
|
if diff_line:match('^%+') then
|
||||||
while j <= #hunk.lines do
|
-- This is an added line - it exists in the current buffer at new_line_num
|
||||||
local line = hunk.lines[j]
|
|
||||||
if line:match('^%-') and j < #hunk.lines and hunk.lines[j + 1]:match('^%+') then
|
|
||||||
-- This is a replacement: deletion followed by addition
|
|
||||||
table.insert(replacements, {
|
|
||||||
old_text = line:sub(2),
|
|
||||||
new_text = hunk.lines[j + 1]:sub(2),
|
|
||||||
line = new_line_num - 1 -- 0-indexed line in buffer
|
|
||||||
})
|
|
||||||
j = j + 2 -- Skip both lines
|
|
||||||
new_line_num = new_line_num + 1
|
|
||||||
old_line_num = old_line_num + 1
|
|
||||||
elseif line:match('^%+') then
|
|
||||||
-- Pure addition
|
|
||||||
table.insert(additions, new_line_num - 1) -- Convert to 0-indexed for extmarks
|
table.insert(additions, new_line_num - 1) -- Convert to 0-indexed for extmarks
|
||||||
new_line_num = new_line_num + 1
|
new_line_num = new_line_num + 1
|
||||||
j = j + 1
|
-- Don't advance old_line_num for additions
|
||||||
elseif line:match('^%-') then
|
elseif diff_line:match('^%-') then
|
||||||
-- Pure deletion
|
-- This is a deleted line - show as virtual text above current position
|
||||||
table.insert(deletions, {
|
table.insert(deletions, {
|
||||||
line = new_line_num - 1, -- 0-indexed, show above current position
|
line = new_line_num - 1, -- 0-indexed, show above current position
|
||||||
text = line:sub(2),
|
text = diff_line:sub(2),
|
||||||
})
|
})
|
||||||
old_line_num = old_line_num + 1
|
old_line_num = old_line_num + 1
|
||||||
j = j + 1
|
-- Don't advance new_line_num for deletions
|
||||||
elseif line:match('^%s') then
|
elseif diff_line:match('^%s') then
|
||||||
-- Context line - advance both
|
-- Context line - advance both
|
||||||
new_line_num = new_line_num + 1
|
new_line_num = new_line_num + 1
|
||||||
old_line_num = old_line_num + 1
|
old_line_num = old_line_num + 1
|
||||||
j = j + 1
|
|
||||||
else
|
|
||||||
j = j + 1
|
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
-- Apply highlighting for pure additions
|
-- Apply highlighting for additions
|
||||||
for _, line_idx in ipairs(additions) do
|
for _, line_idx in ipairs(additions) do
|
||||||
if line_idx >= 0 and line_idx < #buf_lines then
|
if line_idx >= 0 and line_idx < #buf_lines then
|
||||||
vim.api.nvim_buf_set_extmark(bufnr, ns_id, line_idx, 0, {
|
vim.api.nvim_buf_set_extmark(bufnr, ns_id, line_idx, 0, {
|
||||||
|
@ -177,24 +160,6 @@ function M.apply_diff_visualization(bufnr)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
-- Apply highlighting for replacements (show old text inline with new text)
|
|
||||||
for j, repl in ipairs(replacements) do
|
|
||||||
if repl.line >= 0 and repl.line < #buf_lines then
|
|
||||||
-- Highlight the line as an addition (since it shows the new text)
|
|
||||||
vim.api.nvim_buf_set_extmark(bufnr, ns_id, repl.line, 0, {
|
|
||||||
line_hl_group = 'DiffAdd',
|
|
||||||
id = 5000 + i * 100 + j
|
|
||||||
})
|
|
||||||
|
|
||||||
-- Show the old text as virtual text at end of line
|
|
||||||
vim.api.nvim_buf_set_extmark(bufnr, ns_id, repl.line, 0, {
|
|
||||||
virt_text = {{' ← was: ' .. repl.old_text, 'Comment'}},
|
|
||||||
virt_text_pos = 'eol',
|
|
||||||
id = 6000 + i * 100 + j
|
|
||||||
})
|
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|
||||||
-- Show pure deletions as virtual text above their position
|
-- Show pure deletions as virtual text above their position
|
||||||
for j, del in ipairs(deletions) do
|
for j, del in ipairs(deletions) do
|
||||||
if del.line >= 0 and del.line <= #buf_lines then
|
if del.line >= 0 and del.line <= #buf_lines then
|
||||||
|
@ -209,12 +174,10 @@ function M.apply_diff_visualization(bufnr)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
-- Add sign in gutter for hunk (use first addition, replacement, or deletion line)
|
-- Add sign in gutter for hunk (use first addition or deletion line)
|
||||||
local sign_line = nil
|
local sign_line = nil
|
||||||
if #additions > 0 then
|
if #additions > 0 then
|
||||||
sign_line = additions[1]
|
sign_line = additions[1]
|
||||||
elseif #replacements > 0 then
|
|
||||||
sign_line = replacements[1].line
|
|
||||||
elseif #deletions > 0 then
|
elseif #deletions > 0 then
|
||||||
sign_line = deletions[1].line
|
sign_line = deletions[1].line
|
||||||
else
|
else
|
||||||
|
@ -224,13 +187,10 @@ function M.apply_diff_visualization(bufnr)
|
||||||
local sign_text = '>'
|
local sign_text = '>'
|
||||||
local sign_hl = 'DiffAdd'
|
local sign_hl = 'DiffAdd'
|
||||||
|
|
||||||
-- Choose appropriate sign based on hunk content
|
-- If hunk has deletions, use different sign
|
||||||
if #replacements > 0 then
|
if #deletions > 0 then
|
||||||
sign_text = '~'
|
sign_text = '~'
|
||||||
sign_hl = 'DiffChange'
|
sign_hl = 'DiffChange'
|
||||||
elseif #deletions > 0 then
|
|
||||||
sign_text = '-'
|
|
||||||
sign_hl = 'DiffDelete'
|
|
||||||
end
|
end
|
||||||
|
|
||||||
if sign_line and sign_line >= 0 and sign_line < #buf_lines then
|
if sign_line and sign_line >= 0 and sign_line < #buf_lines then
|
||||||
|
|
Loading…
Reference in New Issue