Skip to main content

Module annotate_diff

Module annotate_diff 

Source
Expand description

Annotates a unified diff with extra markers for moved and reformatted code, on top of the usual +/- lines, to make large mechanical refactors easier to review.

Marker legend:

  • ' ' context (unchanged)
  • '+' addition (green) — genuinely new content
  • '-' removal (red) — genuinely removed content
  • '>' moved out (cyan) — this line left its old spot
  • '<' moved in (blue) — this line arrived from elsewhere
  • 'x' reformat (magenta) — same content, only whitespace/layout changed; only the new side is shown (the old side would be a whitespace-only duplicate, so it’s dropped rather than printed twice)

Detection runs in three passes over each hunk’s “change blocks” (a run of consecutive removed lines immediately followed by a run of consecutive added lines):

  1. Whole-block reformat: if a block’s removed lines and added lines are identical once all whitespace is stripped, the entire block is a pure reflow (handles reindentation and a linter wrapping/joining lines) and gets x.
  2. Global move detection: lines left over after pass 1 are matched, whitespace-normalized, against lines in other blocks (any file). A match seeds an anchor that is then extended line-by-line in both directions within its block; forward extension tolerates a small reflow window (so a moved line that was also split/joined still counts as moved) and a configurable number of lines that differ for real — an edit made during the move — before giving up. The resulting run is only accepted as a move once it totals at least MIN_MOVE_ALNUM_CHARS alphanumeric characters — the same rule git diff --color-moved’s blocks/zebra modes use — otherwise it’s left as plain +/- and the next matching candidate (if any) is tried instead.
  3. Local leftover reconciliation: whatever remains unconsumed within a single block is matched against the rest of that same block (whitespace-normalized) to catch an in-place reformat sitting right next to a move or a genuine addition. A match can be a single line or a run of 2+ consecutive removed lines whose text, line-by-line, equals 2+ consecutive added lines (a run never skips over an unrelated line on either side to stay alive). Two matches that cross — an earlier removed line pairing with a later added line than a later removed line does — are both rejected, since that describes the lines swapping order, a real logic change, not a reformat. Anything still unpaired is a plain addition/removal.

Enums§

MoveGap
How many real content changes are tolerated inside an otherwise-moved block before move detection gives up extending into that block (lines beyond this fall back to plain +/-).

Functions§

render
Render diff_text (as produced by git diff) with move/reformat annotations.