Skip to content

Commit b5c75f3

Browse files
author
Benjamin Pasero
committed
strict null - editor service
1 parent 4f8dbd4 commit b5c75f3

4 files changed

Lines changed: 41 additions & 23 deletions

File tree

src/tsconfig.strictNullChecks.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -320,6 +320,7 @@
320320
"./vs/workbench/services/dialogs/electron-browser/dialogService.ts",
321321
"./vs/workbench/services/dialogs/electron-browser/remoteFileDialog.ts",
322322
"./vs/workbench/services/editor/browser/codeEditorService.ts",
323+
"./vs/workbench/services/editor/browser/editorService.ts",
323324
"./vs/workbench/services/editor/common/editorGroupsService.ts",
324325
"./vs/workbench/services/editor/common/editorService.ts",
325326
"./vs/workbench/services/extensionManagement/node/multiExtensionManagement.ts",

src/vs/workbench/common/editor.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -142,7 +142,7 @@ export interface IEditorControl extends ICompositeControl { }
142142

143143
export interface IFileInputFactory {
144144

145-
createFileInput(resource: URI, encoding: string, instantiationService: IInstantiationService): IFileEditorInput;
145+
createFileInput(resource: URI, encoding: string | undefined, instantiationService: IInstantiationService): IFileEditorInput;
146146

147147
isFileInput(obj: any): obj is IFileEditorInput;
148148
}

src/vs/workbench/contrib/files/common/editors/fileEditorInput.ts

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ export class FileEditorInput extends EditorInput implements IFileEditorInput {
3636
*/
3737
constructor(
3838
private resource: URI,
39-
preferredEncoding: string,
39+
preferredEncoding: string | undefined,
4040
@IInstantiationService private readonly instantiationService: IInstantiationService,
4141
@ITextFileService private readonly textFileService: ITextFileService,
4242
@ITextModelService private readonly textModelResolverService: ITextModelService,
@@ -45,7 +45,9 @@ export class FileEditorInput extends EditorInput implements IFileEditorInput {
4545
) {
4646
super();
4747

48-
this.setPreferredEncoding(preferredEncoding);
48+
if (preferredEncoding) {
49+
this.setPreferredEncoding(preferredEncoding);
50+
}
4951

5052
this.registerListeners();
5153
}
@@ -100,10 +102,7 @@ export class FileEditorInput extends EditorInput implements IFileEditorInput {
100102

101103
setPreferredEncoding(encoding: string): void {
102104
this.preferredEncoding = encoding;
103-
104-
if (encoding) {
105-
this.forceOpenAsText = true; // encoding is a good hint to open the file as text
106-
}
105+
this.forceOpenAsText = true; // encoding is a good hint to open the file as text
107106
}
108107

109108
setForceOpenAsText(): void {

src/vs/workbench/services/editor/browser/editorService.ts

Lines changed: 34 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ import { isCodeEditor, isDiffEditor, ICodeEditor, IDiffEditor } from 'vs/editor/
2727
import { IEditorGroupView, IEditorOpeningEvent, EditorServiceImpl } from 'vs/workbench/browser/parts/editor/editor';
2828
import { ILabelService } from 'vs/platform/label/common/label';
2929
import { registerSingleton } from 'vs/platform/instantiation/common/extensions';
30-
import { withNullAsUndefined } from 'vs/base/common/types';
30+
import { withNullAsUndefined, withUndefinedAsNull } from 'vs/base/common/types';
3131

3232
type ICachedEditorInput = ResourceEditorInput | IFileEditorInput | DataUriEditorInput;
3333

@@ -148,7 +148,7 @@ export class EditorService extends Disposable implements EditorServiceImpl {
148148
for (const handler of this.openEditorHandlers) {
149149
const result = handler(event.editor, event.options, group);
150150
if (result && result.override) {
151-
event.prevent((() => result.override));
151+
event.prevent((() => result.override!.then(editor => withNullAsUndefined(editor))));
152152
break;
153153
}
154154
}
@@ -359,12 +359,12 @@ export class EditorService extends Disposable implements EditorServiceImpl {
359359
}
360360

361361
// Open in targets
362-
const result: Promise<IEditor>[] = [];
362+
const result: Promise<IEditor | null>[] = [];
363363
mapGroupToEditors.forEach((editorsWithOptions, group) => {
364364
result.push(group.openEditors(editorsWithOptions));
365365
});
366366

367-
return Promise.all(result);
367+
return Promise.all(result).then(editors => coalesce(editors));
368368
}
369369

370370
//#endregion
@@ -393,7 +393,10 @@ export class EditorService extends Disposable implements EditorServiceImpl {
393393

394394
let groups: IEditorGroup[] = [];
395395
if (typeof group === 'number') {
396-
groups.push(this.editorGroupService.getGroup(group));
396+
const groupView = this.editorGroupService.getGroup(group);
397+
if (groupView) {
398+
groups.push(groupView);
399+
}
397400
} else if (group) {
398401
groups.push(group);
399402
} else {
@@ -455,7 +458,11 @@ export class EditorService extends Disposable implements EditorServiceImpl {
455458
});
456459

457460
const targetGroup = typeof group === 'number' ? this.editorGroupService.getGroup(group) : group;
458-
return targetGroup.replaceEditors(typedEditors);
461+
if (targetGroup) {
462+
return targetGroup.replaceEditors(typedEditors);
463+
}
464+
465+
return Promise.resolve();
459466
}
460467

461468
//#endregion
@@ -480,7 +487,7 @@ export class EditorService extends Disposable implements EditorServiceImpl {
480487

481488
//#region createInput()
482489

483-
createInput(input: IEditorInputWithOptions | IEditorInput | IResourceEditor): EditorInput | null {
490+
createInput(input: IEditorInputWithOptions | IEditorInput | IResourceEditor): EditorInput {
484491

485492
// Typed Editor Input Support (EditorInput)
486493
if (input instanceof EditorInput) {
@@ -498,9 +505,10 @@ export class EditorService extends Disposable implements EditorServiceImpl {
498505
if (resourceSideBySideInput.masterResource && resourceSideBySideInput.detailResource) {
499506
const masterInput = this.createInput({ resource: resourceSideBySideInput.masterResource, forceFile: resourceSideBySideInput.forceFile });
500507
const detailInput = this.createInput({ resource: resourceSideBySideInput.detailResource, forceFile: resourceSideBySideInput.forceFile });
508+
const label = resourceSideBySideInput.label || masterInput.getName() || localize('sideBySideLabels', "{0} - {1}", this.toDiffLabel(masterInput), this.toDiffLabel(detailInput));
501509

502510
return new SideBySideEditorInput(
503-
resourceSideBySideInput.label || masterInput.getName(),
511+
label,
504512
typeof resourceSideBySideInput.description === 'string' ? resourceSideBySideInput.description : masterInput.getDescription(),
505513
detailInput,
506514
masterInput
@@ -514,7 +522,7 @@ export class EditorService extends Disposable implements EditorServiceImpl {
514522
const rightInput = this.createInput({ resource: resourceDiffInput.rightResource, forceFile: resourceDiffInput.forceFile });
515523
const label = resourceDiffInput.label || localize('compareLabels', "{0} ↔ {1}", this.toDiffLabel(leftInput), this.toDiffLabel(rightInput));
516524

517-
return new DiffEditorInput(label, resourceDiffInput.description, leftInput, rightInput);
525+
return new DiffEditorInput(label, withUndefinedAsNull(resourceDiffInput.description), leftInput, rightInput);
518526
}
519527

520528
// Untitled file support
@@ -539,17 +547,24 @@ export class EditorService extends Disposable implements EditorServiceImpl {
539547
return this.createOrGet(resourceInput.resource, this.instantiationService, label, resourceInput.description, resourceInput.encoding, resourceInput.forceFile) as EditorInput;
540548
}
541549

542-
return null;
550+
throw new Error('Unknown input type');
543551
}
544552

545-
private createOrGet(resource: URI, instantiationService: IInstantiationService, label: string, description: string, encoding?: string, forceFile?: boolean): ICachedEditorInput {
553+
private createOrGet(resource: URI, instantiationService: IInstantiationService, label: string | undefined, description: string | undefined, encoding: string | undefined, forceFile: boolean | undefined): ICachedEditorInput {
546554
if (EditorService.CACHE.has(resource)) {
547-
const input = EditorService.CACHE.get(resource);
555+
const input = EditorService.CACHE.get(resource)!;
548556
if (input instanceof ResourceEditorInput) {
549-
input.setName(label);
550-
input.setDescription(description);
557+
if (label) {
558+
input.setName(label);
559+
}
560+
561+
if (description) {
562+
input.setDescription(description);
563+
}
551564
} else if (!(input instanceof DataUriEditorInput)) {
552-
input.setPreferredEncoding(encoding);
565+
if (encoding) {
566+
input.setPreferredEncoding(encoding);
567+
}
553568
}
554569

555570
return input;
@@ -582,9 +597,12 @@ export class EditorService extends Disposable implements EditorServiceImpl {
582597

583598
private toDiffLabel(input: EditorInput): string | null {
584599
const res = input.getResource();
600+
if (!res) {
601+
return null;
602+
}
585603

586604
// Do not try to extract any paths from simple untitled editors
587-
if (res && res.scheme === Schemas.untitled && !this.untitledEditorService.hasAssociatedFilePath(res)) {
605+
if (res.scheme === Schemas.untitled && !this.untitledEditorService.hasAssociatedFilePath(res)) {
588606
return input.getName();
589607
}
590608

0 commit comments

Comments
 (0)