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

Check sequences is strongly equal. #219937

Open
wants to merge 6 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -155,6 +155,8 @@ export interface ISequence {
* It prevents shifting to less matching lines.
*/
isStronglyEqual(offset1: number, offset2: number): boolean;

getLineValue(offset: number): string;
}

export interface ITimeout {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,9 +48,30 @@ function joinSequenceDiffsByShifting(sequence1: ISequence, sequence2: ISequence,
const length = cur.seq1Range.start - prevResult.seq1Range.endExclusive;
let d;
for (d = 1; d <= length; d++) {
const seq1OffsetS = cur.seq1Range.start - d;
const seq1OffsetE = cur.seq1Range.endExclusive - d;
const seq2OffsetS = cur.seq2Range.start - d;
const seq2OffsetE = cur.seq2Range.endExclusive - d;
if (
sequence1.getElement(cur.seq1Range.start - d) !== sequence1.getElement(cur.seq1Range.endExclusive - d) ||
sequence2.getElement(cur.seq2Range.start - d) !== sequence2.getElement(cur.seq2Range.endExclusive - d)) {
sequence1.getElement(seq1OffsetS) !== sequence1.getElement(seq1OffsetE) ||
sequence2.getElement(seq2OffsetS) !== sequence2.getElement(seq2OffsetE) ||
/**
* fixes issues like this:
* seq1: seq2:
* [ pass]=============[ pass]
* [.] ^
* [.] |
* [.] |
* [ pass]
*
* 1. The start and end elements of seq1 are equal.
* 2. The start item of seq1 and seq2 need to be strongly equal.
*
* Satisfying the above will skip optimization.
*/
((!sequence1.isStronglyEqual(seq1OffsetS, seq1OffsetE) ||
!sequence2.isStronglyEqual(seq2OffsetS, seq2OffsetE)) &&
sequence1.getLineValue(seq1OffsetS) === sequence2.getLineValue(seq2OffsetS))) {
break;
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,10 @@ export class LineSequence implements ISequence {
isStronglyEqual(offset1: number, offset2: number): boolean {
return this.lines[offset1] === this.lines[offset2];
}

public getLineValue(offset: number): string {
return this.lines[offset];
}
}

function getIndentation(str: string): number {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -165,6 +165,10 @@ export class LinesSliceCharSequence implements ISequence {
const end = findFirstMonotonous(this.firstCharOffsetByLine, x => range.endExclusive <= x) ?? this.elements.length;
return new OffsetRange(start, end);
}

public getLineValue(offset: number): string {
return this.lines[offset];
}
}

function isWordChar(charCode: number): boolean {
Expand Down
12 changes: 12 additions & 0 deletions src/vs/editor/test/node/diffing/fixtures/issue-214962/1.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
print("Left")


if 1:
if 2:
pass

if 3:
pass


print("Done")
9 changes: 9 additions & 0 deletions src/vs/editor/test/node/diffing/fixtures/issue-214962/2.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
print("Right")


if 1:
if 2:
pass


print("Done")
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
{
"original": {
"content": "print(\"Left\")\n\n\nif 1:\n if 2:\n pass\n\nif 3:\n pass\n\n\nprint(\"Done\")\n",
"fileName": "./1.txt"
},
"modified": {
"content": "print(\"Right\")\n\n\nif 1:\n if 2:\n pass\n\n\nprint(\"Done\")\n",
"fileName": "./2.txt"
},
"diffs": [
{
"originalRange": "[1,2)",
"modifiedRange": "[1,2)",
"innerChanges": [
{
"originalRange": "[1,8 -> 1,12]",
"modifiedRange": "[1,8 -> 1,13]"
}
]
},
{
"originalRange": "[8,11)",
"modifiedRange": "[8,8)",
"innerChanges": [
{
"originalRange": "[8,1 -> 11,1 EOL]",
"modifiedRange": "[8,1 -> 8,1 EOL]"
}
]
}
]
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
{
"original": {
"content": "print(\"Left\")\n\n\nif 1:\n if 2:\n pass\n\nif 3:\n pass\n\n\nprint(\"Done\")\n",
"fileName": "./1.txt"
},
"modified": {
"content": "print(\"Right\")\n\n\nif 1:\n if 2:\n pass\n\n\nprint(\"Done\")\n",
"fileName": "./2.txt"
},
"diffs": [
{
"originalRange": "[1,2)",
"modifiedRange": "[1,2)",
"innerChanges": [
{
"originalRange": "[1,8 -> 1,11]",
"modifiedRange": "[1,8 -> 1,12]"
}
]
},
{
"originalRange": "[8,11)",
"modifiedRange": "[8,8)",
"innerChanges": null
}
]
}