From 2e2654c97f508f7232ef467c80525f219e3c5b08 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jo=C3=A3o=20Moreno?= Date: Fri, 22 Apr 2022 12:31:15 +0200 Subject: [PATCH] address heuristic when stage ranges act on same line number diff (#145899) Co-authored-by: @lszomoru Fixes: #126989 --- extensions/git/src/staging.ts | 28 ++++++++++++++++++++++------ 1 file changed, 22 insertions(+), 6 deletions(-) diff --git a/extensions/git/src/staging.ts b/extensions/git/src/staging.ts index c2af4a575b5..36e9c6f26e1 100644 --- a/extensions/git/src/staging.ts +++ b/extensions/git/src/staging.ts @@ -109,12 +109,28 @@ export function intersectDiffWithRange(textDocument: TextDocument, diff: LineCha if (diff.modifiedEndLineNumber === 0) { return diff; } else { - return { - originalStartLineNumber: diff.originalStartLineNumber, - originalEndLineNumber: diff.originalEndLineNumber, - modifiedStartLineNumber: intersection.start.line + 1, - modifiedEndLineNumber: intersection.end.line + 1 - }; + const modifiedStartLineNumber = intersection.start.line + 1; + const modifiedEndLineNumber = intersection.end.line + 1; + + // heuristic: same number of lines on both sides, let's assume line by line + if (diff.originalEndLineNumber - diff.originalStartLineNumber === diff.modifiedEndLineNumber - diff.modifiedStartLineNumber) { + const delta = modifiedStartLineNumber - diff.modifiedStartLineNumber; + const length = modifiedEndLineNumber - modifiedStartLineNumber; + + return { + originalStartLineNumber: diff.originalStartLineNumber + delta, + originalEndLineNumber: diff.originalStartLineNumber + delta + length, + modifiedStartLineNumber, + modifiedEndLineNumber + }; + } else { + return { + originalStartLineNumber: diff.originalStartLineNumber, + originalEndLineNumber: diff.originalEndLineNumber, + modifiedStartLineNumber, + modifiedEndLineNumber + }; + } } }