Skip to content

diffview 实验性 ​

实验性

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

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

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

Package diffview is the diff editor of ui/editor: two editors side by side (or one inline view) showing the differences between an original and a modified text, computed by ui/editor/diff in the background.

New makes a State of two Models, View shows it every frame:

d := diffview.New(original, modified, th)
defer d.Close()
...
diffview.View(c, d)

The package is built only on the public API of ui/editor (decorations, view zones, hidden areas, scrolling), like Monaco's diff editor on top of its code editor; see docs/specs/diff-view.md. The sides are aligned with blank view zones (whole rows: both editors share a line height) and scroll together block by block; the diff is recomputed after a debounce on every edit, the previous result being mapped through the edits meanwhile; unchanged regions can be folded behind a placeholder; changes can be reverted one by one or handed to the host's actions (ViewW.HunkActions); Alt+F5 and Shift+Alt+F5 move between changes and announce them to screen readers.

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

常量 ​

go
const (
	CmdNext            = "editor.action.diffReview.next"
	CmdPrev            = "editor.action.diffReview.prev"
	CmdRevert          = "diffEditor.revert"
	CmdSwitchSide      = "diffEditor.switchSide"
	CmdToggleUnchanged = "diffEditor.toggleCollapseUnchangedRegions"
	CmdShowAll         = "diffEditor.showAllUnchangedRegions"
	CmdCollapseAll     = "diffEditor.collapseAllUnchangedRegions"
)

Command IDs (Monaco's, except the navigation, which is VS Code's diffReview pair bound to Alt+F5 / Shift+Alt+F5).

类型 ​

HunkAction ​

go
type HunkAction struct {
	Label   string
	Tooltip string
	// Run runs on the UI goroutine when the action is clicked; the view
	// recomputes the diff right after.
	Run func()
}

HunkAction is an action a host offers on a change (View.HunkActions): accepting or rejecting an AI edit, taking a side of a merge conflict.

Mode ​

go
type Mode uint8

Mode is how a diff view shows the two texts.

go
const (
	// SideBySide shows the original on the left, the modified text on
	// the right (Monaco renderSideBySide: true).
	SideBySide Mode = iota
	// Inline shows the modified text only, with the deleted lines of the
	// original between its lines.
	Inline
)

Options ​

go
type Options struct {
	Mode Mode // SideBySide
	// InlineBreakpoint is the width under which the view switches to
	// Inline when UseInlineWhenSpaceIsLimited (900 logical pixels).
	InlineBreakpoint            float32
	UseInlineWhenSpaceIsLimited bool // true
	IgnoreTrimWhitespace        bool // true
	// ComputeMoves detects moved blocks (false; forced off for large
	// files). Moves are not drawn yet.
	ComputeMoves       bool
	MaxComputationTime time.Duration // 5s (diff.DefaultTimeout)
	// Debounce is how long the view waits after an edit before it
	// recomputes the diff (200ms); meanwhile the last result is mapped
	// through the edits.
	Debounce             time.Duration
	HideUnchangedRegions UnchangedRegionsOptions
	// OriginalEditable lets the user edit the original side (false: it is
	// read-only).
	OriginalEditable bool
	// RenderIndicators draws "+" and "−" in the changed lines' gutters
	// (true).
	RenderIndicators bool
	// RenderMarginRevertIcon draws a revert arrow next to every change
	// while the modified side is editable (true).
	RenderMarginRevertIcon bool
	// RenderOverviewRuler draws the merged two-lane overview ruler right of
	// the modified side in SideBySide mode (true).
	RenderOverviewRuler bool
	// DiffWordWrap wraps both sides ("": each side's own Options.WordWrap,
	// Monaco's "inherit").
	DiffWordWrap editor.WrapMode
}

Options are a diff view's settings; DefaultOptions has Monaco's defaults (diffEditor.js). Change them on State.Options; they apply from the next frame.

func DefaultOptions ​

go
func DefaultOptions() Options

DefaultOptions returns Monaco's defaults.

State ​

go
type State struct {
	// Original and Modified are the views of both sides. In Inline mode
	// Original is not shown but kept (switching back restores it). The
	// diff view owns some of their Options (folding, minimap, overview
	// ruler lanes, sticky scroll while folding unchanged regions, the
	// original's ReadOnly, the font size and padding of the original) and
	// sets them every frame.
	Original, Modified *editor.State
	Options            Options
	// contains filtered or unexported fields
}

State is a diff view of two Models: the editor states of both sides and the latest diff. The application holds it, shows it with View and closes it with Close. Use it on the UI goroutine only.

func New ​

go
func New(original, modified *editor.Model, th *theme.Theme) *State

New starts a diff view of original and modified, both drawn with th. The two Models are independent; modified may be open in other editors as well (the diff's decorations, zones and hidden areas belong to this view's States and do not show there). The first diff is computed right away in the background.

func (*State) Close ​

go
func (d *State) Close()

Close cancels the background computation and closes both editor states. The State is not usable afterwards.

func (*State) CollapseAllUnchanged ​

go
func (d *State) CollapseAllUnchanged()

CollapseAllUnchanged folds every unchanged region again.

func (*State) GoTo ​

go
func (d *State) GoTo(i int)

GoTo moves the caret of the focused side to change i, as Next.

func (*State) Next ​

go
func (d *State) Next() bool

Next moves the caret of the focused side to the next change (wrapping around), reveals and announces it; false without changes.

func (*State) OnDidUpdateDiff ​

go
func (d *State) OnDidUpdateDiff(fn func(diff.Result)) (cancel func())

OnDidUpdateDiff calls fn on the UI goroutine with every newly computed diff (Monaco's onDidUpdateDiff), from the frame that takes it; cancel removes fn.

func (*State) Prev ​

go
func (d *State) Prev() bool

Prev moves to the previous change, as Next.

func (*State) Result ​

go
func (d *State) Result() (r diff.Result, upToDate bool)

Result returns the diff of the current texts and whether it was computed for them: while a recomputation is due after an edit, it is the previous result mapped through the edits (changed lines near the edits are approximate).

func (*State) Revert ​

go
func (d *State) Revert(i int) error

Revert replaces change i's lines of the modified text with the original's (Monaco's "revert block"), as one undo step, and recomputes the diff right away. It fails while the modified side is read-only.

func (*State) ShowAllUnchanged ​

go
func (d *State) ShowAllUnchanged()

ShowAllUnchanged reveals every folded unchanged region.

UnchangedRegionsOptions ​

go
type UnchangedRegionsOptions struct {
	Enabled bool // false (Monaco's default; hosts showing AI edits turn it on)
	// ContextLineCount unchanged lines stay visible next to every change
	// (3); a run is only folded when it hides at least MinimumLineCount
	// lines (3); the placeholder's "show more" buttons reveal
	// RevealLineCount lines (20).
	ContextLineCount, MinimumLineCount, RevealLineCount int
}

UnchangedRegionsOptions folds the unchanged lines between changes (Monaco's hideUnchangedRegions).

ViewW ​

go
type ViewW struct {
	// contains filtered or unexported fields
}

ViewW is the diff view of a State: in SideBySide mode the original's editor and the modified's side by side in a splitter (the panes are not mirrored right to left, like VS Code's), the revert arrows between them and the merged overview ruler at the right; in Inline mode the modified's editor alone with the deleted lines between its lines. Both editors scroll together, block by block.

func View ​

go
func View(c *ui.Context, d *State) *ViewW

View shows d. The application holds d; call View every frame.

func (*ViewW) Box ​

go
func (w *ViewW) Box() *ui.Box

Box returns the view's box.

func (*ViewW) Changed ​

go
func (w *ViewW) Changed() bool

Changed reports whether either side's text was edited through the view this frame.

func (*ViewW) Editors ​

go
func (w *ViewW) Editors() (original, modified *editor.ViewW)

Editors returns this frame's editor views (original is nil in Inline mode), for the host's focus and per-frame modifiers. Their input was applied in View already: modifiers that change it (ReadOnly, Keymap) take effect from the next frame.

func (*ViewW) HunkActions ​

go
func (w *ViewW) HunkActions(fn func(i int, ch diff.Change) []HunkAction) *ViewW

HunkActions lets the host offer actions on every change, drawn in a row above it (replacing the revert arrows); fn returns the actions of change i (none: no row). The rows are rebuilt when the diff changes; the frame is built again when HunkActions starts or stops being set.

func (*ViewW) Labels ​

go
func (w *ViewW) Labels(original, modified string) *ViewW

Labels sets the accessible names of the two editors (default: "原文(只 读)— <URI>" and "修改后 — <URI>" from the locale).

func (*ViewW) Layout ​

go
func (w *ViewW) Layout(opts ...layout.Option) *ViewW

Layout applies layout options to the view's box (it grows by default).

func (*ViewW) Mode ​

go
func (w *ViewW) Mode(m Mode) *ViewW

Mode overrides the mode this frame (the frame is built again when it differs from the mode the view was built for).

以 MIT 许可发布