mirror of
https://github.com/silvanocerza/github-gitless-sync.git
synced 2026-07-22 05:41:36 +00:00
Handle line numbers greater than document lines number
This commit is contained in:
parent
3a7454b86f
commit
aae730f0f1
1 changed files with 15 additions and 1 deletions
|
|
@ -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[] {
|
||||
|
|
|
|||
Loading…
Reference in a new issue