editorReplacer -> editorOverride (#16041)

* editorReplacer -> editorOverride

* Update lifecycle phsae

* add back input factories

* Add comment

* add back tests

* comments

* fix log

* comments
This commit is contained in:
Charles Gagnon
2021-07-09 08:46:50 -07:00
committed by GitHub
parent 7ba0e49673
commit 8f202d91b6
19 changed files with 253 additions and 509 deletions

View File

@@ -21,6 +21,10 @@ import { IConfigurationService } from 'vs/platform/configuration/common/configur
const editorInputFactoryRegistry = Registry.as<IEditorInputFactoryRegistry>(EditorExtensions.EditorInputFactories);
export class NotebookEditorInputAssociation implements ILanguageAssociation {
/**
* The language IDs that are associated with Notebooks. These are case sensitive for comparing with what's
* registered in the ModeService registry.
*/
static readonly languages = [NotebookLanguage.Notebook, NotebookLanguage.Ipynb];
constructor(@IInstantiationService private readonly instantiationService: IInstantiationService, @IConfigurationService private readonly configurationService: IConfigurationService) { }
@@ -40,6 +44,10 @@ export class NotebookEditorInputAssociation implements ILanguageAssociation {
return undefined;
}
syncConvertinput(activeEditor: IEditorInput): NotebookInput | DiffNotebookInput | undefined {
return this.convertInput(activeEditor);
}
createBase(activeEditor: NotebookInput): IEditorInput {
return activeEditor.textInput;
}
@@ -65,7 +73,7 @@ export class FileNoteBookEditorInputSerializer implements IEditorInputSerializer
}
}
export class UntitledNoteBookEditorInputFactory implements IEditorInputSerializer {
export class UntitledNotebookEditorInputSerializer implements IEditorInputSerializer {
serialize(editorInput: UntitledNotebookInput): string {
const factory = editorInputFactoryRegistry.getEditorInputSerializer(UntitledTextEditorInput.ID);
if (factory) {

View File

@@ -9,11 +9,10 @@ import { SyncDescriptor } from 'vs/platform/instantiation/common/descriptors';
import { IInstantiationService, ServicesAccessor } from 'vs/platform/instantiation/common/instantiation';
import { localize } from 'vs/nls';
import { IEditorInputFactoryRegistry, ActiveEditorContext, IEditorInput, EditorExtensions } from 'vs/workbench/common/editor';
import { ILanguageAssociationRegistry, Extensions as LanguageAssociationExtensions } from 'sql/workbench/services/languageAssociation/common/languageAssociation';
import { UntitledNotebookInput } from 'sql/workbench/contrib/notebook/browser/models/untitledNotebookInput';
import { FileNotebookInput } from 'sql/workbench/contrib/notebook/browser/models/fileNotebookInput';
import { FileNoteBookEditorInputSerializer, UntitledNoteBookEditorInputFactory, NotebookEditorInputAssociation } from 'sql/workbench/contrib/notebook/browser/models/notebookInputFactory';
import { FileNoteBookEditorInputSerializer, NotebookEditorInputAssociation, UntitledNotebookEditorInputSerializer } from 'sql/workbench/contrib/notebook/browser/models/notebookInputFactory';
import { IWorkbenchActionRegistry, Extensions as WorkbenchActionsExtensions } from 'vs/workbench/common/actions';
import { SyncActionDescriptor, registerAction2, MenuRegistry, MenuId, Action2 } from 'vs/platform/actions/common/actions';
@@ -43,7 +42,7 @@ import { IHostService } from 'vs/workbench/services/host/browser/host';
import { MarkdownOutputComponent } from 'sql/workbench/contrib/notebook/browser/outputs/markdownOutput.component';
import { registerCellComponent } from 'sql/platform/notebooks/common/outputRegistry';
import { TextCellComponent } from 'sql/workbench/contrib/notebook/browser/cellViews/textCell.component';
import { IWorkbenchContributionsRegistry, Extensions as WorkbenchExtensions } from 'vs/workbench/common/contributions';
import { IWorkbenchContributionsRegistry, Extensions as WorkbenchExtensions, IWorkbenchContribution } from 'vs/workbench/common/contributions';
import { NotebookThemingContribution } from 'sql/workbench/contrib/notebook/browser/notebookThemingContribution';
import { LifecyclePhase } from 'vs/workbench/services/lifecycle/common/lifecycle';
import { ToggleTabFocusModeAction } from 'vs/editor/contrib/toggleTabFocusMode/toggleTabFocusMode';
@@ -56,18 +55,24 @@ import { NotebookInput } from 'sql/workbench/contrib/notebook/browser/models/not
import { INotebookModel } from 'sql/workbench/services/notebook/browser/models/modelInterfaces';
import { INotebookManager } from 'sql/workbench/services/notebook/browser/notebookService';
import { NotebookExplorerViewletViewsContribution } from 'sql/workbench/contrib/notebook/browser/notebookExplorer/notebookExplorerViewlet';
import { Disposable, DisposableStore } from 'vs/base/common/lifecycle';
import { ContributedEditorPriority, IEditorOverrideService } from 'vs/workbench/services/editor/common/editorOverrideService';
import { FileEditorInput } from 'vs/workbench/contrib/files/common/editors/fileEditorInput';
import { IModeService } from 'vs/editor/common/services/modeService';
import { ILogService } from 'vs/platform/log/common/log';
import { DiffEditorInput } from 'vs/workbench/common/editor/diffEditorInput';
Registry.as<IEditorInputFactoryRegistry>(EditorExtensions.EditorInputFactories)
.registerEditorInputSerializer(FileNotebookInput.ID, FileNoteBookEditorInputSerializer);
Registry.as<IEditorInputFactoryRegistry>(EditorExtensions.EditorInputFactories)
.registerEditorInputSerializer(UntitledNotebookInput.ID, UntitledNoteBookEditorInputFactory);
.registerEditorInputSerializer(UntitledNotebookInput.ID, UntitledNotebookEditorInputSerializer);
Registry.as<ILanguageAssociationRegistry>(LanguageAssociationExtensions.LanguageAssociations)
.registerLanguageAssociation(NotebookEditorInputAssociation.languages, NotebookEditorInputAssociation);
Registry.as<IEditorRegistry>(EditorExtensions.Editors)
.registerEditor(EditorDescriptor.create(NotebookEditor, NotebookEditor.ID, localize('notebookEditor.name', "Notebook Editor")), [new SyncDescriptor(UntitledNotebookInput), new SyncDescriptor(FileNotebookInput)]);
.registerEditor(EditorDescriptor.create(NotebookEditor, NotebookEditor.ID, NotebookEditor.LABEL), [new SyncDescriptor(UntitledNotebookInput), new SyncDescriptor(FileNotebookInput)]);
Registry.as<IWorkbenchContributionsRegistry>(WorkbenchExtensions.Workbench)
.registerWorkbenchContribution(NotebookThemingContribution, LifecyclePhase.Restored);
@@ -658,3 +663,74 @@ configurationRegistry.registerConfiguration({
},
}
});
const languageAssociationRegistry = Registry.as<ILanguageAssociationRegistry>(LanguageAssociationExtensions.LanguageAssociations);
export class NotebookEditorOverrideContribution extends Disposable implements IWorkbenchContribution {
private _registeredOverrides = new DisposableStore();
constructor(
@ILogService private _logService: ILogService,
@IEditorService private _editorService: IEditorService,
@IEditorOverrideService private _editorOverrideService: IEditorOverrideService,
@IModeService private _modeService: IModeService
) {
super();
this.registerEditorOverride();
}
private registerEditorOverride(): void {
// Refresh the editor overrides whenever the languages change so we ensure we always have
// the latest up to date list of extensions for each language
this._modeService.onLanguagesMaybeChanged(() => {
this._registeredOverrides.clear();
// List of language IDs to associate the query editor for. These are case sensitive.
NotebookEditorInputAssociation.languages.map(lang => {
const langExtensions = this._modeService.getExtensions(lang);
if (langExtensions.length === 0) {
return;
}
// Create the selector from the list of all the language extensions we want to associate with the
// notebook editor (filtering out any languages which didn't have any extensions registered yet)
const selector = `*{${langExtensions.join(',')}}`;
this._registeredOverrides.add(this._editorOverrideService.registerContributionPoint(
selector,
{
id: NotebookEditor.ID,
label: NotebookEditor.LABEL,
describes: (currentEditor) => currentEditor instanceof FileNotebookInput,
priority: ContributedEditorPriority.builtin
},
{},
(resource, options, group) => {
const fileInput = this._editorService.createEditorInput({
resource: resource
}) as FileEditorInput;
// Try to convert the input, falling back to just a plain file input if we're unable to
const newInput = this.tryConvertInput(fileInput, lang) ?? fileInput;
return { editor: newInput, options: options, group: group };
},
(diffEditorInput, options, group) => {
// Try to convert the input, falling back to the original input if we're unable to
const newInput = this.tryConvertInput(diffEditorInput, lang) ?? diffEditorInput;
return { editor: newInput, options: options, group: group };
}
));
});
});
}
private tryConvertInput(input: IEditorInput, lang: string): IEditorInput | undefined {
const langAssociation = languageAssociationRegistry.getAssociationForLanguage(lang);
const notebookEditorInput = langAssociation?.syncConvertinput?.(input);
if (!notebookEditorInput) {
this._logService.warn('Unable to create input for overriding editor ', input instanceof DiffEditorInput ? `${input.primary.resource.toString()} <-> ${input.secondary.resource.toString()}` : input.resource.toString());
return undefined;
}
return notebookEditorInput;
}
}
Registry.as<IWorkbenchContributionsRegistry>(WorkbenchExtensions.Workbench)
.registerWorkbenchContribution(NotebookEditorOverrideContribution, LifecyclePhase.Restored);

View File

@@ -2,6 +2,8 @@
* Copyright (c) Microsoft Corporation. All rights reserved.
* Licensed under the Source EULA. See License.txt in the project root for license information.
*--------------------------------------------------------------------------------------------*/
import { localize } from 'vs/nls';
import { ITelemetryService } from 'vs/platform/telemetry/common/telemetry';
import { IThemeService } from 'vs/platform/theme/common/themeService';
import { EditorPane } from 'vs/workbench/browser/parts/editor/editorPane';
@@ -9,7 +11,6 @@ import { EditorOptions, IEditorOpenContext } from 'vs/workbench/common/editor';
import * as DOM from 'vs/base/browser/dom';
import { bootstrapAngular } from 'sql/workbench/services/bootstrap/browser/bootstrapService';
import { IInstantiationService } from 'vs/platform/instantiation/common/instantiation';
import { CancellationToken } from 'vs/base/common/cancellation';
import { NotebookInput } from 'sql/workbench/contrib/notebook/browser/models/notebookInput';
import { NotebookModule } from 'sql/workbench/contrib/notebook/browser/notebook.module';
@@ -38,6 +39,8 @@ import { onUnexpectedError } from 'vs/base/common/errors';
export class NotebookEditor extends EditorPane implements IFindNotebookController {
public static ID: string = 'workbench.editor.notebookEditor';
public static LABEL: string = localize('notebookEditor.name', "Notebook Editor");
private _notebookContainer: HTMLElement;
private _currentDimensions: DOM.Dimension;
private _overlay: HTMLElement;