|
| 1 | +from difflib import ndiff |
| 2 | +from enum import Enum |
| 3 | +from typing import Iterable, Iterator, Self # noqa: UP035 |
| 4 | + |
| 5 | +from .repo import File |
| 6 | + |
| 7 | + |
| 8 | +class DiffLineStatus(Enum): |
| 9 | + COPIED = "COPIED" |
| 10 | + DIFFERENT = "DIFFERENT" |
| 11 | + |
| 12 | + |
| 13 | +class Rounding(Enum): |
| 14 | + DOWN = "DOWN" |
| 15 | + UP = "UP" |
| 16 | + |
| 17 | + |
| 18 | +class DiffLine: |
| 19 | + def __init__(self, left: str | None, status: DiffLineStatus, right: str | None, left_line: int, right_line: int): |
| 20 | + self.left: str | None = left |
| 21 | + self.status: DiffLineStatus = status |
| 22 | + self.right: str | None = right |
| 23 | + self.left_line: int = left_line |
| 24 | + self.right_line: int = right_line |
| 25 | + |
| 26 | + |
| 27 | +class CollapsedDiffLine(DiffLine): |
| 28 | + def __init__(self, left_start_line: int, right_start_line: int, num_identical_lines: int): |
| 29 | + self.left_start_line: int = left_start_line |
| 30 | + self.right_start_line: int = right_start_line |
| 31 | + self.num_identical_lines: int = num_identical_lines |
| 32 | + self.left: str |
| 33 | + self.right: str |
| 34 | + super().__init__( |
| 35 | + f"<{self.num_identical_lines} identical lines starting on line {self.left_start_line}>", |
| 36 | + DiffLineStatus.COPIED, |
| 37 | + f"<{self.num_identical_lines} identical lines starting on line {self.right_start_line}>", |
| 38 | + left_line=left_start_line, |
| 39 | + right_line=right_start_line, |
| 40 | + ) |
| 41 | + |
| 42 | + |
| 43 | +class Document: |
| 44 | + def __init__(self, lines: Iterable[str], start_line: int = 1): |
| 45 | + self.lines: tuple[str, ...] = tuple(lines) |
| 46 | + self.start_line: int = start_line |
| 47 | + self._line_start_offsets: list[int] = [0] |
| 48 | + for line in self.lines[:-1]: |
| 49 | + self._line_start_offsets.append(self._line_start_offsets[-1] + len(line)) |
| 50 | + |
| 51 | + def __len__(self) -> int: |
| 52 | + return len(self.lines) |
| 53 | + |
| 54 | + def __iter__(self) -> Iterator[str]: |
| 55 | + return iter(self.lines) |
| 56 | + |
| 57 | + def __getitem__(self, index: int | slice) -> str | tuple[str, ...]: |
| 58 | + return self.lines[index] |
| 59 | + |
| 60 | + def get_line(self, byte_offset: int, rounding: Rounding = Rounding.DOWN, min_line: int = 0) -> int: |
| 61 | + if byte_offset < 0: |
| 62 | + byte_offset = self._line_start_offsets[-1] + len(self.lines[-1]) + byte_offset |
| 63 | + if rounding == Rounding.DOWN: |
| 64 | + start = max(min_line, 0) |
| 65 | + while start + 1 < len(self._line_start_offsets) and self._line_start_offsets[start + 1] < byte_offset: |
| 66 | + start += 1 |
| 67 | + return start |
| 68 | + # round up |
| 69 | + end = max(min_line, 0) |
| 70 | + while end < len(self._line_start_offsets) and self._line_start_offsets[end] < byte_offset: |
| 71 | + end += 1 |
| 72 | + return end |
| 73 | + |
| 74 | + @classmethod |
| 75 | + def from_str(cls, text: str) -> Self: |
| 76 | + return cls(text.splitlines(keepends=True)) |
| 77 | + |
| 78 | + @classmethod |
| 79 | + def from_file(cls, file: File) -> Self: |
| 80 | + with file.repo: |
| 81 | + return cls.from_str(file.path.read_text()) |
| 82 | + |
| 83 | + |
| 84 | +class DiffContext: |
| 85 | + def __init__( |
| 86 | + self, |
| 87 | + test_start_line: int = 0, |
| 88 | + test_end_line: int = -1, |
| 89 | + source_start_line: int = 0, |
| 90 | + source_end_line: int = -1, |
| 91 | + collapse_identical_lines_threshold: int = 10, |
| 92 | + ): |
| 93 | + self.test_start_line: int = test_start_line |
| 94 | + self.test_end_line: int = test_end_line |
| 95 | + self.source_start_line: int = source_start_line |
| 96 | + self.source_end_line: int = source_end_line |
| 97 | + self.collapse_identical_lines_threshold: int = collapse_identical_lines_threshold |
| 98 | + self.rows: list[DiffLine] = [] |
| 99 | + self.same_lines: int = 0 |
| 100 | + self.test_line: int = 0 |
| 101 | + self.source_line: int = 0 |
| 102 | + |
| 103 | + def add_test_row(self, code: str) -> None: |
| 104 | + insertion_point: DiffLine | None = None |
| 105 | + for line in reversed(self.rows): |
| 106 | + if line.left is not None: |
| 107 | + break |
| 108 | + insertion_point = line |
| 109 | + if insertion_point is None: |
| 110 | + self.rows.append(DiffLine(code, DiffLineStatus.DIFFERENT, None, self.test_line, -1)) |
| 111 | + else: |
| 112 | + insertion_point.left = code |
| 113 | + insertion_point.left_line = self.test_line |
| 114 | + self.same_lines = 0 |
| 115 | + self.test_line += 1 |
| 116 | + |
| 117 | + def add_source_row(self, code: str) -> None: |
| 118 | + insertion_point: DiffLine | None = None |
| 119 | + for line in reversed(self.rows): |
| 120 | + if line.right is not None: |
| 121 | + break |
| 122 | + insertion_point = line |
| 123 | + if insertion_point is None: |
| 124 | + self.rows.append(DiffLine(None, DiffLineStatus.DIFFERENT, code, -1, self.source_line)) |
| 125 | + else: |
| 126 | + insertion_point.right = code |
| 127 | + insertion_point.right_line = self.source_line |
| 128 | + self.same_lines = 0 |
| 129 | + self.source_line += 1 |
| 130 | + |
| 131 | + def add_identical_row(self, num_identical: int) -> Iterator[DiffLine]: |
| 132 | + for _ in range(num_identical): |
| 133 | + self.rows.pop() |
| 134 | + yield from self.rows |
| 135 | + self.rows.clear() |
| 136 | + yield CollapsedDiffLine(self.test_line, self.source_line, num_identical) |
| 137 | + |
| 138 | + |
| 139 | +class Differ: |
| 140 | + def __init__( |
| 141 | + self, |
| 142 | + test: Document, |
| 143 | + source: Document, |
| 144 | + ): |
| 145 | + self.test: Document = test |
| 146 | + self.source: Document = source |
| 147 | + |
| 148 | + def diff(self, context: DiffContext | None = None) -> Iterator[DiffLine]: |
| 149 | + if context is None: |
| 150 | + context = DiffContext() |
| 151 | + test_slice_content = self.test[max(0, context.test_start_line) : context.test_end_line] |
| 152 | + source_slice_content = self.source[max(0, context.source_start_line) : context.source_end_line] |
| 153 | + |
| 154 | + context.test_line = max(1, context.test_start_line + 1) |
| 155 | + context.source_line = max(1, context.source_start_line + 1) |
| 156 | + context.rows = [] |
| 157 | + context.same_lines = 0 |
| 158 | + |
| 159 | + for diff_line in ndiff(test_slice_content, source_slice_content): |
| 160 | + if diff_line.startswith(" "): |
| 161 | + context.same_lines += 1 |
| 162 | + copied_str = diff_line[2:].rstrip() |
| 163 | + context.rows.append( |
| 164 | + DiffLine( |
| 165 | + copied_str, |
| 166 | + DiffLineStatus.COPIED, |
| 167 | + copied_str, |
| 168 | + context.test_line, |
| 169 | + context.source_line, |
| 170 | + ) |
| 171 | + ) |
| 172 | + context.test_line += 1 |
| 173 | + context.source_line += 1 |
| 174 | + else: |
| 175 | + if diff_line[:2] in ("- ", "+ ") and context.same_lines >= context.collapse_identical_lines_threshold: |
| 176 | + yield from context.add_identical_row(context.same_lines) |
| 177 | + if diff_line.startswith("- "): |
| 178 | + context.add_test_row(diff_line[2:].rstrip()) |
| 179 | + elif diff_line.startswith("+ "): |
| 180 | + context.add_source_row(diff_line[2:].rstrip()) |
| 181 | + |
| 182 | + if context.same_lines >= context.collapse_identical_lines_threshold: |
| 183 | + yield from context.add_identical_row(context.same_lines) |
| 184 | + |
| 185 | + yield from context.rows |
| 186 | + |
| 187 | + def diff_from_offsets( |
| 188 | + self, |
| 189 | + test_start_offset: int = 0, |
| 190 | + test_end_offset: int = -1, |
| 191 | + source_start_offset: int = 0, |
| 192 | + source_end_offset: int = -1, |
| 193 | + collapse_identical_lines_threshold: int = 10, |
| 194 | + ) -> Iterator[DiffLine]: |
| 195 | + test_start = self.test.get_line(test_start_offset, rounding=Rounding.DOWN) |
| 196 | + test_end = self.test.get_line(test_end_offset, rounding=Rounding.UP, min_line=test_start + 1) |
| 197 | + source_start = self.source.get_line(source_start_offset, rounding=Rounding.DOWN) |
| 198 | + source_end = self.source.get_line(source_end_offset, rounding=Rounding.UP, min_line=source_start + 1) |
| 199 | + return self.diff( |
| 200 | + DiffContext( |
| 201 | + test_start_line=test_start, |
| 202 | + test_end_line=test_end, |
| 203 | + source_start_line=source_start, |
| 204 | + source_end_line=source_end, |
| 205 | + collapse_identical_lines_threshold=collapse_identical_lines_threshold, |
| 206 | + ) |
| 207 | + ) |
0 commit comments