fix: cursor planning mode bad cases (#1290)
This commit is contained in:
@@ -2,6 +2,7 @@ local api = vim.api
|
|||||||
|
|
||||||
local Config = require("avante.config")
|
local Config = require("avante.config")
|
||||||
local Utils = require("avante.utils")
|
local Utils = require("avante.utils")
|
||||||
|
local Highlights = require("avante.highlights")
|
||||||
|
|
||||||
local H = {}
|
local H = {}
|
||||||
local M = {}
|
local M = {}
|
||||||
@@ -76,10 +77,10 @@ local name_map = {
|
|||||||
cursor = "cursor",
|
cursor = "cursor",
|
||||||
}
|
}
|
||||||
|
|
||||||
local CURRENT_HL = "AvanteConflictCurrent"
|
local CURRENT_HL = Highlights.CURRENT
|
||||||
local INCOMING_HL = "AvanteConflictIncoming"
|
local INCOMING_HL = Highlights.INCOMING
|
||||||
local CURRENT_LABEL_HL = "AvanteConflictCurrentLabel"
|
local CURRENT_LABEL_HL = Highlights.CURRENT_LABEL
|
||||||
local INCOMING_LABEL_HL = "AvanteConflictIncomingLabel"
|
local INCOMING_LABEL_HL = Highlights.INCOMING_LABEL
|
||||||
local PRIORITY = vim.highlight.priorities.user
|
local PRIORITY = vim.highlight.priorities.user
|
||||||
local NAMESPACE = api.nvim_create_namespace("avante-conflict")
|
local NAMESPACE = api.nvim_create_namespace("avante-conflict")
|
||||||
local KEYBINDING_NAMESPACE = api.nvim_create_namespace("avante-conflict-keybinding")
|
local KEYBINDING_NAMESPACE = api.nvim_create_namespace("avante-conflict-keybinding")
|
||||||
|
|||||||
@@ -16,15 +16,14 @@ local Highlights = {
|
|||||||
POPUP_HINT = { name = "AvantePopupHint", link = "NormalFloat" },
|
POPUP_HINT = { name = "AvantePopupHint", link = "NormalFloat" },
|
||||||
INLINE_HINT = { name = "AvanteInlineHint", link = "Keyword" },
|
INLINE_HINT = { name = "AvanteInlineHint", link = "Keyword" },
|
||||||
TO_BE_DELETED = { name = "AvanteToBeDeleted", bg = "#ffcccc", strikethrough = true },
|
TO_BE_DELETED = { name = "AvanteToBeDeleted", bg = "#ffcccc", strikethrough = true },
|
||||||
|
TO_BE_DELETED_WITHOUT_STRIKETHROUGH = { name = "AvanteToBeDeletedWOStrikethrough", bg = "#562C30" },
|
||||||
}
|
}
|
||||||
|
|
||||||
Highlights.conflict = {
|
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 },
|
CURRENT_LABEL = { name = "AvanteConflictCurrentLabel", link = "CURRENT", shade = 60 },
|
||||||
INCOMING = { name = "AvanteConflictIncoming", bg = 3229523, bold = true }, -- #314753
|
INCOMING = { name = "AvanteConflictIncoming", bg = 3229523, bold = true }, -- #314753
|
||||||
INCOMING_LABEL = { name = "AvanteConflictIncomingLabel", link = "INCOMING", shade = 60 },
|
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
|
--- helper
|
||||||
|
|||||||
@@ -531,7 +531,11 @@ local function extract_cursor_planning_code_snippets_map(response_content)
|
|||||||
|
|
||||||
local lines = vim.split(response_content, "\n")
|
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 line:match("^%s*```") then
|
||||||
if in_code_block then
|
if in_code_block then
|
||||||
in_code_block = false
|
in_code_block = false
|
||||||
@@ -550,12 +554,19 @@ local function extract_cursor_planning_code_snippets_map(response_content)
|
|||||||
lang = lang_ or "unknown"
|
lang = lang_ or "unknown"
|
||||||
local filepath_ = line:match("^%s*```%w+:(.+)$")
|
local filepath_ = line:match("^%s*```%w+:(.+)$")
|
||||||
filepath = filepath_ or ""
|
filepath = filepath_ or ""
|
||||||
-- local line_ = line:gsub(".*(:.+)$", "")
|
if filepath == "" then
|
||||||
-- lines[idx] = line_
|
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
|
end
|
||||||
elseif in_code_block then
|
elseif in_code_block then
|
||||||
table.insert(current_snippet, line)
|
table.insert(current_snippet, line)
|
||||||
end
|
end
|
||||||
|
idx = idx + 1
|
||||||
end
|
end
|
||||||
|
|
||||||
local snippets_map = {}
|
local snippets_map = {}
|
||||||
@@ -979,7 +990,7 @@ function Sidebar:apply(current_cursor)
|
|||||||
|
|
||||||
---@diagnostic disable-next-line: assign-type-mismatch, missing-fields
|
---@diagnostic disable-next-line: assign-type-mismatch, missing-fields
|
||||||
local patch = vim.diff(original_lines_content, resp_lines_content, { ---@type integer[][]
|
local patch = vim.diff(original_lines_content, resp_lines_content, { ---@type integer[][]
|
||||||
algorithm = "histogram",
|
algorithm = "minimal",
|
||||||
result_type = "indices",
|
result_type = "indices",
|
||||||
ctxlen = vim.o.scrolloff,
|
ctxlen = vim.o.scrolloff,
|
||||||
})
|
})
|
||||||
@@ -990,17 +1001,27 @@ function Sidebar:apply(current_cursor)
|
|||||||
local start_a, count_a, start_b, count_b = unpack(hunk)
|
local start_a, count_a, start_b, count_b = unpack(hunk)
|
||||||
|
|
||||||
for i = start_a, start_a + count_a - 1 do
|
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
|
end
|
||||||
|
|
||||||
local new_lines = vim.list_slice(resp_lines_to_process, start_b, start_b + count_b - 1)
|
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
|
local virt_lines = vim
|
||||||
.iter(new_lines)
|
.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()
|
:totable()
|
||||||
api.nvim_buf_set_extmark(bufnr, ns_id, math.max(0, start_a + count_a - 2), 0, {
|
api.nvim_buf_set_extmark(bufnr, ns_id, math.max(0, start_a + count_a - 2), 0, {
|
||||||
virt_lines = virt_lines,
|
virt_lines = virt_lines,
|
||||||
virt_text_pos = "overlay",
|
hl_eol = true,
|
||||||
hl_mode = "combine",
|
hl_mode = "combine",
|
||||||
})
|
})
|
||||||
end
|
end
|
||||||
@@ -1009,10 +1030,16 @@ function Sidebar:apply(current_cursor)
|
|||||||
|
|
||||||
local winid = Utils.get_winid(bufnr)
|
local winid = Utils.get_winid(bufnr)
|
||||||
|
|
||||||
|
if winid == nil then return end
|
||||||
|
|
||||||
--- goto window winid
|
--- goto window winid
|
||||||
api.nvim_set_current_win(winid)
|
api.nvim_set_current_win(winid)
|
||||||
--- goto the last line
|
--- 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")
|
vim.cmd("normal! zz")
|
||||||
end,
|
end,
|
||||||
on_stop = function(stop_opts)
|
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("<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 resp_lines = vim.split(resp_content, "\n")
|
||||||
local original_lines = vim.list_slice(original_code_lines, 1, #resp_lines)
|
local original_lines = vim.list_slice(original_code_lines, 1, #resp_lines)
|
||||||
local resp_lines_content = table.concat(resp_lines, "\n")
|
local resp_lines_content = table.concat(resp_lines, "\n")
|
||||||
|
|||||||
Reference in New Issue
Block a user