diff --git a/CHANGES.md b/CHANGES.md index eaa962f918d..203b054fb91 100644 --- a/CHANGES.md +++ b/CHANGES.md @@ -14,6 +14,8 @@ - Don't double-decode input, causing non-UTF-8 files to be corrupted (#4964) +- Fix `--line-ranges` not marking the first block as changed when it contains + modifications before the first matching content (#4997) ### Preview style diff --git a/src/black/ranges.py b/src/black/ranges.py index d7e003db83f..f8378523106 100644 --- a/src/black/ranges.py +++ b/src/black/ranges.py @@ -492,7 +492,7 @@ def _calculate_lines_mappings( original_end=block.a, modified_start=1, modified_end=block.b, - is_changed_block=False, + is_changed_block=True, ) ) else: diff --git a/tests/test_ranges.py b/tests/test_ranges.py index 0ed0e989123..1ddc8991e89 100644 --- a/tests/test_ranges.py +++ b/tests/test_ranges.py @@ -183,6 +183,23 @@ def test_diffs(lines: list[tuple[int, int]], adjusted: list[tuple[int, int]]) -> assert adjusted == adjusted_lines(lines, original_source, modified_source) +def test_first_block_changed() -> None: + """Changes in the first block (before any matching content) should be included.""" + original_source = """\ +x = 1 +y = 2 +z = 3 +""" + modified_source = """\ +x = 10 +y = 2 +z = 3 +""" + # Line 1 was changed, so range (1, 1) should map to (1, 1) in the output. + result = adjusted_lines([(1, 1)], original_source, modified_source) + assert result == [(1, 1)] + + @pytest.mark.parametrize( "lines,sanitized", [