Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix (sidebar) rendering and applying snippets for new files #991

Merged
merged 1 commit into from
Dec 24, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 14 additions & 1 deletion lua/avante/sidebar.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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] == "<SEARCH>" then
Expand Down Expand Up @@ -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")

Expand All @@ -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
Expand Down
Loading