From f489dc7b2f91786d7af6a75ce36ca735047e1d79 Mon Sep 17 00:00:00 2001 From: Benjamin Pasero Date: Fri, 2 Jul 2021 17:08:41 +0200 Subject: [PATCH] editors - improved `isEditorInput` check --- src/vs/workbench/common/editor.ts | 26 +++++++++---------- .../common/editor/editorGroupModel.ts | 6 ++--- src/vs/workbench/common/editor/editorInput.ts | 9 ++----- .../search/browser/anythingQuickAccess.ts | 7 +++-- .../services/editor/browser/editorService.ts | 4 +-- .../editor/common/editorGroupsService.ts | 3 +-- .../services/history/browser/history.ts | 4 +-- .../browser/parts/editor/editorInput.test.ts | 9 ++----- 8 files changed, 28 insertions(+), 40 deletions(-) diff --git a/src/vs/workbench/common/editor.ts b/src/vs/workbench/common/editor.ts index cdab14fb84d1f..d7ec99c358d25 100644 --- a/src/vs/workbench/common/editor.ts +++ b/src/vs/workbench/common/editor.ts @@ -5,9 +5,9 @@ import { localize } from 'vs/nls'; import { Event } from 'vs/base/common/event'; -import { areFunctions, assertIsDefined } from 'vs/base/common/types'; +import { assertIsDefined } from 'vs/base/common/types'; import { URI } from 'vs/base/common/uri'; -import { IDisposable, toDisposable } from 'vs/base/common/lifecycle'; +import { Disposable, IDisposable, toDisposable } from 'vs/base/common/lifecycle'; import { IEditor, IEditorViewState, IDiffEditor } from 'vs/editor/common/editorCommon'; import { IEditorModel, IEditorOptions, ITextEditorOptions, IBaseResourceEditorInput, IResourceEditorInput, ITextResourceEditorInput, IBaseTextResourceEditorInput } from 'vs/platform/editor/common/editor'; import { IInstantiationService, IConstructorSignature0, ServicesAccessor, BrandedService } from 'vs/platform/instantiation/common/instantiation'; @@ -309,7 +309,7 @@ export interface IResourceDiffEditorInput extends IBaseResourceEditorInput { } export function isResourceEditorInput(editor: unknown): editor is IResourceEditorInput { - if (isIEditorInput(editor)) { + if (isEditorInput(editor)) { return false; // make sure to not accidentally match on typed editor inputs } @@ -319,7 +319,7 @@ export function isResourceEditorInput(editor: unknown): editor is IResourceEdito } export function isResourceDiffEditorInput(editor: unknown): editor is IResourceDiffEditorInput { - if (isIEditorInput(editor)) { + if (isEditorInput(editor)) { return false; // make sure to not accidentally match on typed editor inputs } @@ -329,7 +329,7 @@ export function isResourceDiffEditorInput(editor: unknown): editor is IResourceD } export function isUntitledResourceEditorInput(editor: unknown): editor is IUntitledTextResourceEditorInput { - if (isIEditorInput(editor)) { + if (isEditorInput(editor)) { return false; // make sure to not accidentally match on typed editor inputs } @@ -633,12 +633,12 @@ export interface IEditorInput extends IDisposable { isDisposed(): boolean; } -export function isIEditorInput(editor: unknown): editor is IEditorInput { - const candidate = editor as IEditorInput | undefined; +export abstract class BaseEditorInput extends Disposable { + // Marker class for implementing `isEditorInput` +} - return typeof candidate?.typeId === 'string' && - typeof candidate.capabilities === 'number' && - areFunctions(candidate.matches, candidate.toUntyped, candidate.resolve); +export function isEditorInput(editor: unknown): editor is IEditorInput { + return editor instanceof BaseEditorInput; } export interface IEditorInputWithPreferredResource { @@ -685,7 +685,7 @@ export interface ISideBySideEditorInput extends IEditorInput { export function isSideBySideEditorInput(editor: unknown): editor is ISideBySideEditorInput { const candidate = editor as ISideBySideEditorInput | undefined; - return isIEditorInput(candidate?.primary) && isIEditorInput(candidate?.secondary); + return isEditorInput(candidate?.primary) && isEditorInput(candidate?.secondary); } /** @@ -759,7 +759,7 @@ export interface IEditorInputWithOptions { export function isEditorInputWithOptions(editor: unknown): editor is IEditorInputWithOptions { const candidate = editor as IEditorInputWithOptions | undefined; - return isIEditorInput(candidate?.editor); + return isEditorInput(candidate?.editor); } /** @@ -786,7 +786,7 @@ export interface IEditorIdentifier { export function isEditorIdentifier(identifier: unknown): identifier is IEditorIdentifier { const candidate = identifier as IEditorIdentifier | undefined; - return typeof candidate?.groupId === 'number' && isIEditorInput(candidate.editor); + return typeof candidate?.groupId === 'number' && isEditorInput(candidate.editor); } /** diff --git a/src/vs/workbench/common/editor/editorGroupModel.ts b/src/vs/workbench/common/editor/editorGroupModel.ts index 1bf22636e9cc2..8005dd01b1ef4 100644 --- a/src/vs/workbench/common/editor/editorGroupModel.ts +++ b/src/vs/workbench/common/editor/editorGroupModel.ts @@ -54,10 +54,10 @@ export interface ISerializedEditorGroupModel { sticky?: number; } -export function isSerializedEditorGroupModel(obj?: unknown): obj is ISerializedEditorGroupModel { - const group = obj as ISerializedEditorGroupModel; +export function isSerializedEditorGroupModel(group?: unknown): group is ISerializedEditorGroupModel { + const candidate = group as ISerializedEditorGroupModel | undefined; - return !!(obj && typeof obj === 'object' && Array.isArray(group.editors) && Array.isArray(group.mru)); + return !!(candidate && typeof candidate === 'object' && Array.isArray(candidate.editors) && Array.isArray(candidate.mru)); } export class EditorGroupModel extends Disposable { diff --git a/src/vs/workbench/common/editor/editorInput.ts b/src/vs/workbench/common/editor/editorInput.ts index 31ad09aff7dc9..0f1215b8a3e30 100644 --- a/src/vs/workbench/common/editor/editorInput.ts +++ b/src/vs/workbench/common/editor/editorInput.ts @@ -5,17 +5,16 @@ import { Emitter } from 'vs/base/common/event'; import { URI } from 'vs/base/common/uri'; -import { Disposable } from 'vs/base/common/lifecycle'; import { IEditorModel } from 'vs/platform/editor/common/editor'; import { firstOrDefault } from 'vs/base/common/arrays'; -import { IEditorInput, EditorInputCapabilities, Verbosity, GroupIdentifier, ISaveOptions, IRevertOptions, IMoveResult, IEditorDescriptor, IEditorPane, IUntypedEditorInput, UntypedEditorContext, EditorResourceAccessor } from 'vs/workbench/common/editor'; +import { IEditorInput, EditorInputCapabilities, Verbosity, GroupIdentifier, ISaveOptions, IRevertOptions, IMoveResult, IEditorDescriptor, IEditorPane, IUntypedEditorInput, UntypedEditorContext, EditorResourceAccessor, BaseEditorInput, isEditorInput } from 'vs/workbench/common/editor'; import { isEqual } from 'vs/base/common/resources'; /** * Editor inputs are lightweight objects that can be passed to the workbench API to open inside the editor part. * Each editor input is mapped to an editor that is capable of opening it through the Platform facade. */ -export abstract class EditorInput extends Disposable implements IEditorInput { +export abstract class EditorInput extends BaseEditorInput implements IEditorInput { protected readonly _onDidChangeDirty = this._register(new Emitter()); readonly onDidChangeDirty = this._onDidChangeDirty.event; @@ -164,7 +163,3 @@ export abstract class EditorInput extends Disposable implements IEditorInput { super.dispose(); } } - -export function isEditorInput(editor: unknown): editor is IEditorInput { - return editor instanceof EditorInput; -} diff --git a/src/vs/workbench/contrib/search/browser/anythingQuickAccess.ts b/src/vs/workbench/contrib/search/browser/anythingQuickAccess.ts index 94bad0ab6a1b9..4283f377aae01 100644 --- a/src/vs/workbench/contrib/search/browser/anythingQuickAccess.ts +++ b/src/vs/workbench/contrib/search/browser/anythingQuickAccess.ts @@ -27,8 +27,7 @@ import { IModeService } from 'vs/editor/common/services/modeService'; import { localize } from 'vs/nls'; import { IWorkingCopyService } from 'vs/workbench/services/workingCopy/common/workingCopyService'; import { IConfigurationService } from 'vs/platform/configuration/common/configuration'; -import { IWorkbenchEditorConfiguration, IEditorInput, EditorResourceAccessor } from 'vs/workbench/common/editor'; -import { isEditorInput } from 'vs/workbench/common/editor/editorInput'; +import { IWorkbenchEditorConfiguration, IEditorInput, EditorResourceAccessor, isEditorInput } from 'vs/workbench/common/editor'; import { IEditorService, SIDE_GROUP, ACTIVE_GROUP } from 'vs/workbench/services/editor/common/editorService'; import { Range, IRange } from 'vs/editor/common/core/range'; import { ThrottledDelayer } from 'vs/base/common/async'; @@ -61,9 +60,9 @@ interface IEditorSymbolAnythingQuickPickItem extends IAnythingQuickPickItem { } function isEditorSymbolQuickPickItem(pick?: IAnythingQuickPickItem): pick is IEditorSymbolAnythingQuickPickItem { - const candidate = pick ? pick as IEditorSymbolAnythingQuickPickItem : undefined; + const candidate = pick as IEditorSymbolAnythingQuickPickItem | undefined; - return !!candidate && !!candidate.range && !!candidate.resource; + return !!candidate?.range && !!candidate.resource; } export class AnythingQuickAccessProvider extends PickerQuickAccessProvider { diff --git a/src/vs/workbench/services/editor/browser/editorService.ts b/src/vs/workbench/services/editor/browser/editorService.ts index 6dd3e81390399..c9dc1115bfd8d 100644 --- a/src/vs/workbench/services/editor/browser/editorService.ts +++ b/src/vs/workbench/services/editor/browser/editorService.ts @@ -5,8 +5,8 @@ import { IInstantiationService } from 'vs/platform/instantiation/common/instantiation'; import { IResourceEditorInput, IEditorOptions, EditorActivation, EditorOverride, IResourceEditorInputIdentifier, ITextEditorOptions, ITextResourceEditorInput } from 'vs/platform/editor/common/editor'; -import { SideBySideEditor, IEditorInput, IEditorPane, GroupIdentifier, IFileEditorInput, IUntitledTextResourceEditorInput, IResourceDiffEditorInput, IEditorInputFactoryRegistry, EditorExtensions, IEditorInputWithOptions, isEditorInputWithOptions, IEditorIdentifier, IEditorCloseEvent, ITextEditorPane, ITextDiffEditorPane, IRevertOptions, SaveReason, EditorsOrder, isTextEditorPane, IWorkbenchEditorConfiguration, EditorResourceAccessor, IVisibleEditorPane, EditorInputCapabilities, isResourceDiffEditorInput, IUntypedEditorInput, DEFAULT_EDITOR_ASSOCIATION, UntypedEditorContext, isResourceEditorInput } from 'vs/workbench/common/editor'; -import { EditorInput, isEditorInput } from 'vs/workbench/common/editor/editorInput'; +import { SideBySideEditor, IEditorInput, IEditorPane, GroupIdentifier, IFileEditorInput, IUntitledTextResourceEditorInput, IResourceDiffEditorInput, IEditorInputFactoryRegistry, EditorExtensions, IEditorInputWithOptions, isEditorInputWithOptions, IEditorIdentifier, IEditorCloseEvent, ITextEditorPane, ITextDiffEditorPane, IRevertOptions, SaveReason, EditorsOrder, isTextEditorPane, IWorkbenchEditorConfiguration, EditorResourceAccessor, IVisibleEditorPane, EditorInputCapabilities, isResourceDiffEditorInput, IUntypedEditorInput, DEFAULT_EDITOR_ASSOCIATION, UntypedEditorContext, isResourceEditorInput, isEditorInput } from 'vs/workbench/common/editor'; +import { EditorInput } from 'vs/workbench/common/editor/editorInput'; import { SideBySideEditorInput } from 'vs/workbench/common/editor/sideBySideEditorInput'; import { TextResourceEditorInput } from 'vs/workbench/common/editor/textResourceEditorInput'; import { Registry } from 'vs/platform/registry/common/platform'; diff --git a/src/vs/workbench/services/editor/common/editorGroupsService.ts b/src/vs/workbench/services/editor/common/editorGroupsService.ts index 11fcc014f6c70..12b5dbe12e298 100644 --- a/src/vs/workbench/services/editor/common/editorGroupsService.ts +++ b/src/vs/workbench/services/editor/common/editorGroupsService.ts @@ -5,8 +5,7 @@ import { Event } from 'vs/base/common/event'; import { createDecorator } from 'vs/platform/instantiation/common/instantiation'; -import { IEditorInput, IEditorPane, GroupIdentifier, IEditorInputWithOptions, CloseDirection, IEditorPartOptions, IEditorPartOptionsChangeEvent, EditorsOrder, IVisibleEditorPane, IEditorCloseEvent, IEditorMoveEvent, IEditorOpenEvent, IUntypedEditorInput } from 'vs/workbench/common/editor'; -import { isEditorInput } from 'vs/workbench/common/editor/editorInput'; +import { IEditorInput, IEditorPane, GroupIdentifier, IEditorInputWithOptions, CloseDirection, IEditorPartOptions, IEditorPartOptionsChangeEvent, EditorsOrder, IVisibleEditorPane, IEditorCloseEvent, IEditorMoveEvent, IEditorOpenEvent, IUntypedEditorInput, isEditorInput } from 'vs/workbench/common/editor'; import { IEditorOptions } from 'vs/platform/editor/common/editor'; import { IConfigurationService } from 'vs/platform/configuration/common/configuration'; import { IDimension } from 'vs/editor/common/editorCommon'; diff --git a/src/vs/workbench/services/history/browser/history.ts b/src/vs/workbench/services/history/browser/history.ts index ca436c63d75dd..90e52929abe72 100644 --- a/src/vs/workbench/services/history/browser/history.ts +++ b/src/vs/workbench/services/history/browser/history.ts @@ -8,8 +8,8 @@ import { URI, UriComponents } from 'vs/base/common/uri'; import { parse, stringify } from 'vs/base/common/marshalling'; import { IEditor } from 'vs/editor/common/editorCommon'; import { ITextEditorOptions, IResourceEditorInput, TextEditorSelectionRevealType, IEditorOptions } from 'vs/platform/editor/common/editor'; -import { IEditorInput, IEditorPane, IEditorCloseEvent, EditorResourceAccessor, IEditorIdentifier, GroupIdentifier, EditorsOrder, SideBySideEditor, IUntypedEditorInput, UntypedEditorContext, isResourceEditorInput } from 'vs/workbench/common/editor'; -import { EditorInput, isEditorInput } from 'vs/workbench/common/editor/editorInput'; +import { IEditorInput, IEditorPane, IEditorCloseEvent, EditorResourceAccessor, IEditorIdentifier, GroupIdentifier, EditorsOrder, SideBySideEditor, IUntypedEditorInput, UntypedEditorContext, isResourceEditorInput, isEditorInput } from 'vs/workbench/common/editor'; +import { EditorInput } from 'vs/workbench/common/editor/editorInput'; import { IEditorService } from 'vs/workbench/services/editor/common/editorService'; import { IHistoryService } from 'vs/workbench/services/history/common/history'; import { FileChangesEvent, IFileService, FileChangeType, FILES_EXCLUDE_CONFIG, FileOperationEvent, FileOperation } from 'vs/platform/files/common/files'; diff --git a/src/vs/workbench/test/browser/parts/editor/editorInput.test.ts b/src/vs/workbench/test/browser/parts/editor/editorInput.test.ts index 84b8869689710..dc0c80612510a 100644 --- a/src/vs/workbench/test/browser/parts/editor/editorInput.test.ts +++ b/src/vs/workbench/test/browser/parts/editor/editorInput.test.ts @@ -5,8 +5,8 @@ import * as assert from 'assert'; import { URI } from 'vs/base/common/uri'; -import { isIEditorInput, isResourceDiffEditorInput, isResourceEditorInput, isUntitledResourceEditorInput } from 'vs/workbench/common/editor'; -import { EditorInput, isEditorInput } from 'vs/workbench/common/editor/editorInput'; +import { isEditorInput, isResourceDiffEditorInput, isResourceEditorInput, isUntitledResourceEditorInput } from 'vs/workbench/common/editor'; +import { EditorInput } from 'vs/workbench/common/editor/editorInput'; import { TestEditorInput } from 'vs/workbench/test/browser/workbenchTestServices'; suite('EditorInput', () => { @@ -28,11 +28,6 @@ suite('EditorInput', () => { assert.ok(!isEditorInput({ resource: URI.file('/') })); assert.ok(!isEditorInput({})); - assert.ok(isIEditorInput(input)); - assert.ok(!isIEditorInput(undefined)); - assert.ok(!isIEditorInput({ resource: URI.file('/') })); - assert.ok(!isIEditorInput({})); - assert.ok(!isResourceEditorInput(input)); assert.ok(!isUntitledResourceEditorInput(input)); assert.ok(!isResourceDiffEditorInput(input));