Skip to content

Commit 17e0dec

Browse files
committed
Fixes #198471
1 parent 93a6a2a commit 17e0dec

File tree

2 files changed

+22
-4
lines changed

2 files changed

+22
-4
lines changed

src/vs/editor/browser/widget/diffEditor/components/diffEditorViewZones/diffEditorViewZones.ts

Lines changed: 20 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -159,10 +159,17 @@ export class DiffEditorViewZones extends Disposable {
159159

160160
const deletedCodeLineBreaksComputer = !renderSideBySide ? this._editors.modified._getViewModel()?.createLineBreaksComputer() : undefined;
161161
if (deletedCodeLineBreaksComputer) {
162+
const originalModel = this._editors.original.getModel()!;
162163
for (const a of alignmentsVal) {
163164
if (a.diff) {
164165
for (let i = a.originalRange.startLineNumber; i < a.originalRange.endLineNumberExclusive; i++) {
165-
deletedCodeLineBreaksComputer?.addRequest(this._editors.original.getModel()!.getLineContent(i), null, null);
166+
// `i` can be out of bound when the diff has not been updated yet.
167+
// In this case, we do an early return.
168+
// TODO@hediet: Fix this by applying the edit directly to the diff model, so that the diff is always valid.
169+
if (i > originalModel.getLineCount()) {
170+
return { orig: origViewZones, mod: modViewZones };
171+
}
172+
deletedCodeLineBreaksComputer?.addRequest(originalModel.getLineContent(i), null, null);
166173
}
167174
}
168175
}
@@ -186,8 +193,15 @@ export class DiffEditorViewZones extends Disposable {
186193

187194
const deletedCodeDomNode = document.createElement('div');
188195
deletedCodeDomNode.classList.add('view-lines', 'line-delete', 'monaco-mouse-cursor-text');
196+
const originalModel = this._editors.original.getModel()!;
197+
// `a.originalRange` can be out of bound when the diff has not been updated yet.
198+
// In this case, we do an early return.
199+
// TODO@hediet: Fix this by applying the edit directly to the diff model, so that the diff is always valid.
200+
if (a.originalRange.endLineNumberExclusive - 1 > originalModel.getLineCount()) {
201+
return { orig: origViewZones, mod: modViewZones };
202+
}
189203
const source = new LineSource(
190-
a.originalRange.mapToLineArray(l => this._editors.original.getModel()!.tokenization.getLineTokens(l)),
204+
a.originalRange.mapToLineArray(l => originalModel.tokenization.getLineTokens(l)),
191205
a.originalRange.mapToLineArray(_ => lineBreakData[lineBreakDataIdx++]),
192206
mightContainNonBasicASCII,
193207
mightContainRTL,
@@ -551,7 +565,10 @@ function computeRangeAlignment(
551565
// There is some unmodified text on this line before the diff
552566
emitAlignment(i.originalRange.startLineNumber, i.modifiedRange.startLineNumber);
553567
}
554-
if (i.originalRange.endColumn < originalEditor.getModel()!.getLineMaxColumn(i.originalRange.endLineNumber)) {
568+
const originalModel = originalEditor.getModel()!;
569+
// When the diff is invalid, the ranges might be out of bounds (this should be fixed in the diff model by applying edits directly).
570+
const maxColumn = i.originalRange.endLineNumber <= originalModel.getLineCount() ? originalModel.getLineMaxColumn(i.originalRange.endLineNumber) : Number.MAX_SAFE_INTEGER;
571+
if (i.originalRange.endColumn < maxColumn) {
555572
// // There is some unmodified text on this line after the diff
556573
emitAlignment(i.originalRange.endLineNumber, i.modifiedRange.endLineNumber);
557574
}

src/vs/editor/common/services/editorSimpleWorker.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -410,7 +410,8 @@ export class EditorSimpleWorker implements IRequestHandler, IDisposable {
410410
return null;
411411
}
412412

413-
return EditorSimpleWorker.computeDiff(original, modified, options, algorithm);
413+
const result = EditorSimpleWorker.computeDiff(original, modified, options, algorithm);
414+
return result;
414415
}
415416

416417
private static computeDiff(originalTextModel: ICommonModel | ITextModel, modifiedTextModel: ICommonModel | ITextModel, options: IDocumentDiffProviderOptions, algorithm: DiffAlgorithmName): IDiffComputationResult {

0 commit comments

Comments
 (0)