Skip to content

diff 实验性 ​

实验性

本包属于实验层:任何 0.x 版本都可能有不兼容变化(docs/release.md「API 稳定性」)。

go
import "github.com/tangoui/tango/ui/editor/diff"

本页由 site/tools/apigen 从源码注释生成(按 darwin 构建标签选文件),请勿手改。

Package diff computes line and character level differences between two versions of a text, the algorithm layer behind a diff editor.

The design follows Monaco's DefaultLinesDiffComputer (monaco-editor 0.56): lines are hashed after trimming, aligned with a dynamic-programming LCS for small inputs or Myers' O(ND) algorithm for large ones, cleaned up by heuristics (sliding changes to indentation and word boundaries, joining changes split by trivial matches), and every changed block is refined to character ranges. Moved blocks can optionally be detected. A Timeout or a cancelled context stops the work early; the result is then still a valid (coarser) diff and TimedOut is set.

Lines and columns are 0-based. Columns are UTF-8 byte columns, like buffer.Pos; character-level refinement works on runes so ranges never split a UTF-8 sequence. Line ranges are half-open.

Lines diffs two slices of lines (see SplitLines); Snapshots diffs two buffer.Snapshots. The result also answers the questions a diff view asks: which unchanged regions can be folded (UnchangedRegions) and where the previous or next change is (Result.Prev, Result.Next). The functions keep no state and may run on any goroutine, typically a background one with a context that is cancelled when either text changes again.

Experimental: this package's API may change in any 0.x release (see docs/release.md, "API 稳定性").

常量 ​

go
const DefaultTimeout = 5 * time.Second

DefaultTimeout is the time budget Monaco's diff editor uses. Options with a zero Timeout run without a limit; pass DefaultTimeout (or a context with a deadline) when the diff runs for an interactive view.

函数 ​

func SplitLines ​

go
func SplitLines(text string) []string

SplitLines splits text into lines at "\r\n", "\r" and "\n", like buffer.NewSnapshot. Text ending in a line break ends with an empty line; empty text is one empty line.

类型 ​

Change ​

go
type Change struct {
	Original, Modified LineRange
	Inner              []RangeMapping
}

Change is one changed block: lines Original of the original text became lines Modified of the modified text. One of the ranges is empty for a pure insertion or deletion. Inner lists the character-level replacements inside the block, in order; it is never empty for changes produced by this package.

func (Change) String ​

go
func (c Change) String() string

LineRange ​

go
type LineRange struct{ Start, End int }

LineRange is the half-open range [Start, End) of 0-based line indices. An empty range marks the position between line Start-1 and line Start.

func (LineRange) Contains ​

go
func (r LineRange) Contains(line int) bool

Contains reports whether line lies in r.

func (LineRange) Empty ​

go
func (r LineRange) Empty() bool

Empty reports whether r contains no line.

func (LineRange) Len ​

go
func (r LineRange) Len() int

Len returns the number of lines in r.

func (LineRange) String ​

go
func (r LineRange) String() string

Move ​

go
type Move struct {
	Original, Modified LineRange
	Changes            []Change
}

Move is a block of lines that moved: Original and Modified have similar (not necessarily identical) text. Changes lists the differences between the two copies, in the coordinates of the whole texts. The lines of a move are also covered by Result.Changes, which describe them as a deletion and an insertion.

Options ​

go
type Options struct {
	// IgnoreTrimWhitespace treats lines that differ only in leading or
	// trailing whitespace as equal, and leaves such whitespace out of
	// character ranges.
	IgnoreTrimWhitespace bool
	// ComputeMoves detects blocks of lines that were moved (see Move).
	ComputeMoves bool
	// ExtendToSubwords widens character ranges to whole camelCase sub-words
	// when most of the sub-word changed.
	ExtendToSubwords bool
	// Timeout bounds the computation; 0 means no limit. When the budget runs
	// out the remaining work falls back to coarser results and
	// Result.TimedOut is set.
	Timeout time.Duration
}

Options control a diff computation. The zero value compares whitespace, skips move detection and has no time limit.

Range ​

go
type Range struct{ Start, End buffer.Pos }

Range is the text between two positions; End is exclusive. Columns are UTF-8 byte columns into the line (see buffer.Pos).

func (Range) Empty ​

go
func (r Range) Empty() bool

Empty reports whether r selects no text.

func (Range) String ​

go
func (r Range) String() string

RangeMapping ​

go
type RangeMapping struct{ Original, Modified Range }

RangeMapping says that the text at Original was replaced by the text at Modified.

func (RangeMapping) String ​

go
func (m RangeMapping) String() string

Result ​

go
type Result struct {
	// Changes are the changed blocks, sorted and disjoint on both sides,
	// with at least one unchanged line between neighbours.
	Changes []Change
	// Moves are the detected moves, sorted by Original.Start; nil unless
	// Options.ComputeMoves is set.
	Moves []Move
	// Identical reports that the two texts are equal line for line. Changes
	// can be empty while Identical is false, when IgnoreTrimWhitespace hid
	// all differences.
	Identical bool
	// TimedOut reports that the time budget ran out or the context was
	// cancelled. The changes are still correct, but blocks may be larger
	// and character ranges coarser than with more time.
	TimedOut bool
	// OriginalLines and ModifiedLines are the line counts of the two texts
	// (at least 1).
	OriginalLines, ModifiedLines int
}

Result is the outcome of a diff.

func Lines ​

go
func Lines(ctx context.Context, original, modified []string, opts Options) Result

Lines diffs two texts given as lines without line breaks (see SplitLines). An empty slice is treated as one empty line. The context can cancel the computation, which then ends like a timeout.

func Snapshots ​

go
func Snapshots(ctx context.Context, original, modified buffer.Snapshot, opts Options) Result

Snapshots diffs two snapshots. Snapshots store LF-normalised text, so a different line-ending style (Snapshot.EOL) is not a change; compare EOL() separately if that matters.

func (Result) Next ​

go
func (r Result) Next(s Side, line int) (index int, ok bool)

Next returns the index in r.Changes of the first change that starts after line on side s, wrapping around to the first change at the end of the text (and when line is the last line, so that repeated "next" never gets stuck on a change at the very end). ok is false when there are no changes.

func (Result) Prev ​

go
func (r Result) Prev(s Side, line int) (index int, ok bool)

Prev returns the index in r.Changes of the last change that starts before line on side s, wrapping around to the last change. ok is false when there are no changes.

func (Result) UnchangedRegions ​

go
func (r Result) UnchangedRegions(context, minHidden int) []UnchangedRegion

UnchangedRegions is UnchangedRegions for the changes and line counts of r.

Side ​

go
type Side uint8

Side selects one of the two texts of a diff.

go
const (
	Original Side = iota
	Modified
)

The two sides of a diff.

func (Side) String ​

go
func (s Side) String() string

UnchangedRegion ​

go
type UnchangedRegion struct{ Original, Modified LineRange }

UnchangedRegion is a run of unchanged lines that a diff view may fold: Original and Modified have the same length and the same text (up to ignored whitespace).

func UnchangedRegions ​

go
func UnchangedRegions(changes []Change, originalLines, modifiedLines, context, minHidden int) []UnchangedRegion

UnchangedRegions returns the foldable unchanged regions between changes, keeping context unchanged lines visible next to every change. A region is only reported when it would hide at least minHidden lines; regions at the start or end of the texts need context lines on one side only. Monaco's diff editor uses context = 3 and minHidden = 3. originalLines and modifiedLines are the line counts of the texts.

以 MIT 许可发布