Skip to content

Commit 5557c0b

Browse files
committed
Code cleanup
1 parent 70a4e12 commit 5557c0b

File tree

6 files changed

+110
-137
lines changed

6 files changed

+110
-137
lines changed

src/vs/editor/browser/widget/diffEditorWidget2/diffEditorDecorations.ts

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,8 @@
66
import { Disposable } from 'vs/base/common/lifecycle';
77
import { IObservable, derived } from 'vs/base/common/observable';
88
import { isDefined } from 'vs/base/common/types';
9-
import { CodeEditorWidget } from 'vs/editor/browser/widget/codeEditorWidget';
109
import { arrowRevertChange, diffAddDecoration, diffAddDecorationEmpty, diffDeleteDecoration, diffDeleteDecorationEmpty, diffLineAddDecorationBackground, diffLineAddDecorationBackgroundWithIndicator, diffLineDeleteDecorationBackground, diffLineDeleteDecorationBackgroundWithIndicator } from 'vs/editor/browser/widget/diffEditorWidget2/decorations';
10+
import { DiffEditorEditors } from 'vs/editor/browser/widget/diffEditorWidget2/diffEditorEditors';
1111
import { DiffEditorOptions } from 'vs/editor/browser/widget/diffEditorWidget2/diffEditorOptions';
1212
import { DiffEditorViewModel } from 'vs/editor/browser/widget/diffEditorWidget2/diffEditorViewModel';
1313
import { MovedBlocksLinesPart } from 'vs/editor/browser/widget/diffEditorWidget2/movedBlocksLines';
@@ -19,15 +19,14 @@ import { IModelDeltaDecoration } from 'vs/editor/common/model';
1919

2020
export class DiffEditorDecorations extends Disposable {
2121
constructor(
22-
private readonly _originalEditor: CodeEditorWidget,
23-
private readonly _modifiedEditor: CodeEditorWidget,
22+
private readonly _editors: DiffEditorEditors,
2423
private readonly _diffModel: IObservable<DiffEditorViewModel | undefined>,
2524
private readonly _options: DiffEditorOptions,
2625
) {
2726
super();
2827

29-
this._register(applyObservableDecorations(this._originalEditor, this._decorations.map(d => d?.originalDecorations || [])));
30-
this._register(applyObservableDecorations(this._modifiedEditor, this._decorations.map(d => d?.modifiedDecorations || [])));
28+
this._register(applyObservableDecorations(this._editors.original, this._decorations.map(d => d?.originalDecorations || [])));
29+
this._register(applyObservableDecorations(this._editors.modified, this._decorations.map(d => d?.modifiedDecorations || [])));
3130
}
3231

3332
private readonly _decorations = derived('decorations', (reader) => {

src/vs/editor/browser/widget/diffEditorWidget2/diffEditorWidget2.ts

Lines changed: 42 additions & 66 deletions
Original file line numberDiff line numberDiff line change
@@ -104,56 +104,10 @@ export class DiffEditorWidget2 extends DelegatingEditor implements IDiffEditor {
104104
(i, c, o, o2) => this._createInnerEditor(i, c, o, o2),
105105
));
106106

107-
108-
this._register(this._editors.original.onDidChangeCursorPosition(e => {
109-
const m = this._diffModel.get();
110-
if (!m) { return; }
111-
112-
const movedText = m.diff.get()!.movedTexts.find(m => m.lineRangeMapping.originalRange.contains(e.position.lineNumber));
113-
m.syncedMovedTexts.set(movedText, undefined);
114-
}));
115-
this._register(this._editors.modified.onDidChangeCursorPosition(e => {
116-
const m = this._diffModel.get();
117-
if (!m) { return; }
118-
119-
const movedText = m.diff.get()!.movedTexts.find(m => m.lineRangeMapping.modifiedRange.contains(e.position.lineNumber));
120-
m.syncedMovedTexts.set(movedText, undefined);
121-
}));
122-
// Revert change when an arrow is clicked.
123-
this._register(this._editors.modified.onMouseDown(event => {
124-
if (!event.event.rightButton && event.target.position && event.target.element?.className.includes('arrow-revert-change')) {
125-
const lineNumber = event.target.position.lineNumber;
126-
const viewZone = event.target as IMouseTargetViewZone | undefined;
127-
128-
const model = this._diffModel.get();
129-
if (!model) {
130-
return;
131-
}
132-
const diffs = model.diff.get()?.mappings;
133-
if (!diffs) {
134-
return;
135-
}
136-
const diff = diffs.find(d =>
137-
viewZone?.detail.afterLineNumber === d.lineRangeMapping.modifiedRange.startLineNumber - 1 ||
138-
d.lineRangeMapping.modifiedRange.startLineNumber === lineNumber
139-
);
140-
if (!diff) {
141-
return;
142-
}
143-
this.revert(diff.lineRangeMapping);
144-
145-
event.event.stopPropagation();
146-
return;
147-
}
148-
}));
149-
150-
151107
this._sash = derivedWithStore('sash', (reader, store) => {
152108
const showSash = this._options.renderSideBySide.read(reader);
153109
this.elements.root.classList.toggle('side-by-side', showSash);
154-
if (!showSash) {
155-
return undefined;
156-
}
110+
if (!showSash) { return undefined; }
157111
const result = store.add(new DiffEditorSash(
158112
this._options,
159113
this.elements.root,
@@ -173,36 +127,25 @@ export class DiffEditorWidget2 extends DelegatingEditor implements IDiffEditor {
173127
this._register(keepAlive(this._sash, true));
174128

175129
this._register(autorunWithStore2('unchangedRangesFeature', (reader, store) => {
176-
this.unchangedRangesFeature = store.add(new (readHotReloadableExport(UnchangedRangesFeature, reader))(
177-
this._editors.original,
178-
this._editors.modified,
179-
this._diffModel,
180-
this._options.renderSideBySide,
181-
));
130+
this.unchangedRangesFeature = store.add(new (readHotReloadableExport(UnchangedRangesFeature, reader))(this._editors, this._diffModel, this._options));
182131
}));
183132

184133
this._register(autorunWithStore2('decorations', (reader, store) => {
185-
store.add(new (readHotReloadableExport(DiffEditorDecorations, reader))(
186-
this._editors.original,
187-
this._editors.modified,
188-
this._diffModel,
189-
this._options,
190-
));
134+
store.add(new (readHotReloadableExport(DiffEditorDecorations, reader))(this._editors, this._diffModel, this._options));
191135
}));
192136

193137
this._register(this._instantiationService.createInstance(
194138
ViewZoneManager,
195-
this._editors.original,
196-
this._editors.modified,
139+
this._editors,
197140
this._diffModel,
198141
this._options,
199142
this,
200143
() => this.unchangedRangesFeature.isUpdatingViewZones,
201144
));
202145

203-
this._register(this._instantiationService.createInstance(OverviewRulerPart,
204-
this._editors.original,
205-
this._editors.modified,
146+
this._register(this._instantiationService.createInstance(
147+
OverviewRulerPart,
148+
this._editors,
206149
this.elements.root,
207150
this._diffModel,
208151
this._rootSizeObserver.width,
@@ -227,8 +170,7 @@ export class DiffEditorWidget2 extends DelegatingEditor implements IDiffEditor {
227170
this._diffModel,
228171
this._layoutInfo.map(i => i.originalEditor),
229172
this._layoutInfo.map(i => i.modifiedEditor),
230-
this._editors.original,
231-
this._editors.modified,
173+
this._editors,
232174
));
233175

234176
this._register(applyStyle(this.elements.overlay, {
@@ -238,6 +180,40 @@ export class DiffEditorWidget2 extends DelegatingEditor implements IDiffEditor {
238180
? 'visible' : 'hidden'
239181
),
240182
}));
183+
184+
this._register(this._editors.original.onDidChangeCursorPosition(e => {
185+
const m = this._diffModel.get();
186+
if (!m) { return; }
187+
const movedText = m.diff.get()!.movedTexts.find(m => m.lineRangeMapping.originalRange.contains(e.position.lineNumber));
188+
m.syncedMovedTexts.set(movedText, undefined);
189+
}));
190+
this._register(this._editors.modified.onDidChangeCursorPosition(e => {
191+
const m = this._diffModel.get();
192+
if (!m) { return; }
193+
const movedText = m.diff.get()!.movedTexts.find(m => m.lineRangeMapping.modifiedRange.contains(e.position.lineNumber));
194+
m.syncedMovedTexts.set(movedText, undefined);
195+
}));
196+
197+
// Revert change when an arrow is clicked.
198+
this._register(this._editors.modified.onMouseDown(event => {
199+
if (!event.event.rightButton && event.target.position && event.target.element?.className.includes('arrow-revert-change')) {
200+
const lineNumber = event.target.position.lineNumber;
201+
const viewZone = event.target as IMouseTargetViewZone | undefined;
202+
203+
const model = this._diffModel.get();
204+
if (!model) { return; }
205+
const diffs = model.diff.get()?.mappings;
206+
if (!diffs) { return; }
207+
const diff = diffs.find(d =>
208+
viewZone?.detail.afterLineNumber === d.lineRangeMapping.modifiedRange.startLineNumber - 1 ||
209+
d.lineRangeMapping.modifiedRange.startLineNumber === lineNumber
210+
);
211+
if (!diff) { return; }
212+
this.revert(diff.lineRangeMapping);
213+
214+
event.event.stopPropagation();
215+
}
216+
}));
241217
}
242218

243219
protected _createInnerEditor(instantiationService: IInstantiationService, container: HTMLElement, options: Readonly<IEditorConstructionOptions>, editorWidgetOptions: ICodeEditorWidgetOptions): CodeEditorWidget {

src/vs/editor/browser/widget/diffEditorWidget2/lineAlignment.ts

Lines changed: 32 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ import { IViewZone } from 'vs/editor/browser/editorBrowser';
1616
import { StableEditorScrollState } from 'vs/editor/browser/stableEditorScroll';
1717
import { CodeEditorWidget } from 'vs/editor/browser/widget/codeEditorWidget';
1818
import { diffDeleteDecoration, diffRemoveIcon } from 'vs/editor/browser/widget/diffEditorWidget2/decorations';
19+
import { DiffEditorEditors } from 'vs/editor/browser/widget/diffEditorWidget2/diffEditorEditors';
1920
import { DiffEditorViewModel, DiffMapping } from 'vs/editor/browser/widget/diffEditorWidget2/diffEditorViewModel';
2021
import { DiffEditorWidget2 } from 'vs/editor/browser/widget/diffEditorWidget2/diffEditorWidget2';
2122
import { InlineDiffDeletedCodeMargin } from 'vs/editor/browser/widget/diffEditorWidget2/inlineDiffDeletedCodeMargin';
@@ -49,8 +50,7 @@ export class ViewZoneManager extends Disposable {
4950
private readonly _modifiedScrollOffsetAnimated = animatedObservable(this._modifiedScrollOffset, this._store);
5051

5152
constructor(
52-
private readonly _originalEditor: CodeEditorWidget,
53-
private readonly _modifiedEditor: CodeEditorWidget,
53+
private readonly _editors: DiffEditorEditors,
5454
private readonly _diffModel: IObservable<DiffEditorViewModel | undefined>,
5555
private readonly _options: DiffEditorOptions,
5656
private readonly _diffEditorWidget: DiffEditorWidget2,
@@ -64,11 +64,11 @@ export class ViewZoneManager extends Disposable {
6464

6565
const originalViewZonesChanged = observableSignalFromEvent(
6666
'origViewZonesChanged',
67-
e => this._originalEditor.onDidChangeViewZones((args) => { if (!isChangingViewZones && !this._canIgnoreViewZoneUpdateEvent()) { e(args); } })
67+
e => this._editors.original.onDidChangeViewZones((args) => { if (!isChangingViewZones && !this._canIgnoreViewZoneUpdateEvent()) { e(args); } })
6868
);
6969
const modifiedViewZonesChanged = observableSignalFromEvent(
7070
'modViewZonesChanged',
71-
e => this._modifiedEditor.onDidChangeViewZones((args) => { if (!isChangingViewZones && !this._canIgnoreViewZoneUpdateEvent()) { e(args); } })
71+
e => this._editors.modified.onDidChangeViewZones((args) => { if (!isChangingViewZones && !this._canIgnoreViewZoneUpdateEvent()) { e(args); } })
7272
);
7373

7474
const originalModelTokenizationCompleted = this._diffModel.map(m =>
@@ -84,7 +84,7 @@ export class ViewZoneManager extends Disposable {
8484
if (!diffModel || !diff) { return null; }
8585
originalViewZonesChanged.read(reader);
8686
modifiedViewZonesChanged.read(reader);
87-
return computeRangeAlignment(this._originalEditor, this._modifiedEditor, diff.mappings, alignmentViewZoneIdsOrig, alignmentViewZoneIdsMod);
87+
return computeRangeAlignment(this._editors.original, this._editors.modified, diff.mappings, alignmentViewZoneIdsOrig, alignmentViewZoneIdsMod);
8888
});
8989

9090
const alignmentsSyncedMovedText = derived<ILineRangeAlignment[] | null>('alignments', (reader) => {
@@ -94,7 +94,7 @@ export class ViewZoneManager extends Disposable {
9494
modifiedViewZonesChanged.read(reader);
9595
const mappings = syncedMovedText.changes.map(c => new DiffMapping(c));
9696
// TODO dont include alignments outside syncedMovedText
97-
return computeRangeAlignment(this._originalEditor, this._modifiedEditor, mappings, alignmentViewZoneIdsOrig, alignmentViewZoneIdsMod);
97+
return computeRangeAlignment(this._editors.original, this._editors.modified, mappings, alignmentViewZoneIdsOrig, alignmentViewZoneIdsMod);
9898
});
9999

100100
function createFakeLinesDiv(): HTMLElement {
@@ -133,12 +133,12 @@ export class ViewZoneManager extends Disposable {
133133

134134
const renderSideBySide = this._options.renderSideBySide.read(reader);
135135

136-
const deletedCodeLineBreaksComputer = !renderSideBySide ? this._modifiedEditor._getViewModel()?.createLineBreaksComputer() : undefined;
136+
const deletedCodeLineBreaksComputer = !renderSideBySide ? this._editors.modified._getViewModel()?.createLineBreaksComputer() : undefined;
137137
if (deletedCodeLineBreaksComputer) {
138138
for (const a of alignmentsVal) {
139139
if (a.diff) {
140140
for (let i = a.originalRange.startLineNumber; i < a.originalRange.endLineNumberExclusive; i++) {
141-
deletedCodeLineBreaksComputer?.addRequest(this._originalEditor.getModel()!.getLineContent(i), null, null);
141+
deletedCodeLineBreaksComputer?.addRequest(this._editors.original.getModel()!.getLineContent(i), null, null);
142142
}
143143
}
144144
}
@@ -147,13 +147,13 @@ export class ViewZoneManager extends Disposable {
147147
const lineBreakData = deletedCodeLineBreaksComputer?.finalize() ?? [];
148148
let lineBreakDataIdx = 0;
149149

150-
const modLineHeight = this._modifiedEditor.getOption(EditorOption.lineHeight);
150+
const modLineHeight = this._editors.modified.getOption(EditorOption.lineHeight);
151151

152152
const syncedMovedText = this._diffModel.read(reader)?.syncedMovedTexts.read(reader);
153153

154-
const mightContainNonBasicASCII = this._originalEditor.getModel()?.mightContainNonBasicASCII() ?? false;
155-
const mightContainRTL = this._originalEditor.getModel()?.mightContainRTL() ?? false;
156-
const renderOptions = RenderOptions.fromEditor(this._modifiedEditor);
154+
const mightContainNonBasicASCII = this._editors.original.getModel()?.mightContainNonBasicASCII() ?? false;
155+
const mightContainRTL = this._editors.original.getModel()?.mightContainRTL() ?? false;
156+
const renderOptions = RenderOptions.fromEditor(this._editors.modified);
157157

158158
for (const a of alignmentsVal) {
159159
if (a.diff && !renderSideBySide) {
@@ -163,7 +163,7 @@ export class ViewZoneManager extends Disposable {
163163
const deletedCodeDomNode = document.createElement('div');
164164
deletedCodeDomNode.classList.add('view-lines', 'line-delete', 'monaco-mouse-cursor-text');
165165
const source = new LineSource(
166-
a.originalRange.mapToLineArray(l => this._originalEditor.getModel()!.tokenization.getLineTokens(l)),
166+
a.originalRange.mapToLineArray(l => this._editors.original.getModel()!.tokenization.getLineTokens(l)),
167167
a.originalRange.mapToLineArray(_ => lineBreakData[lineBreakDataIdx++]),
168168
mightContainNonBasicASCII,
169169
mightContainRTL,
@@ -196,11 +196,11 @@ export class ViewZoneManager extends Disposable {
196196
new InlineDiffDeletedCodeMargin(
197197
() => assertIsDefined(zoneId),
198198
marginDomNode,
199-
this._modifiedEditor,
199+
this._editors.modified,
200200
a.diff,
201201
this._diffEditorWidget,
202202
result.viewLineCounts,
203-
this._originalEditor.getModel()!,
203+
this._editors.original.getModel()!,
204204
this._contextMenuService,
205205
this._clipboardService,
206206
)
@@ -309,11 +309,11 @@ export class ViewZoneManager extends Disposable {
309309
});
310310

311311
this._register(autorunWithStore2('alignment viewzones', (reader) => {
312-
const scrollState = StableEditorScrollState.capture(this._modifiedEditor);
312+
const scrollState = StableEditorScrollState.capture(this._editors.modified);
313313

314314
const alignmentViewZones_ = alignmentViewZones.read(reader);
315315
isChangingViewZones = true;
316-
this._originalEditor.changeViewZones((aOrig) => {
316+
this._editors.original.changeViewZones((aOrig) => {
317317
for (const id of alignmentViewZoneIdsOrig) { aOrig.removeZone(id); }
318318
alignmentViewZoneIdsOrig.clear();
319319
for (const z of alignmentViewZones_.orig) {
@@ -324,7 +324,7 @@ export class ViewZoneManager extends Disposable {
324324
alignmentViewZoneIdsOrig.add(id);
325325
}
326326
});
327-
this._modifiedEditor.changeViewZones(aMod => {
327+
this._editors.modified.changeViewZones(aMod => {
328328
for (const id of alignmentViewZoneIdsMod) { aMod.removeZone(id); }
329329
alignmentViewZoneIdsMod.clear();
330330
for (const z of alignmentViewZones_.mod) {
@@ -337,27 +337,27 @@ export class ViewZoneManager extends Disposable {
337337
});
338338
isChangingViewZones = false;
339339

340-
scrollState.restore(this._modifiedEditor);
340+
scrollState.restore(this._editors.modified);
341341
}));
342342

343343
let ignoreChange = false;
344-
this._register(this._originalEditor.onDidScrollChange(e => {
344+
this._register(this._editors.original.onDidScrollChange(e => {
345345
if (e.scrollLeftChanged && !ignoreChange) {
346346
ignoreChange = true;
347-
this._modifiedEditor.setScrollLeft(e.scrollLeft);
347+
this._editors.modified.setScrollLeft(e.scrollLeft);
348348
ignoreChange = false;
349349
}
350350
}));
351-
this._register(this._modifiedEditor.onDidScrollChange(e => {
351+
this._register(this._editors.modified.onDidScrollChange(e => {
352352
if (e.scrollLeftChanged && !ignoreChange) {
353353
ignoreChange = true;
354-
this._originalEditor.setScrollLeft(e.scrollLeft);
354+
this._editors.original.setScrollLeft(e.scrollLeft);
355355
ignoreChange = false;
356356
}
357357
}));
358358

359-
this._originalScrollTop = observableFromEvent(this._originalEditor.onDidScrollChange, () => this._originalEditor.getScrollTop());
360-
this._modifiedScrollTop = observableFromEvent(this._modifiedEditor.onDidScrollChange, () => this._modifiedEditor.getScrollTop());
359+
this._originalScrollTop = observableFromEvent(this._editors.original.onDidScrollChange, () => this._editors.original.getScrollTop());
360+
this._modifiedScrollTop = observableFromEvent(this._editors.modified.onDidScrollChange, () => this._editors.modified.getScrollTop());
361361

362362
// origExtraHeight + origOffset - origScrollTop = modExtraHeight + modOffset - modScrollTop
363363

@@ -371,17 +371,17 @@ export class ViewZoneManager extends Disposable {
371371
const newScrollTopModified = this._originalScrollTop.read(reader)
372372
- (this._originalScrollOffsetAnimated.get() - this._modifiedScrollOffsetAnimated.read(reader))
373373
- (this._originalTopPadding.get() - this._modifiedTopPadding.read(reader));
374-
if (newScrollTopModified !== this._modifiedEditor.getScrollTop()) {
375-
this._modifiedEditor.setScrollTop(newScrollTopModified, ScrollType.Immediate);
374+
if (newScrollTopModified !== this._editors.modified.getScrollTop()) {
375+
this._editors.modified.setScrollTop(newScrollTopModified, ScrollType.Immediate);
376376
}
377377
}));
378378

379379
this._register(autorun('update scroll original', (reader) => {
380380
const newScrollTopOriginal = this._modifiedScrollTop.read(reader)
381381
- (this._modifiedScrollOffsetAnimated.get() - this._originalScrollOffsetAnimated.read(reader))
382382
- (this._modifiedTopPadding.get() - this._originalTopPadding.read(reader));
383-
if (newScrollTopOriginal !== this._originalEditor.getScrollTop()) {
384-
this._originalEditor.setScrollTop(newScrollTopOriginal, ScrollType.Immediate);
383+
if (newScrollTopOriginal !== this._editors.original.getScrollTop()) {
384+
this._editors.original.setScrollTop(newScrollTopOriginal, ScrollType.Immediate);
385385
}
386386
}));
387387

@@ -391,8 +391,8 @@ export class ViewZoneManager extends Disposable {
391391

392392
let deltaOrigToMod = 0;
393393
if (m) {
394-
const trueTopOriginal = this._originalEditor.getTopForLineNumber(m.lineRangeMapping.originalRange.startLineNumber, true) - this._originalTopPadding.get();
395-
const trueTopModified = this._modifiedEditor.getTopForLineNumber(m.lineRangeMapping.modifiedRange.startLineNumber, true) - this._modifiedTopPadding.get();
394+
const trueTopOriginal = this._editors.original.getTopForLineNumber(m.lineRangeMapping.originalRange.startLineNumber, true) - this._originalTopPadding.get();
395+
const trueTopModified = this._editors.modified.getTopForLineNumber(m.lineRangeMapping.modifiedRange.startLineNumber, true) - this._modifiedTopPadding.get();
396396
deltaOrigToMod = trueTopModified - trueTopOriginal;
397397
}
398398

@@ -409,7 +409,7 @@ export class ViewZoneManager extends Disposable {
409409
}, 400);
410410
}
411411

412-
if (this._modifiedEditor.hasTextFocus()) {
412+
if (this._editors.modified.hasTextFocus()) {
413413
this._originalScrollOffset.set(this._modifiedScrollOffset.get() - deltaOrigToMod, undefined, true);
414414
} else {
415415
this._modifiedScrollOffset.set(this._originalScrollOffset.get() + deltaOrigToMod, undefined, true);

0 commit comments

Comments
 (0)