diff --git a/lua/avante/llm_tools/create.lua b/lua/avante/llm_tools/create.lua index a9a91a7..9ecb13b 100644 --- a/lua/avante/llm_tools/create.lua +++ b/lua/avante/llm_tools/create.lua @@ -61,7 +61,7 @@ function M.func(input, opts) if Path:new(abs_path):exists() then return false, "File already exists: " .. abs_path end local lines = vim.split(input.file_text, "\n") if #lines == 1 and input.file_text:match("\\n") then - local text = Utils.trim_slashes(input.file_text) + local text = Utils.trim_escapes(input.file_text) lines = vim.split(text, "\n") end local bufnr, err = Helpers.get_bufnr(abs_path) diff --git a/lua/avante/llm_tools/write_to_file.lua b/lua/avante/llm_tools/write_to_file.lua index de2b978..89d2637 100644 --- a/lua/avante/llm_tools/write_to_file.lua +++ b/lua/avante/llm_tools/write_to_file.lua @@ -66,6 +66,10 @@ function M.func(input, opts) if not Helpers.has_permission_to_access(abs_path) then return false, "No permission to access path: " .. abs_path end if input.content == nil then return false, "content not provided" end if type(input.content) ~= "string" then input.content = vim.json.encode(input.content) end + if Utils.count_lines(input.content) == 1 then + Utils.debug("Trimming escapes from content") + input.content = Utils.trim_escapes(input.content) + end local old_lines = Utils.read_file_from_buf_or_disk(abs_path) local old_content = table.concat(old_lines or {}, "\n") local str_replace = require("avante.llm_tools.str_replace") diff --git a/lua/avante/utils/init.lua b/lua/avante/utils/init.lua index f0fc36f..2ece994 100644 --- a/lua/avante/utils/init.lua +++ b/lua/avante/utils/init.lua @@ -608,7 +608,7 @@ function M.trim_space(text) return text:gsub("%s*", "") end -function M.trim_slashes(text) +function M.trim_escapes(text) if not text then return text end local res = text :gsub("//n", "/n") @@ -677,14 +677,14 @@ function M.fuzzy_match(original_lines, target_lines) start_line, end_line = M.try_find_match( original_lines, target_lines, - function(line_a, line_b) return line_a == M.trim_slashes(line_b) end + function(line_a, line_b) return line_a == M.trim_escapes(line_b) end ) if start_line ~= nil and end_line ~= nil then return start_line, end_line end ---trim slashes and trim_space match start_line, end_line = M.try_find_match( original_lines, target_lines, - function(line_a, line_b) return M.trim_space(line_a) == M.trim_space(M.trim_slashes(line_b)) end + function(line_a, line_b) return M.trim_space(line_a) == M.trim_space(M.trim_escapes(line_b)) end ) return start_line, end_line end