quickdiff

package
v1.9.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: 12 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 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

View Source
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

func Compare(a, b Change) int

Compare orders changes by their place in the modified text, then in the original (VS Code's compareChanges).

func FindNext

func FindNext(changes []Change, line int, inclusive bool) int

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

func FindPrevious(changes []Change, line int, inclusive bool) int

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).

func URIOf

func URIOf(u document.URI) string

URIOf is the URI of a document as the providers are asked about it: a file is file:///path with the characters of the path that a URI does not allow escaped, as the extensions have them (the root of a source control, the files of a group); another document is its own String.

Types

type Change

type Change struct {
	OriginalStart, OriginalEnd int
	ModifiedStart, ModifiedEnd int
}

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

func Changes(ctx context.Context, original, modified []string, opts DiffOptions) ([]Change, error)

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

func (c Change) Height() int

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

func (c Change) Intersects(line int) bool

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

func (c Change) ModifiedEndLine() int

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
)

func (ChangeType) String

func (t ChangeType) String() string

String implements fmt.Stringer.

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.

func (Event) Covers

func (e Event) Covers(uri string) bool

Covers tells whether a file is below the root of the event.

type Kind

type Kind int

Kind tells how the changes of a provider are shown (VS Code's QuickDiffProviderKind).

const (
	// KindPrimary is the provider whose changes are the marks of a file, and whose original the format on save of modifications uses.
	KindPrimary Kind = iota
	// KindSecondary is a provider whose changes are drawn in the secondary colors, where the primary provider has none.
	KindSecondary
)

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

func NewModel(svc *Service, doc *document.Document, cfg Config) *Model

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

func (m *Model) OriginalText(providerID string) (string, bool)

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.

func (*Model) Recompute

func (m *Model) Recompute()

Recompute compares the text with the originals now, asking the providers again.

func (*Model) Result

func (m *Model) Result() Result

Result is the changes found last. They are for Result.Version of the document; the editor's marks move with the edits since.

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.

func (Result) Equal

func (r Result) Equal(o Result) bool

Equal tells whether two results have the same providers and changes (the version is not compared).

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

func NewService(opts Options) *Service

NewService makes a service with no providers.

func (*Service) Add

func (s *Service) Add(p Provider) (id string, remove func())

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) Count

func (s *Service) Count() int

Count is the number of providers.

func (*Service) Originals

func (s *Service) Originals(ctx context.Context, uri string) ([]Original, error)

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

func (s *Service) PrimaryOriginal(ctx context.Context, uri string) (string, bool, error)

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).

func (*Service) Refresh

func (s *Service) Refresh(rootURI string)

Refresh tells the models that the originals of the files below the root (every file when empty) may have changed.

func (*Service) Subscribe

func (s *Service) Subscribe(fn func(Event)) (stop func())

Subscribe calls fn, on the goroutine that changed something, after each change of the providers and each Refresh, and returns the function that stops it. fn must not block.

type TextFunc

type TextFunc func(ctx context.Context, uri string) (string, error)

TextFunc reads the text of the document at a URI (the original that a provider named). It runs off the UI thread.

Jump to

Keyboard shortcuts

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