Help us shape the future of Go! Take the 2026 Go Developer Survey

diff

package
v0.7.0 Latest Latest
Warning

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

Go to latest
Published: Sep 9, 2026 License: MIT Imports: 2 Imported by: 0

Documentation

Overview

Package diff turns two versions of a file into the whole-file line sequence the Backups preview renders: every line of the file in order, tagged with what turning one version into the other does to it.

It is deliberately UI-free - no theme, no lipgloss, no bubbles. The rendering phase (docs/plans/backups-rework.md, Phase 5) colours these lines; the engine stays pure and cheap to test on its own.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Kind

type Kind int

Kind classifies one line of a whole-file diff.

const (
	// Equal is a line both sides of the diff agree on.
	Equal Kind = iota
	// Insert is a line the second side has that the first does not. In the
	// preview's direction (live, backup) it is content a restore would add.
	Insert
	// Delete is a line the first side has that the second does not. In the
	// preview's direction it is content a restore would remove from the
	// live file.
	Delete
)

type Line

type Line struct {
	Kind Kind
	// Content is the line's text without its trailing newline: the renderer
	// draws one Line per row, and a newline inside Content would render as
	// a blank row.
	Content string
	Spans   []Span
}

Line is one line of the whole-file view of a diff.

func Lines

func Lines(before, after string) []Line

Lines diffs before against after into the sequence of lines a whole-file view renders: every line in file order, each tagged Equal, Insert or Delete.

Called as Lines(live, backup) it answers "what does restoring this copy do to my file right now?" - a Delete line is content the restore would remove from the live file, an Insert line is content it would add, and an Equal line is content both agree on.

The result covers the whole file, not hunks: the panel renders every line and scrolls to the first change, so Equal context is kept without a context limit.

Chunking follows udiff's line-boundary expansion: an edit inside a line restates that line as a Delete+Insert pair with identical content. The case that reaches real files is an append to a file whose last line has no newline (see Lines_test.go). Smoothing those pairs out is a pairing heuristic, and it belongs to the rendering phase's deferred intra-line work, not to this engine.

Lines returns nil when there is nothing to diff (both inputs empty), and on the unreachable internal error below. Callers read nil as "diff unavailable" and fall back to showing the copy's plain bytes - the same degradation a missing live file gets - rather than as a separate error.

type Span

type Span struct {
	Start int
	End   int
}

Span is a half-open byte range within a Line's Content that carries an intra-line change.

Spans is present and empty from day one so the intra-line highlighting work (docs/plans/backups-rework.md, *Deferred*) fills a field rather than changing this type's shape - and so every caller that ignores it today is already correct when it starts arriving.

Jump to

Keyboard shortcuts

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