From 9e440ff0c5d0e9fc776eab133cd59ec40527f53e Mon Sep 17 00:00:00 2001 From: Matt Bierner Date: Fri, 15 Feb 2019 13:31:28 -0800 Subject: [PATCH] Strict null work --- .../browser/parts/editor/editorStatus.ts | 37 ++++++++++--------- .../browser/parts/editor/titleControl.ts | 4 +- .../search/browser/searchResultsView.ts | 4 +- .../contrib/search/browser/searchView.ts | 12 +++--- 4 files changed, 29 insertions(+), 28 deletions(-) diff --git a/src/vs/workbench/browser/parts/editor/editorStatus.ts b/src/vs/workbench/browser/parts/editor/editorStatus.ts index 49b6089b484..6d36ad6d9e7 100644 --- a/src/vs/workbench/browser/parts/editor/editorStatus.ts +++ b/src/vs/workbench/browser/parts/editor/editorStatus.ts @@ -585,10 +585,12 @@ export class EditorStatus implements IStatusbarItem { this.onEOLChange(activeCodeEditor); const selections = activeCodeEditor.getSelections(); - for (const change of e.changes) { - if (selections.some(selection => Range.areIntersecting(selection, change.range))) { - this.onSelectionChange(activeCodeEditor); - break; + if (selections) { + for (const change of e.changes) { + if (selections.some(selection => Range.areIntersecting(selection, change.range))) { + this.onSelectionChange(activeCodeEditor); + break; + } } } })); @@ -662,7 +664,7 @@ export class EditorStatus implements IStatusbarItem { this.updateState(update); } - private onMetadataChange(editor: IBaseEditor): void { + private onMetadataChange(editor: IBaseEditor | undefined): void { const update: StateDelta = { metadata: undefined }; if (editor instanceof BaseBinaryResourceEditor || editor instanceof BinaryResourceDiffEditor) { @@ -762,7 +764,7 @@ export class EditorStatus implements IStatusbarItem { // We only support text based editors if (e && (isCodeEditor(e.getControl()) || isDiffEditor(e.getControl()))) { - const encodingSupport: IEncodingSupport = toEditorWithEncodingSupport(e.input); + const encodingSupport: IEncodingSupport | null = e.input ? toEditorWithEncodingSupport(e.input) : null; if (encodingSupport) { const rawEncoding = encodingSupport.getEncoding(); const encodingInfo = SUPPORTED_ENCODINGS[rawEncoding]; @@ -858,19 +860,19 @@ export class ChangeModeAction extends Action { } const textModel = activeTextEditorWidget.getModel(); - const resource = toResource(this.editorService.activeEditor, { supportSideBySide: true }); + const resource = this.editorService.activeEditor ? toResource(this.editorService.activeEditor, { supportSideBySide: true }) : null; let hasLanguageSupport = !!resource; - if (resource.scheme === Schemas.untitled && !this.untitledEditorService.hasAssociatedFilePath(resource)) { + if (resource && resource.scheme === Schemas.untitled && !this.untitledEditorService.hasAssociatedFilePath(resource)) { hasLanguageSupport = false; // no configuration for untitled resources (e.g. "Untitled-1") } // Compute mode - let currentModeId: string; + let currentModeId: string | undefined; let modeId: string; if (textModel) { modeId = textModel.getLanguageIdentifier().language; - currentModeId = this.modeService.getLanguageName(modeId); + currentModeId = this.modeService.getLanguageName(modeId) || undefined; } // All languages are valid picks @@ -910,7 +912,7 @@ export class ChangeModeAction extends Action { let configureModeAssociations: IQuickPickItem; let configureModeSettings: IQuickPickItem; let galleryAction: Action; - if (hasLanguageSupport) { + if (hasLanguageSupport && resource) { const ext = extname(resource) || basename(resource); galleryAction = this.instantiationService.createInstance(ShowLanguageExtensionsAction, ext); @@ -1026,11 +1028,7 @@ export class ChangeModeAction extends Action { } // Make sure to write into the value of the target and not the merged value from USER and WORKSPACE config - let currentAssociations = deepClone((target === ConfigurationTarget.WORKSPACE) ? fileAssociationsConfig.workspace : fileAssociationsConfig.user); - if (!currentAssociations) { - currentAssociations = Object.create(null); - } - + const currentAssociations = deepClone((target === ConfigurationTarget.WORKSPACE) ? fileAssociationsConfig.workspace : fileAssociationsConfig.user) || Object.create(null); currentAssociations[associationKey] = language.id; this.configurationService.updateValue(FILES_ASSOCIATIONS_CONFIG, currentAssociations, target); @@ -1161,7 +1159,10 @@ export class ChangeEncodingAction extends Action { } let activeControl = this.editorService.activeControl; - let encodingSupport: IEncodingSupport = toEditorWithEncodingSupport(activeControl.input); + if (!activeControl) { + return this.quickInputService.pick([{ label: nls.localize('noEditor', "No text editor active at this time") }]); + } + let encodingSupport: IEncodingSupport | null = toEditorWithEncodingSupport(activeControl.input); if (!encodingSupport) { return this.quickInputService.pick([{ label: nls.localize('noFileEditor', "No file active at this time") }]); } @@ -1206,7 +1207,7 @@ export class ChangeEncodingAction extends Action { const configuredEncoding = this.textResourceConfigurationService.getValue(resource, 'files.encoding'); let directMatchIndex: number | undefined; - let aliasMatchIndex: number; + let aliasMatchIndex: number | undefined; // All encodings are valid picks const picks: QuickPickInput[] = Object.keys(SUPPORTED_ENCODINGS) diff --git a/src/vs/workbench/browser/parts/editor/titleControl.ts b/src/vs/workbench/browser/parts/editor/titleControl.ts index 07e6c0ab19a..0a2b6fb57c7 100644 --- a/src/vs/workbench/browser/parts/editor/titleControl.ts +++ b/src/vs/workbench/browser/parts/editor/titleControl.ts @@ -217,7 +217,7 @@ export abstract class TitleControl extends Themable { this.editorToolBarMenuDisposables = dispose(this.editorToolBarMenuDisposables); // Update the resource context - this.resourceContext.set(toResource(this.group.activeEditor, { supportSideBySide: true })); + this.resourceContext.set(this.group.activeEditor ? toResource(this.group.activeEditor, { supportSideBySide: true }) : null); // Editor actions require the editor control to be there, so we retrieve it via service const activeControl = this.group.activeControl; @@ -312,7 +312,7 @@ export abstract class TitleControl extends Themable { }); } - private getKeybinding(action: IAction): ResolvedKeybinding { + private getKeybinding(action: IAction): ResolvedKeybinding | undefined { return this.keybindingService.lookupKeybinding(action.id); } diff --git a/src/vs/workbench/contrib/search/browser/searchResultsView.ts b/src/vs/workbench/contrib/search/browser/searchResultsView.ts index 855060d561b..c7f40a2eff2 100644 --- a/src/vs/workbench/contrib/search/browser/searchResultsView.ts +++ b/src/vs/workbench/contrib/search/browser/searchResultsView.ts @@ -308,7 +308,7 @@ export class SearchAccessibilityProvider implements IAccessibilityProvider; + private readonly selectCurrentMatchEmitter: Emitter; private delayedRefresh: Delayer; private changedWhileHidden: boolean; @@ -704,7 +704,7 @@ export class SearchView extends Viewlet implements IViewlet, IPanel { } selectNextMatch(): void { - const [selected]: RenderableMatch[] = this.tree.getSelection(); + const [selected] = this.tree.getSelection(); // Expand the initial selected node, if needed if (selected instanceof FileMatch) { @@ -742,7 +742,7 @@ export class SearchView extends Viewlet implements IViewlet, IPanel { } selectPreviousMatch(): void { - const [selected]: RenderableMatch[] = this.tree.getSelection(); + const [selected] = this.tree.getSelection(); let navigator = this.tree.navigate(selected); let prev = navigator.previous(); @@ -757,7 +757,7 @@ export class SearchView extends Viewlet implements IViewlet, IPanel { // This is complicated because .last will set the navigator to the last FileMatch, // so expand it and FF to its last child this.tree.expand(prev); - let tmp: RenderableMatch; + let tmp: RenderableMatch | null; while (tmp = navigator.next()) { prev = tmp; } @@ -967,7 +967,7 @@ export class SearchView extends Viewlet implements IViewlet, IPanel { } } - private getSearchTextFromEditor(allowUnselectedWord: boolean): string { + private getSearchTextFromEditor(allowUnselectedWord: boolean): string | null { if (!this.editorService.activeEditor) { return null; } @@ -985,7 +985,7 @@ export class SearchView extends Viewlet implements IViewlet, IPanel { } } - if (!isCodeEditor(activeTextEditorWidget)) { + if (!isCodeEditor(activeTextEditorWidget) || !activeTextEditorWidget.hasModel()) { return null; } -- GitLab