Make your computer a home for your AI partner. Avibe is the local-first Agent OS: Vibey, its built-in AI partner, works out of the box, and Claude Code, Codex & OpenCode run unchanged beside it — reach them from your browser, phone, or any chat app.
Python agentagent-osaiai-agentsai-partnerchatopsclaudeclaude-codecodexdevtoolsdiscord-botlark-botllmlocal-firstlong-term-memoryopencodeslack-botvibe-codingvibeywechat
"""Matching and diff helpers for ``edit`` (C-7 section 4).
Ported from Pi ``packages/coding-agent/src/core/tools/edit-diff.ts`` (MIT,
Copyright (c) 2025 Mario Zechner): exact match first, then Pi's deterministic
normalized match with no similarity threshold, every edit matched against the
original, overlapping edits rejected. Avibe adds ``replaceAll`` (from Claude
Code and OpenCode): every occurrence in the tier that matched, each one a span
for the overlap check. Avibe departures from Pi: each edit matches in its own
tier, so one normalized edit no longer switches the whole batch to normalized
text; uniqueness is counted in the tier that matched; and the file is never
re-encoded: replacements are spliced into the original text, so its BOM, its
line breaks (mixed or not), and every byte outside a replaced span survive.
Avibe additions to Pi's messages: an ambiguous match lists where its first
occurrences are, and a successful edit reports the lines it rewrote, so the
model need not read the file again to disambiguate or to inspect the result.
"""
from __future__ import annotations
import bisect
import difflib
import re
import unicodedata
from dataclasses import dataclass
from typing import Callable, Optional
from core.agent_core.tools.shell import SHELL_ADVICE
from core.agent_core.tools.text import shown
BOM = "\ufeff"
# JavaScript's String.prototype.trimEnd set, which Pi uses (Python's isspace differs).
_BREAK = re.compile(r"\r\n|\r|\n")
_TRAILING_SPACE = " \t\v\f\r\u00a0\u1680" + "".join(map(chr, range(0x2000, 0x200B))) + "\u2028\u2029\u202f\u205f\u3000\ufeff"
_SMART_SINGLE = re.compile("[\u2018\u2019\u201a\u201b]")
_SMART_DOUBLE = re.compile("[\u201c\u201d\u201e\u201f]")
_DASHES = re.compile("[\u2010\u2011\u2012\u2013\u2014\u2015\u2212]")
_SPECIAL_SPACES = re.compile("[\u00a0\u2002-\u200a\u202f\u205f\u3000]")
class EditError(ValueError):
"""An edit that cannot be applied; the message is the model-facing error."""
@dataclass(frozen=True)
class Edit:
old_text: str
new_text: str
replace_all: bool = False
@dataclass(frozen=True)
class _Replacement:
edit_index: int
index: int
length: int
new_text: str
def normalize_to_lf(text: str) -> str:
return text.replace("\r\n", "\n").replace("\r", "\n")
def normalize_for_fuzzy_match(text: str) -> str:
"""NFKC, no trailing whitespace per line, ASCII quotes and dashes, plain spaces."""
return _after_nfkc(unicodedata.normalize("NFKC", text))
def _bounded_fuzzy_view(view: str) -> Optional[str]:
"""``normalize_for_fuzzy_match(view)``, or ``None`` once NFKC would grow it by more than
``MAX_FUZZY_GROWTH_CHARS`` (one character can become eighteen, U+FDFA).
Text already in NFKC is not normalized again. Otherwise it is normalized a chunk at a time, each
chunk ending just before an ASCII character (NFKC never composes anything with a following ASCII
character, so the pieces join to the whole), stopping at the budget. A run of more than
``2 * _NFKC_CHUNK_CHARS`` characters without an ASCII character that is not in NFKC stops it too.
"""
if unicodedata.is_normalized("NFKC", view):
return _after_nfkc(view)
budget = len(view) + MAX_FUZZY_GROWTH_CHARS
pieces: list[str] = []
total = pos = 0
while pos < len(view):
boundary = _ASCII.search(view, pos + _NFKC_CHUNK_CHARS)
end = boundary.start() if boundary else len(view)
chunk = view[pos:end]
if len(chunk) > 2 * _NFKC_CHUNK_CHARS and not unicodedata.is_normalized("NFKC", chunk):
return None
piece = unicodedata.normalize("NFKC", chunk)
total += len(piece)
if total > budget:
return None
pieces.append(piece)
pos = end
return _after_nfkc("".join(pieces))
def _after_nfkc(text: str) -> str:
# rstrip, not a "[...]+$" regex: the regex retries at every space of a run, which is quadratic.
text = "\n".join(line.rstrip(_TRAILING_SPACE) for line in text.split("\n"))
text = _SMART_SINGLE.sub("'", text)
text = _SMART_DOUBLE.sub('"', text)
text = _DASHES.sub("-", text)
return _SPECIAL_SPACES.sub(" ", text)
#: Avibe: replacements one edit call may make. Each costs a few hundred bytes of Python objects in
#: the process every Session shares, so the budget is structural, not only the file's size.
MAX_REPLACEMENTS = 10_000
#: Avibe: NFKC can make one character eighteen (U+FDFA), and Pi normalizes the whole file. Loose
#: matching stops once normalization has grown the file by this many characters.
MAX_FUZZY_GROWTH_CHARS = 1024 * 1024
_NFKC_CHUNK_CHARS = 64 * 1024
_ASCII = re.compile("[\x00-\x7f]")
#: Avibe: every edit scans the whole file (``str.count``/``find``, which hold the GIL), so the number of
#: edits times the file's length is bounded: about a quarter second of scanning.
MAX_EDIT_SCAN_CHARS = 256 * 1024 * 1024
#: Avibe: occurrences an ambiguous match lists, and rewritten line ranges a result lists, before "...and N more".
MAX_LOCATIONS = 5
#: Avibe: characters of a long line an occurrence's excerpt shows.
EXCERPT_CHARS = 100
class ResultTooLarge(Exception):
"""The edits insert more text than the result may hold; nothing was built."""
def _find_all(content: str, needle: str, limit: int) -> list[int]:
found: list[int] = []
index = content.find(needle)
while index != -1 and len(found) < limit:
found.append(index)
index = content.find(needle, index + len(needle))
return found
def _tier(content: str, normalized: Callable[[], Optional[str]], old_text: str) -> tuple[bool, str, str, int]:
"""``(used_normalized, haystack, needle, count)`` for the first tier where the edit occurs.
Counting is ``str.count`` (no objects per match), so a match budget holds before any are built.
Text that normalizes to nothing never matches in the normalized tier.
"""
count = content.count(old_text)
if count:
return False, content, old_text, count
if old_text.count("\n") > content.count("\n"):
# Normalization keeps every line break (NFKC never makes or removes "\n"), so text with more lines
# than the file cannot match; and model text that long is never split into per-line objects.
return True, "", "", 0
haystack = normalized()
if haystack is None:
return True, "", "", 0
fuzzy_old = normalize_for_fuzzy_match(old_text)
if not fuzzy_old:
return True, "", "", 0
return True, haystack, fuzzy_old, haystack.count(fuzzy_old)
def _not_found(path: str, index: int, total: int, *, loose_unavailable: bool = False) -> EditError:
# Avibe addition: say when the normalized tier could not be tried, rather than that it found nothing.
end = ": the file is too large to match it loosely after Unicode normalization." if loose_unavailable else "."
if total == 1:
return EditError(
f"Could not find the exact text in {path}. The old text must match exactly including all whitespace "
f"and newlines{end}"
)
return EditError(
f"Could not find edits[{index}] in {path}. The oldText must match exactly including all whitespace and "
f"newlines{end}"
)
def _duplicate(path: str, index: int, total: int, occurrences: int, locations: list[str]) -> EditError:
# Avibe addition: where the first occurrences are, so the model can add context without reading the file.
shown_locations = "\n".join(locations)
if occurrences > len(locations):
shown_locations += f"\n...and {occurrences - len(locations)} more"
if total == 1:
return EditError(
f"Found {occurrences} occurrences of the text in {path}. The text must be unique. Please provide more "
f"context to make it unique. Occurrences:\n{shown_locations}"
)
return EditError(
f"Found {occurrences} occurrences of edits[{index}] in {path}. Each oldText must be unique. Please provide "
f"more context to make it unique. Occurrences:\n{shown_locations}"
)
def _too_many(path: str, total: int, replacements: int) -> EditError:
if total == 1:
return EditError(
f"Found {replacements} occurrences of the text in {path}, over the {MAX_REPLACEMENTS} replaceAll limit. "
f"Use {SHELL_ADVICE} to replace this many."
)
return EditError(
f"The edits make {replacements} replacements in {path}, over the {MAX_REPLACEMENTS} replacement limit. "
f"Use {SHELL_ADVICE} to replace this many."
)
def _too_much_work(path: str, total: int, chars: int) -> EditError:
return EditError(
f"{total} edits over {path} ({chars} characters) are over the edit work limit. Split them into several "
f"edit calls, or use {SHELL_ADVICE}."
)
def _empty(path: str, index: int, total: int) -> EditError:
if total == 1:
return EditError(f"oldText must not be empty in {path}.")
return EditError(f"edits[{index}].oldText must not be empty in {path}.")
def _no_change(path: str, total: int) -> EditError:
if total == 1:
return EditError(
f"No changes made to {path}. The replacement produced identical content. This might indicate an issue "
"with special characters or the text not existing as expected."
)
return EditError(f"No changes made to {path}. The replacements produced identical content.")
def _apply(content: str, replacements: list[_Replacement], offset: int = 0) -> str:
"""``content`` with disjoint ``replacements`` applied, in one pass (``replaceAll`` can make thousands)."""
pieces: list[str] = []
pos = 0
for replacement in sorted(replacements, key=lambda r: r.index):
start = replacement.index - offset
pieces += (content[pos:start], replacement.new_text)
pos = start + replacement.length
pieces.append(content[pos:])
return "".join(pieces)
def _line_starts(text: str) -> list[int]:
starts = [0]
index = text.find("\n")
while index != -1:
starts.append(index + 1)
index = text.find("\n", index + 1)
return starts
def _overlap(path: str, first: int, second: int) -> EditError:
return EditError(
f"edits[{first}] and edits[{second}] overlap in {path}. Merge them into one edit or target disjoint regions."
)
def _check_disjoint(path: str, replacements: list[_Replacement]) -> None:
ordered = sorted(replacements, key=lambda r: r.index)
for previous, current in zip(ordered, ordered[1:]):
if previous.index + previous.length > current.index:
raise _overlap(path, previous.edit_index, current.edit_index)
class _Lines:
"""The file as lines with their own breaks, and an LF view whose offsets map back to the file.
Matching happens in the view, as Pi matches LF-normalized text; replacements are spliced into
the original, so a BOM, every line break, and every byte outside a replaced span stay as they were.
"""
def __init__(self, text: str) -> None:
self.text = text
prefix = len(BOM) if text.startswith(BOM) else 0
self.contents: list[str] = []
self.breaks: list[str] = []
pos = prefix
for match in _BREAK.finditer(text, prefix):
self.contents.append(text[pos : match.start()])
self.breaks.append(match.group())
pos = match.end()
self.contents.append(text[pos:])
self.breaks.append("")
self.first_break = next((brk for brk in self.breaks if brk), "\n")
# What read showed (one U+FFFD per undecodable byte), one character for one, so offsets map back.
self.view = shown("\n".join(self.contents))
self.view_starts: list[int] = []
self.starts: list[int] = []
view_pos, pos = 0, prefix
for content, brk in zip(self.contents, self.breaks):
self.view_starts.append(view_pos)
self.starts.append(pos)
view_pos += len(content) + 1
pos += len(content) + len(brk)
def read_line(self, line: int) -> int:
"""The number ``read`` shows for ``line``: read breaks lines only at ``\\n``, not at a lone ``\\r``."""
return self.text.count("\n", 0, self.starts[line]) + 1
def to_original(self, view_offset: int) -> int:
line = bisect.bisect_right(self.view_starts, view_offset) - 1
return self.starts[line] + (view_offset - self.view_starts[line])
def line_end(self, line: int) -> int:
"""Original offset just past ``line`` and its break."""
return self.starts[line + 1] if line + 1 < len(self.starts) else len(self.text)
def break_for(self, lo: int) -> str:
"""The break new text uses for a span starting at ``lo``: the first break at or after it (inside the
span, else the one after it), which is the break of ``lo``'s line, else the file's first."""
return self.breaks[bisect.bisect_right(self.starts, lo) - 1] or self.first_break
def with_breaks(self, lf_text: str, lo: int, own: Optional[list[str]] = None) -> str:
"""``lf_text`` with each ``\\n`` turned into ``own``'s next break, then the break of the span at ``lo``.
Only the ``\\n`` that ``own`` covers (lines of the file) are visited one by one; the rest, which
model text can make many of, are replaced in one pass without per-line objects.
"""
if "\n" not in lf_text:
return lf_text
fallback = self.break_for(lo)
out: list[str] = []
pos = 0
for brk in own or ():
newline = lf_text.find("\n", pos)
if newline == -1:
break
out += (lf_text[pos:newline], brk or fallback)
pos = newline + 1
rest = lf_text[pos:]
out.append(rest if fallback == "\n" else rest.replace("\n", fallback))
return "".join(out)
def _excerpt(line: str, column: int) -> str:
"""``line`` without its indentation; a long line only around ``column``."""
if len(line) <= EXCERPT_CHARS:
return line.strip()
start = max(0, min(column - EXCERPT_CHARS // 4, len(line) - EXCERPT_CHARS))
end = start + EXCERPT_CHARS
return ("..." if start else "") + line[start:end].strip() + ("..." if end < len(line) else "")
def _original_column(line: str, column: int) -> int:
"""The column of ``line`` where its normalized form reaches ``column``.
Only NFKC changes lengths (the substitutions after it are one character for one, and trailing
whitespace is stripped after any occurrence). The line is measured in pieces ending just before an
ASCII character, as ``_bounded_fuzzy_view`` normalizes it; a piece already in NFKC maps one for
one, and the piece that changes length across ``column`` is bisected. ``_bounded_fuzzy_view``
gave up on any longer run that is not in NFKC, so a bisected piece stays bounded.
"""
pos = reached = 0
while pos < len(line):
boundary = _ASCII.search(line, pos + _NFKC_CHUNK_CHARS)
end = boundary.start() if boundary else len(line)
piece = line[pos:end]
if unicodedata.is_normalized("NFKC", piece):
if reached + len(piece) > column:
return pos + column - reached
reached += len(piece)
else:
grown = len(unicodedata.normalize("NFKC", piece))
if reached + grown > column:
lo, hi = 0, len(piece)
while lo < hi:
mid = (lo + hi + 1) // 2
if len(unicodedata.normalize("NFKC", piece[:mid])) <= column - reached:
lo = mid
else:
hi = mid - 1
return pos + lo
reached += grown
pos = end
return len(line)
def _locations(lines: _Lines, haystack: str, needle: str, normalized: bool) -> list[str]:
"""``line {n}: {excerpt}`` for the first ``MAX_LOCATIONS`` occurrences of ``needle`` in ``haystack``.
``haystack`` is the view or, when ``normalized``, its normalized form, which keeps every line break,
so its line ``i`` is the view's line ``i``; the excerpt is that line of the view, as read showed it.
"""
out = []
for at in _find_all(haystack, needle, MAX_LOCATIONS):
line = haystack.count("\n", 0, at)
column = at - (haystack.rfind("\n", 0, at) + 1)
start = lines.view_starts[line]
text = lines.view[start : start + len(lines.contents[line])]
if normalized and len(text) > EXCERPT_CHARS:
# A column of the normalized form; NFKC may have changed lengths earlier on the line.
column = _original_column(text, column)
out.append(f"line {lines.read_line(line)}: {_excerpt(text, column)}")
return out
def _common_prefix(a: str, b: str, limit: int) -> int:
"""How many leading characters, up to ``limit``, ``a`` and ``b`` share; bisected over slice
comparisons, so a long shared run costs a few memcmp calls rather than a loop per character."""
lo, hi = 0, limit
while lo < hi:
mid = (lo + hi + 1) // 2
if a[:mid] == b[:mid]:
lo = mid
else:
hi = mid - 1
return lo
def _common_suffix(a: str, b: str, limit: int) -> int:
"""How many trailing characters, up to ``limit``, ``a`` and ``b`` share."""
lo, hi = 0, limit
while lo < hi:
mid = (lo + hi + 1) // 2
if a[len(a) - mid :] == b[len(b) - mid :]:
lo = mid
else:
hi = mid - 1
return lo
def _changed_lines(text: str, replacements: list[_Replacement]) -> tuple[tuple[int, int], ...]:
"""The new file's rewritten line ranges, numbered as ``read`` shows them; touching ranges merge.
A replacement counts from its first to its last character that differs from the text it replaced,
so unchanged context at either end of an edit, or a replacement that changed nothing, is not
reported. This says where to look, not a line diff: an unchanged line between two changes of one
edit is part of its range.
"""
ranges: list[tuple[int, int]] = []
line, pos = 1, 0
for replacement in sorted(replacements, key=lambda r: r.index):
line += text.count("\n", pos, replacement.index)
old, new = text[replacement.index : replacement.index + replacement.length], replacement.new_text
if old != new:
head = _common_prefix(old, new, min(len(old), len(new)))
tail = _common_suffix(old, new, min(len(old), len(new)) - head)
first = line + new.count("\n", 0, head)
# The last differing character's line; a removal marks the line it was removed from.
last = line + new.count("\n", 0, len(new) - tail - 1) if len(new) - tail > head else first
if ranges and first <= ranges[-1][1] + 1:
ranges[-1] = (ranges[-1][0], max(ranges[-1][1], last))
else:
ranges.append((first, last))
line += new.count("\n")
pos = replacement.index + replacement.length
return tuple(ranges)
def _exact_replacements(lines: _Lines, matches: list[_Replacement]) -> list[_Replacement]:
out = []
for match in matches:
lo, hi = lines.to_original(match.index), lines.to_original(match.index + match.length)
out.append(_Replacement(match.edit_index, lo, hi - lo, lines.with_breaks(match.new_text, lo)))
return out
def _line_groups(lines: _Lines, normalized: str, matches: list[_Replacement]) -> list[_Replacement]:
"""Normalized-tier matches as whole-line replacements of the original.
Normalization keeps every line break, so line ``i`` of the normalized view is line ``i`` of the
file. Matches sharing lines form one group; its lines are rewritten from the normalized view with
the group's matches applied (Pi's way of applying a normalized match), and each rewritten line
keeps its own break.
"""
normalized_starts = _line_starts(normalized)
if len(normalized_starts) != len(lines.starts):
raise EditError("Cannot preserve unchanged lines because the base content has a different line count.")
def line_of(index: int) -> int:
return bisect.bisect_right(normalized_starts, index) - 1
groups: list[list] = [] # [first_line, last_line, members]
for match in sorted(matches, key=lambda r: r.index):
first, last = line_of(match.index), line_of(match.index + match.length - 1)
if groups and first <= groups[-1][1]:
groups[-1][1] = max(groups[-1][1], last)
groups[-1][2].append(match)
else:
groups.append([first, last, [match]])
out: list[_Replacement] = []
for first, last, members in groups:
lo = normalized_starts[first]
hi = normalized_starts[last + 1] if last + 1 < len(normalized_starts) else len(normalized)
rewritten = _apply(normalized[lo:hi], members, lo)
original_lo, original_hi = lines.starts[first], lines.line_end(last)
text = lines.with_breaks(rewritten, original_lo, lines.breaks[first : last + 1])
out.append(_Replacement(members[0].edit_index, original_lo, original_hi - original_lo, text))
return out
def apply_edits(
text: str, edits: list[Edit], path: str, max_result_chars: Optional[int] = None
) -> tuple[str, str, str, tuple[int, ...], tuple[tuple[int, int], ...]]:
"""Apply every edit to the file's ``text``; return the new text, the LF views before and after, how
many occurrences each edit replaced, and the new text's rewritten line ranges (``read``'s numbers).
Each edit matches in its own tier against the original, in the file's LF view. Exact edits replace
exactly the text they matched; a normalized edit rewrites the whole lines it touches from the
normalized view. Uniqueness, ``replaceAll``, overlap, and application are all per edit in original
coordinates, and replacements are spliced into the original, so nothing an edit did not match
changes: not a byte, not a line break. (Pi matches a whole batch in normalized text as soon as one
edit needs it, and re-encodes every line break in the file.)
"""
edits = [Edit(normalize_to_lf(e.old_text), normalize_to_lf(e.new_text), e.replace_all) for e in edits]
total = len(edits)
for index, edit in enumerate(edits):
if not edit.old_text:
raise _empty(path, index, total)
if total * len(text) > MAX_EDIT_SCAN_CHARS:
raise _too_much_work(path, total, len(text))
lines = _Lines(text)
cache: list[Optional[str]] = []
def normalized() -> Optional[str]:
"""The normalized view, or ``None`` when NFKC would grow it past its budget (no loose matching)."""
if not cache:
cache.append(_bounded_fuzzy_view(lines.view))
return cache[0]
exact: list[_Replacement] = []
fuzzy: list[_Replacement] = []
inserted = 0
replacements_made = 0
counts: list[int] = []
for index, edit in enumerate(edits):
used_normalized, haystack, needle, count = _tier(lines.view, normalized, edit.old_text)
if not count:
raise _not_found(path, index, total, loose_unavailable=bool(cache) and cache[0] is None)
if not edit.replace_all and count > 1:
raise _duplicate(path, index, total, count, _locations(lines, haystack, needle, used_normalized))
replacements_made += count
counts.append(count)
if replacements_made > MAX_REPLACEMENTS:
raise _too_many(path, total, replacements_made)
# The result holds at least every inserted text, so this bound never refuses a result that fits.
inserted += count * len(edit.new_text)
if max_result_chars is not None and inserted > max_result_chars:
raise ResultTooLarge()
(fuzzy if used_normalized else exact).extend(
_Replacement(index, at, len(needle), edit.new_text) for at in _find_all(haystack, needle, count)
)
_check_disjoint(path, fuzzy) # in normalized coordinates, before they are grouped into lines
replacements = _exact_replacements(lines, exact) + (_line_groups(lines, normalized() or "", fuzzy) if fuzzy else [])
_check_disjoint(path, replacements)
new_text = _apply(text, replacements)
if new_text == text:
raise _no_change(path, total)
return new_text, lines.view, lf_view(new_text), tuple(counts), _changed_lines(text, replacements)
def lf_view(text: str) -> str:
"""``_Lines(text).view`` without per-line state: the result may have far more lines than the file."""
return shown(normalize_to_lf(text[len(BOM) :] if text.startswith(BOM) else text))
#: The display diff runs difflib only on the changed middle, and skips it when either side is longer.
DIFF_MAX_LINES = 1000
#: Lines either side may have before the display diff splits it into per-line strings at all.
DIFF_MAX_SPLIT_LINES = 100_000
class _FixedOpcodes(difflib.SequenceMatcher):
"""difflib's hunk grouping over opcodes computed elsewhere."""
def __init__(self, opcodes: list[tuple[str, int, int, int, int]]) -> None:
super().__init__(None, (), ()) # no sequences: difflib would index all of b, which grouping never uses
self._fixed = opcodes
def get_opcodes(self) -> list[tuple[str, int, int, int, int]]: # type: ignore[override]
return self._fixed
def display_diff(path: str, old: str, new: str, context: int = 4) -> Optional[tuple[str, Optional[int], str]]:
"""``(diff, first_changed_line, patch)`` for display, or ``None`` when the change is too large to show.
The common leading and trailing lines are trimmed in linear time, and difflib (whose worst case
is quadratic, for example on repetitive lines) sees only the changed middle, at most
``DIFF_MAX_LINES`` lines on each side.
"""
if old.count("\n") > DIFF_MAX_SPLIT_LINES or new.count("\n") > DIFF_MAX_SPLIT_LINES:
return None
a, b = _lines_with_breaks(old), _lines_with_breaks(new)
shortest = min(len(a), len(b))
prefix = 0
while prefix < shortest and a[prefix] == b[prefix]:
prefix += 1
suffix = 0
while suffix < shortest - prefix and a[len(a) - 1 - suffix] == b[len(b) - 1 - suffix]:
suffix += 1
a_end, b_end = len(a) - suffix, len(b) - suffix
if a_end - prefix > DIFF_MAX_LINES or b_end - prefix > DIFF_MAX_LINES:
return None
middle = difflib.SequenceMatcher(None, a[prefix:a_end], b[prefix:b_end]).get_opcodes()
opcodes = [("equal", 0, prefix, 0, prefix)] if prefix else []
opcodes += [(tag, i1 + prefix, i2 + prefix, j1 + prefix, j2 + prefix) for tag, i1, i2, j1, j2 in middle]
if suffix:
opcodes.append(("equal", a_end, len(a), b_end, len(b)))
diff, first_changed_line = _render_diff(a, b, opcodes, context)
return diff, first_changed_line, _unified_patch(path, a, b, opcodes, context)
def _lines_with_breaks(text: str) -> list[str]:
"""Lines split at "\n" only, each keeping its "\n", as jsdiff's ``diffLines`` sees them.
``str.splitlines`` also breaks at \f, \v, \x1c-\x1e, \x85, U+2028 and U+2029, which are not line
breaks for ``read``, ``edit``, or a patch.
"""
lines = [line + "\n" for line in text.split("\n")]
lines[-1] = lines[-1][:-1]
return lines if lines[-1] else lines[:-1]
def _unified_range(start: int, stop: int) -> str:
"""jsdiff's hunk range: always ``start,count``, and an empty range starts at the line before."""
length = stop - start
return f"{start + 1 if length else start},{length}"
def _patch_line(sign: str, line: str) -> str:
# Only a file's last line can lack "\n"; jsdiff marks it the way GNU diff does.
return sign + line if line.endswith("\n") else f"{sign}{line}\n\\ No newline at end of file\n"
def _unified_patch(path: str, a: list[str], b: list[str], opcodes: list, context: int) -> str:
"""Pi's ``generateUnifiedPatch`` (jsdiff ``createTwoFilesPatch`` with file headers only), from the opcodes."""
groups = list(_FixedOpcodes(opcodes).get_grouped_opcodes(context))
if not groups:
return ""
out = [f"--- {path}\n", f"+++ {path}\n"]
for group in groups:
first, last = group[0], group[-1]
out.append(f"@@ -{_unified_range(first[1], last[2])} +{_unified_range(first[3], last[4])} @@\n")
for tag, i1, i2, j1, j2 in group:
if tag == "equal":
out.extend(_patch_line(" ", line) for line in a[i1:i2])
continue
out.extend(_patch_line("-", line) for line in a[i1:i2])
out.extend(_patch_line("+", line) for line in b[j1:j2])
return "".join(out)
def _split_count(lines: list[str]) -> int:
return len(lines) + (0 if lines and not lines[-1].endswith("\n") else 1)
def _render_diff(a: list[str], b: list[str], opcodes: list, context: int) -> tuple[str, Optional[int]]:
"""Pi's display diff: numbered ``+``/``-``/context lines and the first changed line of the new file."""
# Pi's width: the digits of the larger ``split("\n")`` count, which has one more entry after a final "\n".
width = len(str(max(_split_count(a), _split_count(b))))
a = [line[:-1] if line.endswith("\n") else line for line in a]
b = [line[:-1] if line.endswith("\n") else line for line in b]
parts: list[tuple[str, list[str]]] = []
for tag, i1, i2, j1, j2 in opcodes:
if tag == "equal":
parts.append((" ", a[i1:i2]))
continue
if i2 > i1:
parts.append(("-", a[i1:i2]))
if j2 > j1:
parts.append(("+", b[j1:j2]))
out: list[str] = []
old_no = new_no = 1
last_was_change = False
first_changed: Optional[int] = None
blank = " " * width
for position, (kind, raw) in enumerate(parts):
if kind != " ":
if first_changed is None:
first_changed = new_no
for line in raw:
if kind == "+":
out.append(f"+{str(new_no).rjust(width)} {line}")
new_no += 1
else:
out.append(f"-{str(old_no).rjust(width)} {line}")
old_no += 1
last_was_change = True
continue
trailing = position < len(parts) - 1 and parts[position + 1][0] != " "
if last_was_change and trailing and len(raw) > context * 2:
shown_head, skipped, shown_tail = raw[:context], len(raw) - 2 * context, raw[-context:]
elif last_was_change and trailing:
shown_head, skipped, shown_tail = raw, 0, []
elif last_was_change:
shown_head, skipped, shown_tail = raw[:context], max(0, len(raw) - context), []
elif trailing:
skipped = max(0, len(raw) - context)
shown_head, shown_tail = [], raw[skipped:]
else:
shown_head, skipped, shown_tail = [], len(raw), []
old_no += skipped
new_no += skipped
last_was_change = False
continue
for line in shown_head:
out.append(f" {str(old_no).rjust(width)} {line}")
old_no += 1
new_no += 1
if skipped:
out.append(f" {blank} ...")
old_no += skipped
new_no += skipped
for line in shown_tail:
out.append(f" {str(old_no).rjust(width)} {line}")
old_no += 1
new_no += 1
last_was_change = False
return "\n".join(out), first_changed