Skip to content

Commit

Permalink
Make getEditorOverrides take a uri instead of a editor input
Browse files Browse the repository at this point in the history
Better fix for #96591 to also work correctly with notebooks

In the explorer->open with command, we need to call `getEditorOverrides` but do not have a real editor input. Rather than creating a fake editor input, we should just take uri for `getEditorOverrides`
  • Loading branch information
mjbvz committed Apr 30, 2020
1 parent 3f2e83c commit 0593e5b
Show file tree
Hide file tree
Showing 9 changed files with 21 additions and 30 deletions.
2 changes: 1 addition & 1 deletion src/vs/workbench/browser/contextkeys.ts
Expand Up @@ -210,7 +210,7 @@ export class WorkbenchContextKeysHandler extends Disposable {
this.activeEditorContext.set(activeEditorPane.getId());
this.activeEditorIsReadonly.set(activeEditorPane.input.isReadonly());

const editors = this.editorService.getEditorOverrides(activeEditorPane.input, undefined, activeGroup);
const editors = activeEditorPane.input.resource ? this.editorService.getEditorOverrides(activeEditorPane.input.resource, undefined, activeGroup) : [];
this.activeEditorAvailableEditorIds.set(editors.map(([_, entry]) => entry.id).join(','));
} else {
this.activeEditorContext.reset();
Expand Down
11 changes: 3 additions & 8 deletions src/vs/workbench/contrib/customEditor/browser/customEditors.ts
Expand Up @@ -426,19 +426,14 @@ export class CustomEditorContribution extends Disposable implements IWorkbenchCo
open: (editor, options, group, id) => {
return this.onEditorOpening(editor, options, group, id);
},
getEditorOverrides: (editor: IEditorInput, _options: IEditorOptions | undefined, group: IEditorGroup | undefined): IOpenEditorOverrideEntry[] => {
const resource = editor.resource;
if (!resource) {
return [];
}

const matchedEditor = group?.editors.find(editor => isEqual(editor.resource, resource));
getEditorOverrides: (resource: URI, _options: IEditorOptions | undefined, group: IEditorGroup | undefined): IOpenEditorOverrideEntry[] => {
const currentEditor = group?.editors.find(editor => isEqual(editor.resource, resource));

const customEditors = this.customEditorService.getAllCustomEditors(resource);
return customEditors.allEditors.map(entry => {
return {
id: entry.id,
active: matchedEditor instanceof CustomEditorInput && matchedEditor.viewType === entry.id,
active: currentEditor instanceof CustomEditorInput && currentEditor.viewType === entry.id,
label: entry.displayName,
detail: entry.providerDisplayName,
};
Expand Down
6 changes: 3 additions & 3 deletions src/vs/workbench/contrib/files/browser/fileActions.ts
Expand Up @@ -16,7 +16,7 @@ import { dispose, IDisposable } from 'vs/base/common/lifecycle';
import { VIEWLET_ID, IExplorerService, IFilesConfiguration, VIEW_ID } from 'vs/workbench/contrib/files/common/files';
import { ITextFileService } from 'vs/workbench/services/textfile/common/textfiles';
import { IFileService } from 'vs/platform/files/common/files';
import { toResource, SideBySideEditor, IEditorInput } from 'vs/workbench/common/editor';
import { toResource, SideBySideEditor } from 'vs/workbench/common/editor';
import { ExplorerViewPaneContainer } from 'vs/workbench/contrib/files/browser/explorerViewlet';
import { IQuickInputService, ItemActivation } from 'vs/platform/quickinput/common/quickInput';
import { IViewletService } from 'vs/workbench/services/viewlet/browser/viewlet';
Expand Down Expand Up @@ -476,7 +476,7 @@ export class GlobalCompareResourcesAction extends Action {

// Compare with next editor that opens
const toDispose = this.editorService.overrideOpenEditor({
getEditorOverrides: (editor: IEditorInput, options: IEditorOptions | undefined, group: IEditorGroup | undefined) => {
getEditorOverrides: (resource: URI, options: IEditorOptions | undefined, group: IEditorGroup | undefined) => {
return [];
},
open: editor => {
Expand Down Expand Up @@ -570,7 +570,7 @@ export class ToggleEditorTypeCommand extends Action {
const options = activeEditorPane.options;
const group = activeEditorPane.group;

const overrides = getAllAvailableEditors(input, input.resource, options, group, this.editorService);
const overrides = getAllAvailableEditors(input.resource, options, group, this.editorService);
const firstNonActiveOverride = overrides.find(([_, entry]) => !entry.active);
if (!firstNonActiveOverride) {
return;
Expand Down
3 changes: 1 addition & 2 deletions src/vs/workbench/contrib/files/browser/views/explorerView.ts
Expand Up @@ -471,8 +471,7 @@ export class ExplorerView extends ViewPane {
this.rootContext.set(!stat || (stat && stat.isRoot));

if (resource) {
const fileEditorInput = this.editorService.createEditorInput({ resource, forceFile: true });
const overrides = this.editorService.getEditorOverrides(fileEditorInput, undefined, undefined);
const overrides = resource ? this.editorService.getEditorOverrides(resource, undefined, undefined) : [];
this.availableEditorIdsContext.set(overrides.map(([, entry]) => entry.id).join(','));
} else {
this.availableEditorIdsContext.reset();
Expand Down
5 changes: 2 additions & 3 deletions src/vs/workbench/contrib/files/common/openWith.ts
Expand Up @@ -38,7 +38,7 @@ export async function openEditorWith(
return;
}

const allEditorOverrides = getAllAvailableEditors(input, resource, options, group, editorService);
const allEditorOverrides = getAllAvailableEditors(resource, options, group, editorService);
if (!allEditorOverrides.length) {
return;
}
Expand Down Expand Up @@ -115,13 +115,12 @@ export async function openEditorWith(
* Get a list of all available editors, including the default text editor.
*/
export function getAllAvailableEditors(
input: IEditorInput,
resource: URI,
options: IEditorOptions | ITextEditorOptions | undefined,
group: IEditorGroup,
editorService: IEditorService,
): Array<[IOpenEditorOverrideHandler, IOpenEditorOverrideEntry]> {
const overrides = editorService.getEditorOverrides(input, options, group);
const overrides = editorService.getEditorOverrides(resource, options, group);
if (!overrides.some(([_, entry]) => entry.id === DEFAULT_EDITOR_ID)) {
overrides.unshift([
{
Expand Down
Expand Up @@ -107,11 +107,8 @@ export class NotebookContribution implements IWorkbenchContribution {

) {
this.editorService.overrideOpenEditor({
getEditorOverrides: (editor: IEditorInput, options: IEditorOptions | undefined, group: IEditorGroup | undefined) => {
let resource = editor.resource;
if (!resource) {
return [];
}
getEditorOverrides: (resource: URI, options: IEditorOptions | undefined, group: IEditorGroup | undefined) => {
const currentEditorForResource = group?.editors.find(editor => isEqual(editor.resource, resource));

const associatedEditors = distinct([
...this.getUserAssociatedNotebookEditors(resource),
Expand All @@ -122,7 +119,7 @@ export class NotebookContribution implements IWorkbenchContribution {
return {
label: info.displayName,
id: info.id,
active: editor instanceof NotebookEditorInput && editor.viewType === info.id,
active: currentEditorForResource instanceof NotebookEditorInput && currentEditorForResource.viewType === info.id,
detail: info.providerDisplayName
};
});
Expand Down
8 changes: 4 additions & 4 deletions src/vs/workbench/services/editor/browser/editorService.ts
Expand Up @@ -480,10 +480,10 @@ export class EditorService extends Disposable implements EditorServiceImpl {
return toDisposable(() => remove());
}

getEditorOverrides(editorInput: IEditorInput, options: IEditorOptions | undefined, group: IEditorGroup | undefined): [IOpenEditorOverrideHandler, IOpenEditorOverrideEntry][] {
getEditorOverrides(resource: URI, options: IEditorOptions | undefined, group: IEditorGroup | undefined): [IOpenEditorOverrideHandler, IOpenEditorOverrideEntry][] {
const ret = [];
for (const handler of this.openEditorHandlers) {
const handlers = handler.getEditorOverrides ? handler.getEditorOverrides(editorInput, options, group).map(val => { return [handler, val] as [IOpenEditorOverrideHandler, IOpenEditorOverrideEntry]; }) : [];
const handlers = handler.getEditorOverrides ? handler.getEditorOverrides(resource, options, group).map(val => { return [handler, val] as [IOpenEditorOverrideHandler, IOpenEditorOverrideEntry]; }) : [];
ret.push(...handlers);
}

Expand Down Expand Up @@ -1165,8 +1165,8 @@ export class DelegatingEditorService implements IEditorService {
@IEditorService private editorService: EditorService
) { }

getEditorOverrides(editorInput: IEditorInput, options: IEditorOptions | undefined, group: IEditorGroup | undefined) {
return this.editorService.getEditorOverrides(editorInput, options, group);
getEditorOverrides(resource: URI, options: IEditorOptions | undefined, group: IEditorGroup | undefined) {
return this.editorService.getEditorOverrides(resource, options, group);
}

openEditor(editor: IEditorInput, options?: IEditorOptions | ITextEditorOptions, group?: OpenInEditorGroup): Promise<IEditorPane | undefined>;
Expand Down
5 changes: 3 additions & 2 deletions src/vs/workbench/services/editor/common/editorService.ts
Expand Up @@ -10,6 +10,7 @@ import { Event } from 'vs/base/common/event';
import { IEditor, IDiffEditor } from 'vs/editor/common/editorCommon';
import { IEditorGroup, IEditorReplacement } from 'vs/workbench/services/editor/common/editorGroupsService';
import { IDisposable } from 'vs/base/common/lifecycle';
import { URI } from 'vs/base/common/uri';

export const IEditorService = createDecorator<IEditorService>('editorService');

Expand All @@ -35,7 +36,7 @@ export interface IOpenEditorOverrideEntry {

export interface IOpenEditorOverrideHandler {
open(editor: IEditorInput, options: IEditorOptions | ITextEditorOptions | undefined, group: IEditorGroup, id?: string): IOpenEditorOverride | undefined;
getEditorOverrides?(editor: IEditorInput, options: IEditorOptions | undefined, group: IEditorGroup | undefined): IOpenEditorOverrideEntry[];
getEditorOverrides?(resource: URI, options: IEditorOptions | undefined, group: IEditorGroup | undefined): IOpenEditorOverrideEntry[];
}

export interface IOpenEditorOverride {
Expand Down Expand Up @@ -224,7 +225,7 @@ export interface IEditorService {
/**
* Get all available editor overrides for the editor input.
*/
getEditorOverrides(editorInput: IEditorInput, options: IEditorOptions | undefined, group: IEditorGroup | undefined): [IOpenEditorOverrideHandler, IOpenEditorOverrideEntry][];
getEditorOverrides(resource: URI, options: IEditorOptions | undefined, group: IEditorGroup | undefined): [IOpenEditorOverrideHandler, IOpenEditorOverrideEntry][];

/**
* Allows to override the opening of editors by installing a handler that will
Expand Down
2 changes: 1 addition & 1 deletion src/vs/workbench/test/browser/workbenchTestServices.ts
Expand Up @@ -632,7 +632,7 @@ export class TestEditorService implements EditorServiceImpl {

constructor(private editorGroupService?: IEditorGroupsService) { }
getEditors() { return []; }
getEditorOverrides(editorInput: IEditorInput, options: IEditorOptions | undefined, group: IEditorGroup | undefined): [IOpenEditorOverrideHandler, IOpenEditorOverrideEntry][] { return []; }
getEditorOverrides(resource: URI, options: IEditorOptions | undefined, group: IEditorGroup | undefined): [IOpenEditorOverrideHandler, IOpenEditorOverrideEntry][] { return []; }
overrideOpenEditor(_handler: IOpenEditorOverrideHandler): IDisposable { return toDisposable(() => undefined); }
registerCustomEditorViewTypesHandler(source: string, handler: ICustomEditorViewTypesHandler): IDisposable {
throw new Error('Method not implemented.');
Expand Down

0 comments on commit 0593e5b

Please sign in to comment.