From 615e56906ea6565701b8a189bfa69b54d4ca4509 Mon Sep 17 00:00:00 2001 From: Zanie Date: Thu, 26 Oct 2023 15:52:00 -0500 Subject: [PATCH] Fix added / removed counts for `format` --- python/ruff-ecosystem/ruff_ecosystem/types.py | 16 ++++++++++++---- 1 file changed, 12 insertions(+), 4 deletions(-) diff --git a/python/ruff-ecosystem/ruff_ecosystem/types.py b/python/ruff-ecosystem/ruff_ecosystem/types.py index 665107bf21..b6b5cb19f6 100644 --- a/python/ruff-ecosystem/ruff_ecosystem/types.py +++ b/python/ruff-ecosystem/ruff_ecosystem/types.py @@ -24,12 +24,20 @@ class Serializable(abc.ABC): 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) # Compute added and removed lines once - self.added = list(line[2:] for line in self.lines if line.startswith("+ ")) - self.removed = list(line[2:] for line in self.lines if line.startswith("- ")) + self.added = list( + 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: return bool(self.added or self.removed) @@ -50,7 +58,7 @@ class Diff(Serializable): """ 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: return Diff(