From ec278082f8fca8073fcfe365a7f873d98acac1b2 Mon Sep 17 00:00:00 2001 From: Henning Dieterichs Date: Fri, 2 Jun 2023 15:54:26 +0200 Subject: [PATCH] Fixes line range mapping construction bug. --- .../common/diff/standardLinesDiffComputer.ts | 20 ++++++++------ .../node/diffing/lineRangeMapping.test.ts | 27 ++++++++++++++++--- 2 files changed, 36 insertions(+), 11 deletions(-) diff --git a/src/vs/editor/common/diff/standardLinesDiffComputer.ts b/src/vs/editor/common/diff/standardLinesDiffComputer.ts index 47e7c76da35..c502bf4f0eb 100644 --- a/src/vs/editor/common/diff/standardLinesDiffComputer.ts +++ b/src/vs/editor/common/diff/standardLinesDiffComputer.ts @@ -280,19 +280,23 @@ export function getLineRangeMapping(rangeMapping: RangeMapping, originalLines: s // rangeMapping describes the edit that replaces `rangeMapping.originalRange` with `newText := getText(modifiedLines, rangeMapping.modifiedRange)`. - // original: xxx[ \n <- this line is not modified - // modified: xxx[ \n - if (rangeMapping.modifiedRange.startColumn - 1 >= modifiedLines[rangeMapping.modifiedRange.startLineNumber - 1].length - && rangeMapping.originalRange.startColumn - 1 >= originalLines[rangeMapping.originalRange.startLineNumber - 1].length) { - lineStartDelta = 1; // +1 is always possible, as startLineNumber < endLineNumber + 1 - } - // original: ]xxx \n <- this line is not modified // modified: ]xx \n if (rangeMapping.modifiedRange.endColumn === 1 && rangeMapping.originalRange.endColumn === 1 && rangeMapping.originalRange.startLineNumber + lineStartDelta <= rangeMapping.originalRange.endLineNumber && rangeMapping.modifiedRange.startLineNumber + lineStartDelta <= rangeMapping.modifiedRange.endLineNumber) { - lineEndDelta = -1; // We can only do this if the range is not empty yet + // We can only do this if the range is not empty yet + lineEndDelta = -1; + } + + // original: xxx[ \n <- this line is not modified + // modified: xxx[ \n + if (rangeMapping.modifiedRange.startColumn - 1 >= modifiedLines[rangeMapping.modifiedRange.startLineNumber - 1].length + && rangeMapping.originalRange.startColumn - 1 >= originalLines[rangeMapping.originalRange.startLineNumber - 1].length + && rangeMapping.originalRange.startLineNumber <= rangeMapping.originalRange.endLineNumber + lineEndDelta + && rangeMapping.modifiedRange.startLineNumber <= rangeMapping.modifiedRange.endLineNumber + lineEndDelta) { + // We can only do this if the range is not empty yet + lineStartDelta = 1; } const originalLineRange = new LineRange( diff --git a/src/vs/editor/test/node/diffing/lineRangeMapping.test.ts b/src/vs/editor/test/node/diffing/lineRangeMapping.test.ts index 9b2fd2181a0..5cda2c94794 100644 --- a/src/vs/editor/test/node/diffing/lineRangeMapping.test.ts +++ b/src/vs/editor/test/node/diffing/lineRangeMapping.test.ts @@ -9,8 +9,7 @@ import { RangeMapping } from 'vs/editor/common/diff/linesDiffComputer'; import { getLineRangeMapping } from 'vs/editor/common/diff/standardLinesDiffComputer'; suite('lineRangeMapping', () => { - test('lineRangeMapping', () => { - // {[2,1 -> 3,1]->[2,1 -> 2,1]} + test('1', () => { assert.deepStrictEqual( getLineRangeMapping( new RangeMapping( @@ -27,7 +26,29 @@ suite('lineRangeMapping', () => { '' ] ).toString(), - "{[3,4)->[3,3)}" + "{[2,3)->[2,2)}" + ); + }); + + test('2', () => { + assert.deepStrictEqual( + getLineRangeMapping( + new RangeMapping( + new Range(2, 1, 2, 1), + new Range(2, 1, 4, 1), + ), + [ + '', + '', + ], + [ + '', + '', + '', + '', + ] + ).toString(), + "{[2,2)->[2,4)}" ); }); });