Skip to content

Commit

Permalink
editors - merge ignoreOverrides and overrideId into one (#99123)
Browse files Browse the repository at this point in the history
  • Loading branch information
bpasero committed Jun 6, 2020
1 parent c15e3a6 commit 0350010
Show file tree
Hide file tree
Showing 8 changed files with 45 additions and 50 deletions.
14 changes: 7 additions & 7 deletions src/vs/platform/editor/common/editor.ts
Expand Up @@ -121,6 +121,8 @@ export enum EditorOpenContext {
USER
}

export const IGNORE_OVERRIDES = Object.create(null);

export interface IEditorOptions {

/**
Expand Down Expand Up @@ -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.
Expand Down
6 changes: 3 additions & 3 deletions src/vs/workbench/api/browser/mainThreadEditors.ts
Expand Up @@ -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';
Expand Down Expand Up @@ -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 = {
Expand Down Expand Up @@ -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);
Expand Down
26 changes: 10 additions & 16 deletions src/vs/workbench/common/editor.ts
Expand Up @@ -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';
Expand Down Expand Up @@ -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.
Expand Down Expand Up @@ -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') {
Expand Down Expand Up @@ -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;
Expand Down
14 changes: 7 additions & 7 deletions src/vs/workbench/contrib/customEditor/browser/customEditors.ts
Expand Up @@ -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';
Expand Down Expand Up @@ -203,7 +203,7 @@ export class CustomEditorService extends Disposable implements ICustomEditorServ
): Promise<IEditorPane | undefined> {
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)) {
Expand Down Expand Up @@ -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);
}
Expand Down Expand Up @@ -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)
};
}

Expand Down Expand Up @@ -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)
};
Expand Down Expand Up @@ -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));
Expand Down Expand Up @@ -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);
})(),
};
}
Expand Down
4 changes: 2 additions & 2 deletions src/vs/workbench/contrib/files/common/openWith.ts
Expand Up @@ -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';
Expand Down Expand Up @@ -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) };
}
},
Expand Down
Expand Up @@ -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';
Expand Down Expand Up @@ -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.
Expand All @@ -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)
};
}
}
Expand Down Expand Up @@ -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) };
}
}

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

Expand Down
Expand Up @@ -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';
Expand Down Expand Up @@ -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);
})()
};
}
Expand Down
16 changes: 8 additions & 8 deletions src/vs/workbench/services/editor/browser/editorService.ts
Expand Up @@ -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';
Expand Down Expand Up @@ -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))));
Expand Down

0 comments on commit 0350010

Please sign in to comment.