From 429bdd6b613b9d1e06b31a3703cb2dbd0818da1a Mon Sep 17 00:00:00 2001 From: Christopher Brewin Date: Wed, 25 Dec 2024 00:44:02 +1000 Subject: [PATCH] fix (sidebar) rendering and applying snippets for new files and files outside of the sidebar selected file context. (#991) --- lua/avante/sidebar.lua | 15 ++++++++++++++- 1 file changed, 14 insertions(+), 1 deletion(-) diff --git a/lua/avante/sidebar.lua b/lua/avante/sidebar.lua index 4bbb0b8..fe5321c 100644 --- a/lua/avante/sidebar.lua +++ b/lua/avante/sidebar.lua @@ -232,6 +232,14 @@ local function transform_result_content(selected_files, result_content, prev_fil ::continue1:: end + -- when the filetype isn't detected, fallback to matching based on filepath. + -- can happen if the llm tries to edit or create a file outside of it's context. + if not match_filetype then + local snippet_file_path = current_filepath or prev_filepath + local snippet_file_type = vim.filetype.match({ filename = snippet_file_path }) or "unknown" + match_filetype = snippet_file_type + end + local search_start_tag_idx_in_transformed_lines = 0 for j = 1, #transformed_lines do if transformed_lines[j] == "" then @@ -430,7 +438,8 @@ local function ensure_snippets_no_overlap(snippets_map) table.sort(snippets, function(a, b) return a.range[1] < b.range[1] end) local original_content = "" - if Utils.file.exists(filepath) then original_content = Utils.file.read_content(filepath) or "" end + local file_exists = Utils.file.exists(filepath) + if file_exists then original_content = Utils.file.read_content(filepath) or "" end local original_lines = vim.split(original_content, "\n") @@ -440,6 +449,10 @@ local function ensure_snippets_no_overlap(snippets_map) if snippet.range[1] > last_end_line then table.insert(new_snippets, snippet) last_end_line = snippet.range[2] + elseif not file_exists and #snippets <= 1 then + -- if the file doesn't exist, and we only have 1 snippet, then we don't have to check for overlaps. + table.insert(new_snippets, snippet) + last_end_line = snippet.range[2] else local snippet_lines = vim.split(snippet.content, "\n") -- Trim the overlapping part