Documentation
¶
Overview ¶
Package quickdiff is the model of the quick diff (SPEC §10.30 "Quick diff", step 24.11): the changes of a document against the original text a source control says it is compared with, as VS Code's IChange (added, modified, deleted lines, 1-based), computed off the UI thread 250 ms after the last edit with core/diff, and the registry of the providers that say what a file is compared with. It has no UI: the editor draws the marks from a Model's changes.
A Service holds the providers of the source controls of a window. A Model follows one document: it asks the service for the originals of the document's file, compares the text with each and keeps the changes. A Service tells its models when the providers or the originals changed (Refresh), and a model reads the originals again only then, not on every edit.
Ported from VS Code: src/vs/workbench/contrib/scm/browser/quickDiffModel.ts, src/vs/workbench/contrib/scm/common/quickDiff.ts and quickDiffService.ts
Index ¶
- Constants
- func Compare(a, b Change) int
- func FindNext(changes []Change, line int, inclusive bool) int
- func FindPrevious(changes []Change, line int, inclusive bool) int
- func URIOf(u document.URI) string
- type Change
- type ChangeType
- type Config
- type DiffOptions
- type Event
- type Kind
- type Model
- type Options
- type Original
- type Provider
- type ProviderChange
- type ProviderInfo
- type Result
- type Service
- func (s *Service) Add(p Provider) (id string, remove func())
- func (s *Service) Count() int
- func (s *Service) Originals(ctx context.Context, uri string) ([]Original, error)
- func (s *Service) PrimaryOriginal(ctx context.Context, uri string) (string, bool, error)
- func (s *Service) Refresh(rootURI string)
- func (s *Service) Subscribe(fn func(Event)) (stop func())
- type TextFunc
Constants ¶
const ( DefaultDelay = 250 * time.Millisecond DefaultTimeLimit = time.Second DefaultMaxSize = 2 << 20 )
Defaults of the model, VS Code's: the recomputing waits 250 ms after the last edit (the spec of the step; VS Code's throttle is 200 ms), the diff may take 1000 ms, and a text over 2 MiB has no marks.
Variables ¶
This section is empty.
Functions ¶
func Compare ¶
Compare orders changes by their place in the modified text, then in the original (VS Code's compareChanges).
func FindNext ¶
FindNext returns the index of the first change that ends at or after the line when inclusive, or starts after it when not, and 0 when there is none (the list wraps round). Lines are counted from 1 and the list is sorted (VS Code's findNextClosestChange).
func FindPrevious ¶
FindPrevious returns the index of the last change that starts at or before the line when inclusive, or ends before it when not, and the last one when there is none (the list wraps round); -1 for an empty list (VS Code's findPreviousClosestChange).
Types ¶
type Change ¶
Change is a changed block of lines (VS Code's IChange): lines are counted from 1; an End of 0 says that the side has no lines in the block (an added block has OriginalEnd 0 and OriginalStart the line before it, a deleted block has ModifiedEnd 0 and ModifiedStart the line the lines were deleted after, 0 for a deletion at the start of the text).
func Changes ¶
Changes compares the lines of the original with the lines of the modified text (a text that ends with a line break has an empty last line). It fails with the tags of core/diff (DiffCancelled when ctx ends).
func FromMappings ¶
func FromMappings(maps []diff.LineRangeMapping) []Change
FromMappings turns the blocks of a lines diff into changes (VS Code's toLineChanges): an empty block of a side counts as 0 lines and starts at the line before.
Ported from VS Code: src/vs/editor/browser/widget/diffEditor/diffEditorWidget.ts (toLineChanges)
func (Change) Height ¶
Height is the number of lines a change shows (VS Code's getChangeHeight): the lines of the side that has them, both for a modification.
func (Change) Intersects ¶
Intersects tells whether the line of the modified text is in the block (VS Code's lineIntersectsChange): a deletion at the start of the text is at line 1.
func (Change) ModifiedEndLine ¶
ModifiedEndLine is the last line of the block in the modified text for navigation and for the marks: a deleted block counts as the line it follows, the first line at the start of the text (VS Code's getModifiedEndLineNumber).
func (Change) Type ¶
func (c Change) Type() ChangeType
Type is the kind of the change (VS Code's getChangeType).
type ChangeType ¶
type ChangeType int
ChangeType is the kind of a change (VS Code's ChangeType).
const ( // Modify is a change that replaces lines with lines. Modify ChangeType = iota // Add is a change that only adds lines. Add // Delete is a change that only removes lines. Delete )
type Config ¶
type Config struct {
// Post runs fn on the UI thread (the binding's Post). It is required: the results of the background work are handed over with it.
Post func(fn func())
// IgnoreTrimWhitespace says whether the white space at the ends of lines is ignored (scm.diffDecorationsIgnoreTrimWhitespace); it is
// read, on the UI thread, each time the model schedules a comparison (Recompute asks again after the setting changed). Nil does not
// ignore it.
IgnoreTrimWhitespace func() bool
// Delay is how long the model waits after the last edit before it compares; zero is DefaultDelay.
Delay time.Duration
// TimeLimit is the time limit of each diff; zero is DefaultTimeLimit.
TimeLimit time.Duration
// MaxSize is the size in bytes over which the text, or an original, gets no marks; zero is DefaultMaxSize.
MaxSize int
// Now is the clock of the diff's time limit; nil is time.Now.
Now func() time.Time
}
Config is the configuration of a Model.
type DiffOptions ¶
type DiffOptions struct {
// IgnoreTrimWhitespace counts lines that differ only in the white space at their ends as equal.
IgnoreTrimWhitespace bool
// TimeLimit is the time the diff may take (VS Code's 1000 ms for the quick diff); zero means no limit. When it is over, the result
// is the one change that covers both texts.
TimeLimit time.Duration
// Now is the clock of the time limit; nil is time.Now.
Now func() time.Time
}
DiffOptions are the options of Changes.
type Event ¶
type Event struct {
// Root is the folder (file:///path) the change is below; empty for every file.
Root string
}
Event tells the models that the providers changed or that the originals of the files below a root may have.
type Kind ¶
type Kind int
Kind tells how the changes of a provider are shown (VS Code's QuickDiffProviderKind).
type Model ¶
type Model struct {
// contains filtered or unexported fields
}
Model follows one document: it compares its text with the originals the providers of a Service name, off the UI thread, and keeps the changes. Its methods are called on the UI thread, except Close, which may be called from any goroutine.
func NewModel ¶
NewModel makes the model of a document and starts comparing it. The document is a file: its URI (URIOf) is what the providers are asked about.
func (*Model) Close ¶
func (m *Model) Close()
Close stops the model: nothing more is computed or delivered.
func (*Model) OnChange ¶
func (m *Model) OnChange(fn func()) (stop func())
OnChange calls fn, on the UI thread, after each result that differs from the one before, and returns the function that stops it.
func (*Model) OriginalText ¶
OriginalText is the text the provider with the id was last compared with (the original of the file under that provider); false when the provider has none for the file. The peek of a change shows it. Call it on the UI thread.
type Options ¶
type Options struct {
// Texts reads the text of an original. It is required.
Texts TextFunc
// Log is told of a provider or a read that failed, at debug level; nil logs nothing.
Log func(msg string, args ...any)
}
Options are the options of a Service.
type Original ¶
type Original struct {
// ProviderID, Label and Kind describe the provider (VS Code's QuickDiff).
ProviderID string
Label string
Kind Kind
// URI is the document the text was read from.
URI string
// Text is the original text.
Text string
}
Original is the text a file is compared with under one provider.
type Provider ¶
type Provider struct {
// Owner is the extension that registered the provider.
Owner string
// Label names the provider ("Git Local Changes (Working Tree)").
Label string
// Kind is primary or secondary.
Kind Kind
// RootURI is the folder (file:///path) whose files the provider is asked about; empty for every file.
RootURI string
// Original returns the URI of the document that holds the original text of the file at uri, "" for none.
Original func(ctx context.Context, uri string) (string, error)
}
Provider says what the files below a root are compared with.
type ProviderChange ¶
type ProviderChange struct {
Change
// ProviderID is the id of the provider (ProviderInfo.ID).
ProviderID string
// Kind is the kind of the provider.
Kind Kind
}
ProviderChange is a change and the provider it was found by.
type ProviderInfo ¶
type ProviderInfo struct {
// ID is the id the service gave the provider; Label and Kind are the provider's.
ID string
Label string
Kind Kind
// Original is the URI of the document the file is compared with.
Original string
}
ProviderInfo describes the provider of a change.
type Result ¶
type Result struct {
// Version is the version of the document the changes are for.
Version int
// Providers are the providers that had an original for the file, the primary ones first.
Providers []ProviderInfo
// Changes are the changes of every provider in the order of their place in the text (compare). A secondary change that equals a
// change of the primary provider is left out (VS Code keeps it for its API only).
Changes []ProviderChange
}
Result is what a Model found for one version of the text.
type Service ¶
type Service struct {
// contains filtered or unexported fields
}
Service is the registry of the quick diff providers of a window. It is safe for use from several goroutines.
func NewService ¶
NewService makes a service with no providers.
func (*Service) Add ¶
Add registers a provider and returns its id and the function that removes it. The models are told that files below its root may have an original now.
func (*Service) Originals ¶
Originals asks the providers whose root holds the file for its original and reads the text of each answer. A provider that has no original for the file, or fails, or whose document cannot be read, is left out. It runs off the UI thread and stops when ctx does; the error is the error of ctx in that case.
func (*Service) PrimaryOriginal ¶
PrimaryOriginal is the text of the original of the first primary provider that has one for the file; false when none has. The format on save of modifications compares with it (SPEC §9.12).