Skip to content

Commit a4be32a

Browse files
author
Benjamin Pasero
committed
debt - proper strict null typing in subclasses of editor
1 parent 5831974 commit a4be32a

19 files changed

Lines changed: 84 additions & 75 deletions

File tree

src/vs/workbench/browser/parts/editor/baseEditor.ts

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -41,8 +41,8 @@ export abstract class BaseEditor extends Panel implements IEditor {
4141

4242
readonly onDidSizeConstraintsChange: Event<{ width: number; height: number; } | undefined> = Event.None;
4343

44-
protected _input: EditorInput | null = null;
45-
protected _options: EditorOptions | null = null;
44+
protected _input: EditorInput | undefined;
45+
protected _options: EditorOptions | undefined;
4646

4747
private _group?: IEditorGroup;
4848

@@ -55,11 +55,11 @@ export abstract class BaseEditor extends Panel implements IEditor {
5555
super(id, telemetryService, themeService, storageService);
5656
}
5757

58-
get input(): EditorInput | null {
58+
get input(): EditorInput | undefined {
5959
return this._input;
6060
}
6161

62-
get options(): EditorOptions | null {
62+
get options(): EditorOptions | undefined {
6363
return this._options;
6464
}
6565

@@ -78,7 +78,7 @@ export abstract class BaseEditor extends Panel implements IEditor {
7878
* The provided cancellation token should be used to test if the operation
7979
* was cancelled.
8080
*/
81-
setInput(input: EditorInput, options: EditorOptions | null, token: CancellationToken): Promise<void> {
81+
setInput(input: EditorInput, options: EditorOptions | undefined, token: CancellationToken): Promise<void> {
8282
this._input = input;
8383
this._options = options;
8484

@@ -90,8 +90,8 @@ export abstract class BaseEditor extends Panel implements IEditor {
9090
* resources associated with the input should be freed.
9191
*/
9292
clearInput(): void {
93-
this._input = null;
94-
this._options = null;
93+
this._input = undefined;
94+
this._options = undefined;
9595
}
9696

9797
/**
@@ -101,7 +101,7 @@ export abstract class BaseEditor extends Panel implements IEditor {
101101
* Sets the given options to the editor. Clients should apply the options
102102
* to the current input.
103103
*/
104-
setOptions(options: EditorOptions | null): void {
104+
setOptions(options: EditorOptions | undefined): void {
105105
this._options = options;
106106
}
107107

@@ -160,8 +160,8 @@ export abstract class BaseEditor extends Panel implements IEditor {
160160
}
161161

162162
dispose(): void {
163-
this._input = null;
164-
this._options = null;
163+
this._input = undefined;
164+
this._options = undefined;
165165

166166
super.dispose();
167167
}

src/vs/workbench/browser/parts/editor/binaryEditor.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ import { IFileService } from 'vs/platform/files/common/files';
2323
import { IInstantiationService } from 'vs/platform/instantiation/common/instantiation';
2424

2525
export interface IOpenCallbacks {
26-
openInternal: (input: EditorInput, options: EditorOptions) => Promise<void>;
26+
openInternal: (input: EditorInput, options: EditorOptions | undefined) => Promise<void>;
2727
openExternal: (uri: URI) => void;
2828
}
2929

@@ -76,7 +76,7 @@ export abstract class BaseBinaryResourceEditor extends BaseEditor {
7676
parent.appendChild(this.scrollbar.getDomNode());
7777
}
7878

79-
async setInput(input: EditorInput, options: EditorOptions, token: CancellationToken): Promise<void> {
79+
async setInput(input: EditorInput, options: EditorOptions | undefined, token: CancellationToken): Promise<void> {
8080
await super.setInput(input, options, token);
8181
const model = await input.resolve();
8282

@@ -102,7 +102,7 @@ export abstract class BaseBinaryResourceEditor extends BaseEditor {
102102
}, this.instantiationService);
103103
}
104104

105-
private async handleOpenInternalCallback(input: EditorInput, options: EditorOptions): Promise<void> {
105+
private async handleOpenInternalCallback(input: EditorInput, options: EditorOptions | undefined): Promise<void> {
106106
await this.callbacks.openInternal(input, options);
107107

108108
// Signal to listeners that the binary editor has been opened in-place

src/vs/workbench/browser/parts/editor/editorControl.ts

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@ import { IEditorProgressService, LongRunningOperation } from 'vs/platform/progre
1515
import { IEditorGroupView, DEFAULT_EDITOR_MIN_DIMENSIONS, DEFAULT_EDITOR_MAX_DIMENSIONS } from 'vs/workbench/browser/parts/editor/editor';
1616
import { Event, Emitter } from 'vs/base/common/event';
1717
import { IVisibleEditor } from 'vs/workbench/services/editor/common/editorService';
18-
import { withUndefinedAsNull } from 'vs/base/common/types';
1918

2019
export interface IOpenEditorResult {
2120
readonly control: BaseEditor;
@@ -68,7 +67,7 @@ export class EditorControl extends Disposable {
6867
const control = this.doShowEditorControl(descriptor);
6968

7069
// Set input
71-
const editorChanged = await this.doSetInput(control, editor, withUndefinedAsNull(options));
70+
const editorChanged = await this.doSetInput(control, editor, options);
7271
return { control, editorChanged };
7372
}
7473

@@ -151,7 +150,7 @@ export class EditorControl extends Disposable {
151150
this._onDidSizeConstraintsChange.fire(undefined);
152151
}
153152

154-
private async doSetInput(control: BaseEditor, editor: EditorInput, options: EditorOptions | null): Promise<boolean> {
153+
private async doSetInput(control: BaseEditor, editor: EditorInput, options: EditorOptions | undefined): Promise<boolean> {
155154

156155
// If the input did not change, return early and only apply the options
157156
// unless the options instruct us to force open it even if it is the same

src/vs/workbench/browser/parts/editor/sideBySideEditor.ts

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -93,20 +93,20 @@ export class SideBySideEditor extends BaseEditor {
9393
this.updateStyles();
9494
}
9595

96-
async setInput(newInput: EditorInput, options: EditorOptions, token: CancellationToken): Promise<void> {
96+
async setInput(newInput: EditorInput, options: EditorOptions | undefined, token: CancellationToken): Promise<void> {
9797
const oldInput = this.input as SideBySideEditorInput;
9898
await super.setInput(newInput, options, token);
9999

100100
return this.updateInput(oldInput, (newInput as SideBySideEditorInput), options, token);
101101
}
102102

103-
setOptions(options: EditorOptions): void {
103+
setOptions(options: EditorOptions | undefined): void {
104104
if (this.masterEditor) {
105105
this.masterEditor.setOptions(options);
106106
}
107107
}
108108

109-
protected setEditorVisible(visible: boolean, group: IEditorGroup): void {
109+
protected setEditorVisible(visible: boolean, group: IEditorGroup | undefined): void {
110110
if (this.masterEditor) {
111111
this.masterEditor.setVisible(visible, group);
112112
}
@@ -159,7 +159,7 @@ export class SideBySideEditor extends BaseEditor {
159159
return this.detailsEditor;
160160
}
161161

162-
private async updateInput(oldInput: SideBySideEditorInput, newInput: SideBySideEditorInput, options: EditorOptions, token: CancellationToken): Promise<void> {
162+
private async updateInput(oldInput: SideBySideEditorInput, newInput: SideBySideEditorInput, options: EditorOptions | undefined, token: CancellationToken): Promise<void> {
163163
if (!newInput.matches(oldInput)) {
164164
if (oldInput) {
165165
this.disposeEditors();
@@ -173,12 +173,12 @@ export class SideBySideEditor extends BaseEditor {
173173
}
174174

175175
await Promise.all([
176-
this.detailsEditor.setInput(newInput.details, null, token),
176+
this.detailsEditor.setInput(newInput.details, undefined, token),
177177
this.masterEditor.setInput(newInput.master, options, token)
178178
]);
179179
}
180180

181-
private setNewInput(newInput: SideBySideEditorInput, options: EditorOptions, token: CancellationToken): Promise<void> {
181+
private setNewInput(newInput: SideBySideEditorInput, options: EditorOptions | undefined, token: CancellationToken): Promise<void> {
182182
const detailsEditor = this.doCreateEditor(newInput.details, this.detailsEditorContainer);
183183
const masterEditor = this.doCreateEditor(newInput.master, this.masterEditorContainer);
184184

@@ -198,7 +198,7 @@ export class SideBySideEditor extends BaseEditor {
198198
return editor;
199199
}
200200

201-
private async onEditorsCreated(details: BaseEditor, master: BaseEditor, detailsInput: EditorInput, masterInput: EditorInput, options: EditorOptions, token: CancellationToken): Promise<void> {
201+
private async onEditorsCreated(details: BaseEditor, master: BaseEditor, detailsInput: EditorInput, masterInput: EditorInput, options: EditorOptions | undefined, token: CancellationToken): Promise<void> {
202202
this.detailsEditor = details;
203203
this.masterEditor = master;
204204

@@ -210,7 +210,7 @@ export class SideBySideEditor extends BaseEditor {
210210
this.onDidCreateEditors.fire(undefined);
211211

212212
await Promise.all([
213-
this.detailsEditor.setInput(detailsInput, null, token),
213+
this.detailsEditor.setInput(detailsInput, undefined, token),
214214
this.masterEditor.setInput(masterInput, options, token)]
215215
);
216216
}

src/vs/workbench/browser/parts/editor/textDiffEditor.ts

Lines changed: 16 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55

66
import * as nls from 'vs/nls';
77
import * as objects from 'vs/base/common/objects';
8-
import * as types from 'vs/base/common/types';
8+
import { isFunction, isObject, isArray } from 'vs/base/common/types';
99
import { IDiffEditor } from 'vs/editor/browser/editorBrowser';
1010
import { IDiffEditorOptions, IEditorOptions as ICodeEditorOptions } from 'vs/editor/common/config/editorOptions';
1111
import { BaseTextEditor, IEditorConfiguration } from 'vs/workbench/browser/parts/editor/textEditor';
@@ -31,7 +31,7 @@ import { IEditorService } from 'vs/workbench/services/editor/common/editorServic
3131
import { CancellationToken } from 'vs/base/common/cancellation';
3232
import { EditorMemento } from 'vs/workbench/browser/parts/editor/baseEditor';
3333
import { IWindowService } from 'vs/platform/windows/common/windows';
34-
import { EditorActivation } from 'vs/platform/editor/common/editor';
34+
import { EditorActivation, IEditorOptions } from 'vs/platform/editor/common/editor';
3535

3636
/**
3737
* The text editor that leverages the diff text editor for the editing experience.
@@ -73,7 +73,7 @@ export class TextDiffEditor extends BaseTextEditor implements ITextDiffEditor {
7373
return this.instantiationService.createInstance(DiffEditorWidget, parent, configuration);
7474
}
7575

76-
async setInput(input: EditorInput, options: EditorOptions, token: CancellationToken): Promise<void> {
76+
async setInput(input: EditorInput, options: EditorOptions | undefined, token: CancellationToken): Promise<void> {
7777

7878
// Dispose previous diff navigator
7979
this.diffNavigatorDisposables.clear();
@@ -104,7 +104,7 @@ export class TextDiffEditor extends BaseTextEditor implements ITextDiffEditor {
104104

105105
// Apply Options from TextOptions
106106
let optionsGotApplied = false;
107-
if (options && types.isFunction((<TextEditorOptions>options).apply)) {
107+
if (options && isFunction((<TextEditorOptions>options).apply)) {
108108
optionsGotApplied = (<TextEditorOptions>options).apply(diffEditor, ScrollType.Immediate);
109109
}
110110

@@ -133,9 +133,9 @@ export class TextDiffEditor extends BaseTextEditor implements ITextDiffEditor {
133133
}
134134
}
135135

136-
setOptions(options: EditorOptions): void {
136+
setOptions(options: EditorOptions | undefined): void {
137137
const textOptions = <TextEditorOptions>options;
138-
if (textOptions && types.isFunction(textOptions.apply)) {
138+
if (textOptions && isFunction(textOptions.apply)) {
139139
textOptions.apply(this.getControl(), ScrollType.Smooth);
140140
}
141141
}
@@ -156,7 +156,7 @@ export class TextDiffEditor extends BaseTextEditor implements ITextDiffEditor {
156156
return false;
157157
}
158158

159-
private openAsBinary(input: EditorInput, options: EditorOptions): boolean {
159+
private openAsBinary(input: EditorInput, options: EditorOptions | undefined): boolean {
160160
if (input instanceof DiffEditorInput) {
161161
const originalInput = input.originalInput;
162162
const modifiedInput = input.modifiedInput;
@@ -177,7 +177,12 @@ export class TextDiffEditor extends BaseTextEditor implements ITextDiffEditor {
177177
// because we are triggering another openEditor() call
178178
// and do not control the initial intent that resulted
179179
// in us now opening as binary.
180-
options.overwrite({ activation: EditorActivation.PRESERVE });
180+
const preservingOptions: IEditorOptions = { activation: EditorActivation.PRESERVE };
181+
if (options) {
182+
options.overwrite(preservingOptions);
183+
} else {
184+
options = EditorOptions.create(preservingOptions);
185+
}
181186

182187
this.editorService.openEditor(binaryDiffInput, options, this.group);
183188

@@ -191,7 +196,7 @@ export class TextDiffEditor extends BaseTextEditor implements ITextDiffEditor {
191196
const editorConfiguration = super.computeConfiguration(configuration);
192197

193198
// Handle diff editor specially by merging in diffEditor configuration
194-
if (types.isObject(configuration.diffEditor)) {
199+
if (isObject(configuration.diffEditor)) {
195200
objects.mixin(editorConfiguration, configuration.diffEditor);
196201
}
197202

@@ -233,7 +238,7 @@ export class TextDiffEditor extends BaseTextEditor implements ITextDiffEditor {
233238
private isFileBinaryError(error: Error[]): boolean;
234239
private isFileBinaryError(error: Error): boolean;
235240
private isFileBinaryError(error: Error | Error[]): boolean {
236-
if (types.isArray(error)) {
241+
if (isArray(error)) {
237242
const errors = <Error[]>error;
238243

239244
return errors.some(e => this.isFileBinaryError(e));
@@ -269,7 +274,7 @@ export class TextDiffEditor extends BaseTextEditor implements ITextDiffEditor {
269274
return super.loadTextEditorViewState(resource) as IDiffEditorViewState; // overridden for text diff editor support
270275
}
271276

272-
private saveTextDiffEditorViewState(input: EditorInput | null): void {
277+
private saveTextDiffEditorViewState(input: EditorInput | undefined): void {
273278
if (!(input instanceof DiffEditorInput)) {
274279
return; // only supported for diff editor inputs
275280
}

src/vs/workbench/browser/parts/editor/textEditor.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -191,7 +191,7 @@ export abstract class BaseTextEditor extends BaseEditor implements ITextEditor {
191191
return this.instantiationService.createInstance(CodeEditorWidget, parent, configuration, {});
192192
}
193193

194-
async setInput(input: EditorInput, options: EditorOptions, token: CancellationToken): Promise<void> {
194+
async setInput(input: EditorInput, options: EditorOptions | undefined, token: CancellationToken): Promise<void> {
195195
await super.setInput(input, options, token);
196196

197197
// Update editor options after having set the input. We do this because there can be
@@ -200,7 +200,7 @@ export abstract class BaseTextEditor extends BaseEditor implements ITextEditor {
200200
this._editorContainer.setAttribute('aria-label', this.computeAriaLabel());
201201
}
202202

203-
protected setEditorVisible(visible: boolean, group: IEditorGroup): void {
203+
protected setEditorVisible(visible: boolean, group: IEditorGroup | undefined): void {
204204

205205
// Pass on to Editor
206206
if (visible) {

src/vs/workbench/browser/parts/editor/textResourceEditor.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ export class AbstractTextResourceEditor extends BaseTextEditor {
5454
return nls.localize('textEditor', "Text Editor");
5555
}
5656

57-
async setInput(input: EditorInput, options: EditorOptions, token: CancellationToken): Promise<void> {
57+
async setInput(input: EditorInput, options: EditorOptions | undefined, token: CancellationToken): Promise<void> {
5858

5959
// Remember view settings if input changes
6060
this.saveTextResourceEditorViewState(this.input);
@@ -100,7 +100,7 @@ export class AbstractTextResourceEditor extends BaseTextEditor {
100100
}
101101
}
102102

103-
setOptions(options: EditorOptions): void {
103+
setOptions(options: EditorOptions | undefined): void {
104104
const textOptions = <TextEditorOptions>options;
105105
if (textOptions && types.isFunction(textOptions.apply)) {
106106
textOptions.apply(this.getControl(), ScrollType.Smooth);
@@ -164,7 +164,7 @@ export class AbstractTextResourceEditor extends BaseTextEditor {
164164
super.saveState();
165165
}
166166

167-
private saveTextResourceEditorViewState(input: EditorInput | null): void {
167+
private saveTextResourceEditorViewState(input: EditorInput | undefined): void {
168168
if (!(input instanceof UntitledEditorInput) && !(input instanceof ResourceEditorInput)) {
169169
return; // only enabled for untitled and resource inputs
170170
}

src/vs/workbench/common/editor.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -53,12 +53,12 @@ export interface IEditor {
5353
/**
5454
* The assigned input of this editor.
5555
*/
56-
input: IEditorInput | null;
56+
input: IEditorInput | undefined;
5757

5858
/**
5959
* The assigned options of this editor.
6060
*/
61-
options: IEditorOptions | null;
61+
options: IEditorOptions | undefined;
6262

6363
/**
6464
* The assigned group this editor is showing in.

src/vs/workbench/contrib/extensions/browser/extensionEditor.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -300,7 +300,7 @@ export class ExtensionEditor extends BaseEditor {
300300
return disposables;
301301
}
302302

303-
async setInput(input: ExtensionsInput, options: EditorOptions, token: CancellationToken): Promise<void> {
303+
async setInput(input: ExtensionsInput, options: EditorOptions | undefined, token: CancellationToken): Promise<void> {
304304
if (this.template) {
305305
await this.updateTemplate(input, this.template);
306306
}

src/vs/workbench/contrib/files/browser/editors/binaryFileEditor.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ export class BinaryFileEditor extends BaseBinaryResourceEditor {
4949
);
5050
}
5151

52-
private async openInternal(input: EditorInput, options: EditorOptions): Promise<void> {
52+
private async openInternal(input: EditorInput, options: EditorOptions | undefined): Promise<void> {
5353
if (input instanceof FileEditorInput) {
5454
input.setForceOpenAsText();
5555

0 commit comments

Comments
 (0)