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

Refactors diff algorithm #192411

Merged
merged 1 commit into from
Sep 7, 2023
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
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,11 @@ export class MyersDiffAlgorithm implements IDiffAlgorithm {
return DiffAlgorithmResult.trivial(seq1, seq2);
}

const seqX = seq1; // Text on the x axis
const seqY = seq2; // Text on the y axis

function getXAfterSnake(x: number, y: number): number {
while (x < seq1.length && y < seq2.length && seq1.getElement(x) === seq2.getElement(y)) {
while (x < seqX.length && y < seqY.length && seqX.getElement(x) === seqY.getElement(y)) {
x++;
y++;
}
Expand All @@ -29,6 +32,7 @@ export class MyersDiffAlgorithm implements IDiffAlgorithm {
// V[k]: X value of longest d-line that ends in diagonal k.
// d-line: path from (0,0) to (x,y) that uses exactly d non-diagonals.
// diagonal k: Set of points (x,y) with x-y = k.
// k=1 -> (1,0),(2,1)
const V = new FastInt32Array();
V.set(0, getXAfterSnake(0, 0));

Expand All @@ -40,18 +44,21 @@ export class MyersDiffAlgorithm implements IDiffAlgorithm {
loop: while (true) {
d++;
if (!timeout.isValid()) {
return DiffAlgorithmResult.trivialTimedOut(seq1, seq2);
return DiffAlgorithmResult.trivialTimedOut(seqX, seqY);
}
// The paper has `for (k = -d; k <= d; k += 2)`, but we can ignore diagonals that cannot influence the result.
const lowerBound = -Math.min(d, seq2.length + (d % 2));
const upperBound = Math.min(d, seq1.length + (d % 2));
const lowerBound = -Math.min(d, seqY.length + (d % 2));
const upperBound = Math.min(d, seqX.length + (d % 2));
for (k = lowerBound; k <= upperBound; k += 2) {
let step = 0;
// We can use the X values of (d-1)-lines to compute X value of the longest d-lines.
const maxXofDLineTop = k === upperBound ? -1 : V.get(k + 1); // We take a vertical non-diagonal (add a symbol in seq1)
const maxXofDLineLeft = k === lowerBound ? -1 : V.get(k - 1) + 1; // We take a horizontal non-diagonal (+1 x) (delete a symbol in seq1)
const x = Math.min(Math.max(maxXofDLineTop, maxXofDLineLeft), seq1.length);
const maxXofDLineTop = k === upperBound ? -1 : V.get(k + 1); // We take a vertical non-diagonal (add a symbol in seqX)
const maxXofDLineLeft = k === lowerBound ? -1 : V.get(k - 1) + 1; // We take a horizontal non-diagonal (+1 x) (delete a symbol in seqX)
step++;
const x = Math.min(Math.max(maxXofDLineTop, maxXofDLineLeft), seqX.length);
const y = x - k;
if (x > seq1.length || y > seq2.length) {
step++;
if (x > seqX.length || y > seqY.length) {
// This diagonal is irrelevant for the result.
// TODO: Don't pay the cost for this in the next iteration.
continue;
Expand All @@ -61,16 +68,16 @@ export class MyersDiffAlgorithm implements IDiffAlgorithm {
const lastPath = x === maxXofDLineTop ? paths.get(k + 1) : paths.get(k - 1);
paths.set(k, newMaxX !== x ? new SnakePath(lastPath, x, y, newMaxX - x) : lastPath);

if (V.get(k) === seq1.length && V.get(k) - k === seq2.length) {
if (V.get(k) === seqX.length && V.get(k) - k === seqY.length) {
break loop;
}
}
}

let path = paths.get(k);
const result: SequenceDiff[] = [];
let lastAligningPosS1: number = seq1.length;
let lastAligningPosS2: number = seq2.length;
let lastAligningPosS1: number = seqX.length;
let lastAligningPosS2: number = seqY.length;

while (true) {
const endX = path ? path.x + path.length : 0;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,24 @@
import * as assert from 'assert';
import { Range } from 'vs/editor/common/core/range';
import { RangeMapping } from 'vs/editor/common/diff/rangeMapping';
import { getLineRangeMapping } from 'vs/editor/common/diff/defaultLinesDiffComputer/defaultLinesDiffComputer';
import { OffsetRange } from 'vs/editor/common/core/offsetRange';
import { getLineRangeMapping } from 'vs/editor/common/diff/defaultLinesDiffComputer/defaultLinesDiffComputer';
import { LinesSliceCharSequence } from 'vs/editor/common/diff/defaultLinesDiffComputer/linesSliceCharSequence';
import { MyersDiffAlgorithm } from 'vs/editor/common/diff/defaultLinesDiffComputer/algorithms/myersDiffAlgorithm';
import { DynamicProgrammingDiffing } from 'vs/editor/common/diff/defaultLinesDiffComputer/algorithms/dynamicProgrammingDiffing';

suite('lineRangeMapping', () => {
suite('myers', () => {
test('1', () => {
const s1 = new LinesSliceCharSequence(['hello world'], new OffsetRange(0, 1), true);
const s2 = new LinesSliceCharSequence(['hallo welt'], new OffsetRange(0, 1), true);

const a = true ? new MyersDiffAlgorithm() : new DynamicProgrammingDiffing();
a.compute(s1, s2);
});
});

suite('lineRangeMapping', () => {
test('Simple', () => {
assert.deepStrictEqual(
getLineRangeMapping(
new RangeMapping(
Expand All @@ -32,7 +44,7 @@ suite('lineRangeMapping', () => {
);
});

test('2', () => {
test('Empty Lines', () => {
assert.deepStrictEqual(
getLineRangeMapping(
new RangeMapping(
Expand All @@ -56,8 +68,6 @@ suite('lineRangeMapping', () => {
});

suite('LinesSliceCharSequence', () => {
// Create tests for translateOffset

const sequence = new LinesSliceCharSequence(
[
'line1: foo',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import { DetailedLineRangeMapping } from 'vs/editor/common/diff/rangeMapping';
import { LegacyLinesDiffComputer } from 'vs/editor/common/diff/legacyLinesDiffComputer';
import { DefaultLinesDiffComputer } from 'vs/editor/common/diff/defaultLinesDiffComputer/defaultLinesDiffComputer';

suite('diff fixtures', () => {
suite('diffing fixtures', () => {
setup(() => {
setUnexpectedErrorHandler(e => {
throw e;
Expand Down