Merge pull request #3828 from zjy1412/main

fix: correctly detect edit blocks in diff-fenced mode
This commit is contained in:
paul-gauthier 2025-04-19 10:03:31 -07:00 committed by GitHub
commit f1caab9de0
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
2 changed files with 67 additions and 1 deletions

View file

@ -454,7 +454,10 @@ def find_original_update_blocks(content, fence=DEFAULT_FENCE, valid_fnames=None)
"```csh", "```csh",
"```tcsh", "```tcsh",
] ]
next_is_editblock = i + 1 < len(lines) and head_pattern.match(lines[i + 1].strip())
# Check if the next line or the one after that is an editblock
next_is_editblock = (i + 1 < len(lines) and head_pattern.match(lines[i + 1].strip())
or i + 2 < len(lines) and head_pattern.match(lines[i + 2].strip()))
if any(line.strip().startswith(start) for start in shell_starts) and not next_is_editblock: if any(line.strip().startswith(start) for start in shell_starts) and not next_is_editblock:
shell_content = [] shell_content = []

View file

@ -575,6 +575,69 @@ Hope you like it!
edits = list(eb.find_original_update_blocks(edit, fence=quad_backticks)) edits = list(eb.find_original_update_blocks(edit, fence=quad_backticks))
self.assertEqual(edits, [("foo.txt", "", "Tooooo\n")]) self.assertEqual(edits, [("foo.txt", "", "Tooooo\n")])
#Test for shell script blocks with sh language identifier (issue #3785)
def test_find_original_update_blocks_with_sh_language_identifier(self):
# https://github.com/Aider-AI/aider/issues/3785
edit = """
Here's a shell script:
```sh
test_hello.sh
<<<<<<< SEARCH
=======
#!/bin/bash
# Check if exactly one argument is provided
if [ "$#" -ne 1 ]; then
echo "Usage: $0 <argument>" >&2
exit 1
fi
# Echo the first argument
echo "$1"
exit 0
>>>>>>> REPLACE
```
"""
edits = list(eb.find_original_update_blocks(edit))
# Instead of comparing exact strings, check that we got the right file and structure
self.assertEqual(len(edits), 1)
self.assertEqual(edits[0][0], "test_hello.sh")
self.assertEqual(edits[0][1], "")
# Check that the content contains the expected shell script elements
result_content = edits[0][2]
self.assertIn("#!/bin/bash", result_content)
self.assertIn("if [ \"$#\" -ne 1 ];", result_content)
self.assertIn("echo \"Usage: $0 <argument>\"", result_content)
self.assertIn("exit 1", result_content)
self.assertIn("echo \"$1\"", result_content)
self.assertIn("exit 0", result_content)
#Test for C# code blocks with csharp language identifier
def test_find_original_update_blocks_with_csharp_language_identifier(self):
edit = """
Here's a C# code change:
```csharp
Program.cs
<<<<<<< SEARCH
Console.WriteLine("Hello World!");
=======
Console.WriteLine("Hello, C# World!");
>>>>>>> REPLACE
```
"""
edits = list(eb.find_original_update_blocks(edit))
search_text = "Console.WriteLine(\"Hello World!\");\n"
replace_text = "Console.WriteLine(\"Hello, C# World!\");\n"
self.assertEqual(
edits,
[("Program.cs", search_text, replace_text)]
)
if __name__ == "__main__": if __name__ == "__main__":
unittest.main() unittest.main()