Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Hediet/merge-editor-append-apply #164120

Merged
merged 4 commits into from Oct 21, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
46 changes: 46 additions & 0 deletions src/vs/editor/common/diff/algorithms/joinSequenceDiffs.ts
@@ -0,0 +1,46 @@
/*---------------------------------------------------------------------------------------------
* Copyright (c) Microsoft Corporation. All rights reserved.
* Licensed under the MIT License. See License.txt in the project root for license information.
*--------------------------------------------------------------------------------------------*/

import { ISequence, OffsetRange, SequenceDiff } from 'vs/editor/common/diff/algorithms/diffAlgorithm';

/**
* This function fixes issues like this:
* ```
* import { Baz, Bar } from "foo";
* ```
* <->
* ```
* import { Baz, Bar, Foo } from "foo";
* ```
* Computed diff: [ {Add "," after Bar}, {Add "Foo " after space} }
* Improved diff: [{Add ", Foo" after Bar}]
*/
export function joinSequenceDiffs(sequence1: ISequence, sequence2: ISequence, sequenceDiffs: SequenceDiff[]): SequenceDiff[] {
const result: SequenceDiff[] = [];
if (sequenceDiffs.length > 0) {
result.push(sequenceDiffs[0]);
}

for (let i = 1; i < sequenceDiffs.length; i++) {
const lastResult = result[result.length - 1];
const cur = sequenceDiffs[i];

if (cur.seq1Range.start - lastResult.seq1Range.endExclusive === 1) {
if (cur.seq1Range.isEmpty) {
if (sequence2.getElement(cur.seq2Range.start - 1) === sequence2.getElement(cur.seq2Range.endExclusive - 1)) {
result[result.length - 1] = new SequenceDiff(lastResult.seq1Range, new OffsetRange(
lastResult.seq2Range.start,
cur.seq2Range.endExclusive - 1
));
continue;
}
}
}

result.push(cur);
}

return result;
}
4 changes: 3 additions & 1 deletion src/vs/editor/common/diff/standardLinesDiffComputer.ts
Expand Up @@ -8,6 +8,7 @@ import { Position } from 'vs/editor/common/core/position';
import { Range } from 'vs/editor/common/core/range';
import { SequenceFromIntArray, OffsetRange, SequenceDiff, ISequence } from 'vs/editor/common/diff/algorithms/diffAlgorithm';
import { DynamicProgrammingDiffing } from 'vs/editor/common/diff/algorithms/dynamicProgrammingDiffing';
import { joinSequenceDiffs } from 'vs/editor/common/diff/algorithms/joinSequenceDiffs';
import { MyersDiffAlgorithm } from 'vs/editor/common/diff/algorithms/myersDiffAlgorithm';
import { ILinesDiff, ILinesDiffComputer, ILinesDiffComputerOptions, LineRange, LineRangeMapping, RangeMapping } from 'vs/editor/common/diff/linesDiffComputer';

Expand Down Expand Up @@ -108,7 +109,8 @@ export class StandardLinesDiffComputer implements ILinesDiffComputer {
const sourceSlice = new Slice(originalLines, diff.seq1Range);
const targetSlice = new Slice(modifiedLines, diff.seq2Range);

const diffs = this.myersDiffingAlgorithm.compute(sourceSlice, targetSlice);
const originalDiffs = this.myersDiffingAlgorithm.compute(sourceSlice, targetSlice);
const diffs = joinSequenceDiffs(sourceSlice, targetSlice, originalDiffs);
const result = diffs.map(
(d) =>
new RangeMapping(
Expand Down
Expand Up @@ -85,8 +85,7 @@ suite('standardLinesDiffCompute', () => {
{
main: "{[5,6)->[5,8)}",
inner: [
"{[5,41 -> 5,41]->[5,41 -> 5,42]}",
"{[6,1 -> 6,1]->[6,1 -> 8,1]}",
"{[5,41 -> 5,41]->[5,41 -> 7,28]}"
]
}
]
Expand Down
Expand Up @@ -16,7 +16,7 @@ import { LineRange } from 'vs/workbench/contrib/mergeEditor/browser/model/lineRa
import { DetailedLineRangeMapping, DocumentLineRangeMap, DocumentRangeMap, LineRangeMapping } from 'vs/workbench/contrib/mergeEditor/browser/model/mapping';
import { TextModelDiffChangeReason, TextModelDiffs, TextModelDiffState } from 'vs/workbench/contrib/mergeEditor/browser/model/textModelDiffs';
import { leftJoin } from 'vs/workbench/contrib/mergeEditor/browser/utils';
import { ModifiedBaseRange, ModifiedBaseRangeState } from './modifiedBaseRange';
import { ModifiedBaseRange, ModifiedBaseRangeState, ModifiedBaseRangeStateKind } from './modifiedBaseRange';

export interface InputData {
readonly textModel: ITextModel;
Expand All @@ -41,7 +41,7 @@ export class MergeEditorModel extends EditorModel {
this.modifiedBaseRanges.read(reader).map((s) => [
s,
{
accepted: observableValue(`BaseRangeState${s.baseRange}`, ModifiedBaseRangeState.default),
accepted: observableValue(`BaseRangeState${s.baseRange}`, ModifiedBaseRangeState.base),
handled: observableValue(`BaseRangeHandledState${s.baseRange}`, false),
}
])
Expand Down Expand Up @@ -105,7 +105,7 @@ export class MergeEditorModel extends EditorModel {
shouldRecomputeHandledFromAccepted = false;
for (const [_range, observableState] of states) {
const state = observableState.accepted.get();
observableState.handled.set(!(state.isEmpty || state.conflicting), tx);
observableState.handled.set(!(state.kind === ModifiedBaseRangeStateKind.base || state.kind === ModifiedBaseRangeStateKind.unrecognized), tx);
}
}
});
Expand All @@ -132,13 +132,13 @@ export class MergeEditorModel extends EditorModel {
let newState: ModifiedBaseRangeState;
let handled = false;
if (range.input1Diffs.length === 0) {
newState = ModifiedBaseRangeState.default.withInput2(true);
newState = ModifiedBaseRangeState.base.withInputValue(2, true);
handled = true;
} else if (range.input2Diffs.length === 0) {
newState = ModifiedBaseRangeState.default.withInput1(true);
newState = ModifiedBaseRangeState.base.withInputValue(1, true);
handled = true;
} else {
newState = ModifiedBaseRangeState.default;
newState = ModifiedBaseRangeState.base;
handled = false;
}

Expand Down Expand Up @@ -335,7 +335,7 @@ export class MergeEditorModel extends EditorModel {

private computeState(baseRange: ModifiedBaseRange, conflictingDiffs: DetailedLineRangeMapping[]): ModifiedBaseRangeState {
if (conflictingDiffs.length === 0) {
return ModifiedBaseRangeState.default;
return ModifiedBaseRangeState.base;
}
const conflictingEdits = conflictingDiffs.map((d) => d.getLineEdit());

Expand All @@ -348,15 +348,15 @@ export class MergeEditorModel extends EditorModel {
}

if (editsAgreeWithDiffs(baseRange.input1Diffs)) {
return ModifiedBaseRangeState.default.withInput1(true);
return ModifiedBaseRangeState.base.withInputValue(1, true);
}
if (editsAgreeWithDiffs(baseRange.input2Diffs)) {
return ModifiedBaseRangeState.default.withInput2(true);
return ModifiedBaseRangeState.base.withInputValue(2, true);
}

const states = [
ModifiedBaseRangeState.default.withInput1(true).withInput2(true),
ModifiedBaseRangeState.default.withInput2(true).withInput1(true),
ModifiedBaseRangeState.base.withInputValue(1, true).withInputValue(2, true),
ModifiedBaseRangeState.base.withInputValue(2, true).withInputValue(1, true),
];

for (const s of states) {
Expand All @@ -371,7 +371,7 @@ export class MergeEditorModel extends EditorModel {
}
}

return ModifiedBaseRangeState.conflicting;
return ModifiedBaseRangeState.unrecognized;
}

public getState(baseRange: ModifiedBaseRange): IObservable<ModifiedBaseRangeState> {
Expand Down Expand Up @@ -429,8 +429,8 @@ export class MergeEditorModel extends EditorModel {
/** @description Reset Unknown Base Range States */
this.resultTextModel.pushStackElement();
for (const range of this.modifiedBaseRanges.get()) {
if (this.getState(range).get().conflicting) {
this.setState(range, ModifiedBaseRangeState.default, false, tx, false);
if (this.getState(range).get().kind === ModifiedBaseRangeStateKind.unrecognized) {
this.setState(range, ModifiedBaseRangeState.base, false, tx, false);
}
}
this.resultTextModel.pushStackElement();
Expand Down Expand Up @@ -515,7 +515,7 @@ export class MergeEditorModel extends EditorModel {
resultStartLineNumber = resultRange.endLineNumberExclusive;

outputLines.push('<<<<<<<');
if (state.accepted.get().conflicting) {
if (state.accepted.get().kind === ModifiedBaseRangeStateKind.unrecognized) {
// to prevent loss of data, use modified result as "ours"
appendLinesToResult(resultLines, resultRange);
} else {
Expand Down