fix: precise code block matching (#1318)

This commit is contained in:
yetone
2025-02-20 15:46:10 +08:00
committed by GitHub
parent d23735923d
commit 8f1226a74f

View File

@@ -539,6 +539,10 @@ local function extract_cursor_planning_code_snippets_map(response_content)
if line:match("^%s*```") then if line:match("^%s*```") then
if in_code_block then if in_code_block then
in_code_block = false in_code_block = false
if filepath == nil or filepath == "" then
Utils.warn("Failed to parse filepath from code block")
goto continue
end
table.insert(snippets, { table.insert(snippets, {
range = { 0, 0 }, range = { 0, 0 },
content = table.concat(current_snippet, "\n"), content = table.concat(current_snippet, "\n"),
@@ -556,7 +560,7 @@ local function extract_cursor_planning_code_snippets_map(response_content)
filepath = filepath_ or "" filepath = filepath_ or ""
if filepath == "" then if filepath == "" then
local next_line = lines[idx + 1] local next_line = lines[idx + 1]
local filepath2 = next_line:match("[Ff][Ii][Ll][Ee][Pp][Aa][Tt][Hh]:%s*(.+)") local filepath2 = next_line:match("[Ff][Ii][Ll][Ee][Pp][Aa][Tt][Hh]:%s*(.+)%s*")
if filepath2 then if filepath2 then
filepath = filepath2 filepath = filepath2
idx = idx + 1 idx = idx + 1
@@ -566,6 +570,7 @@ local function extract_cursor_planning_code_snippets_map(response_content)
elseif in_code_block then elseif in_code_block then
table.insert(current_snippet, line) table.insert(current_snippet, line)
end end
::continue::
idx = idx + 1 idx = idx + 1
end end
@@ -771,13 +776,24 @@ local function parse_codeblocks(buf)
if in_codeblock and not lang_ then if in_codeblock and not lang_ then
table.insert(codeblocks, { start_line = start_line, end_line = i - 1, lang = lang }) table.insert(codeblocks, { start_line = start_line, end_line = i - 1, lang = lang })
in_codeblock = false in_codeblock = false
elseif elseif lang_ then
lang_ and Config.behaviour.enable_cursor_planning_mode if Config.behaviour.enable_cursor_planning_mode then
or lines[i - 1]:match("^%s*(%d*)[%.%)%s]*[Aa]?n?d?%s*[Rr]eplace%s+[Ll]ines:?%s*(%d+)%-(%d+)") local filepath = line:match("^%s*```%w+:(.*)$")
then if not filepath then
lang = lang_ if lines[i + 1] then filepath = lines[i + 1]:match("[Ff][Ii][Ll][Ee][Pp][Aa][Tt][Hh]:%s*(.*)$") end
start_line = i - 1 end
in_codeblock = true if filepath then
lang = lang_
start_line = i - 1
in_codeblock = true
end
else
if lines[i - 1]:match("^%s*(%d*)[%.%)%s]*[Aa]?n?d?%s*[Rr]eplace%s+[Ll]ines:?%s*(%d+)%-(%d+)") then
lang = lang_
start_line = i - 1
in_codeblock = true
end
end
end end
end end
end end