diff --git a/aider/coders/editblock_coder.py b/aider/coders/editblock_coder.py index c96423dfb..b63f577a2 100644 --- a/aider/coders/editblock_coder.py +++ b/aider/coders/editblock_coder.py @@ -95,7 +95,7 @@ def replace_part_with_missing_leading_whitespace(whole, part, replace): # If all lines in the part start with whitespace, then honor it. # But GPT often outdents the part and replace blocks completely, # thereby discarding the actual leading whitespace in the file. - if all((len(pline) > 0 and pline[0].isspace()) for pline in part_lines): + if all((not pline or pline[0].isspace()) for pline in part_lines): return for i in range(len(whole_lines) - len(part_lines) + 1): diff --git a/tests/test_editblock.py b/tests/test_editblock.py index 1c140223b..68cc118b5 100644 --- a/tests/test_editblock.py +++ b/tests/test_editblock.py @@ -209,6 +209,20 @@ These changes replace the `subprocess.run` patches with `subprocess.check_output result = eb.replace_part_with_missing_leading_whitespace(whole, part, replace) self.assertEqual(result, expected_output) + def test_replace_part_with_missing_leading_whitespace_including_blank_lines(self): + """ + The part has leading whitespace on all lines, so should be ignored. + But it has a *blank* line with no whitespace at all, which was causing a + bug per issue #25. Test case to repro and confirm fix. + """ + whole = " line1\n line2\n line3\n" + part = "\n line1\n line2" + replace = "new_line1\nnew_line2" + expected_output = None + + result = utils.replace_part_with_missing_leading_whitespace(whole, part, replace) + self.assertEqual(result, expected_output) + if __name__ == "__main__": unittest.main()