mirror of https://github.com/astral-sh/ruff
Fix added / removed counts for `format`
This commit is contained in:
parent
ce290076b8
commit
615e56906e
|
|
@ -24,12 +24,20 @@ class Serializable(abc.ABC):
|
||||||
|
|
||||||
|
|
||||||
class Diff(Serializable):
|
class Diff(Serializable):
|
||||||
def __init__(self, lines: Iterable[str]) -> None:
|
def __init__(self, lines: Iterable[str], leading_spaces: int = 0) -> None:
|
||||||
self.lines = list(lines)
|
self.lines = list(lines)
|
||||||
|
|
||||||
# Compute added and removed lines once
|
# Compute added and removed lines once
|
||||||
self.added = list(line[2:] for line in self.lines if line.startswith("+ "))
|
self.added = list(
|
||||||
self.removed = list(line[2:] for line in self.lines if line.startswith("- "))
|
line[2:]
|
||||||
|
for line in self.lines
|
||||||
|
if line.startswith("+" + " " * leading_spaces)
|
||||||
|
)
|
||||||
|
self.removed = list(
|
||||||
|
line[2:]
|
||||||
|
for line in self.lines
|
||||||
|
if line.startswith("-" + " " * leading_spaces)
|
||||||
|
)
|
||||||
|
|
||||||
def __bool__(self) -> bool:
|
def __bool__(self) -> bool:
|
||||||
return bool(self.added or self.removed)
|
return bool(self.added or self.removed)
|
||||||
|
|
@ -50,7 +58,7 @@ class Diff(Serializable):
|
||||||
"""
|
"""
|
||||||
Construct a diff from before and after.
|
Construct a diff from before and after.
|
||||||
"""
|
"""
|
||||||
return cls(difflib.ndiff(baseline, comparison))
|
return cls(difflib.ndiff(baseline, comparison), leading_spaces=1)
|
||||||
|
|
||||||
def without_unchanged_lines(self) -> Diff:
|
def without_unchanged_lines(self) -> Diff:
|
||||||
return Diff(
|
return Diff(
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue