From aae730f0f1a1aecba3d153901c036882d7c269bc Mon Sep 17 00:00:00 2001 From: Silvano Cerza Date: Thu, 20 Feb 2025 19:15:56 +0100 Subject: [PATCH] Handle line numbers greater than document lines number --- src/views/conflicts-resolution/diff.ts | 16 +++++++++++++++- 1 file changed, 15 insertions(+), 1 deletion(-) diff --git a/src/views/conflicts-resolution/diff.ts b/src/views/conflicts-resolution/diff.ts index 3ff7a66..4c8563d 100644 --- a/src/views/conflicts-resolution/diff.ts +++ b/src/views/conflicts-resolution/diff.ts @@ -87,7 +87,21 @@ function diff(oldText: string, newText: string): DiffChunk[] { } } - return mergeDiffs(result); + return mergeDiffs( + result.map((chunk) => ({ + ...chunk, + startLeftLine: clampLine(chunk.startLeftLine, oldLines.length), + endLeftLine: clampLine(chunk.endLeftLine, oldLines.length), + startRightLine: clampLine(chunk.startRightLine, newLines.length), + endRightLine: clampLine(chunk.endRightLine, newLines.length), + })), + ); +} + +// Use to make sure the line number in the diff chunk doesn't exceed +// the number of lines of the document. +function clampLine(line: number, maxLines: number): number { + return Math.min(Math.max(1, line), maxLines + 1); } function mergeDiffs(chunks: DiffChunk[]): DiffChunk[] {