From adb61b690791d1cb839a259852466d103cd2c3c3 Mon Sep 17 00:00:00 2001 From: Dennis Dillert Date: Sun, 15 Sep 2024 00:52:38 +0200 Subject: [PATCH] Improve pattern matching to handle deviations from local LLMs - Extend pattern matching beyond "Replace lines: {{start_line}}-{{end_line}}" to support variations such as: - Extra whitespace - Numbered changes - Optional colons - Case insensitivity (upper- and lowercase) - Singular line references - Add support for indented code snippets, allowing whitespace before code blocks beginning with three backticks (```) --- lua/avante/sidebar.lua | 17 ++++++++++++----- 1 file changed, 12 insertions(+), 5 deletions(-) diff --git a/lua/avante/sidebar.lua b/lua/avante/sidebar.lua index c938c06..4ecfb2f 100644 --- a/lua/avante/sidebar.lua +++ b/lua/avante/sidebar.lua @@ -330,12 +330,19 @@ local function extract_code_snippets(code_content, response_content) local explanation = "" for idx, line in ipairs(vim.split(response_content, "\n")) do - local start_line_str, end_line_str = line:match("^Replace lines: (%d+)-(%d+)") + local _, start_line_str, end_line_str = + line:match("^%s*(%d*)[%.%)%s]*[Aa]?n?d?%s*[Rr]eplace%s+[Ll]ines:?%s*(%d+)%-(%d+)") if start_line_str ~= nil and end_line_str ~= nil then start_line = tonumber(start_line_str) end_line = tonumber(end_line_str) + else + _, start_line_str = line:match("^%s*(%d*)[%.%)%s]*[Aa]?n?d?%s*[Rr]eplace%s+[Ll]ine:?%s*(%d+)") + if start_line_str ~= nil then + start_line = tonumber(start_line_str) + end_line = tonumber(start_line_str) + end end - if line:match("^```") then + if line:match("^%s*```") then if in_code_block then if start_line ~= nil and end_line ~= nil then local snippet = { @@ -354,7 +361,7 @@ local function extract_code_snippets(code_content, response_content) explanation = "" in_code_block = false else - lang = line:match("^```(%w+)") + lang = line:match("^%s*```(%w+)") if not lang or lang == "" then lang = "text" end in_code_block = true start_line_in_response_buf = idx @@ -479,9 +486,9 @@ local function parse_codeblocks(buf) local lines = Utils.get_buf_lines(0, -1, buf) for i, line in ipairs(lines) do - if line:match("^```") then + if line:match("^%s*```") then -- parse language - local lang_ = line:match("^```(%w+)") + local lang_ = line:match("^%s*```(%w+)") if in_codeblock and not lang_ then table.insert(codeblocks, { start_line = start_line, end_line = i - 1, lang = lang }) in_codeblock = false