diffeditor

package
v1.8.0 Latest Latest
Warning

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

Go to latest
Published: Nov 26, 2026 License: MIT Imports: 18 Imported by: 0 modules, 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 diffeditor holds the diff editor (SPEC §9.11).

Index

Constants

View Source
const DebounceDelay = 200 * time.Millisecond

DebounceDelay is how long the diff waits after the last edit of either text before it is computed again (VS Code's diff editor waits 200 ms).

View Source
const DefaultInlineBreakpoint = 900.0

DefaultInlineBreakpoint is the width, in pixels, at or under which the inline view is used when the space is limited (VS Code's `diffEditor.renderSideBySideInlineBreakpoint`).

RulerWidth is the width of the overview ruler at the right edge of the diff editor, the width of the scrollbar of an editor.

Variables

This section is empty.

Functions

func AdjustOptions

func AdjustOptions(o editor.Options, s Side) editor.Options

AdjustOptions are the options an editor of the diff editor works with, from the options the settings give it (VS Code's DiffEditorWidget adjusts the options of its two editors the same way): the original editor is read-only, and the minimap, the folding controls and sticky scroll are off in both, since the diff editor has the overview ruler, and rows that fold or stick would not line up with the other side.

func ChangeToGo

func ChangeToGo(changes []diff.LineRangeMapping, cursorLine, lineCount int, next bool) (int, bool)

ChangeToGo picks the change that Go to Next Change or Go to Previous Change moves to, as the index in changes, from the line (counted from 1) of the cursor of the modified editor. Next is the first change that starts below the cursor's line, or the first change when there is none or the cursor is on the last line of the text; previous is the last change that starts above the cursor's line, or the last change when there is none. It reports false when there are no changes.

func Marks

func Marks(changes []diff.LineRangeMapping, s Side, c Colors, snap buffer.Snapshot) []editor.BackgroundMark

Marks are the background marks of the changes for one side: the lines of every change that has lines on the side, with the margin along them, and the characters of its character changes.

func NewFiller

func NewFiller() widgets.Widget

NewFiller is the widget of a filler zone, for the other editors that line texts up with view zones (the inputs of the merge editor, step 9.7, use the same diagonal fill).

func RowsBefore

func RowsBefore(line int, extra []LineExtra, zones []Zone) int

RowsBefore gives, for a side, the number of rows above the line with a number (counted from 1) when the side has the zones: the lines before it, the extra rows of the wrapped lines among them and the filler. It is how the tests check that an alignment makes both sides line up.

func TranslatePosition

func TranslatePosition(changes []diff.LineRangeMapping, from Side, p buffer.Position) buffer.Range

TranslatePosition maps a position of one side to the range it corresponds to on the other side, through the changes of the diff: a position after a change moves by the lines the changes added or removed; in a change it goes to the start of the other side's change, or, when the change has character changes, into the one at or before the position. from tells which side the position is of. Lines are counted from 0 and columns are bytes, as the buffer's positions are.

Types

type AlignOptions

type AlignOptions struct {
	// Original and Modified are the wrapped lines of each side, sorted by line. Without wrapping they are empty.
	Original, Modified []LineExtra
	// InnerHunkAlignment cuts a change at the lines its character changes share between the sides: the side by
	// side view sets it (VS Code's `innerHunkAlignment`), so that a line that is changed in the middle stays next
	// to its counterpart when the hunk has more lines on one side.
	InnerHunkAlignment bool
	// OriginalLineLength gives the length in bytes of a line of the original text, counted from 1; false for a
	// line the text does not have (a diff that is older than the text). Without it every line is taken to be
	// longer than any change.
	OriginalLineLength func(line int) (int, bool)
}

AlignOptions are the options of ComputeAlignment.

type Alignment

type Alignment struct {
	// Original and Modified are the line ranges (counted from 1, the end excluded) the alignment covers.
	Original, Modified diff.LineRange
	// OriginalRows and ModifiedRows are the rows the ranges take, wrapped lines counted.
	OriginalRows, ModifiedRows int
	// Change is the change the alignment is part of; nil outside the changes.
	Change *diff.LineRangeMapping
}

Alignment is a stretch of both texts that must be as tall on both sides: after it the same row of the screen shows the end of both ranges. Inside a change it is the change (or the part of it that inner changes cut at lines they share); outside the changes it is one line of each side (only where wrapped lines are taller than their counterparts).

func ComputeAlignment

func ComputeAlignment(changes []diff.LineRangeMapping, opts AlignOptions) []Alignment

ComputeAlignment is VS Code's computeRangeAlignment: it cuts the texts into the stretches that line up. The changes are those of a diff (sorted, apart).

type Colors

type Colors struct {
	// InsertedText and RemovedText are the backgrounds of the characters that were inserted and removed, and
	// InsertedTextBorder and RemovedTextBorder their outlines (the high contrast themes).
	InsertedText, RemovedText, InsertedTextBorder, RemovedTextBorder ui.Color
	// InsertedLine and RemovedLine are the backgrounds of the lines, InsertedGutter and RemovedGutter the colors of the
	// margin along them.
	InsertedLine, RemovedLine, InsertedGutter, RemovedGutter ui.Color
	// InsertedOverview and RemovedOverview are the marks on the overview ruler.
	InsertedOverview, RemovedOverview ui.Color
	// Border is the line between the two editors, DiagonalFill the lines of the filler.
	Border, DiagonalFill ui.Color
}

Colors are the colors the diff editor paints with, from the theme (SPEC §9.11). A color the theme leaves transparent takes the one VS Code's style sheet falls back to: the margin of a changed line and the mark on the overview ruler take the background of the line, and the line between the two editors takes the border between editor groups.

func ColorsOf

func ColorsOf(c kit.Colors) Colors

ColorsOf takes the diff editor's colors from the workbench colors.

type GutterAction added in v1.0.0

type GutterAction struct {
	// Icon is the Codicon (a fonts.Codicon... constant) and Tooltip the hover.
	Icon    int
	Tooltip string
	// Run is called when the button is pressed.
	Run func(GutterContext)
}

GutterAction is a button of the toolbar: a Codicon with a tooltip, and what it does with the context.

type GutterContext added in v1.0.0

type GutterContext struct {
	// OriginalURI and ModifiedURI are the documents of the two sides.
	OriginalURI, ModifiedURI string
	// OriginalWithModifiedChanges is the original text with the hunk, or the selected lines, applied.
	OriginalWithModifiedChanges string
}

GutterContext is what a command of the toolbar is run with (VS Code's DiffEditorSelectionHunkToolbarContext).

type InlineZone

type InlineZone struct {
	// Change is the index of the change in Changes.
	Change int
	// AfterLine is the zone's place in the modified text: the number of lines above it.
	AfterLine int
	// Rows is how many rows it takes: the deleted lines.
	Rows int
}

InlineZone is a zone of deleted lines of the inline view.

type LineExtra

type LineExtra struct {
	// Line is the line number, counted from 1.
	Line int
	// Rows is the rows of the line after the first.
	Rows int
}

LineExtra is a line of a side that is taller than one row because it is wrapped: Rows is the number of rows it has more than one.

type Mode

type Mode int

Mode is how the diff editor lays out the two texts.

const (
	// SideBySide shows the original and the modified editor next to each other.
	SideBySide Mode = iota
	// Inline shows the modified editor alone, with the deleted lines in view zones.
	Inline
)

func (Mode) String

func (m Mode) String() string

String is "sideBySide" or "inline".

type Model

type Model struct {
	foundation.ChangeNotifier
	// contains filtered or unexported fields
}

Model is the state of a diff editor: the diff of the two texts, kept up to date as they change, the filler zones that line the two editors up, the marks that color the changes, the scroll position the two editors share and the navigation between the changes. It works on the UI thread only; the diff is computed on a goroutine with a context that the next edit cancels, and the result comes back through ModelOpts.Post.

func NewModel

func NewModel(opts ModelOpts) *Model

NewModel makes the model of a diff editor. It computes nothing until Start.

func (*Model) Alignments

func (m *Model) Alignments() []Alignment

Alignments are the stretches of the two texts that must be as tall on both sides, for the diff as it is and the wrapping of the lines as it is now.

func (*Model) Applied

func (m *Model) Applied() int

Applied is how many results of the computation the model has taken in. A result that was cancelled or outdated does not count.

func (*Model) BreakpointReached

func (m *Model) BreakpointReached() bool

BreakpointReached tells whether the side by side view is asked for but the widget is no wider than the breakpoint: the context key diffEditorRenderSideBySideInlineBreakpointReached (VS Code's `couldShowInlineViewBecauseOfSize`). It is false until the widget has been laid out.

func (*Model) Changes

func (m *Model) Changes() []diff.LineRangeMapping

Changes are the changes of the diff as last computed: lines counted from 1, the end excluded, with the character changes of each. They describe the texts as they were when the computation started; while a new computation is pending they may be a little behind the texts.

func (*Model) Colors

func (m *Model) Colors() Colors

Colors are the colors of the changes the model uses.

func (*Model) Computed

func (m *Model) Computed() bool

Computed tells whether a diff has been computed.

func (*Model) Computing

func (m *Model) Computing() bool

Computing tells whether a computation is running or waiting for the edits to stop.

func (*Model) Dispose

func (m *Model) Dispose()

Dispose stops the computation, the timers and the listeners and takes the zones and the marks off the editors.

func (*Model) Embedded added in v1.0.0

func (m *Model) Embedded() bool

Embedded says that the model is the small diff editor of a peek (ModelOpts.Embedded).

func (*Model) Focus

func (m *Model) Focus(s Side) *editor.EditorFocus

Focus gives the keyboard focus to the editor of a side while it is on screen.

func (*Model) FocusSide

func (m *Model) FocusSide(s Side) bool

FocusSide gives the keyboard focus to the editor of a side.

The inline view has one editor, the modified one, which every side stands for.

func (*Model) FocusedSide

func (m *Model) FocusedSide() (Side, bool)

FocusedSide tells which editor has the keyboard focus; false when neither does.

func (*Model) GoToChange

func (m *Model) GoToChange(next bool) bool

GoToChange moves the cursor of the modified editor to the start of the next or the previous change, wrapping around, and shows it in the middle of the editor (VS Code's goToDiff). It reports false when there are no changes.

func (*Model) HitTimeout

func (m *Model) HitTimeout() bool

HitTimeout tells whether the last computation ran out of time (diffEditor.maxComputationTime): the changes are then coarse, whole blocks, not an error.

func (*Model) HunkAt added in v1.0.0

func (m *Model) HunkAt(line int) (int, bool)

HunkAt is the change whose modified lines hold the line (counted from 1); false when none does. A change that only deletes lines has no modified lines and holds none.

func (*Model) InlineBreakpoint

func (m *Model) InlineBreakpoint() float64

InlineBreakpoint is the setting diffEditor.renderSideBySideInlineBreakpoint in pixels (default 900).

func (*Model) InlineZones

func (m *Model) InlineZones() []InlineZone

InlineZones are the zones of the deleted lines the modified editor has now, in the order of the changes: the change each is of (an index in Changes), where it is and how many rows it has.

func (*Model) MapToOtherSide

func (m *Model) MapToOtherSide(from Side) buffer.Selection

MapToOtherSide gives the selection of the editor of a side as it is on the other side (VS Code's mapToOtherSide): its ends are translated through the changes.

func (*Model) Mode

func (m *Model) Mode() Mode

Mode is the mode the editors are laid out for now. It follows WantedMode a frame after a setting or the width changed: the change is made off the build.

func (*Model) NoChanges

func (m *Model) NoChanges() bool

NoChanges tells whether the texts are the same: the diff is computed and has no change.

func (*Model) Recompute

func (m *Model) Recompute()

Recompute computes the diff now, without waiting for the edits to stop (a setting changed, or the editor opened).

func (*Model) RenderSideBySide

func (m *Model) RenderSideBySide() bool

RenderSideBySide is the setting diffEditor.renderSideBySide (default true).

func (*Model) RulerMarks

func (m *Model) RulerMarks() (marks []RulerMark, total float64)

RulerMarks are the marks of the changes, one for every change that has lines on a side. Total is the height of all the rows of the editors (they are as tall as each other once aligned), which the marks are scaled to.

func (*Model) ScrollToFraction

func (m *Model) ScrollToFraction(y, height float64)

ScrollToFraction scrolls the editors so that the place at a height of the overview ruler, whose own height is given, is in the middle of the view (a click or a drag on the ruler).

func (*Model) SetColors

func (m *Model) SetColors(c Colors)

SetColors sets the colors of the changes; the marks are made again when they differ from the ones in use.

func (*Model) SetMode

func (m *Model) SetMode(mode Mode)

SetMode lays the editors out for a mode: the zones and the marks of the other mode are taken off, and the ones of this mode put on. The modified editor is the same in both modes, so its cursors and its scroll position stay. Listeners are told even when the mode is the same, since the context keys may have changed.

func (*Model) SetWidth

func (m *Model) SetWidth(w float64)

SetWidth tells the model how wide the widget is, which may change the mode. It is called while the widget is built; the mode changes a little later, on the UI thread, and the listeners are told.

func (*Model) Start

func (m *Model) Start()

Start computes the diff for the first time.

func (*Model) SwitchSide

func (m *Model) SwitchSide()

SwitchSide moves the keyboard focus to the other editor, with the selection mapped through the changes (VS Code's diffEditor.switchSide). The side that has the focus is the modified one when neither has it.

func (*Model) TextWithHunk added in v1.0.0

func (m *Model) TextWithHunk(i int) string

TextWithHunk is the original text with change i applied: its original lines replaced by its modified lines.

func (*Model) TextWithLines added in v1.0.0

func (m *Model) TextWithLines(first, last int) (string, bool)

TextWithLines is the original text with the changes the modified lines first to last (counted from 1, both included) touch applied. A change with as many lines on both sides is cut to the selected lines, line by line; any other is applied whole. It reports false when no change touches the lines.

func (*Model) UseInlineViewWhenSpaceIsLimited

func (m *Model) UseInlineViewWhenSpaceIsLimited() bool

UseInlineViewWhenSpaceIsLimited is the setting diffEditor.useInlineViewWhenSpaceIsLimited (default true).

func (*Model) View

func (m *Model) View(s Side) *editor.ViewModel

View is the view model of a side.

func (*Model) WantedMode

func (m *Model) WantedMode() Mode

WantedMode is the mode the settings and the width ask for: inline when the side by side view is turned off, or when the space is limited and the setting that uses the inline view then is on (VS Code's `renderSideBySide`).

func (*Model) Width

func (m *Model) Width() float64

Width is the width of the widget as it was last laid out (0 before it was).

func (*Model) ZonesOf

func (m *Model) ZonesOf(s Side) []Zone

ZonesOf are the filler zones an editor has now, in order.

type ModelOpts

type ModelOpts struct {
	// Original and Modified are the view models of the two editors, which the model puts the filler zones and
	// the marks of the changes on, and keeps scrolled together.
	Original, Modified *editor.ViewModel
	// Settings is read for diffEditor.ignoreTrimWhitespace and diffEditor.maxComputationTime, which the model
	// follows live; without it the defaults are used (white space ignored, 5 s).
	Settings *settings.Store
	// Post hands a function to the UI thread (the binding's Post); it is how the result of the computation, which
	// runs on a goroutine, arrives. Required.
	Post func(func())
	// AfterTimer calls fn on the UI thread after d and gives the function that cancels the call (the binding's
	// gestures.NewTimer); without it an edit recomputes the diff at once.
	AfterTimer func(d time.Duration, fn func()) (cancel func())
	// Now is the clock of the time limit of the diff (diff.Options.Now); nil means time.Now.
	Now func() time.Time
	// Context ends the computations when it is done; without it context.Background().
	Context context.Context
	// OnError is told about a computation that failed (the cancelled ones are not reported).
	OnError func(error)
	// OriginalFocus and ModifiedFocus give the keyboard focus to the editors (the widget hands them to the
	// editors it builds); without them the model has its own.
	OriginalFocus, ModifiedFocus *editor.EditorFocus
	// Embedded makes the diff editor the small one of the peek of a change (VS Code's EmbeddedDiffEditorWidget with the options of
	// the quick diff: renderSideBySide, ignoreTrimWhitespace and renderOverviewRuler off): always the inline view, white space at the
	// ends of lines counts, and the widget draws no overview ruler. The context key isEmbeddedDiffEditor is set in both editors.
	Embedded bool
}

ModelOpts are the options of NewModel.

type RulerMark

type RulerMark struct {
	// Side is the editor the mark is of: the left half of the ruler is the original side's, the right half the
	// modified side's.
	Side Side
	// Top and Bottom are the extent of the change in pixels from the top of the editor's rows, zones included.
	Top, Bottom float64
}

RulerMark is a mark of the overview ruler: the rows a change takes in the editor of a side, as a part of the height of all the rows of the editor.

type Side

type Side int

Side is one of the two editors of the diff editor.

const (
	// Original is the editor on the left, which shows the text the changes start from.
	Original Side = iota
	// Modified is the editor on the right, which shows the text the changes lead to.
	Modified
)

func (Side) Other

func (s Side) Other() Side

Other is the other side.

func (Side) String

func (s Side) String() string

String names the side.

type Widget

type Widget struct {
	widgets.StatefulWidget
	// contains filtered or unexported fields
}

Widget is the diff editor (SPEC §9.11): the original editor on the left and the modified editor on the right, of the same width, with a line between them and the overview ruler at the right edge. The model keeps the editors lined up and scrolled together and colors the changes; this widget lays them out.

func NewWidget

func NewWidget(model *Model, opts ...WidgetOpts) *Widget

NewWidget makes the diff editor of a model.

func (*Widget) CreateState

func (w *Widget) CreateState() widgets.State

CreateState implements widgets.StatefulWidget.

func (*Widget) Model

func (w *Widget) Model() *Model

Model is the model the widget shows.

type WidgetOpts

type WidgetOpts struct {
	// Original and Modified are the options of the two editors. The diff editor gives each the model's focus
	// handle when it has none, and puts both in a scope of context keys of its own below Original.Keys, which has
	// isInDiffEditor, diffEditorOriginalWritable and diffEditorModifiedWritable set.
	Original, Modified editor.EditorOpts
	// HunkActions and SelectionActions give the buttons of the toolbar in the gutter of the modified editor: those over the lines of a
	// change under the pointer, and those over a selection that touches changes (the items of MenuDiffEditorGutterHunk and
	// MenuDiffEditorGutterSelection). Without them, or while the modified side is read-only, there is no toolbar.
	HunkActions, SelectionActions func() []GutterAction
}

WidgetOpts configure the Widget; every field is optional.

type Zone

type Zone struct {
	AfterLine, Rows int
}

Zone is a stretch of filler rows to put in one side: Rows empty rows after the line with the number AfterLine (0: above the first line).

func Zones

func Zones(alignments []Alignment) (original, modified []Zone)

Zones turns the alignments into the filler of each side: where one side takes fewer rows than the other for the same stretch, it gets the difference after the last line of its stretch. Zones that come after the same line are one.

Jump to

Keyboard shortcuts

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