diff --git a/lua/avante/llm.lua b/lua/avante/llm.lua index 5ebf80a..48c5844 100644 --- a/lua/avante/llm.lua +++ b/lua/avante/llm.lua @@ -118,6 +118,7 @@ function M.generate_prompts(opts) code_lang = opts.code_lang, selected_files = opts.selected_files, selected_code = opts.selected_code, + recently_viewed_files = opts.recently_viewed_files, project_context = opts.project_context, diagnostics = opts.diagnostics, system_info = system_info, diff --git a/lua/avante/sidebar.lua b/lua/avante/sidebar.lua index 9333571..c6ba02f 100644 --- a/lua/avante/sidebar.lua +++ b/lua/avante/sidebar.lua @@ -2400,6 +2400,7 @@ function Sidebar:create_input_container(opts) ask = opts.ask or true, project_context = vim.json.encode(project_context), selected_files = selected_files_contents, + recently_viewed_files = Utils.get_recent_filepaths(), diagnostics = vim.json.encode(diagnostics), history_messages = history_messages, code_lang = filetype, diff --git a/lua/avante/templates/_context.avanterules b/lua/avante/templates/_context.avanterules index 77b2f81..45ce298 100644 --- a/lua/avante/templates/_context.avanterules +++ b/lua/avante/templates/_context.avanterules @@ -23,3 +23,11 @@ {%- endif %} + +{% if recently_viewed_files -%} + +{%- for file in recently_viewed_files %} +{{loop.index}}. {{file}} +{%- endfor %} + +{%- endif %} diff --git a/lua/avante/templates/_tools-guidelines.avanterules b/lua/avante/templates/_tools-guidelines.avanterules index 787bd94..f0c3018 100644 --- a/lua/avante/templates/_tools-guidelines.avanterules +++ b/lua/avante/templates/_tools-guidelines.avanterules @@ -5,6 +5,7 @@ Tools Usage Guide: - Please DON'T be so aggressive in using tools, as many tasks can be better completed without tools. - Files will be provided to you as context through tag! - Before using the `read_file` tool each time, always repeatedly check whether the file is already in the tag. If it is already there, do not use the `read_file` tool, just read the file content directly from the tag. + - If you use the `read_file` tool when file content is already provided in the tag, you will be fired! - If the `rag_search` tool exists, prioritize using it to do the search! - If the `rag_search` tool exists, only use tools like `search_keyword` `search_files` `read_file` `list_files` etc when absolutely necessary! - Keep the `query` parameter of `rag_search` tool as concise as possible! Try to keep it within five English words! diff --git a/lua/avante/types.lua b/lua/avante/types.lua index c65e57d..7755760 100644 --- a/lua/avante/types.lua +++ b/lua/avante/types.lua @@ -298,6 +298,7 @@ vim.g.avante_login = vim.g.avante_login ---@class AvanteTemplateOptions ---@field ask boolean ---@field code_lang string +---@field recently_viewed_files string[] | nil ---@field selected_code AvanteSelectedCode | nil ---@field project_context string | nil ---@field selected_files AvanteSelectedFile[] | nil diff --git a/lua/avante/utils/init.lua b/lua/avante/utils/init.lua index 7f1cbac..c3d80df 100644 --- a/lua/avante/utils/init.lua +++ b/lua/avante/utils/init.lua @@ -642,15 +642,21 @@ end -- Get recent filepaths in the same project and same file ext ---@param limit? integer ---@param filenames? string[] +---@param same_file_ext? boolean ---@return string[] -function M.get_recent_filepaths(limit, filenames) +function M.get_recent_filepaths(limit, filenames, same_file_ext) local project_root = M.get_project_root() local current_ext = fn.expand("%:e") local oldfiles = vim.v.oldfiles local recent_files = {} for _, file in ipairs(oldfiles) do - if vim.startswith(file, project_root) and M.is_same_file_ext(current_ext, file) then + if vim.startswith(file, project_root) then + local has_ext = file:match("%.%w+$") + if not has_ext then goto continue end + if same_file_ext then + if not M.is_same_file_ext(current_ext, file) then goto continue end + end if filenames and #filenames > 0 then for _, filename in ipairs(filenames) do if file:find(filename) then table.insert(recent_files, file) end @@ -660,6 +666,7 @@ function M.get_recent_filepaths(limit, filenames) end if #recent_files >= (limit or 10) then break end end + ::continue:: end return recent_files