Documentation
¶
Overview ¶
Package diff computes Myers line diffs and unified-diff hunks.
It replaces the original `pi-diff` crate, which the TypeScript side reached through a NAPI binding. The run structure is a faithful port of the original's O(ND) implementation, including its tie-breaking, so a diff produced here matches one produced there token for token.
Index ¶
Constants ¶
const DefaultContextLines = 3
DefaultContextLines is the context either side of a change when the caller does not choose. It matches git's default.
const NoNewlineMarker = `\ No newline at end of file`
NoNewlineMarker is the line unified diff emits after a row that ended without a trailing newline.
Variables ¶
This section is empty.
Functions ¶
func Intern ¶
Intern maps tokens to dense ids under exact equality.
Interning is what makes the comparison O(1) per token: the algorithm works on integers, and a generic token would be compared with a hash per step.
func LineTokens ¶
LineTokens splits text into jsdiff-compatible line tokens.
The trailing newline stays with its line, so joining the tokens reproduces the input exactly — including a final line that lacks one.
Go's SplitAfter emits a trailing empty element for a text ending in a newline, where the original's split_inclusive does not. That empty token would become a phantom context line, so it is dropped here: the difference is a hunk header claiming one more line than the file has.
Types ¶
type Change ¶
type Change struct {
// Value is the joined token text for this run.
Value string
// Count is the number of tokens in this run.
Count int
// Added is true when the run exists only in the new input.
Added bool
// Removed is true when the run exists only in the old input.
Removed bool
}
Change is one run mapped back to its text.
func BuildChanges ¶
BuildChanges maps runs back to values.
Common runs take their text from the new tokens, because the two texts agree there and only the new one carries the version that was kept.
type Hunk ¶
type Hunk struct {
// OldStart is the 1-based first line in the old text.
OldStart int
// OldLines counts old-text lines covered.
OldLines int
// NewStart is the 1-based first line in the new text.
NewStart int
// NewLines counts new-text lines covered.
NewLines int
// Lines are the body rows.
Lines []HunkLine
}
Hunk is one contiguous region of a unified diff.
Starts are 1-based and lengths count the lines of each text the hunk covers, matching the `@@ -old_start,old_lines +new_start,new_lines @@` header.
func HunksFromTokens ¶
HunksFromTokens builds hunks from precomputed line tokens and runs.
Splitting once and reusing the tokens is what lets a caller render the same diff twice without re-tokenising.
func StructuredHunks ¶
StructuredHunks builds unified hunks from two texts.
contextLines is the number of unchanged lines either side of a change; zero uses DefaultContextLines. Two changes within contextLines of each other are merged into one hunk, which is what keeps a scattered edit readable.
type HunkLine ¶
type HunkLine struct {
// Kind is the row's role.
Kind HunkLineKind
// Text is the line content, without its trailing newline.
Text string
}
HunkLine is one row of a unified hunk.
type HunkLineKind ¶
type HunkLineKind string
HunkLineKind is the role of one row in a unified hunk.
const ( // KindContext is " ": the line is in both texts. KindContext HunkLineKind = "context" // KindRemoved is "-": the line is only in the old text. KindRemoved HunkLineKind = "removed" // KindAdded is "+": the line is only in the new text. KindAdded HunkLineKind = "added" // KindNoNewlineAtEof marks a preceding row that ended without a newline. KindNoNewlineAtEof HunkLineKind = "no-newline" )
Hunk row kinds.
type Run ¶
type Run struct {
// Count is the number of tokens in this run.
Count int
// Added is true when the run exists only in the new input.
Added bool
// Removed is true when the run exists only in the old input.
Removed bool
}
Run is one contiguous span of the diff.
Exactly one of Added or Removed is set; a run with neither is a common run present in both inputs.
func Myers ¶
Myers computes the O(ND) diff of two interned token sequences.
The tie-breaking is deliberate and load-bearing: when a removal and an insertion reach equally far, the insertion path is preferred, matching the original. Changing the preference produces a different — still valid — edit script, which is why the run structure is asserted rather than just the resulting text.