Documentation
¶
Overview ¶
Package diff computes line and character diffs and holds the three-way merge model (SPEC §9.11).
Line diff (step 9.1) ¶
ComputeLines is VS Code's default ("advanced") lines diff computer (DefaultLinesDiffComputer.computeDiff) up to, and excluding, the refinement of the changed line blocks to character changes: the lines are compared by their trimmed text, aligned with dynamic programming (fewer than 1700 lines in both texts together) or Myers' O(ND) algorithm, and improved by VS Code's heuristics (optimizeSequenceDiffs: joining changes that a shift makes meet and shifting insertions and deletions to the boundary with the least indentation, and removeVeryShortMatchingLinesBetweenDiffs). Lines that are equal after trimming but not equal are reported as one-line changes unless Options.IgnoreTrimWhitespace is set. A time limit (Options.MaxComputationTime, read through an injectable clock) turns the result into one change that covers both texts, with LinesDiff.HitTimeout set; a cancelled context stops the work with a DiffCancelled error. Moved-code detection is not built (SPEC §2).
Character diff (step 9.2) ¶
Compute runs the line stage and then refines every changed block of lines to character changes, as VS Code's refineDiff does: the characters of the block (LinesSliceCharSequence, with the white space at the start and end of the lines left out under Options.IgnoreTrimWhitespace) are aligned with dynamic programming (fewer than 500 characters in both blocks together) or Myers' algorithm; the changes are improved by optimizeSequenceDiffs with the boundary scores of characters, extended to whole words (extendDiffsToEntireWordIfAppropriate; to sub-words with Options.ExtendToSubwords), joined when they have at most two characters between them (removeShortMatches) and when large changes have a short text between them (removeVeryShortMatchingTextBetweenLongDiffs); the lines between the blocks that differ only in white space are refined as blocks of their own. The character changes are then grouped into line blocks (lineRangeMappingFromRangeMappings), which are the LineRangeMapping values with their Inner.
Columns are byte columns of the line (SPEC D6), where VS Code counts UTF-16 code units. The elements of the character sequences are code points (a byte that is not valid UTF-8 is an element of its own), so a change never splits a character; the limits that VS Code counts in code units (500 characters for dynamic programming, 20 for the text between changes, 100 for a large change) count code points here.
The time limit covers both stages with one clock; see Compute. ComputeLines is the line stage alone, and leaves Inner empty.
Three-way merge (step 9.3) ¶
ParseConflictMarkers finds the conflicts of a file with conflict markers (VS Code's merge-conflict extension), MergeInputsFromMarkers and MergeInputsFromFiles make the three texts of a merge (base, input 1 = incoming, input 2 = current), and MergeModel holds the merge: the ModifiedBaseRange values (the places where the inputs differ from the base, VS Code's diff3), the result, which starts as the base with every change that is not a conflict applied, and the conflicts with their place in the result, their state and whether they are handled. Accept, AcceptAll, Reset and ApplyResultEdit change the result and return the edits to make to the document of the editor. The model has no UI and no callbacks and is not safe for concurrent use.
Apply rebuilds the modified lines from the original lines and the changes, and Validate checks that a list of changes is a consistent diff of two texts of the given sizes.
Ported from VS Code: src/vs/editor/common/diff/defaultLinesDiffComputer/defaultLinesDiffComputer.ts Ported from VS Code: src/vs/editor/common/diff/defaultLinesDiffComputer/algorithms/diffAlgorithm.ts Ported from VS Code: src/vs/editor/common/diff/defaultLinesDiffComputer/algorithms/dynamicProgrammingDiffing.ts Ported from VS Code: src/vs/editor/common/diff/defaultLinesDiffComputer/algorithms/myersDiffAlgorithm.ts Ported from VS Code: src/vs/editor/common/diff/defaultLinesDiffComputer/heuristicSequenceOptimizations.ts Ported from VS Code: src/vs/editor/common/diff/defaultLinesDiffComputer/linesSliceCharSequence.ts Ported from VS Code: src/vs/editor/common/diff/defaultLinesDiffComputer/lineSequence.ts Ported from VS Code: src/vs/editor/common/diff/rangeMapping.ts Ported from VS Code: src/vs/editor/common/diff/linesDiffComputer.ts Ported from VS Code: extensions/merge-conflict/src/mergeConflictParser.ts Ported from VS Code: src/vs/workbench/contrib/mergeEditor/browser/model/modifiedBaseRange.ts Ported from VS Code: src/vs/workbench/contrib/mergeEditor/browser/model/mapping.ts Ported from VS Code: src/vs/workbench/contrib/mergeEditor/browser/model/mergeEditorModel.ts Ported from VS Code: src/vs/workbench/contrib/mergeEditor/browser/model/textModelDiffs.ts
Index ¶
- func Apply(original, modified []string, changes []LineRangeMapping) []string
- func ContainsConflictMarkers(text string) bool
- func JoinLines(lines []string, eol string) string
- func SplitLines(text string) (lines []string, eol string)
- func Validate(changes []LineRangeMapping, originalLines, modifiedLines int) error
- type Choice
- type Edit
- type Input
- type InputState
- type LineRange
- type LineRangeMapping
- type LinesDiff
- type MarkerConflict
- type MarkerSection
- type MergeInputs
- type MergeModel
- func (m *MergeModel) Accept(conflict int, choice Choice) (Edit, error)
- func (m *MergeModel) AcceptAll(choice Choice) ([]Edit, error)
- func (m *MergeModel) ApplyResultEdit(startLine, endLine int, newLines []string) error
- func (m *MergeModel) ConflictCount() int
- func (m *MergeModel) Conflicts() []Region
- func (m *MergeModel) EOL() string
- func (m *MergeModel) HandledCount() int
- func (m *MergeModel) HitTimeout() bool
- func (m *MergeModel) Inputs() MergeInputs
- func (m *MergeModel) RegionAtResultLine(line int) (r Region, ok bool)
- func (m *MergeModel) Regions() []Region
- func (m *MergeModel) Reset(conflict int) (Edit, error)
- func (m *MergeModel) Result() []string
- func (m *MergeModel) ResultLineCount() int
- func (m *MergeModel) ResultText() string
- func (m *MergeModel) SetHandled(conflict int, handled bool) error
- func (m *MergeModel) SetState(conflict int, s RangeState) (Edit, error)
- func (m *MergeModel) Toggle(conflict int, in Input) (Edit, error)
- func (m *MergeModel) UnhandledCount() int
- type MergeOptions
- type ModifiedBaseRange
- func (r ModifiedBaseRange) BaseLines() []string
- func (r ModifiedBaseRange) InputCombinedDiff(in Input) (m LineRangeMapping, ok bool)
- func (r ModifiedBaseRange) InputDiffs(in Input) []LineRangeMapping
- func (r ModifiedBaseRange) InputLines(in Input) []string
- func (r ModifiedBaseRange) InputRange(in Input) LineRange
- func (r ModifiedBaseRange) IsConflicting() bool
- func (r ModifiedBaseRange) TextForState(s RangeState) (text []string, effective RangeState)
- type Options
- type Position
- type Range
- type RangeMapping
- type RangeState
- type Region
- type StateKind
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func Apply ¶
func Apply(original, modified []string, changes []LineRangeMapping) []string
Apply rebuilds the modified lines: the lines of original that no change covers, and the lines of modified that the changes map to. Changes must be a valid diff of the two texts (see Validate); line numbers out of range are clipped.
func ContainsConflictMarkers ¶
ContainsConflictMarkers reports whether a text may contain a conflict: it has "<<<<<<<" and ">>>>>>>" somewhere (VS Code's containsConflict). It is a cheap test before ParseConflictMarkers.
func SplitLines ¶
SplitLines splits a text into lines without their line breaks ("\n" or "\r\n"; a text that ends with a line break has an empty last line) and returns the line break the text uses: the one that ends more lines, the first one on a tie, and "\n" for a text with no line break. Lines of a text with mixed line breaks lose theirs, so joining them with the one returned (JoinLines) makes all alike.
func Validate ¶
func Validate(changes []LineRangeMapping, originalLines, modifiedLines int) error
Validate checks that changes is a consistent diff of two texts of originalLines and modifiedLines lines: every range lies inside its text and no change is empty on both sides; the changes are sorted and do not touch (there is at least one unchanged line between two changes); and the unchanged lines before, between and after the changes are as many in the original as in the modified text. VS Code asserts the same.
The character changes (Inner) of a change are checked as far as the line counts allow: their lines lie in the text (a text always has at least one line) and in their block, or in the line before it or after it where the change starts at the end of a line or ends at the start of one; a range is not reversed; and the ranges are sorted and do not overlap. The columns need the text and are not checked here.
Types ¶
type Choice ¶
type Choice int
Choice says what a conflict is resolved to.
const ( // AcceptBase puts the lines of the base. AcceptBase Choice = iota + 1 // AcceptInput1 puts the lines of input 1 (incoming). AcceptInput1 // AcceptInput2 puts the lines of input 2 (current). AcceptInput2 // AcceptBoth puts the lines of input 2 (current) and then those of input // 1 (incoming). AcceptBoth )
The choices of Accept.
type Edit ¶
type Edit struct {
// StartLine is the first line replaced.
StartLine int
// EndLine is the line after the last one replaced.
EndLine int
// NewLines are the lines that take their place.
NewLines []string
}
Edit replaces the lines [StartLine, EndLine) (1-based, end exclusive, as LineRange) of the result by NewLines. An edit that a MergeModel method returns has been applied to the model already; the editor applies it to its document as one undoable edit.
type InputState ¶
type InputState int
InputState tells how an input takes part in a state (VS Code's InputState).
const ( // InputExcluded: the result does not have the input's lines. InputExcluded InputState = iota // InputFirst: the input is the only one included, or comes first in Both. InputFirst // InputSecond: the input comes second in Both. InputSecond )
The roles of an input in a state.
type LineRange ¶
LineRange is a range of lines: StartLine is the first line (1-based) and EndLine is the line after the last one, so a range is empty when both are equal. It is VS Code's LineRange.
type LineRangeMapping ¶
type LineRangeMapping struct {
// Original is the block in the original text.
Original LineRange
// Modified is the block in the modified text.
Modified LineRange
// Inner holds the character changes inside the block, sorted and not
// overlapping. ComputeLines leaves it empty; Compute fills it.
Inner []RangeMapping
}
LineRangeMapping maps a block of lines of the original text to the block of lines of the modified text that replaces it. It is VS Code's DetailedLineRangeMapping.
type LinesDiff ¶
type LinesDiff struct {
// Changes lists the changed blocks.
Changes []LineRangeMapping
// HitTimeout reports that the time limit was reached: the changes are then
// an approximation (one change that covers both texts when the line stage
// ran out of time; with Compute, the blocks that were not refined yet have
// one character change that covers the block).
HitTimeout bool
}
LinesDiff is the result of a lines diff. Changes are sorted, do not touch each other, and have the same number of unchanged lines between them in both texts (see Validate).
func Compute ¶
Compute computes the diff of two texts given as lines (see ComputeLines) with the character changes of every changed block in Inner: VS Code's default lines diff computer, DefaultLinesDiffComputer.computeDiff, without the detection of moved code (SPEC section 2). A text always has at least one line, so no lines at all count as one empty line.
opts.MaxComputationTime limits the whole work, the line stage and the character stage together, with one clock. When it is reached in the line stage the line diff is the one change that covers both texts; when it is reached in the character stage the blocks that are not refined yet have one character change that covers the whole block (as VS Code's algorithms return the trivial diff when their time is over). HitTimeout is set in both cases. The work stops with a DiffCancelled error when ctx is done.
func ComputeLines ¶
func ComputeLines(ctx context.Context, original, modified []string, opts Options) (LinesDiff, error)
ComputeLines computes the lines diff of two texts given as lines (the lines of a text without their line breaks; a text that ends with a line break has an empty last line). It is VS Code's default lines diff computer up to the refinement to character changes (see the package documentation), so each change has an empty Inner.
The work stops with a DiffCancelled error when ctx is done. When opts.MaxComputationTime is reached the result is the one change that covers both texts, with HitTimeout set.
type MarkerConflict ¶
type MarkerConflict struct {
// Range is the whole conflict, from the "<<<<<<<" line to the ">>>>>>>"
// line.
Range LineRange
// Current is the section after "<<<<<<<" (the first side, "ours", HEAD).
Current MarkerSection
// CommonAncestors are the "|||||||" sections (diff3 style) in order; there
// is none in a conflict without the base. There can be several (a merge
// with several bases).
CommonAncestors []MarkerSection
// Splitter is the "=======" line (a range of one line).
Splitter LineRange
// Incoming is the section before ">>>>>>>" (the other side, "theirs"):
// its Header is the footer line and its Content lies between the splitter
// and the footer.
Incoming MarkerSection
}
MarkerConflict is a conflict of a text with conflict markers. All line ranges are 1-based, end exclusive, in the lines that were scanned.
func ParseConflictMarkers ¶
func ParseConflictMarkers(lines []string) []MarkerConflict
ParseConflictMarkers finds the complete conflicts of a text given as lines without their line breaks. It is VS Code's MergeConflictParser.scanDocument:
- a marker line starts with the marker ("<<<<<<<", "|||||||", ">>>>>>>") and may go on with anything, so a longer run of the character ("<<<<<<<<") is a marker too; the splitter is the line "=======" and nothing else; lines that are empty or only white space are skipped;
- "|||||||" and "=======" count only inside a conflict and before its splitter (a second splitter is text of the conflict);
- a ">>>>>>>" inside a conflict ends it; when it has no splitter the conflict is dropped;
- a "<<<<<<<" inside a conflict (before its end) makes the scan give up: the conflicts found so far are returned, and nothing after the line is looked at;
- a conflict that is not closed at the end of the text is dropped.
Markers outside a conflict ("=======", ">>>>>>>") are ignored, so the function has no error. The conflicts are sorted and do not overlap.
type MarkerSection ¶
type MarkerSection struct {
// Header is the line of the marker (a range of one line).
Header LineRange
// Content is the lines of the section: those after the header up to the
// next marker (before it for the incoming side, where Content lies between
// the splitter and the footer). It can be empty.
Content LineRange
// Label is the text after the marker and one more character ("HEAD" in
// "<<<<<<< HEAD"), as VS Code takes it (the text after "<<<<<<<HEAD"
// without the space is "EAD"). It is empty when the marker has no text.
Label string
}
MarkerSection is a part of a conflict that has a header line: the current side (header "<<<<<<<"), a common ancestor (header "|||||||") or the incoming side (the footer ">>>>>>>", which is the header of the incoming content that comes before it).
type MergeInputs ¶
type MergeInputs struct {
// Base is the common ancestor.
Base []string
// Input1 is the incoming text.
Input1 []string
// Input2 is the current text.
Input2 []string
// Input1Label, Input2Label and BaseLabel describe the texts in the panes
// (the labels of the markers, a branch name or a path); they may be empty.
Input1Label, Input2Label, BaseLabel string
// EOL is the line break the result is written with: "\n" or "\r\n"; empty
// means "\n".
EOL string
// MarkedConflicts are the lines of Base where the conflicts of a file with
// conflict markers were (empty for a conflict without a base section, so
// at a position between two lines). A change that touches one is a
// conflict that has to be resolved even if only one side changed the base
// there (the side with no lines against a base with none), which the
// diffs alone could not tell. Empty for the inputs of --merge.
MarkedConflicts []LineRange
}
MergeInputs are the three texts of a three-way merge and what goes with them. Input1 is the incoming side (the left pane of the merge editor; "theirs"; the first path of --merge) and Input2 is the current side (the right pane; "ours", HEAD; the second path of --merge), as in VS Code's merge editor. Texts are lines without their line breaks; a text that ends with a line break has an empty last line, and a text with no line at all counts as one empty line.
func MergeInputsFromFiles ¶
func MergeInputsFromFiles(base, input1, input2 []string) MergeInputs
MergeInputsFromFiles makes the inputs of a merge of three texts (the --merge flow): base, input 1 (incoming) and input 2 (current). The texts are not copied and are not changed by the merge model.
func MergeInputsFromMarkerText ¶
func MergeInputsFromMarkerText(text string) (MergeInputs, error)
MergeInputsFromMarkerText makes the inputs of the merge of a file with conflict markers from its text (see MergeInputsFromMarkers), with the line break of the file in EOL. The error MergeMarkersNotFound reports a text without a complete conflict.
func MergeInputsFromMarkers ¶
func MergeInputsFromMarkers(lines []string, conflicts []MarkerConflict) MergeInputs
MergeInputsFromMarkers makes the inputs of the merge of a file with conflict markers (SPEC section 9.11 (a)): lines is the file and conflicts what ParseConflictMarkers found in it. Input 2 (current) is the file with every conflict taken from its current side, input 1 (incoming) the file with every conflict taken from its incoming side, and the base the file with every conflict taken from its first "|||||||" section, or with the conflict left out when it has none (an empty conflict region). The labels are those of the first conflict that has a label for the side. EOL is left empty.
type MergeModel ¶
type MergeModel struct {
// contains filtered or unexported fields
}
MergeModel is a three-way merge: the base and the two inputs, the ranges where the inputs differ from the base, and the result, which starts as the base with the changes of one input only (or the same change of both) applied. The ranges where both inputs changed the base differently are the conflicts; each holds the base lines and is unhandled until it is accepted. The model tracks the regions through edits of the result.
All lines are without line breaks (see MergeInputs); the result is written with the EOL of the inputs. A MergeModel is not safe for concurrent use.
func NewMergeModel ¶
func NewMergeModel(ctx context.Context, inputs MergeInputs, opts MergeOptions) (*MergeModel, error)
NewMergeModel diffs the base against each input and makes the model: the ranges (ModifiedBaseRangesFromDiffs) and the result, which is the base with the following applied (VS Code's reset and computeAutoMergedResult): a range that only input 2 changed takes the lines of input 2, a range that only input 1 changed the lines of input 1, and a range both changed in the same way the lines of input 1. These regions are handled. A range both changed differently keeps the base lines, in the state Base and unhandled; so does every range that touches one of inputs.MarkedConflicts. A text with no line counts as one empty line. The inputs are copied.
The error from the diffs (DiffCancelled when ctx is done) is returned as it is; MergeEOLInvalid reports an EOL that is neither "\n" nor "\r\n".
func (*MergeModel) Accept ¶
func (m *MergeModel) Accept(conflict int, choice Choice) (Edit, error)
Accept replaces the result of the conflict by what the choice says, marks the conflict handled and returns the edit made (see Edit). AcceptBoth puts the lines of input 2 (current) and then those of input 1 (incoming). A choice that includes only an input that did not change the range gives the base lines. Errors: MergeConflictIndexInvalid, MergeChoiceInvalid.
func (*MergeModel) AcceptAll ¶
func (m *MergeModel) AcceptAll(choice Choice) ([]Edit, error)
AcceptAll accepts the choice for every conflict that is not handled and returns the edits, from the last conflict to the first: each edit is valid both when the edits are applied in this order and relative to the result before the call. Error: MergeChoiceInvalid.
func (*MergeModel) ApplyResultEdit ¶
func (m *MergeModel) ApplyResultEdit(startLine, endLine int, newLines []string) error
ApplyResultEdit tells the model that the lines [startLine, endLine) of the result were replaced by newLines (a change made by the user to the result); the model makes the same change to its result. The regions follow the edit: those before it stay, those after it move, and those it overlaps (it replaces a line of them, or inserts inside them, or into an empty one) take the new lines (the first one overlapped) and are marked edited: their state becomes the one their lines are equal to (StateBase, StateInput1, StateInput2 or StateBoth with input 2 first, as VS Code's computeState does), or StateUnrecognized. The handled flag stays as it was. An edit at the very start or end of a region that does not replace a line of it is outside it. An edit that replaces what it replaces with the same lines changes nothing. Error: MergeEditInvalid for lines that are not in the result.
func (*MergeModel) ConflictCount ¶
func (m *MergeModel) ConflictCount() int
ConflictCount returns the number of conflicts.
func (*MergeModel) Conflicts ¶
func (m *MergeModel) Conflicts() []Region
Conflicts returns the regions the user has to resolve, in order; the index in the slice is the conflict index the other methods take.
func (*MergeModel) EOL ¶
func (m *MergeModel) EOL() string
EOL returns the line break the result is written with.
func (*MergeModel) HandledCount ¶
func (m *MergeModel) HandledCount() int
HandledCount returns the number of conflicts that are handled.
func (*MergeModel) HitTimeout ¶
func (m *MergeModel) HitTimeout() bool
HitTimeout reports that a diff hit its time limit, so the ranges are an approximation.
func (*MergeModel) Inputs ¶
func (m *MergeModel) Inputs() MergeInputs
Inputs returns the (normalized) inputs of the model; the slices are shared with the model and must not be changed.
func (*MergeModel) RegionAtResultLine ¶
func (m *MergeModel) RegionAtResultLine(line int) (r Region, ok bool)
RegionAtResultLine returns the region whose result range holds the line (1-based); ok is false when no region does. An empty region holds no line.
func (*MergeModel) Regions ¶
func (m *MergeModel) Regions() []Region
Regions returns every modified base range, conflicts or not, in order. Those that are not conflicts were applied to the result when the model was made.
func (*MergeModel) Reset ¶
func (m *MergeModel) Reset(conflict int) (Edit, error)
Reset puts the base lines in the result of the conflict again, in the state Base and unhandled, and returns the edit made. Error: MergeConflictIndexInvalid.
func (*MergeModel) Result ¶
func (m *MergeModel) Result() []string
Result returns the lines of the result (a copy).
func (*MergeModel) ResultLineCount ¶
func (m *MergeModel) ResultLineCount() int
ResultLineCount returns the number of lines of the result (without copying them, as Result does).
func (*MergeModel) ResultText ¶
func (m *MergeModel) ResultText() string
ResultText returns the result as a text with the model's line breaks: what a completed merge writes. The model does not forbid it while conflicts are unhandled (UnhandledCount); asking the user is for the caller.
func (*MergeModel) SetHandled ¶
func (m *MergeModel) SetHandled(conflict int, handled bool) error
SetHandled marks the conflict handled or not without touching the result (VS Code's "mark as handled"). Error: MergeConflictIndexInvalid.
func (*MergeModel) SetState ¶
func (m *MergeModel) SetState(conflict int, s RangeState) (Edit, error)
SetState puts the result of the conflict in the state (the general form of Accept; with Toggle it serves the checkboxes of the merge editor), marks the conflict handled and returns the edit made. StateUnrecognized is not a state to put (error MergeStateInvalid, as for a Both without a first input); MergeConflictIndexInvalid for a conflict that does not exist.
func (*MergeModel) Toggle ¶
func (m *MergeModel) Toggle(conflict int, in Input) (Edit, error)
Toggle includes the input in the conflict's result if it is not in, and excludes it if it is (RangeState.Toggle), as SetState does.
func (*MergeModel) UnhandledCount ¶
func (m *MergeModel) UnhandledCount() int
UnhandledCount returns the number of conflicts that are not handled.
type MergeOptions ¶
type MergeOptions struct {
// MaxComputationTime is the time limit of each of the two diffs (see
// Options.MaxComputationTime); zero means no limit.
MaxComputationTime time.Duration
// Now is the clock of the limit; nil means time.Now.
Now func() time.Time
}
MergeOptions are the options of NewMergeModel; the zero value is what VS Code's merge editor uses (no limit of time, white space counts).
type ModifiedBaseRange ¶
type ModifiedBaseRange struct {
// Base is the range of the base lines.
Base LineRange
// Input1 is the range of input 1 that corresponds to Base: Base with the
// changes of input 1 applied (equal in length to Base when input 1 did not
// change it).
Input1 LineRange
// Input2 is the same for input 2.
Input2 LineRange
// Input1Diffs are the changes from the base to input 1 inside Base, with
// their character changes; Input2Diffs those to input 2. At least one of
// the two is not empty.
Input1Diffs, Input2Diffs []LineRangeMapping
// IsEqualChange reports that both inputs changed the range in the same
// way: the same changes of the same lines to the same text.
IsEqualChange bool
// contains filtered or unexported fields
}
ModifiedBaseRange describes the modifications of input 1 and input 2 in one range of the base: the changes of the inputs that overlap or touch each other (a change of one input that shares a line with, or lies right next to, a change of the other is in the same range, as in VS Code). It is immutable; the slices are shared with the model and must not be changed.
func ModifiedBaseRangesFromDiffs ¶
func ModifiedBaseRangesFromDiffs(diffs1, diffs2 []LineRangeMapping, base, in1, in2 []string) ([]ModifiedBaseRange, error)
ModifiedBaseRangesFromDiffs aligns the changes from the base to input 1 and from the base to input 2 (both sorted, as Compute makes them) into ModifiedBaseRange values (VS Code's ModifiedBaseRange.fromDiffs and MappingAlignment.compute). The changes are sorted by their first base line (a change of input 1 before a change of input 2 on a tie) and are grouped as long as the base range of the next one intersects or touches the group (touching is: a change that ends at the line where the next begins; two insertions at the same place touch). The range of an input is the base range moved by the lines the earlier changes of that input added, and joined with its changes in the group.
The texts are the lines of the base and of the inputs the changes were computed from.
func (ModifiedBaseRange) BaseLines ¶
func (r ModifiedBaseRange) BaseLines() []string
BaseLines returns the lines of the base in this range.
func (ModifiedBaseRange) InputCombinedDiff ¶
func (r ModifiedBaseRange) InputCombinedDiff(in Input) (m LineRangeMapping, ok bool)
InputCombinedDiff returns the smallest change that covers the changes of the input in this range; ok is false when the input has none (VS Code's getInputCombinedDiff; the character changes are not kept).
func (ModifiedBaseRange) InputDiffs ¶
func (r ModifiedBaseRange) InputDiffs(in Input) []LineRangeMapping
InputDiffs returns the changes from the base to the input in this range.
func (ModifiedBaseRange) InputLines ¶
func (r ModifiedBaseRange) InputLines(in Input) []string
InputLines returns the lines of the input in this range (the lines of InputRange).
func (ModifiedBaseRange) InputRange ¶
func (r ModifiedBaseRange) InputRange(in Input) LineRange
InputRange returns the range of the input that corresponds to Base.
func (ModifiedBaseRange) IsConflicting ¶
func (r ModifiedBaseRange) IsConflicting() bool
IsConflicting reports that both inputs changed the range (VS Code's isConflicting; it is also true when they made the same change, see IsEqualChange).
func (ModifiedBaseRange) TextForState ¶
func (r ModifiedBaseRange) TextForState(s RangeState) (text []string, effective RangeState)
TextForState returns the lines that replace the range in the result when it is in the given state, and the state the lines really are (VS Code's getEditForBase, which returns the lines of the edit that applied to the base lines gives these): an input without changes in the range adds nothing, so a state that includes only such inputs gives the base lines and the state Base, and a state that includes one input with changes gives the lines of that input and the state of that input alone. The state Both gives the lines of the first input and then the lines of the other, whole ranges (VS Code's "dumb" combination; the "smart" one, which weaves the character changes of both inputs into the base, is not built).
type Options ¶
type Options struct {
// IgnoreTrimWhitespace makes lines that differ only in the white space at
// their start and end count as equal (VS Code's
// diffEditor.ignoreTrimWhitespace).
IgnoreTrimWhitespace bool
// MaxComputationTime is the time limit of the diff; zero means no limit
// (VS Code's diffEditor.maxComputationTime); it must not be negative.
MaxComputationTime time.Duration
// Now is the clock the time limit is measured with; nil means time.Now.
Now func() time.Time
// ExtendToSubwords makes a character change that covers part of a
// sub-word (fooBar has the sub-words foo and bar) cover all of it when
// the sub-word is mostly changed. VS Code's diff editor does not set it.
ExtendToSubwords bool
}
Options are the options of a diff.
type Position ¶
Position is a position in a text: Line and Column are 1-based, and Column counts bytes of the line, so the end of a line is len(line)+1.
type RangeMapping ¶
RangeMapping maps a range of the original text to the range of the modified text that replaces it (a character change).
type RangeState ¶
type RangeState struct {
// Kind is the kind of state.
Kind StateKind
// FirstInput is the input whose lines come first in StateBoth; it is zero
// in every other kind.
FirstInput Input
}
RangeState tells what the result has in the place of a ModifiedBaseRange (VS Code's ModifiedBaseRangeState); the zero value is the base. A state is a value; compare two with ==.
func BothState ¶
func BothState(first Input) RangeState
BothState returns the state that has the lines of both inputs, those of first before the others.
func (RangeState) IncludesInput ¶
func (s RangeState) IncludesInput(in Input) bool
IncludesInput reports whether the result has the lines of the input.
func (RangeState) InputState ¶
func (s RangeState) InputState(in Input) InputState
InputState returns the role of the input in the state.
func (RangeState) String ¶
func (s RangeState) String() string
String returns the kind of the state, with the first input for Both.
func (RangeState) Swap ¶
func (s RangeState) Swap() RangeState
Swap returns the state with the inputs exchanged.
func (RangeState) Toggle ¶
func (s RangeState) Toggle(in Input) RangeState
Toggle returns the state with the input included if it was not and excluded if it was (VS Code's toggle).
func (RangeState) WithInputValue ¶
func (s RangeState) WithInputValue(in Input, value bool) RangeState
WithInputValue returns the state that includes the input (value true) or not (value false) and the other input as before. Including the second input into a state of one input gives Both with the input of the state first. (VS Code's state of input 1 with input 2 excluded returns the state of input 2, which is a slip; here it stays as it is.)
type Region ¶
type Region struct {
// Index is the index of the region in Regions.
Index int
// ConflictIndex is the index of the region in Conflicts, or -1.
ConflictIndex int
// Range holds the base range, the ranges of the inputs and the changes.
Range ModifiedBaseRange
// Result is where the region is in the result now (1-based, end
// exclusive; it follows the edits of the result).
Result LineRange
// State tells what the result has there; StateUnrecognized when the
// region was edited by hand and is none of the other states.
State RangeState
// Handled reports that the region is dealt with: it was applied by the
// model (a change of one input) or accepted by the user; or the user said
// so (SetHandled). A manual edit does not change it.
Handled bool
// Conflict reports that the user has to resolve the region: both inputs
// changed it differently (or it was a conflict in the marker file).
Conflict bool
}
Region is a modified base range with its place in the result and its state (a snapshot: it does not change when the model does).
type StateKind ¶
type StateKind int
StateKind is the kind of a RangeState.
const ( // StateBase: the result has the lines of the base (the zero value). StateBase StateKind = iota // StateInput1: the result has the changes of input 1 (incoming) only. StateInput1 // StateInput2: the result has the changes of input 2 (current) only. StateInput2 // StateBoth: the result has the lines of both inputs, FirstInput first. StateBoth // StateUnrecognized: the result is none of the above (it was edited by // hand). It is never set by the model's own operations. StateUnrecognized )
The kinds of state of a range of the result.