worddiff

package
v0.17.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Apr 12, 2026 License: MIT Imports: 2 Imported by: 0

Documentation

Overview

Package worddiff provides intra-line word-diff algorithms and a shared text-range highlight insertion engine. It owns tokenization, LCS computation, byte-offset range building, similarity gating, and line pairing for add/remove diff blocks.

The public API is exposed as methods on the stateless Differ type, enabling consumer-side interface wrapping in the ui package (same pattern as style.SGR).

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Differ

type Differ struct{}

Differ provides intra-line word-diff algorithms and highlight marker insertion. stateless — the receiver carries no mutable state. exists to group related methods under a type for consumer-side interface wrapping (same pattern as style.SGR).

func New

func New() *Differ

New returns a Differ. the constructor exists for consistency with the DI pattern (main.go calls worddiff.New() and injects into ModelConfig).

func (*Differ) ComputeIntraRanges

func (d *Differ) ComputeIntraRanges(minusLine, plusLine string) ([]Range, []Range)

ComputeIntraRanges computes changed byte-offset ranges for a pair of minus/plus lines. returns ranges for the minus line and plus line respectively. returns nil ranges if either line is empty, exceeds maxLineLenForDiff, or fails the similarity gate (< 30% common non-whitespace tokens).

func (*Differ) InsertHighlightMarkers

func (d *Differ) InsertHighlightMarkers(s string, matches []Range, hlOn, hlOff string) string

InsertHighlightMarkers walks the string inserting hlOn/hlOff ANSI sequences at match positions, skipping over existing ANSI escape sequences to preserve them. tracks background ANSI state so that match-end restores to the correct bg (e.g. word-diff bg) rather than always using the static hlOff (line bg). when the input has no bg sequences (word-diff caller), restoreBg stays at hlOff and behavior is unchanged.

func (*Differ) PairLines

func (d *Differ) PairLines(lines []LinePair) []Pair

PairLines pairs remove and add lines within a contiguous change block. equal-length runs pair 1:1 in order. unequal runs use greedy best-match scoring. the indices in the returned Pair values are indices into the input lines slice.

type LinePair

type LinePair struct {
	Content  string
	IsRemove bool
}

LinePair represents a line of content with its change direction for pairing.

type Pair

type Pair struct {
	RemoveIdx int
	AddIdx    int
}

Pair represents a matched pair of remove/add line indices for intra-line diffing.

type Range

type Range struct {
	Start int // byte offset of range start
	End   int // byte offset past last byte
}

Range represents a byte-offset range in a text line. used for both intra-line word-diff highlighting and search match highlighting.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL