From ff5e7462b26536463c90673704de029801cad57a Mon Sep 17 00:00:00 2001 From: rebornix Date: Thu, 20 Feb 2020 15:34:40 -0800 Subject: [PATCH] activate extension by notebook view type --- extensions/notebook-test/package.json | 3 +- .../notebook/browser/notebookEditor.ts | 2 +- .../notebook/browser/notebookEditorInput.ts | 39 ++++++------------- .../notebook/browser/notebookService.ts | 26 ++++++++++++- .../services/editor/browser/editorService.ts | 2 +- 5 files changed, 40 insertions(+), 32 deletions(-) diff --git a/extensions/notebook-test/package.json b/extensions/notebook-test/package.json index cc9e01b0e3e..d2756f899d4 100644 --- a/extensions/notebook-test/package.json +++ b/extensions/notebook-test/package.json @@ -19,7 +19,8 @@ "Other" ], "activationEvents": [ - "*" + "onNotebookEditor:jupyter", + "onNotebookEditor:jupytertest" ], "contributes": { "commands": [ diff --git a/src/vs/workbench/contrib/notebook/browser/notebookEditor.ts b/src/vs/workbench/contrib/notebook/browser/notebookEditor.ts index 54d1f0494b6..5290d41ef6e 100644 --- a/src/vs/workbench/contrib/notebook/browser/notebookEditor.ts +++ b/src/vs/workbench/contrib/notebook/browser/notebookEditor.ts @@ -288,7 +288,7 @@ export class NotebookEditor extends BaseEditor implements INotebookEditor { return input.resolve(); }) .then(async model => { - if (this.model !== undefined && this.model.textModel === model.textModel && this.webview !== null) { + if (this.model !== undefined && this.model === model && this.webview !== null) { return; } diff --git a/src/vs/workbench/contrib/notebook/browser/notebookEditorInput.ts b/src/vs/workbench/contrib/notebook/browser/notebookEditorInput.ts index f19af4e6304..d0ce1ae8b12 100644 --- a/src/vs/workbench/contrib/notebook/browser/notebookEditorInput.ts +++ b/src/vs/workbench/contrib/notebook/browser/notebookEditorInput.ts @@ -4,8 +4,6 @@ *--------------------------------------------------------------------------------------------*/ import { EditorInput, EditorModel, IEditorInput, GroupIdentifier, ISaveOptions } from 'vs/workbench/common/editor'; -import { ITextModelService } from 'vs/editor/common/services/resolverService'; -import { ITextModel } from 'vs/editor/common/model'; import { Emitter, Event } from 'vs/base/common/event'; import { INotebookService } from 'vs/workbench/contrib/notebook/browser/notebookService'; import { INotebook, ICell, NotebookCellsSplice } from 'vs/workbench/contrib/notebook/common/notebookCommon'; @@ -21,8 +19,7 @@ export class NotebookEditorModel extends EditorModel { get onDidChangeCells(): Event { return this._onDidChangeCells.event; } constructor( - public readonly textModel: ITextModel, - private _notebook: INotebook | undefined + private _notebook: INotebook ) { super(); @@ -44,14 +41,7 @@ export class NotebookEditorModel extends EditorModel { } getNotebook(): INotebook { - if (this._notebook) { - return this._notebook; - } - - // TODO, remove file based notebook from core - let content = this.textModel.getValue(); - this._notebook = JSON.parse(content); - return this._notebook!; + return this._notebook; } insertCell(cell: ICell, index: number) { @@ -100,8 +90,7 @@ export class NotebookEditorInput extends EditorInput { public resource: URI, public name: string, public readonly viewType: string | undefined, - @INotebookService private readonly notebookService: INotebookService, - @ITextModelService private readonly textModelResolverService: ITextModelService + @INotebookService private readonly notebookService: INotebookService ) { super(); } @@ -128,21 +117,15 @@ export class NotebookEditorInput extends EditorInput { return undefined; } - resolve(): Promise { + async resolve(): Promise { if (!this.promise) { - this.promise = this.textModelResolverService.createModelReference(this.resource) - .then(async ref => { - const textModel = ref.object.textEditorModel; - - let notebook: INotebook | undefined = undefined; - if (this.viewType !== undefined) { - notebook = await this.notebookService.resolveNotebook(this.viewType, this.resource); - } - - this.textModel = new NotebookEditorModel(textModel, notebook); - this.textModel.onDidChangeDirty(() => this._onDidChangeDirty.fire()); - return this.textModel; - }); + await this.notebookService.canResolve(this.viewType!); + + this.promise = this.notebookService.resolveNotebook(this.viewType!, this.resource).then(notebook => { + this.textModel = new NotebookEditorModel(notebook!); + this.textModel.onDidChangeDirty(() => this._onDidChangeDirty.fire()); + return this.textModel; + }); } return this.promise; diff --git a/src/vs/workbench/contrib/notebook/browser/notebookService.ts b/src/vs/workbench/contrib/notebook/browser/notebookService.ts index c6516ab056d..1c6330d71b6 100644 --- a/src/vs/workbench/contrib/notebook/browser/notebookService.ts +++ b/src/vs/workbench/contrib/notebook/browser/notebookService.ts @@ -11,6 +11,7 @@ import { NotebookProviderInfo } from 'vs/workbench/contrib/notebook/common/noteb import { NotebookExtensionDescription } from 'vs/workbench/api/common/extHost.protocol'; import { Emitter, Event } from 'vs/base/common/event'; import { INotebook, ICell, INotebookMimeTypeSelector } from 'vs/workbench/contrib/notebook/common/notebookCommon'; +import { IExtensionService } from 'vs/workbench/services/extensions/common/extensions'; function MODEL_ID(resource: URI): string { return resource.toString(); @@ -30,6 +31,7 @@ export interface IMainNotebookController { export interface INotebookService { _serviceBrand: undefined; + canResolve(viewType: string): Promise; onDidChangeActiveEditor: Event<{ viewType: string, uri: URI }>; registerNotebookController(viewType: string, extensionData: NotebookExtensionDescription, controller: IMainNotebookController): void; unregisterNotebookProvider(viewType: string): void; @@ -97,8 +99,11 @@ export class NotebookService extends Disposable implements INotebookService { private readonly _models: { [modelId: string]: ModelData; }; private _onDidChangeActiveEditor = new Emitter<{ viewType: string, uri: URI }>(); onDidChangeActiveEditor: Event<{ viewType: string, uri: URI }> = this._onDidChangeActiveEditor.event; + private _resolvePool = new Map void>(); - constructor() { + constructor( + @IExtensionService private readonly extensionService: IExtensionService + ) { super(); this._models = {}; @@ -119,8 +124,27 @@ export class NotebookService extends Disposable implements INotebookService { } + async canResolve(viewType: string): Promise { + if (this._notebookProviders.has(viewType)) { + return; + } + + this.extensionService.activateByEvent(`onNotebookEditor:${viewType}`); + + let resolve: () => void; + const promise = new Promise(r => { resolve = r; }); + this._resolvePool.set(viewType, resolve!); + return promise; + } + registerNotebookController(viewType: string, extensionData: NotebookExtensionDescription, controller: IMainNotebookController) { this._notebookProviders.set(viewType, { extensionData, controller }); + + let resolve = this._resolvePool.get(viewType); + if (resolve) { + resolve(); + this._resolvePool.delete(viewType); + } } unregisterNotebookProvider(viewType: string): void { diff --git a/src/vs/workbench/services/editor/browser/editorService.ts b/src/vs/workbench/services/editor/browser/editorService.ts index ee3a3a9cef8..f3c798f3b2d 100644 --- a/src/vs/workbench/services/editor/browser/editorService.ts +++ b/src/vs/workbench/services/editor/browser/editorService.ts @@ -5,7 +5,7 @@ import { IInstantiationService, ServicesAccessor } from 'vs/platform/instantiation/common/instantiation'; import { IResourceInput, ITextEditorOptions, IEditorOptions, EditorActivation } from 'vs/platform/editor/common/editor'; -import { SideBySideEditor as SideBySideEditorChoice, IEditorInput, IEditor, GroupIdentifier, IFileEditorInput, IUntitledTextResourceInput, IResourceDiffInput, IResourceSideBySideInput, IEditorInputFactoryRegistry, Extensions as EditorExtensions, IFileInputFactory, EditorInput, SideBySideEditorInput, IEditorInputWithOptions, isEditorInputWithOptions, EditorOptions, TextEditorOptions, IEditorIdentifier, IEditorCloseEvent, ITextEditor, ITextDiffEditor, ITextSideBySideEditor, IRevertOptions, SaveReason, EditorsOrder, isTextEditor, ICompositeCodeEditor, IWorkbenchEditorConfiguration, toResource } from 'vs/workbench/common/editor'; +import { SideBySideEditor as SideBySideEditorChoice, IEditorInput, IEditor, GroupIdentifier, IFileEditorInput, IUntitledTextResourceInput, IResourceDiffInput, IResourceSideBySideInput, IEditorInputFactoryRegistry, Extensions as EditorExtensions, EditorInput, SideBySideEditorInput, IEditorInputWithOptions, isEditorInputWithOptions, EditorOptions, TextEditorOptions, IEditorIdentifier, IEditorCloseEvent, ITextEditor, ITextDiffEditor, ITextSideBySideEditor, IRevertOptions, SaveReason, EditorsOrder, isTextEditor, ICompositeCodeEditor, IWorkbenchEditorConfiguration, toResource } from 'vs/workbench/common/editor'; import { ResourceEditorInput } from 'vs/workbench/common/editor/resourceEditorInput'; import { Registry } from 'vs/platform/registry/common/platform'; import { ResourceMap } from 'vs/base/common/map'; -- GitLab