Skip to content

Commit cff1e8e

Browse files
committed
Removes accessibleDiffViewer dependency to DiffEditorEditors
1 parent b28c2de commit cff1e8e

File tree

2 files changed

+92
-25
lines changed

2 files changed

+92
-25
lines changed

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

Lines changed: 90 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@ import { Disposable, DisposableStore, toDisposable } from 'vs/base/common/lifecy
1515
import { IObservable, ITransaction, autorun, autorunWithStore, derived, derivedWithStore, observableValue, subtransaction, transaction } from 'vs/base/common/observable';
1616
import { ThemeIcon } from 'vs/base/common/themables';
1717
import { applyFontInfo } from 'vs/editor/browser/config/domFontInfo';
18-
import { DiffEditorEditors } from 'vs/editor/browser/widget/diffEditor/components/diffEditorEditors';
1918
import { applyStyle } from 'vs/editor/browser/widget/diffEditor/utils';
2019
import { EditorFontLigatures, EditorOption, IComputedEditorOptions } from 'vs/editor/common/config/editorOptions';
2120
import { LineRange } from 'vs/editor/common/core/lineRange';
@@ -34,11 +33,33 @@ import { AccessibilitySignal, IAccessibilitySignalService } from 'vs/platform/ac
3433
import { IInstantiationService } from 'vs/platform/instantiation/common/instantiation';
3534
import { registerIcon } from 'vs/platform/theme/common/iconRegistry';
3635
import 'vs/css!./accessibleDiffViewer';
36+
import { DiffEditorEditors } from 'vs/editor/browser/widget/diffEditor/components/diffEditorEditors';
3737

3838
const accessibleDiffViewerInsertIcon = registerIcon('diff-review-insert', Codicon.add, localize('accessibleDiffViewerInsertIcon', 'Icon for \'Insert\' in accessible diff viewer.'));
3939
const accessibleDiffViewerRemoveIcon = registerIcon('diff-review-remove', Codicon.remove, localize('accessibleDiffViewerRemoveIcon', 'Icon for \'Remove\' in accessible diff viewer.'));
4040
const accessibleDiffViewerCloseIcon = registerIcon('diff-review-close', Codicon.close, localize('accessibleDiffViewerCloseIcon', 'Icon for \'Close\' in accessible diff viewer.'));
4141

42+
export interface IAccessibleDiffViewerModel {
43+
getOriginalModel(): ITextModel;
44+
getOriginalOptions(): IComputedEditorOptions;
45+
46+
/**
47+
* Should do: `setSelection`, `revealLine` and `focus`
48+
*/
49+
originalReveal(range: Range): void;
50+
51+
getModifiedModel(): ITextModel;
52+
getModifiedOptions(): IComputedEditorOptions;
53+
/**
54+
* Should do: `setSelection`, `revealLine` and `focus`
55+
*/
56+
modifiedReveal(range?: Range): void;
57+
modifiedSetSelection(range: Range): void;
58+
modifiedFocus(): void;
59+
60+
getModifiedPosition(): Position | undefined;
61+
}
62+
4263
export class AccessibleDiffViewer extends Disposable {
4364
public static _ttPolicy = createTrustedTypesPolicy('diffReview', { createHTML: value => value });
4465

@@ -50,7 +71,7 @@ export class AccessibleDiffViewer extends Disposable {
5071
private readonly _width: IObservable<number>,
5172
private readonly _height: IObservable<number>,
5273
private readonly _diffs: IObservable<DetailedLineRangeMapping[] | undefined>,
53-
private readonly _editors: DiffEditorEditors,
74+
private readonly _models: IAccessibleDiffViewerModel,
5475
@IInstantiationService private readonly _instantiationService: IInstantiationService,
5576
) {
5677
super();
@@ -62,8 +83,8 @@ export class AccessibleDiffViewer extends Disposable {
6283
if (!visible) {
6384
return null;
6485
}
65-
const model = store.add(this._instantiationService.createInstance(ViewModel, this._diffs, this._editors, this._setVisible, this._canClose));
66-
const view = store.add(this._instantiationService.createInstance(View, this._parentNode, model, this._width, this._height, this._editors));
86+
const model = store.add(this._instantiationService.createInstance(ViewModel, this._diffs, this._models, this._setVisible, this._canClose));
87+
const view = store.add(this._instantiationService.createInstance(View, this._parentNode, model, this._width, this._height, this._models));
6788
return { model, view, };
6889
}).recomputeInitiallyAndOnChange(this._store);
6990

@@ -106,7 +127,7 @@ class ViewModel extends Disposable {
106127

107128
constructor(
108129
private readonly _diffs: IObservable<DetailedLineRangeMapping[] | undefined>,
109-
private readonly _editors: DiffEditorEditors,
130+
private readonly _models: IAccessibleDiffViewerModel,
110131
private readonly _setVisible: (visible: boolean, tx: ITransaction | undefined) => void,
111132
public readonly canClose: IObservable<boolean>,
112133
@IAccessibilitySignalService private readonly _accessibilitySignalService: IAccessibilitySignalService,
@@ -123,12 +144,12 @@ class ViewModel extends Disposable {
123144

124145
const groups = computeViewElementGroups(
125146
diffs,
126-
this._editors.original.getModel()!.getLineCount(),
127-
this._editors.modified.getModel()!.getLineCount()
147+
this._models.getOriginalModel().getLineCount(),
148+
this._models.getModifiedModel().getLineCount()
128149
);
129150

130151
transaction(tx => {
131-
const p = this._editors.modified.getPosition();
152+
const p = this._models.getModifiedPosition();
132153
if (p) {
133154
const nextGroup = groups.findIndex(g => p?.lineNumber < g.range.modified.endLineNumberExclusive);
134155
if (nextGroup !== -1) {
@@ -155,7 +176,7 @@ class ViewModel extends Disposable {
155176
const currentViewItem = this.currentElement.read(reader);
156177
if (currentViewItem && currentViewItem.type !== LineType.Header) {
157178
const lineNumber = currentViewItem.modifiedLineNumber ?? currentViewItem.diff.modified.startLineNumber;
158-
this._editors.modified.setSelection(Range.fromPositions(new Position(lineNumber, 1)));
179+
this._models.modifiedSetSelection(Range.fromPositions(new Position(lineNumber, 1)));
159180
}
160181
}));
161182
}
@@ -194,27 +215,27 @@ class ViewModel extends Disposable {
194215
}
195216

196217
revealCurrentElementInEditor(): void {
218+
if (!this.canClose.get()) { return; }
197219
this._setVisible(false, undefined);
198220

199221
const curElem = this.currentElement.get();
200222
if (curElem) {
201223
if (curElem.type === LineType.Deleted) {
202-
this._editors.original.setSelection(Range.fromPositions(new Position(curElem.originalLineNumber, 1)));
203-
this._editors.original.revealLine(curElem.originalLineNumber);
204-
this._editors.original.focus();
224+
this._models.originalReveal(Range.fromPositions(new Position(curElem.originalLineNumber, 1)));
205225
} else {
206-
if (curElem.type !== LineType.Header) {
207-
this._editors.modified.setSelection(Range.fromPositions(new Position(curElem.modifiedLineNumber, 1)));
208-
this._editors.modified.revealLine(curElem.modifiedLineNumber);
209-
}
210-
this._editors.modified.focus();
226+
this._models.modifiedReveal(
227+
curElem.type !== LineType.Header
228+
? Range.fromPositions(new Position(curElem.modifiedLineNumber, 1))
229+
: undefined
230+
);
211231
}
212232
}
213233
}
214234

215235
close(): void {
236+
if (!this.canClose.get()) { return; }
216237
this._setVisible(false, undefined);
217-
this._editors.modified.focus();
238+
this._models.modifiedFocus();
218239
}
219240
}
220241

@@ -327,7 +348,7 @@ class View extends Disposable {
327348
private readonly _model: ViewModel,
328349
private readonly _width: IObservable<number>,
329350
private readonly _height: IObservable<number>,
330-
private readonly _editors: DiffEditorEditors,
351+
private readonly _models: IAccessibleDiffViewerModel,
331352
@ILanguageService private readonly _languageService: ILanguageService,
332353
) {
333354
super();
@@ -412,8 +433,8 @@ class View extends Disposable {
412433
}
413434

414435
private _render(store: DisposableStore): void {
415-
const originalOptions = this._editors.original.getOptions();
416-
const modifiedOptions = this._editors.modified.getOptions();
436+
const originalOptions = this._models.getOriginalOptions();
437+
const modifiedOptions = this._models.getModifiedOptions();
417438

418439
const container = document.createElement('div');
419440
container.className = 'diff-review-table';
@@ -423,8 +444,8 @@ class View extends Disposable {
423444

424445
reset(this._content, container);
425446

426-
const originalModel = this._editors.original.getModel();
427-
const modifiedModel = this._editors.modified.getModel();
447+
const originalModel = this._models.getOriginalModel();
448+
const modifiedModel = this._models.getModifiedModel();
428449
if (!originalModel || !modifiedModel) {
429450
return;
430451
}
@@ -659,3 +680,49 @@ class View extends Disposable {
659680
return r.html;
660681
}
661682
}
683+
684+
export class AccessibleDiffViewerModelFromEditors implements IAccessibleDiffViewerModel {
685+
constructor(private readonly editors: DiffEditorEditors) { }
686+
687+
getOriginalModel(): ITextModel {
688+
return this.editors.original.getModel()!;
689+
}
690+
691+
getOriginalOptions(): IComputedEditorOptions {
692+
return this.editors.original.getOptions();
693+
}
694+
695+
originalReveal(range: Range): void {
696+
this.editors.original.revealRange(range);
697+
this.editors.original.setSelection(range);
698+
this.editors.original.focus();
699+
}
700+
701+
getModifiedModel(): ITextModel {
702+
return this.editors.modified.getModel()!;
703+
}
704+
705+
getModifiedOptions(): IComputedEditorOptions {
706+
return this.editors.modified.getOptions();
707+
}
708+
709+
modifiedReveal(range?: Range | undefined): void {
710+
if (range) {
711+
this.editors.modified.revealRange(range);
712+
this.editors.modified.setSelection(range);
713+
}
714+
this.editors.modified.focus();
715+
}
716+
717+
modifiedSetSelection(range: Range): void {
718+
this.editors.modified.setSelection(range);
719+
}
720+
721+
modifiedFocus(): void {
722+
this.editors.modified.focus();
723+
}
724+
725+
getModifiedPosition(): Position | undefined {
726+
return this.editors.modified.getPosition() ?? undefined;
727+
}
728+
}

src/vs/editor/browser/widget/diffEditor/diffEditorWidget.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ import { EditorExtensionsRegistry, IDiffEditorContributionDescription } from 'vs
1717
import { ICodeEditorService } from 'vs/editor/browser/services/codeEditorService';
1818
import { StableEditorScrollState } from 'vs/editor/browser/stableEditorScroll';
1919
import { CodeEditorWidget, ICodeEditorWidgetOptions } from 'vs/editor/browser/widget/codeEditorWidget';
20-
import { AccessibleDiffViewer } from 'vs/editor/browser/widget/diffEditor/components/accessibleDiffViewer';
20+
import { AccessibleDiffViewer, AccessibleDiffViewerModelFromEditors } from 'vs/editor/browser/widget/diffEditor/components/accessibleDiffViewer';
2121
import { DiffEditorDecorations } from 'vs/editor/browser/widget/diffEditor/components/diffEditorDecorations';
2222
import { DiffEditorSash } from 'vs/editor/browser/widget/diffEditor/components/diffEditorSash';
2323
import { HideUnchangedRegionsFeature } from 'vs/editor/browser/widget/diffEditor/features/hideUnchangedRegionsFeature';
@@ -233,7 +233,7 @@ export class DiffEditorWidget extends DelegatingEditor implements IDiffEditor {
233233
this._rootSizeObserver.width,
234234
this._rootSizeObserver.height,
235235
this._diffModel.map((m, r) => m?.diff.read(r)?.mappings.map(m => m.lineRangeMapping)),
236-
this._editors,
236+
new AccessibleDiffViewerModelFromEditors(this._editors),
237237
)
238238
).recomputeInitiallyAndOnChange(this._store);
239239

0 commit comments

Comments
 (0)