Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

IOpenEditorOverrideHandler is very weird #117210

Closed
bpasero opened this issue Feb 22, 2021 · 6 comments
Closed

IOpenEditorOverrideHandler is very weird #117210

bpasero opened this issue Feb 22, 2021 · 6 comments
Assignees
Labels
custom-editors Custom editor API (webview based editors) debt Code quality issues workbench-editor-resolver Issues resolving the editor inputs
Milestone

Comments

@bpasero
Copy link
Member

bpasero commented Feb 22, 2021

I feel IOpenEditorOverrideHandler is weird.

Why does open return a IOpenEditorOverride | undefined where IOpenEditorOverride#override is optional? Why can we not directly return Promise<IEditorPane | undefined> from open.

And what is the purpose of getEditorOverrides. It seems weird to me that a contributor can return IOpenEditorOverrideEntry[] but in the open call there are no traces anymore of what override entry was used.

@bpasero bpasero added the custom-editors Custom editor API (webview based editors) label Feb 22, 2021
@bpasero bpasero added the debt Code quality issues label Feb 22, 2021
@bpasero
Copy link
Member Author

bpasero commented Feb 22, 2021

My hope is that maybe with #100281 all of this weird indirections could go away.

@jrieken
Copy link
Member

jrieken commented Mar 3, 2021

Just for reference, the implementation of notebook's open. Maybe we aren't doing this right but I have the feeling this shouldn't be so much code

private onEditorOpening(originalInput: IEditorInput, options: IEditorOptions | ITextEditorOptions | undefined, group: IEditorGroup): IOpenEditorOverride | undefined {
let id = typeof options?.override === 'string' ? options.override : undefined;
if (id === undefined && originalInput.resource?.scheme === Schemas.untitled) {
return undefined;
}
if (originalInput instanceof DiffEditorInput && this.configurationService.getValue(NotebookTextDiffEditorPreview) && !this._accessibilityService.isScreenReaderOptimized()) {
return this._handleDiffEditorInput(originalInput, options, group);
}
if (!originalInput.resource) {
return undefined;
}
// Run reopen with ...
if (id) {
// from the editor tab context menu
if (originalInput instanceof NotebookEditorInput) {
if (originalInput.viewType === id) {
// reopen with the same type
return undefined;
} else {
return {
override: (async () => {
const notebookInput = NotebookEditorInput.create(this.instantiationService, originalInput.resource, id);
const originalEditorIndex = group.getIndexOfEditor(originalInput);
await group.closeEditor(originalInput);
originalInput.dispose();
const newEditor = await group.openEditor(notebookInput, { ...options, index: originalEditorIndex, override: EditorOverride.DISABLED });
if (newEditor) {
return newEditor;
} else {
return undefined;
}
})()
};
}
} else {
// from the file explorer
const existingEditors = this.editorService.findEditors(originalInput.resource, group).filter(editor => editor instanceof NotebookEditorInput) as NotebookEditorInput[];
if (existingEditors.length) {
// there are notebook editors with the same resource
if (existingEditors.find(editor => editor.viewType === id)) {
return { override: this.editorService.openEditor(existingEditors.find(editor => editor.viewType === id)!, { ...options, override: EditorOverride.DISABLED }, group) };
} else {
return {
override: (async () => {
const firstEditor = existingEditors[0]!;
const originalEditorIndex = group.getIndexOfEditor(firstEditor);
await group.closeEditor(firstEditor);
firstEditor.dispose();
const notebookInput = NotebookEditorInput.create(this.instantiationService, originalInput.resource!, id);
const newEditor = await group.openEditor(notebookInput, { ...options, index: originalEditorIndex, override: EditorOverride.DISABLED });
if (newEditor) {
return newEditor;
} else {
return undefined;
}
})()
};
}
}
}
}
// Click on the editor tab
if (id === undefined && originalInput instanceof NotebookEditorInput) {
const existingEditors = this.editorService.findEditors(originalInput.resource, group).filter(editor => editor instanceof NotebookEditorInput && editor === originalInput);
if (existingEditors.length) {
// same
return undefined;
}
}
if (originalInput instanceof NotebookDiffEditorInput) {
return undefined;
}
let notebookUri: URI = originalInput.resource;
let cellOptions: IResourceEditorInput | undefined;
const data = CellUri.parse(originalInput.resource);
if (data) {
notebookUri = data.notebook;
cellOptions = { resource: originalInput.resource, options };
}
if (id === undefined && originalInput instanceof ResourceEditorInput) {
const exitingNotebookEditor = <NotebookEditorInput | undefined>group.editors.find(editor => editor instanceof NotebookEditorInput && isEqual(editor.resource, notebookUri));
id = exitingNotebookEditor?.viewType;
}
if (id === undefined) {
const existingEditors = this.editorService.findEditors(notebookUri, group).filter(editor =>
!(editor instanceof NotebookEditorInput)
&& !(editor instanceof NotebookDiffEditorInput)
);
if (existingEditors.length) {
return undefined;
}
const userAssociatedEditors = this.getUserAssociatedEditors(notebookUri);
const notebookEditor = userAssociatedEditors.filter(association => this.notebookService.getContributedNotebookProvider(association.viewType));
if (userAssociatedEditors.length && !notebookEditor.length) {
// user pick a non-notebook editor for this resource
return undefined;
}
// user might pick a notebook editor
const associatedEditors = distinct([
...this.getUserAssociatedNotebookEditors(notebookUri),
...(this.getContributedEditors(notebookUri).filter(editor => editor.priority === NotebookEditorPriority.default))
], editor => editor.id);
if (!associatedEditors.length) {
// there is no notebook editor contribution which is enabled by default
return undefined;
}
}
const infos = this.notebookService.getContributedNotebookProviders(notebookUri);
let info = infos.find(info => (!id || info.id === id) && info.exclusive) || infos.find(info => !id || info.id === id);
if (!info && id !== undefined) {
info = this.notebookService.getContributedNotebookProvider(id);
}
if (!info) {
return undefined;
}
/**
* Scenario: we are reopening a file editor input which is pinned, we should open in a new editor tab.
*/
let index = undefined;
if (group.activeEditor === originalInput && isEqual(originalInput.resource, notebookUri)) {
const originalEditorIndex = group.getIndexOfEditor(originalInput);
index = group.isPinned(originalInput) ? originalEditorIndex + 1 : originalEditorIndex;
}
const notebookInput = NotebookEditorInput.create(this.instantiationService, notebookUri, info.id);
const notebookOptions = new NotebookEditorOptions({ ...options, cellOptions, override: EditorOverride.DISABLED, index });
return { override: this.editorService.openEditor(notebookInput, notebookOptions, group) };
}

@jrieken jrieken added this to the March 2021 milestone Mar 3, 2021
@jrieken jrieken added the workbench-editors Managing of editor widgets in workbench window label Mar 3, 2021
@jrieken
Copy link
Member

jrieken commented Mar 3, 2021

tab spying

this._disposables.add(editorService.overrideOpenEditor({

@lramos15 lramos15 modified the milestones: March 2021, Backlog Mar 22, 2021
@lramos15
Copy link
Member

@jrieken I have fixed the tab spying with an OnWillMoveEditor event. However, the actual opening logic cannot be reduced without adding an intermediary opener that services just custom editors and notebooks as what they do is too specialized to enter into the EditorService.

@lramos15
Copy link
Member

This "intermediary" service has been added and upon completion of #121668 this handler will go away all together.

@bpasero bpasero added workbench-editor-resolver Issues resolving the editor inputs and removed workbench-editors Managing of editor widgets in workbench window labels May 21, 2021
@bpasero bpasero modified the milestones: Backlog, May 2021 May 27, 2021
@bpasero
Copy link
Member Author

bpasero commented May 27, 2021

The handler is gone 👏

@bpasero bpasero closed this as completed May 27, 2021
@github-actions github-actions bot locked and limited conversation to collaborators Jul 11, 2021
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
custom-editors Custom editor API (webview based editors) debt Code quality issues workbench-editor-resolver Issues resolving the editor inputs
Projects
None yet
Development

No branches or pull requests

4 participants