textdiff

package
v0.9.6 Latest Latest
Warning

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

Go to latest
Published: Sep 22, 2026 License: MIT Imports: 1 Imported by: 0

Documentation

Overview

Package textdiff computes line diffs for display: what an edit or write changed in a file, grouped into hunks with a little surrounding context. It has no dependencies and no notion of styling — the chat layer builds diffs from tool calls and the render layer paints them.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Diff

type Diff struct {
	Lines   []Line
	Added   int
	Removed int
}

Diff is a displayable diff: its rows (hunks separated by Gap rows) and the total count of added and removed lines.

func Compute

func Compute(old, new string, ctx int) Diff

Compute diffs old against new line by line and groups the result into hunks with ctx lines of context on each side. A negative ctx keeps every unchanged line (no Gap rows).

func Fragment

func Fragment(old, new string) Diff

Fragment diffs two snippets whose position in the file is unknown — an edit's old and new strings when the file itself could not be read. Every line is kept and line numbers are cleared, so a renderer shows no number gutter rather than numbers that would be wrong.

func (Diff) Empty

func (d Diff) Empty() bool

Empty reports whether the diff changes nothing.

type Kind

type Kind int

Kind classifies one line of a diff.

const (
	Context Kind = iota // unchanged line shown around a change
	Add                 // line present only in the new text
	Del                 // line present only in the old text
	Gap                 // elided run of unchanged lines between two hunks
)

type Line

type Line struct {
	Kind  Kind
	OldNo int
	NewNo int
	Text  string
}

Line is one displayed row of a diff. OldNo and NewNo are 1-based line numbers in the old and new text; 0 means the side has no such line (an Add has no OldNo) or the numbers are unknown (a fragment diff, see Fragment).

Jump to

Keyboard shortcuts

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