Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions CHANGES.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@
<!-- Changes that affect Black's stable style -->

- 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

Expand Down
2 changes: 1 addition & 1 deletion src/black/ranges.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
17 changes: 17 additions & 0 deletions tests/test_ranges.py
Original file line number Diff line number Diff line change
Expand Up @@ -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",
[
Expand Down
Loading