Documentation
¶
Overview ¶
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 稳定性").
Index ¶
- Constants
- type HunkAction
- type Mode
- type Options
- type State
- func (d *State) Close()
- func (d *State) CollapseAllUnchanged()
- func (d *State) GoTo(i int)
- func (d *State) Next() bool
- func (d *State) OnDidUpdateDiff(fn func(diff.Result)) (cancel func())
- func (d *State) Prev() bool
- func (d *State) Result() (r diff.Result, upToDate bool)
- func (d *State) Revert(i int) error
- func (d *State) ShowAllUnchanged()
- type UnchangedRegionsOptions
- type ViewW
- func (w *ViewW) Box() *ui.Box
- func (w *ViewW) Changed() bool
- func (w *ViewW) Editors() (original, modified *editor.ViewW)
- func (w *ViewW) HunkActions(fn func(i int, ch diff.Change) []HunkAction) *ViewW
- func (w *ViewW) Labels(original, modified string) *ViewW
- func (w *ViewW) Layout(opts ...layout.Option) *ViewW
- func (w *ViewW) Mode(m Mode) *ViewW
Constants ¶
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).
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type HunkAction ¶
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.
type Options ¶
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.
type State ¶
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 ¶
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 ¶
func (d *State) Close()
Close cancels the background computation and closes both editor states. The State is not usable afterwards.
func (*State) CollapseAllUnchanged ¶
func (d *State) CollapseAllUnchanged()
CollapseAllUnchanged folds every unchanged region again.
func (*State) Next ¶
Next moves the caret of the focused side to the next change (wrapping around), reveals and announces it; false without changes.
func (*State) OnDidUpdateDiff ¶
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) Result ¶
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 ¶
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 ¶
func (d *State) ShowAllUnchanged()
ShowAllUnchanged reveals every folded unchanged region.
type UnchangedRegionsOptions ¶
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).
type ViewW ¶
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 (*ViewW) Changed ¶
Changed reports whether either side's text was edited through the view this frame.
func (*ViewW) Editors ¶
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 ¶
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 ¶
Labels sets the accessible names of the two editors (default: "原文(只 读)— <URI>" and "修改后 — <URI>" from the locale).