fix (sidebar) rendering and applying snippets for new files and files outside of the sidebar selected file context. (#991)

This commit is contained in:
Christopher Brewin 2024-12-25 00:44:02 +10:00 committed by GitHub
parent 9f3793b579
commit 429bdd6b61
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

View File

@ -232,6 +232,14 @@ local function transform_result_content(selected_files, result_content, prev_fil
::continue1:: ::continue1::
end 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 local search_start_tag_idx_in_transformed_lines = 0
for j = 1, #transformed_lines do for j = 1, #transformed_lines do
if transformed_lines[j] == "<SEARCH>" then if transformed_lines[j] == "<SEARCH>" 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) table.sort(snippets, function(a, b) return a.range[1] < b.range[1] end)
local original_content = "" 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") 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 if snippet.range[1] > last_end_line then
table.insert(new_snippets, snippet) table.insert(new_snippets, snippet)
last_end_line = snippet.range[2] 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 else
local snippet_lines = vim.split(snippet.content, "\n") local snippet_lines = vim.split(snippet.content, "\n")
-- Trim the overlapping part -- Trim the overlapping part