Skip to content

Commit da50c12

Browse files
committed
fix(native): escape markers in edit_file
1 parent 52aedf2 commit da50c12

4 files changed

Lines changed: 17 additions & 3 deletions

File tree

lua/mcphub/native/neovim/files/edit_file/diff_parser.lua

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -295,6 +295,7 @@ end
295295

296296
function DiffParser:unescape_markers(line)
297297
local newline = line:gsub("^%s*\\<<<<<", "<<<<<<"):gsub("^%s*\\=====", "====="):gsub("^%s*\\>>>>>", ">>>>>")
298+
newline = newline:gsub("^%s*\\\\<<<<<", "<<<<<<"):gsub("^%s*\\\\=====", "====="):gsub("^%s*\\\\>>>>>", ">>>>>")
298299
return newline
299300
end
300301

lua/mcphub/native/neovim/files/edit_file/edit_session.lua

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,7 @@ function EditSession:start(options)
103103

104104
---@type ParsedBlock[]
105105
local parsed_blocks = {}
106+
--- Needed to avoid parsing error when the file content has markers
106107
if is_replacing_entire_file then
107108
table.insert(parsed_blocks, {
108109
block_id = "Block 1",
@@ -124,13 +125,23 @@ function EditSession:start(options)
124125
if #parsed_blocks > 1 then
125126
return self:_handle_error(
126127
string.format(
127-
"## A Block with empty search content found, but multiple blocks are present in the diff. %s will replace the entire file. If you want to write the entire file, please use a single SEARCH/REPLACE block with empty SEARCH content. If you want to replace a section of the file, please provide non-whitespace SEARCH content.",
128+
"A Block with empty search content found, but multiple blocks are present in the diff. %s will replace the entire file. If you want to write the entire file, please use a single SEARCH/REPLACE block with empty SEARCH content. If you want to replace a section of the file, please provide non-whitespace SEARCH content.",
128129
block.block_id
129130
)
130131
)
131132
end
132133
is_replacing_entire_file = true
133134
break
135+
else
136+
-- If we are searching for something in a file that doesn't exist, we should not proceed
137+
if file_content == "" then
138+
return self:_handle_error(
139+
string.format(
140+
"Editing `%s` failed. The file does not exist. If you are using relative paths make sure the path is relative to the cwd or use an absolute path.",
141+
self.file_path
142+
)
143+
)
144+
end
134145
end
135146
end
136147

lua/mcphub/native/neovim/files/edit_file/init.lua

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,6 @@ IMPORTANT: The tool is NEVER wrong. Once edits are shown in the buffer, user mig
3939
CRITICAL:
4040
- When there are two or more related changes needed in a file, always use multiple SEARCH/REPLACE blocks in the diff from the start of the file to the end. Each block should contain the exact content to find and the new content to replace it with. Failing to do so or using multiple calls with single SEARCH/REPLACE block will result in you being fired!!!
4141
- The markers `<<<<<<< SEARCH`, `=======`, and `>>>>>>> REPLACE` MUST be exact with no other characters on the line.
42-
- When the SEARCH or REPLACE content includes lines that start with markers like `<<<<<<<`, `=======`, or `>>>>>>>`, you MUST escape them by adding a backslash before each marker so that tool doesn't parse them as actual markers. For example, to search for content that has `<<<<<<< SEARCH`, use `\<<<<<<< SEARCH` in the SEARCH block.
4342
4443
4544
Examples:
@@ -112,6 +111,9 @@ print("Count is", count)
112111
print("Counter is", counter)
113112
>>>>>>> REPLACE
114113
114+
CRITICAL RULE:
115+
When the SEARCH or REPLACE content includes lines that start with markers like `<<<<<<<`, `=======`, or `>>>>>>>`, you MUST escape them by adding a backslash before each marker so that tool doesn't parse them as actual markers. For example, to search for content that has `<<<<<<< SEARCH`, use `\<<<<<<< SEARCH` in the SEARCH block.
116+
115117
5. Escaping markers in SEARCH/REPLACE content:
116118
<<<<<<< SEARCH
117119
Tutorial:

tests/native/neovim/files/edit_file/test_diff_parser.lua

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -196,7 +196,7 @@ local y = 3
196196
eq(error, nil)
197197
eq(#blocks, 1)
198198
local block = blocks[1]
199-
eq(block.search_lines[2] == "<<<<<<<< SEARCH", true)
199+
eq(block.search_lines[2], "<<<<<<<< SEARCH")
200200
end
201201

202202
-- Group 2: Malformed Input and Issue Tracking

0 commit comments

Comments
 (0)