From 05b3b843c8cc4c4f810fc3671ea38febdfd3cca4 Mon Sep 17 00:00:00 2001 From: yetone Date: Mon, 10 Mar 2025 00:17:24 +0800 Subject: [PATCH] fix: selected code (#1542) --- lua/avante/sidebar.lua | 17 +++++++++-------- lua/avante/types.lua | 2 +- lua/avante/utils/history.lua | 10 +++++++--- 3 files changed, 17 insertions(+), 12 deletions(-) diff --git a/lua/avante/sidebar.lua b/lua/avante/sidebar.lua index c6ba02f..416938f 100644 --- a/lua/avante/sidebar.lua +++ b/lua/avante/sidebar.lua @@ -2031,13 +2031,13 @@ local function get_timestamp() return os.date("%Y-%m-%d %H:%M:%S") end ---@param model string ---@param request string ---@param selected_filepaths string[] ----@param selected_code {filetype: string, content: string}? +---@param selected_code AvanteSelectedCode? ---@return string local function render_chat_record_prefix(timestamp, provider, model, request, selected_filepaths, selected_code) provider = provider or "unknown" model = model or "unknown" local res = "- Datetime: " .. timestamp .. "\n\n" .. "- Model: " .. provider .. "/" .. model - if selected_filepaths ~= nil then + if selected_filepaths ~= nil and #selected_filepaths > 0 then res = res .. "\n\n- Selected files:" for _, path in ipairs(selected_filepaths) do res = res .. "\n - " .. path @@ -2047,7 +2047,8 @@ local function render_chat_record_prefix(timestamp, provider, model, request, se res = res .. "\n\n- Selected code: " .. "\n\n```" - .. selected_code.filetype + .. (selected_code.file_type or "") + .. (selected_code.path and ":" .. selected_code.path or "") .. "\n" .. selected_code.content .. "\n```" @@ -2085,7 +2086,7 @@ function Sidebar:render_history_content(history) for idx, entry in ipairs(history.entries) do if entry.reset_memory then content = content .. "***MEMORY RESET***\n\n" - if idx < #history then content = content .. "-------\n\n" end + if idx < #history.entries then content = content .. "-------\n\n" end goto continue end local selected_filepaths = entry.selected_filepaths @@ -2102,7 +2103,7 @@ function Sidebar:render_history_content(history) ) content = content .. prefix content = content .. entry.response .. "\n\n" - if idx < #history then content = content .. "-------\n\n" end + if idx < #history.entries then content = content .. "-------\n\n" end ::continue:: end return content @@ -2432,14 +2433,14 @@ function Sidebar:create_input_container(opts) local timestamp = get_timestamp() - local filetype = api.nvim_get_option_value("filetype", { buf = self.code.bufnr }) - local selected_filepaths = self.file_selector:get_selected_filepaths() + ---@type AvanteSelectedCode | nil local selected_code = nil if self.code.selection ~= nil then selected_code = { - filetype = filetype, + path = self.code.selection.filepath, + file_type = self.code.selection.filetype, content = self.code.selection.content, } end diff --git a/lua/avante/types.lua b/lua/avante/types.lua index 7755760..f44088f 100644 --- a/lua/avante/types.lua +++ b/lua/avante/types.lua @@ -367,7 +367,7 @@ vim.g.avante_login = vim.g.avante_login ---@field response string ---@field original_response string ---@field selected_file {filepath: string}? ----@field selected_code {filetype: string, content: string}? +---@field selected_code AvanteSelectedCode | nil ---@field reset_memory boolean? ---@field selected_filepaths string[] | nil --- diff --git a/lua/avante/utils/history.lua b/lua/avante/utils/history.lua index 89085fa..5527b8b 100644 --- a/lua/avante/utils/history.lua +++ b/lua/avante/utils/history.lua @@ -31,13 +31,17 @@ function M.entries_to_llm_messages(entries) local messages = {} for _, entry in ipairs(entries) do local user_content = "" - if entry.selected_file ~= nil then - user_content = user_content .. "SELECTED FILE: " .. entry.selected_file.filepath .. "\n\n" + if entry.selected_filepaths ~= nil then + user_content = user_content .. "SELECTED FILES:\n\n" + for _, filepath in ipairs(entry.selected_filepaths) do + user_content = user_content .. filepath .. "\n" + end end if entry.selected_code ~= nil then user_content = user_content .. "SELECTED CODE:\n\n```" - .. entry.selected_code.filetype + .. (entry.selected_code.file_type or "") + .. (entry.selected_code.path and ":" .. entry.selected_code.path or "") .. "\n" .. entry.selected_code.content .. "\n```\n\n"