Skip to content

Commit

Permalink
editors - improved isEditorInput check
Browse files Browse the repository at this point in the history
  • Loading branch information
bpasero committed Jul 2, 2021
1 parent 3e32c1a commit f489dc7
Show file tree
Hide file tree
Showing 8 changed files with 28 additions and 40 deletions.
26 changes: 13 additions & 13 deletions src/vs/workbench/common/editor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down Expand Up @@ -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
}

Expand All @@ -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
}

Expand All @@ -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
}

Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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);
}

/**
Expand Down Expand Up @@ -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);
}

/**
Expand All @@ -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);
}

/**
Expand Down
6 changes: 3 additions & 3 deletions src/vs/workbench/common/editor/editorGroupModel.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
9 changes: 2 additions & 7 deletions src/vs/workbench/common/editor/editorInput.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<void>());
readonly onDidChangeDirty = this._onDidChangeDirty.event;
Expand Down Expand Up @@ -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;
}
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down Expand Up @@ -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<IAnythingQuickPickItem> {
Expand Down
4 changes: 2 additions & 2 deletions src/vs/workbench/services/editor/browser/editorService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down
4 changes: 2 additions & 2 deletions src/vs/workbench/services/history/browser/history.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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', () => {
Expand All @@ -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));
Expand Down

0 comments on commit f489dc7

Please sign in to comment.