diff

package
v0.3.1 Latest Latest
Warning

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

Go to latest
Published: Nov 25, 2026 License: Apache-2.0 Imports: 14 Imported by: 1 module, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

Documentation

Overview

Package diff is base's DiffView, a port of NSH's: the changes between two texts, or the files of a patch, side by side or one over the other, the words changed within lines marked, the unchanged lines between changes collapsed into separators that expand, and the text colored by the Editor's highlighting. It has no look of its own: Styles gives every part, the added and deleted tints among them, its look.

It is a package of its own because its highlighting links chroma's lexers.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Annotation

type Annotation struct {
	ID   string
	Side Side
	Line int
}

Annotation is the app's content under a line (a review comment, a thread), drawn by RenderAnnotation across its side, as tall as it lays out. ID tells it apart from the others; Line is from 0 of Side's text. It shows while its line does.

type Change

type Change struct{ OldValue, NewValue string }

Change is the texts after a change was reverted or kept.

type Classes

type Classes struct {
	Side, Gutter, LineNumber, LineNumberAdded, LineNumberDeleted string
	Content, Line, LineAdded, LineDeleted, Filler                string
	WordAdded, WordDeleted                                       string
	IndicatorAdded, IndicatorDeleted                             string
	Separator, HunkActions, FileHeader                           string
	LineSelected, Annotation, AddAnnotation, Region              string
}

Classes are Tailwind classes of a DiffView's parts, by Styles' names, each under its part's style.

type DiffView

type DiffView struct {
	OldValue, NewValue func() string
	// Patch is a unified diff of one file or several: git diff's, git
	// format-patch's, diff -u's. Each hunk shows under a separator of its
	// header, its lines numbered as in the file.
	Patch func() string
	// Language colors the text; a patch's files by their paths without
	// it.
	Language string
	// Highlighter gives the app's highlighter for a language's name or a
	// file's path, nil for the built-in one.
	Highlighter func(language string) Highlighter
	// Unified shows one column, the deleted lines over the added ones,
	// rather than two sides.
	Unified    bool
	Inline     Inline
	Indicators Indicators
	// ContextLines is the unchanged lines shown around each change: 3 when
	// 0, none when negative. ExpandAll shows every one.
	ContextLines int
	ExpandAll    bool
	// NoLineNumbers hides the numbers; SoftWrap wraps long lines at the
	// side's width.
	NoLineNumbers, SoftWrap bool
	// Selectable lets the user select and copy each column's text, its
	// numbers and separators left out.
	Selectable bool
	// FontFamily (the monospaced font when empty), FontSize (13) and
	// LineHeight (20). SeparatorHeight is the height of separators and
	// file headers, LineHeight when 0.
	FontFamily                            string
	FontSize, LineHeight, SeparatorHeight core.Points
	// RenderFileHeader draws a patch's file headers, a header above a
	// patch of one file too; its controls call Ref.ToggleFile. Without it
	// a header shows the file's path and counts, and a press toggles it.
	RenderFileHeader func(cx *core.Scope, f File) core.Node
	// RenderSeparator draws the separators of collapsed lines and a
	// patch's hunk headers; its controls call Ref.Expand. Without it a
	// separator says what it hides, and a press shows every line.
	RenderSeparator func(cx *core.Scope, s Separator) core.Node
	// RenderHunkActions draws the app's actions on a change (revert,
	// keep), at its first row's right.
	RenderHunkActions func(cx *core.Scope, h Hunk) core.Node
	// RenderRegionActions draws the app's actions on lines (stage, discard)
	// in a frame around them, at its top right: the change the pointer is
	// over, or the whole lines the text selection touches or those selected
	// on the numbers, which take its place while they last. The text is
	// selectable with it.
	RenderRegionActions func(cx *core.Scope, r Region) core.Node
	// Annotations are the app's content under lines, each drawn by
	// RenderAnnotation across its line's side (the view unified).
	Annotations      func() []Annotation
	RenderAnnotation func(cx *core.Scope, a Annotation) core.Node
	// RenderAddAnnotation draws the app's control to add an annotation,
	// beside the line the pointer is over and the last selected one: lines
	// are the selected ones when its line is one of them, else its line.
	RenderAddAnnotation func(cx *core.Scope, lines LineRange) core.Node
	// Classes go under Styles, and Class under Style.
	Classes    Classes
	Styles     Styles
	StylesFunc func() Styles
	Class      string
	Style      core.Style
	StyleFunc  func() core.Style
	Ref        *Ref
	TestID     string
	// OnLayout and Tooltip are its view's, as a core.View's.
	OnLayout func(core.Bounds)
	Tooltip  core.Node

	// OnChange receives the texts after a change was reverted or kept.
	OnChange          func(Change)
	OnLineNumberPress func(Line)
	// OnLineSelectionChange receives the lines selected on the numbers, nil
	// for none: a press selects a line, a Shift-press extends to it, and a
	// mouse drag over the numbers selects those it goes over.
	OnLineSelectionChange func(lines *LineRange)
	// OnStatsChange receives the counts once and whenever a diff counts
	// otherwise.
	OnStatsChange func(Stats)
	OnFileToggle  func(File)
	// OnScroll receives the rows' scroll position from the first layout
	// on, with each scroll, resize or relayout that changes it.
	OnScroll func(core.ScrollEvent)
	// OnHunkBoundsChange receives each change's box when one moves.
	OnHunkBoundsChange func([]HunkBounds)
	// OnError receives a patch that cannot be read.
	OnError func(error)
}

DiffView shows the changes between OldValue and NewValue, or the files of Patch when they are nil.

func (DiffView) Render

func (p DiffView) Render(cx *core.Scope) core.Node

Render implements core.Node.

type Expansion

type Expansion uint8

Expansion is which of a separator's lines Ref.Expand shows.

const (
	// ExpandEvery shows every line it hides.
	ExpandEvery Expansion = iota
	// ExpandUp shows the lines just above the change below it.
	ExpandUp
	// ExpandDown shows the lines just below the change above it.
	ExpandDown
)

type File

type File struct {
	Index            int
	Path, OldPath    string
	Status           FileStatus
	Binary           bool
	Added, Deleted   int
	Hunks, FirstHunk int
	Collapsed        bool
}

File is a file of a patch: its index among the patch's files, its paths (OldPath for renames and copies), status, counts, its changes from FirstHunk, and whether it is collapsed.

type FileStatus

type FileStatus uint8

FileStatus is what a patch did to a file.

const (
	FileModified FileStatus = iota
	FileAdded
	FileDeleted
	FileRenamed
	FileCopied
)

type Highlighter

type Highlighter = syntax.Highlighter

Highlighter is the app's highlighting of a language, in place of the built-in one, as the Editor takes it.

type Hunk

type Hunk struct {
	Index, File                            int
	OldStart, OldLines, NewStart, NewLines int
}

Hunk is a change: its index, its file's index in a patch (0 for two texts), and its lines as 1-based numbers and counts on each side.

type HunkBounds

type HunkBounds struct {
	Index  int
	Bounds core.Bounds
}

HunkBounds is the box of change Index's rows: wrapped lines and annotations count, and a collapsed file's changes have none.

type Indicators

type Indicators uint8

Indicators is how changed lines are marked beside their numbers.

const (
	// IndicatorBars is a bar at the gutter's left edge.
	IndicatorBars Indicators = iota
	// IndicatorClassic is + and −.
	IndicatorClassic
	IndicatorNone
)

type Inline

type Inline uint8

Inline is what is highlighted within changed lines.

const (
	// InlineWordAlt marks the words that changed, joined when one
	// character apart, so a changed phrase reads as one.
	InlineWordAlt Inline = iota
	// InlineWord marks each word that changed.
	InlineWord
	// InlineChar marks each character that changed.
	InlineChar
	// InlineNone marks nothing: the lines' tints alone.
	InlineNone
)

type Line

type Line struct {
	Side Side
	Line int
}

Line is a line number pressed: Line of Side, from 0.

type LineRange

type LineRange struct {
	Side       Side
	Start, End int
}

LineRange is lines Start to End (both included, from 0) of a side's text: the lines selected on the numbers, or those an annotation is added to.

type Ref

type Ref struct {
	// contains filtered or unexported fields
}

Ref is a DiffView's methods, bound once it mounts.

func (*Ref) AcceptHunk

func (r *Ref) AcceptHunk(i int)

AcceptHunk makes change i's new lines the old text's and reports the texts.

func (*Ref) AcceptRegion

func (r *Ref) AcceptRegion(x Region)

AcceptRegion makes r's lines the old text's, as staging them does, and reports the texts. It takes two texts' lines, not a patch's.

func (*Ref) CollapseAll

func (r *Ref) CollapseAll()

CollapseAll collapses the unchanged lines again.

func (*Ref) CollapseFile

func (r *Ref) CollapseFile(path string)

CollapseFile shows only the header of the file at path.

func (*Ref) CurrentHunk

func (r *Ref) CurrentHunk() (int, bool)

CurrentHunk returns the change gone to while the view stays there, else the one at the top of the view or the first in view; false for none in view.

func (*Ref) Expand

func (r *Ref) Expand(gap int, how Expansion, lines int)

Expand shows lines separator gap hides: every one, or lines (20 when 0) up or down.

func (*Ref) ExpandAll

func (r *Ref) ExpandAll()

ExpandAll shows every unchanged line; CollapseAll collapses them again.

func (*Ref) ExpandFile

func (r *Ref) ExpandFile(path string)

ExpandFile shows the lines of the file at path again.

func (*Ref) Files

func (r *Ref) Files() []File

Files returns a patch's files, in order.

func (*Ref) GoToHunk

func (r *Ref) GoToHunk(i int)

GoToHunk scrolls change i's first row to the top, expanding a collapsed file first.

func (*Ref) HunkBounds

func (r *Ref) HunkBounds() []HunkBounds

HunkBounds returns each change's box across the viewport, in the rows' coordinates, for an overview ruler.

func (*Ref) Hunks

func (r *Ref) Hunks() []Hunk

Hunks returns the changes, in order.

func (*Ref) NextHunk

func (r *Ref) NextHunk() (int, bool)

NextHunk scrolls the next change to the top and returns its index, after the last the first; false without changes. It goes on from the change gone to while the view stays there, else from the top of the view.

func (*Ref) PreviousHunk

func (r *Ref) PreviousHunk() (int, bool)

PreviousHunk scrolls the previous change to the top and returns its index, before the first the last.

func (*Ref) RevertHunk

func (r *Ref) RevertHunk(i int)

RevertHunk puts change i's old lines back into the new text and reports the texts.

func (*Ref) RevertRegion

func (r *Ref) RevertRegion(x Region)

RevertRegion puts r's lines back as they are in the old text and reports the texts. It takes two texts' lines, not a patch's.

func (*Ref) ScrollOffset

func (r *Ref) ScrollOffset() core.Points

ScrollOffset returns how far the rows are scrolled.

func (*Ref) ScrollToFile

func (r *Ref) ScrollToFile(path string)

ScrollToFile scrolls the header of the file at path to the top.

func (*Ref) SelectedLines

func (r *Ref) SelectedLines() (LineRange, bool)

SelectedLines returns the lines selected on the numbers; ok is false for none.

func (*Ref) SelectedText

func (r *Ref) SelectedText() string

SelectedText returns the selected lines' text.

func (*Ref) SetScrollOffset

func (r *Ref) SetScrollOffset(y core.Points)

SetScrollOffset scrolls the rows to y at once, as far as they go, for a thumb that follows the pointer.

func (*Ref) SetSelectedLines

func (r *Ref) SetSelectedLines(lines *LineRange)

SetSelectedLines selects lines of a side, nil for none, without OnLineSelectionChange.

func (*Ref) Stats

func (r *Ref) Stats() Stats

Stats returns the lines added and deleted and the number of changes.

func (*Ref) ToggleFile

func (r *Ref) ToggleFile(path string)

ToggleFile collapses the file at path, or expands it.

type Region

type Region struct {
	Old, New []int
	Selected bool
}

Region is what RenderRegionActions acts on: the changed lines of Old and New it holds, from 0, and whether the user selected them rather than the pointer being over a change.

type Separator

type Separator struct {
	Gap, Lines  int
	First, Last bool
	Header      string
}

Separator is what RenderSeparator draws: the gap of unchanged lines it collapses (0 before the first change, i between changes i-1 and i), the lines it hides (0 for a patch's hunk header), whether it is before the first change or after the last, and a patch's hunk header.

type Side

type Side uint8

Side is a side of a diff.

const (
	Old Side = iota
	New
)

type Stats

type Stats struct{ Added, Deleted, Hunks int }

Stats is the lines added and deleted and the number of changes.

type Styles

type Styles struct {
	// Side is each side (split) or the one column (unified); Gutter a
	// side's numbers and indicators; LineNumber every number, with
	// LineNumberAdded and LineNumberDeleted after it on changed lines.
	Side, Gutter, LineNumber, LineNumberAdded, LineNumberDeleted core.Style
	// Content is a side's text beside its gutter; Line every row, with
	// LineAdded and LineDeleted after it, on a changed row's gutter too;
	// Filler the empty half of a split row beside a line the other side
	// lacks, its Color hatching it.
	Content, Line, LineAdded, LineDeleted, Filler core.Style
	// WordAdded and WordDeleted mark the words changed within lines, a
	// box a line tall behind them, whose Background and CornerRadius the
	// words' text leaves to it.
	WordAdded, WordDeleted core.Style
	// IndicatorAdded and IndicatorDeleted are the bars, or the + and −.
	IndicatorAdded, IndicatorDeleted core.Style
	// Separator is a separator of collapsed lines or a patch's hunk
	// header; HunkActions the box of the app's actions on a change;
	// FileHeader a patch's file's header.
	Separator, HunkActions, FileHeader core.Style
	// LineSelected is over the selected lines' rows of their side, gutter
	// and text; Annotation the box of an annotation; AddAnnotation the box
	// of the add control, the gutter's width and a line tall.
	LineSelected, Annotation, AddAnnotation core.Style
	// Region frames the lines RenderRegionActions acts on.
	Region core.Style
}

Styles are the styles of a DiffView's parts, its whole look.

type Token

type Token = syntax.Token

Token is a run of a line a Highlighter colors.

Jump to

Keyboard shortcuts

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