From b20b2caf220cd988b026d5981475bf8928afa27e Mon Sep 17 00:00:00 2001 From: yetone Date: Mon, 17 Feb 2025 22:27:43 +0800 Subject: [PATCH] fix: cursor planning mode bad cases (#1290) --- lua/avante/diff.lua | 9 ++++---- lua/avante/highlights.lua | 5 ++--- lua/avante/sidebar.lua | 45 +++++++++++++++++++++++++++++++-------- 3 files changed, 43 insertions(+), 16 deletions(-) diff --git a/lua/avante/diff.lua b/lua/avante/diff.lua index b7ac15d..f5eead7 100644 --- a/lua/avante/diff.lua +++ b/lua/avante/diff.lua @@ -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") diff --git a/lua/avante/highlights.lua b/lua/avante/highlights.lua index 621d932..b9a4b09 100644 --- a/lua/avante/highlights.lua +++ b/lua/avante/highlights.lua @@ -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 diff --git a/lua/avante/sidebar.lua b/lua/avante/sidebar.lua index 8b28f1c..46f9180 100644 --- a/lua/avante/sidebar.lua +++ b/lua/avante/sidebar.lua @@ -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("\n*", ""):gsub("\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")