fix: cursor planning mode bad cases (#1290)

This commit is contained in:
yetone
2025-02-17 22:27:43 +08:00
committed by GitHub
parent b21d2632d3
commit b20b2caf22
3 changed files with 43 additions and 16 deletions

View File

@@ -2,6 +2,7 @@ local api = vim.api
local Config = require("avante.config")
local Utils = require("avante.utils")
local Highlights = require("avante.highlights")
local H = {}
local M = {}
@@ -76,10 +77,10 @@ local name_map = {
cursor = "cursor",
}
local CURRENT_HL = "AvanteConflictCurrent"
local INCOMING_HL = "AvanteConflictIncoming"
local CURRENT_LABEL_HL = "AvanteConflictCurrentLabel"
local INCOMING_LABEL_HL = "AvanteConflictIncomingLabel"
local CURRENT_HL = Highlights.CURRENT
local INCOMING_HL = Highlights.INCOMING
local CURRENT_LABEL_HL = Highlights.CURRENT_LABEL
local INCOMING_LABEL_HL = Highlights.INCOMING_LABEL
local PRIORITY = vim.highlight.priorities.user
local NAMESPACE = api.nvim_create_namespace("avante-conflict")
local KEYBINDING_NAMESPACE = api.nvim_create_namespace("avante-conflict-keybinding")

View File

@@ -16,15 +16,14 @@ local Highlights = {
POPUP_HINT = { name = "AvantePopupHint", link = "NormalFloat" },
INLINE_HINT = { name = "AvanteInlineHint", link = "Keyword" },
TO_BE_DELETED = { name = "AvanteToBeDeleted", bg = "#ffcccc", strikethrough = true },
TO_BE_DELETED_WITHOUT_STRIKETHROUGH = { name = "AvanteToBeDeletedWOStrikethrough", bg = "#562C30" },
}
Highlights.conflict = {
CURRENT = { name = "AvanteConflictCurrent", bg = 4218238, bold = true }, -- #405d7e
CURRENT = { name = "AvanteConflictCurrent", bg = "#562C30", bold = true }, -- #405d7e
CURRENT_LABEL = { name = "AvanteConflictCurrentLabel", link = "CURRENT", shade = 60 },
INCOMING = { name = "AvanteConflictIncoming", bg = 3229523, bold = true }, -- #314753
INCOMING_LABEL = { name = "AvanteConflictIncomingLabel", link = "INCOMING", shade = 60 },
ANCESTOR = { name = "AvanteConflictAncestor", bg = 6824314, bold = true }, -- #68217A
ANCESTOR_LABEL = { name = "AvanteConflictAncestorLabel", link = "ANCESTOR", shade = 60 },
}
--- helper

View File

@@ -531,7 +531,11 @@ local function extract_cursor_planning_code_snippets_map(response_content)
local lines = vim.split(response_content, "\n")
for idx, line in ipairs(lines) do
local idx = 1
local line_count = #lines
while idx <= line_count do
local line = lines[idx]
if line:match("^%s*```") then
if in_code_block then
in_code_block = false
@@ -550,12 +554,19 @@ local function extract_cursor_planning_code_snippets_map(response_content)
lang = lang_ or "unknown"
local filepath_ = line:match("^%s*```%w+:(.+)$")
filepath = filepath_ or ""
-- local line_ = line:gsub(".*(:.+)$", "")
-- lines[idx] = line_
if filepath == "" then
local next_line = lines[idx + 1]
local filepath2 = next_line:match("[Ff][Ii][Ll][Ee][Pp][Aa][Tt][Hh]:%s*(.+)")
if filepath2 then
filepath = filepath2
idx = idx + 1
end
end
end
elseif in_code_block then
table.insert(current_snippet, line)
end
idx = idx + 1
end
local snippets_map = {}
@@ -979,7 +990,7 @@ function Sidebar:apply(current_cursor)
---@diagnostic disable-next-line: assign-type-mismatch, missing-fields
local patch = vim.diff(original_lines_content, resp_lines_content, { ---@type integer[][]
algorithm = "histogram",
algorithm = "minimal",
result_type = "indices",
ctxlen = vim.o.scrolloff,
})
@@ -990,17 +1001,27 @@ function Sidebar:apply(current_cursor)
local start_a, count_a, start_b, count_b = unpack(hunk)
for i = start_a, start_a + count_a - 1 do
api.nvim_buf_add_highlight(bufnr, ns_id, Highlights.CURRENT, i - 1, 0, -1)
api.nvim_buf_set_extmark(bufnr, ns_id, i - 1, 0, {
hl_group = Highlights.TO_BE_DELETED_WITHOUT_STRIKETHROUGH,
hl_eol = true,
hl_mode = "combine",
end_row = i,
})
end
local new_lines = vim.list_slice(resp_lines_to_process, start_b, start_b + count_b - 1)
local max_col = vim.o.columns
local virt_lines = vim
.iter(new_lines)
:map(function(line) return { { line, Highlights.INCOMING } } end)
:map(function(line)
--- append spaces to the end of the line
local line_ = line .. string.rep(" ", max_col - #line)
return { { line_, Highlights.INCOMING } }
end)
:totable()
api.nvim_buf_set_extmark(bufnr, ns_id, math.max(0, start_a + count_a - 2), 0, {
virt_lines = virt_lines,
virt_text_pos = "overlay",
hl_eol = true,
hl_mode = "combine",
})
end
@@ -1009,10 +1030,16 @@ function Sidebar:apply(current_cursor)
local winid = Utils.get_winid(bufnr)
if winid == nil then return end
--- goto window winid
api.nvim_set_current_win(winid)
--- goto the last line
api.nvim_win_set_cursor(winid, { last_processed_line, 0 })
if last_processed_line > #original_code_lines then
api.nvim_win_set_cursor(winid, { #original_code_lines, 0 })
else
api.nvim_win_set_cursor(winid, { last_processed_line, 0 })
end
vim.cmd("normal! zz")
end,
on_stop = function(stop_opts)
@@ -1026,7 +1053,7 @@ function Sidebar:apply(current_cursor)
resp_content = resp_content:gsub("<updated%-code>\n*", ""):gsub("</updated%-code>\n*", "")
resp_content = resp_content:gsub(".*```%w+\n", ""):gsub("\n```\n.*", "")
resp_content = resp_content:gsub(".*```%w+\n", ""):gsub("\n```\n.*", ""):gsub("\n```", "")
local resp_lines = vim.split(resp_content, "\n")
local original_lines = vim.list_slice(original_code_lines, 1, #resp_lines)
local resp_lines_content = table.concat(resp_lines, "\n")