From 03500103907ae4f4233186b81cc141a75bf53d00 Mon Sep 17 00:00:00 2001 From: Benjamin Pasero Date: Sat, 6 Jun 2020 10:28:16 +0200 Subject: [PATCH] editors - merge ignoreOverrides and overrideId into one (#99123) --- src/vs/platform/editor/common/editor.ts | 14 +++++----- .../api/browser/mainThreadEditors.ts | 6 ++--- src/vs/workbench/common/editor.ts | 26 +++++++------------ .../customEditor/browser/customEditors.ts | 14 +++++----- .../contrib/files/common/openWith.ts | 4 +-- .../notebook/browser/notebook.contribution.ts | 12 ++++----- .../browser/searchEditor.contribution.ts | 3 ++- .../services/editor/browser/editorService.ts | 16 ++++++------ 8 files changed, 45 insertions(+), 50 deletions(-) diff --git a/src/vs/platform/editor/common/editor.ts b/src/vs/platform/editor/common/editor.ts index 7873992809995..75037ef6506fe 100644 --- a/src/vs/platform/editor/common/editor.ts +++ b/src/vs/platform/editor/common/editor.ts @@ -121,6 +121,8 @@ export enum EditorOpenContext { USER } +export const IGNORE_OVERRIDES = Object.create(null); + export interface IEditorOptions { /** @@ -197,14 +199,12 @@ export interface IEditorOptions { readonly ignoreError?: boolean; /** - * Does not use editor overrides while opening the editor - */ - readonly ignoreOverrides?: boolean; - - /** - * An optional id to override the editor used to edit the resource, e.g. custom editor. + * Allows to override the editor that should be used to display the input: + * - `undefined`: let the editor decide for itself + * - `IGNORE_OVERRIDES`: disable overrides + * - `string`: specific override by id */ - readonly overrideId?: string; + readonly override?: typeof IGNORE_OVERRIDES | string; /** * A optional hint to signal in which context the editor opens. diff --git a/src/vs/workbench/api/browser/mainThreadEditors.ts b/src/vs/workbench/api/browser/mainThreadEditors.ts index dc82551273408..b31d759624793 100644 --- a/src/vs/workbench/api/browser/mainThreadEditors.ts +++ b/src/vs/workbench/api/browser/mainThreadEditors.ts @@ -15,7 +15,7 @@ import { ISelection } from 'vs/editor/common/core/selection'; import { IDecorationOptions, IDecorationRenderOptions, ILineChange } from 'vs/editor/common/editorCommon'; import { ISingleEditOperation } from 'vs/editor/common/model'; import { CommandsRegistry } from 'vs/platform/commands/common/commands'; -import { IEditorOptions, ITextEditorOptions, IResourceEditorInput, EditorActivation } from 'vs/platform/editor/common/editor'; +import { IEditorOptions, ITextEditorOptions, IResourceEditorInput, EditorActivation, IGNORE_OVERRIDES } from 'vs/platform/editor/common/editor'; import { ServicesAccessor } from 'vs/platform/instantiation/common/instantiation'; import { IOpenerService } from 'vs/platform/opener/common/opener'; import { MainThreadDocumentsAndEditors } from 'vs/workbench/api/browser/mainThreadDocumentsAndEditors'; @@ -125,7 +125,7 @@ export class MainThreadTextEditors implements MainThreadTextEditorsShape { // preserve pre 1.38 behaviour to not make group active when preserveFocus: true // but make sure to restore the editor to fix https://github.com/microsoft/vscode/issues/79633 activation: options.preserveFocus ? EditorActivation.RESTORE : undefined, - ignoreOverrides: true + override: IGNORE_OVERRIDES }; const input: IResourceEditorInput = { @@ -306,7 +306,7 @@ CommandsRegistry.registerCommand('_workbench.openWith', (accessor: ServicesAcces const [resource, id, options, position] = args; const group = editorGroupsService.getGroup(viewColumnToEditorGroup(editorGroupsService, position)) ?? editorGroupsService.activeGroup; - const textOptions = options ? { ...options, ignoreOverrides: true } : { ignoreOverrides: true }; + const textOptions: ITextEditorOptions = options ? { ...options, override: IGNORE_OVERRIDES } : { override: IGNORE_OVERRIDES }; const input = editorService.createEditorInput({ resource }); return openEditorWith(input, id, textOptions, group, editorService, configurationService, quickInputService); diff --git a/src/vs/workbench/common/editor.ts b/src/vs/workbench/common/editor.ts index 6fe3f98fe4957..1cd8348f0e32c 100644 --- a/src/vs/workbench/common/editor.ts +++ b/src/vs/workbench/common/editor.ts @@ -9,7 +9,7 @@ import { withNullAsUndefined, assertIsDefined } from 'vs/base/common/types'; import { URI } from 'vs/base/common/uri'; import { IDisposable, Disposable, toDisposable } from 'vs/base/common/lifecycle'; import { IEditor, IEditorViewState, ScrollType, IDiffEditor } from 'vs/editor/common/editorCommon'; -import { IEditorModel, IEditorOptions, ITextEditorOptions, IBaseResourceEditorInput, IResourceEditorInput, EditorActivation, EditorOpenContext, ITextEditorSelection, TextEditorSelectionRevealType } from 'vs/platform/editor/common/editor'; +import { IEditorModel, IEditorOptions, ITextEditorOptions, IBaseResourceEditorInput, IResourceEditorInput, EditorActivation, EditorOpenContext, ITextEditorSelection, TextEditorSelectionRevealType, IGNORE_OVERRIDES } from 'vs/platform/editor/common/editor'; import { IInstantiationService, IConstructorSignature0, ServicesAccessor, BrandedService } from 'vs/platform/instantiation/common/instantiation'; import { RawContextKey } from 'vs/platform/contextkey/common/contextkey'; import { Registry } from 'vs/platform/registry/common/platform'; @@ -926,14 +926,12 @@ export class EditorOptions implements IEditorOptions { ignoreError: boolean | undefined; /** - * Does not use editor overrides while opening the editor. + * Allows to override the editor that should be used to display the input: + * - `undefined`: let the editor decide for itself + * - `IGNORE_OVERRIDES`: disable overrides + * - `string`: specific override by id */ - ignoreOverrides: boolean | undefined; - - /** - * An optional id to override the editor used to edit the resource, e.g. custom editor. - */ - overrideId: string | undefined; + override?: typeof IGNORE_OVERRIDES | string; /** * A optional hint to signal in which context the editor opens. @@ -991,12 +989,8 @@ export class EditorOptions implements IEditorOptions { this.index = options.index; } - if (typeof options.ignoreOverrides === 'boolean') { - this.ignoreOverrides = options.ignoreOverrides; - } - - if (typeof options.overrideId === 'string') { - this.overrideId = options.overrideId; + if (typeof options.override === 'string' || options.override === IGNORE_OVERRIDES) { + this.override = options.override; } if (typeof options.context === 'number') { @@ -1364,10 +1358,10 @@ export async function pathsToEditors(paths: IPathData[] | undefined, fileService startColumn: path.columnNumber || 1 }, pinned: true, - overrideId: path.overrideId + override: path.overrideId } : { pinned: true, - overrideId: path.overrideId + override: path.overrideId }; let input: IResourceEditorInput | IUntitledTextResourceEditorInput; diff --git a/src/vs/workbench/contrib/customEditor/browser/customEditors.ts b/src/vs/workbench/contrib/customEditor/browser/customEditors.ts index ef61526a7f22d..a4a93d9617868 100644 --- a/src/vs/workbench/contrib/customEditor/browser/customEditors.ts +++ b/src/vs/workbench/contrib/customEditor/browser/customEditors.ts @@ -13,7 +13,7 @@ import { generateUuid } from 'vs/base/common/uuid'; import * as nls from 'vs/nls'; import { IConfigurationService } from 'vs/platform/configuration/common/configuration'; import { IContextKey, IContextKeyService } from 'vs/platform/contextkey/common/contextkey'; -import { EditorActivation, IEditorOptions, ITextEditorOptions } from 'vs/platform/editor/common/editor'; +import { EditorActivation, IEditorOptions, ITextEditorOptions, IGNORE_OVERRIDES } from 'vs/platform/editor/common/editor'; import { FileOperation, IFileService } from 'vs/platform/files/common/files'; import { IInstantiationService } from 'vs/platform/instantiation/common/instantiation'; import { IQuickInputService, IQuickPickItem } from 'vs/platform/quickinput/common/quickInput'; @@ -203,7 +203,7 @@ export class CustomEditorService extends Disposable implements ICustomEditorServ ): Promise { if (viewType === defaultCustomEditor.id) { const fileEditorInput = this.editorService.createEditorInput({ resource, forceFile: true }); - return this.openEditorForResource(resource, fileEditorInput, { ...options, ignoreOverrides: true }, group); + return this.openEditorForResource(resource, fileEditorInput, { ...options, override: IGNORE_OVERRIDES }, group); } if (!this._contributedEditors.get(viewType)) { @@ -391,7 +391,7 @@ export class CustomEditorService extends Disposable implements ICustomEditorServ } const targetGroup = group || this.editorGroupService.activeGroup; - const newEditor = await this.openEditorForResource(resource, editorToUse.editor, { ...options, ignoreOverrides: true }, targetGroup); + const newEditor = await this.openEditorForResource(resource, editorToUse.editor, { ...options, override: IGNORE_OVERRIDES }, targetGroup); if (targetGroup.id !== editorToUse.group.id) { editorToUse.group.closeEditor(editorToUse.editor); } @@ -499,7 +499,7 @@ export class CustomEditorContribution extends Disposable implements IWorkbenchCo if (id) { return { - override: this.customEditorService.openWith(resource, id, { ...options, ignoreOverrides: true }, group) + override: this.customEditorService.openWith(resource, id, { ...options, override: IGNORE_OVERRIDES }, group) }; } @@ -531,7 +531,7 @@ export class CustomEditorContribution extends Disposable implements IWorkbenchCo return { override: this.editorService.openEditor(existingEditorForResource, { ...options, - ignoreOverrides: true, + override: IGNORE_OVERRIDES, activation: options?.preserveFocus ? EditorActivation.RESTORE : undefined, }, group) }; @@ -562,7 +562,7 @@ export class CustomEditorContribution extends Disposable implements IWorkbenchCo // Open VS Code's standard editor but prompt user to see if they wish to use a custom one instead return { override: (async () => { - const standardEditor = await this.editorService.openEditor(editor, { ...options, ignoreOverrides: true }, group); + const standardEditor = await this.editorService.openEditor(editor, { ...options, override: IGNORE_OVERRIDES }, group); // Give a moment to make sure the editor is showing. // Otherwise the focus shift can cause the prompt to be dismissed right away. await new Promise(resolve => setTimeout(resolve, 20)); @@ -630,7 +630,7 @@ export class CustomEditorContribution extends Disposable implements IWorkbenchCo return { override: (async () => { const input = new DiffEditorInput(editor.getName(), editor.getDescription(), originalOverride || editor.originalInput, modifiedOverride || editor.modifiedInput, true); - return this.editorService.openEditor(input, { ...options, ignoreOverrides: true }, group); + return this.editorService.openEditor(input, { ...options, override: IGNORE_OVERRIDES }, group); })(), }; } diff --git a/src/vs/workbench/contrib/files/common/openWith.ts b/src/vs/workbench/contrib/files/common/openWith.ts index 1d8b7180bab48..7bef0c609ff5f 100644 --- a/src/vs/workbench/contrib/files/common/openWith.ts +++ b/src/vs/workbench/contrib/files/common/openWith.ts @@ -7,7 +7,7 @@ import { basename, extname, isEqual } from 'vs/base/common/resources'; import { URI } from 'vs/base/common/uri'; import * as nls from 'vs/nls'; import { IConfigurationService } from 'vs/platform/configuration/common/configuration'; -import { IEditorOptions, ITextEditorOptions } from 'vs/platform/editor/common/editor'; +import { IEditorOptions, ITextEditorOptions, IGNORE_OVERRIDES } from 'vs/platform/editor/common/editor'; import { IQuickInputService, IQuickPickItem } from 'vs/platform/quickinput/common/quickInput'; import { IEditorInput, IEditorPane } from 'vs/workbench/common/editor'; import { FileEditorInput } from 'vs/workbench/contrib/files/common/editors/fileEditorInput'; @@ -136,7 +136,7 @@ export function getAllAvailableEditors( } const fileEditorInput = editorService.createEditorInput({ resource: input.resource, forceFile: true }); - const textOptions = options ? { ...options, ignoreOverrides: true } : { ignoreOverrides: true }; + const textOptions: IEditorOptions | ITextEditorOptions = options ? { ...options, override: IGNORE_OVERRIDES } : { override: IGNORE_OVERRIDES }; return { override: editorService.openEditor(fileEditorInput, textOptions, group) }; } }, diff --git a/src/vs/workbench/contrib/notebook/browser/notebook.contribution.ts b/src/vs/workbench/contrib/notebook/browser/notebook.contribution.ts index b19376747c174..dc7addc891322 100644 --- a/src/vs/workbench/contrib/notebook/browser/notebook.contribution.ts +++ b/src/vs/workbench/contrib/notebook/browser/notebook.contribution.ts @@ -17,7 +17,7 @@ import { IModeService } from 'vs/editor/common/services/modeService'; import { ITextModelContentProvider, ITextModelService } from 'vs/editor/common/services/resolverService'; import * as nls from 'vs/nls'; import { Extensions, IConfigurationRegistry } from 'vs/platform/configuration/common/configurationRegistry'; -import { IEditorOptions, ITextEditorOptions } from 'vs/platform/editor/common/editor'; +import { IEditorOptions, ITextEditorOptions, IGNORE_OVERRIDES } from 'vs/platform/editor/common/editor'; import { SyncDescriptor } from 'vs/platform/instantiation/common/descriptors'; import { registerSingleton } from 'vs/platform/instantiation/common/extensions'; import { IInstantiationService } from 'vs/platform/instantiation/common/instantiation'; @@ -254,7 +254,7 @@ export class NotebookContribution extends Disposable implements IWorkbenchContri // No need to do anything originalInput.updateGroup(group.id); return { - override: this.editorService.openEditor(originalInput, new NotebookEditorOptions(options || {}).with({ ignoreOverrides: true }), group) + override: this.editorService.openEditor(originalInput, new NotebookEditorOptions(options || {}).with({ override: IGNORE_OVERRIDES }), group) }; } else { // Create a copy of the input. @@ -273,7 +273,7 @@ export class NotebookContribution extends Disposable implements IWorkbenchContri } return { - override: this.editorService.openEditor(copiedInput, new NotebookEditorOptions(options || {}).with({ ignoreOverrides: true }), group) + override: this.editorService.openEditor(copiedInput, new NotebookEditorOptions(options || {}).with({ override: IGNORE_OVERRIDES }), group) }; } } @@ -302,7 +302,7 @@ export class NotebookContribution extends Disposable implements IWorkbenchContri if (existingEditors.length) { // switch to this cell - return { override: this.editorService.openEditor(existingEditors[0], new NotebookEditorOptions(options || {}).with({ ignoreOverrides: true }), group) }; + return { override: this.editorService.openEditor(existingEditors[0], new NotebookEditorOptions(options || {}).with({ override: IGNORE_OVERRIDES }), group) }; } } @@ -311,7 +311,7 @@ export class NotebookContribution extends Disposable implements IWorkbenchContri if (!input!.isDisposed()) { input?.updateGroup(group.id); - return { override: this.editorService.openEditor(input!, new NotebookEditorOptions(options || {}).with({ ignoreOverrides: true }), group) }; + return { override: this.editorService.openEditor(input!, new NotebookEditorOptions(options || {}).with({ override: IGNORE_OVERRIDES }), group) }; } else { this._resourceMapping.delete(resource); } @@ -357,7 +357,7 @@ export class NotebookContribution extends Disposable implements IWorkbenchContri index = group.isPinned(originalInput) ? originalEditorIndex + 1 : originalEditorIndex; } - return { override: this.editorService.openEditor(input, new NotebookEditorOptions(options || {}).with({ ignoreOverrides: true, index }), group) }; + return { override: this.editorService.openEditor(input, new NotebookEditorOptions(options || {}).with({ override: IGNORE_OVERRIDES, index }), group) }; } } diff --git a/src/vs/workbench/contrib/searchEditor/browser/searchEditor.contribution.ts b/src/vs/workbench/contrib/searchEditor/browser/searchEditor.contribution.ts index d3c445104dbcb..6aab81546c1e9 100644 --- a/src/vs/workbench/contrib/searchEditor/browser/searchEditor.contribution.ts +++ b/src/vs/workbench/contrib/searchEditor/browser/searchEditor.contribution.ts @@ -33,6 +33,7 @@ import { createEditorFromSearchResult, modifySearchEditorContextLinesCommand, op import { getOrMakeSearchEditorInput, SearchConfiguration, SearchEditorInput } from 'vs/workbench/contrib/searchEditor/browser/searchEditorInput'; import { parseSavedSearchEditor } from 'vs/workbench/contrib/searchEditor/browser/searchEditorSerialization'; import { IEditorService } from 'vs/workbench/services/editor/common/editorService'; +import { IGNORE_OVERRIDES } from 'vs/platform/editor/common/editor'; const OpenInEditorCommandId = 'search.action.openInEditor'; @@ -93,7 +94,7 @@ class SearchEditorContribution implements IWorkbenchContribution { override: (async () => { const { config } = await instantiationService.invokeFunction(parseSavedSearchEditor, resource); const input = instantiationService.invokeFunction(getOrMakeSearchEditorInput, { backingUri: resource, config }); - return editorService.openEditor(input, { ...options, ignoreOverrides: true }, group); + return editorService.openEditor(input, { ...options, override: IGNORE_OVERRIDES }, group); })() }; } diff --git a/src/vs/workbench/services/editor/browser/editorService.ts b/src/vs/workbench/services/editor/browser/editorService.ts index 8c248dc860b67..f97893559fa9a 100644 --- a/src/vs/workbench/services/editor/browser/editorService.ts +++ b/src/vs/workbench/services/editor/browser/editorService.ts @@ -5,7 +5,7 @@ import * as nls from 'vs/nls'; import { IInstantiationService, ServicesAccessor } from 'vs/platform/instantiation/common/instantiation'; -import { IResourceEditorInput, ITextEditorOptions, IEditorOptions, EditorActivation } from 'vs/platform/editor/common/editor'; +import { IResourceEditorInput, ITextEditorOptions, IEditorOptions, EditorActivation, IGNORE_OVERRIDES } from 'vs/platform/editor/common/editor'; import { SideBySideEditor, IEditorInput, IEditorPane, GroupIdentifier, IFileEditorInput, IUntitledTextResourceEditorInput, IResourceDiffEditorInput, IEditorInputFactoryRegistry, Extensions as EditorExtensions, EditorInput, SideBySideEditorInput, IEditorInputWithOptions, isEditorInputWithOptions, EditorOptions, TextEditorOptions, IEditorIdentifier, IEditorCloseEvent, ITextEditorPane, ITextDiffEditorPane, IRevertOptions, SaveReason, EditorsOrder, isTextEditorPane, IWorkbenchEditorConfiguration, toResource, IVisibleEditorPane } from 'vs/workbench/common/editor'; import { ResourceEditorInput } from 'vs/workbench/common/editor/resourceEditorInput'; import { Registry } from 'vs/platform/registry/common/platform'; @@ -491,22 +491,22 @@ export class EditorService extends Disposable implements EditorServiceImpl { } getEditorOverrides(resource: URI, options: IEditorOptions | undefined, group: IEditorGroup | undefined): [IOpenEditorOverrideHandler, IOpenEditorOverrideEntry][] { - const ret = []; + const overrides = []; for (const handler of this.openEditorHandlers) { - const handlers = handler.getEditorOverrides ? handler.getEditorOverrides(resource, options, group).map(val => { return [handler, val] as [IOpenEditorOverrideHandler, IOpenEditorOverrideEntry]; }) : []; - ret.push(...handlers); + const handlers = handler.getEditorOverrides ? handler.getEditorOverrides(resource, options, group).map(val => [handler, val] as [IOpenEditorOverrideHandler, IOpenEditorOverrideEntry]) : []; + overrides.push(...handlers); } - return ret; + return overrides; } private onGroupWillOpenEditor(group: IEditorGroup, event: IEditorOpeningEvent): void { - if (event.options?.ignoreOverrides) { - return; + if (event.options?.override === IGNORE_OVERRIDES) { + return; // return early when overrides are explicitly disabled } for (const handler of this.openEditorHandlers) { - const result = handler.open(event.editor, event.options, group, event.context ?? OpenEditorContext.NEW_EDITOR, event.options?.overrideId); + const result = handler.open(event.editor, event.options, group, event.context ?? OpenEditorContext.NEW_EDITOR, event.options?.override); const override = result?.override; if (override) { event.prevent((() => override.then(editor => withNullAsUndefined(editor))));