Skip to content

Commit 5284fb5

Browse files
authored
make live preview always show a side-by-side diff (microsoft#187840)
1 parent 201401d commit 5284fb5

File tree

1 file changed

+8
-90
lines changed

1 file changed

+8
-90
lines changed

src/vs/workbench/contrib/inlineChat/browser/inlineChatLivePreviewWidget.ts

Lines changed: 8 additions & 90 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ import { ICodeEditor, IDiffEditor } from 'vs/editor/browser/editorBrowser';
1010
import { EmbeddedCodeEditorWidget, EmbeddedDiffEditorWidget } from 'vs/editor/browser/widget/embeddedCodeEditorWidget';
1111
import { EditorOption } from 'vs/editor/common/config/editorOptions';
1212
import { Range } from 'vs/editor/common/core/range';
13-
import { IModelDecorationOptions, IModelDeltaDecoration, ITextModel } from 'vs/editor/common/model';
13+
import { ITextModel } from 'vs/editor/common/model';
1414
import { ZoneWidget } from 'vs/editor/contrib/zoneWidget/browser/zoneWidget';
1515
import { IInstantiationService } from 'vs/platform/instantiation/common/instantiation';
1616
import * as colorRegistry from 'vs/platform/theme/common/colorRegistry';
@@ -21,7 +21,7 @@ import { LineRange } from 'vs/editor/common/core/lineRange';
2121
import { LineRangeMapping } from 'vs/editor/common/diff/linesDiffComputer';
2222
import { Position } from 'vs/editor/common/core/position';
2323
import { EditorExtensionsRegistry } from 'vs/editor/browser/editorExtensions';
24-
import { IEditorDecorationsCollection, ScrollType } from 'vs/editor/common/editorCommon';
24+
import { ScrollType } from 'vs/editor/common/editorCommon';
2525
import { ILogService } from 'vs/platform/log/common/log';
2626
import { lineRangeAsRange, invertLineRange } from 'vs/workbench/contrib/inlineChat/browser/utils';
2727
import { ResourceLabel } from 'vs/workbench/browser/labels';
@@ -43,7 +43,6 @@ export class InlineChatLivePreviewWidget extends ZoneWidget {
4343

4444
private readonly _sessionStore = this._disposables.add(new DisposableStore());
4545
private readonly _diffEditor: IDiffEditor;
46-
private readonly _inlineDiffDecorations: IEditorDecorationsCollection;
4746
private _dim: Dimension | undefined;
4847
private _isVisible: boolean = false;
4948
private _isDiffLocked: boolean = false;
@@ -59,8 +58,6 @@ export class InlineChatLivePreviewWidget extends ZoneWidget {
5958
super.create();
6059
assertType(editor.hasModel());
6160

62-
this._inlineDiffDecorations = editor.createDecorationsCollection();
63-
6461
const diffContributions = EditorExtensionsRegistry
6562
.getEditorContributions()
6663
.filter(c => c.id !== INLINE_CHAT_ID && c.id !== FoldingController.ID);
@@ -120,10 +117,6 @@ export class InlineChatLivePreviewWidget extends ZoneWidget {
120117
this._disposables.add(themeService.onDidColorThemeChange(doStyle));
121118
}
122119

123-
override dispose(): void {
124-
this._inlineDiffDecorations.clear();
125-
super.dispose();
126-
}
127120

128121
protected override _fillContainer(container: HTMLElement): void {
129122
container.appendChild(this._elements.domNode);
@@ -137,7 +130,6 @@ export class InlineChatLivePreviewWidget extends ZoneWidget {
137130

138131
override hide(): void {
139132
this._cleanupFullDiff();
140-
this._cleanupInlineDiff();
141133
this._sessionStore.clear();
142134
super.hide();
143135
this._isVisible = false;
@@ -175,70 +167,9 @@ export class InlineChatLivePreviewWidget extends ZoneWidget {
175167
return;
176168
}
177169

178-
if (changes.length === 0 || this._session.textModel0.getValueLength() === 0) {
179-
// no change or changes to an empty file
180-
this._logService.debug('[IE] livePreview-mode: no diff');
181-
this.hide();
182-
183-
} else if (changes.every(isInlineDiffFriendly)) {
184-
// simple changes
185-
this._logService.debug('[IE] livePreview-mode: inline diff');
186-
this._cleanupFullDiff();
187-
this._renderChangesWithInlineDiff(changes);
188-
189-
} else {
190-
// complex changes
191-
this._logService.debug('[IE] livePreview-mode: full diff');
192-
this._cleanupInlineDiff();
193-
this._renderChangesWithFullDiff(changes, range);
194-
}
195-
}
196-
197-
// --- inline diff
198-
199-
private _renderChangesWithInlineDiff(changes: readonly LineRangeMapping[]) {
200-
const original = this._session.textModel0;
201-
202-
const decorations: IModelDeltaDecoration[] = [];
203-
204-
for (const { innerChanges } of changes) {
205-
if (!innerChanges) {
206-
continue;
207-
}
208-
for (const { modifiedRange, originalRange } of innerChanges) {
209-
210-
const options: IModelDecorationOptions = {
211-
description: 'interactive-diff-inline',
212-
showIfCollapsed: true,
213-
};
214-
215-
if (!modifiedRange.isEmpty()) {
216-
options.className = 'inline-chat-lines-inserted-range';
217-
}
218-
219-
if (!originalRange.isEmpty()) {
220-
let content = original.getValueInRange(originalRange);
221-
if (content.length > 7) {
222-
content = content.substring(0, 7) + '…';
223-
}
224-
options.before = {
225-
content,
226-
inlineClassName: 'inline-chat-lines-deleted-range-inline'
227-
};
228-
}
229-
230-
decorations.push({
231-
range: modifiedRange,
232-
options
233-
});
234-
}
235-
}
236-
237-
this._inlineDiffDecorations.set(decorations);
238-
}
239-
240-
private _cleanupInlineDiff() {
241-
this._inlineDiffDecorations.clear();
170+
// complex changes
171+
this._logService.debug('[IE] livePreview-mode: full diff');
172+
this._renderChangesWithFullDiff(changes, range);
242173
}
243174

244175
// --- full diff
@@ -273,7 +204,9 @@ export class InlineChatLivePreviewWidget extends ZoneWidget {
273204
}
274205

275206
private _computeHiddenRanges(model: ITextModel, range: Range, changes: readonly LineRangeMapping[]) {
276-
assertType(changes.length > 0);
207+
if (changes.length === 0) {
208+
changes = [new LineRangeMapping(LineRange.fromRange(range), LineRange.fromRange(range), undefined)];
209+
}
277210

278211
let originalLineRange = changes[0].originalRange;
279212
let modifiedLineRange = changes[0].modifiedRange;
@@ -347,21 +280,6 @@ export class InlineChatLivePreviewWidget extends ZoneWidget {
347280
}
348281
}
349282

350-
function isInlineDiffFriendly(mapping: LineRangeMapping): boolean {
351-
if (!mapping.modifiedRange.equals(mapping.originalRange)) {
352-
return false;
353-
}
354-
if (!mapping.innerChanges) {
355-
return false;
356-
}
357-
for (const { modifiedRange, originalRange } of mapping.innerChanges) {
358-
if (Range.spansMultipleLines(modifiedRange) || Range.spansMultipleLines(originalRange)) {
359-
return false;
360-
}
361-
}
362-
return true;
363-
}
364-
365283

366284
export class InlineChatFileCreatePreviewWidget extends ZoneWidget {
367285

0 commit comments

Comments
 (0)