提交 ff5e7462 编写于 作者: R rebornix

activate extension by notebook view type

上级 62f68fec
...@@ -19,7 +19,8 @@ ...@@ -19,7 +19,8 @@
"Other" "Other"
], ],
"activationEvents": [ "activationEvents": [
"*" "onNotebookEditor:jupyter",
"onNotebookEditor:jupytertest"
], ],
"contributes": { "contributes": {
"commands": [ "commands": [
......
...@@ -288,7 +288,7 @@ export class NotebookEditor extends BaseEditor implements INotebookEditor { ...@@ -288,7 +288,7 @@ export class NotebookEditor extends BaseEditor implements INotebookEditor {
return input.resolve(); return input.resolve();
}) })
.then(async model => { .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; return;
} }
......
...@@ -4,8 +4,6 @@ ...@@ -4,8 +4,6 @@
*--------------------------------------------------------------------------------------------*/ *--------------------------------------------------------------------------------------------*/
import { EditorInput, EditorModel, IEditorInput, GroupIdentifier, ISaveOptions } from 'vs/workbench/common/editor'; 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 { Emitter, Event } from 'vs/base/common/event';
import { INotebookService } from 'vs/workbench/contrib/notebook/browser/notebookService'; import { INotebookService } from 'vs/workbench/contrib/notebook/browser/notebookService';
import { INotebook, ICell, NotebookCellsSplice } from 'vs/workbench/contrib/notebook/common/notebookCommon'; import { INotebook, ICell, NotebookCellsSplice } from 'vs/workbench/contrib/notebook/common/notebookCommon';
...@@ -21,8 +19,7 @@ export class NotebookEditorModel extends EditorModel { ...@@ -21,8 +19,7 @@ export class NotebookEditorModel extends EditorModel {
get onDidChangeCells(): Event<NotebookCellsSplice[]> { return this._onDidChangeCells.event; } get onDidChangeCells(): Event<NotebookCellsSplice[]> { return this._onDidChangeCells.event; }
constructor( constructor(
public readonly textModel: ITextModel, private _notebook: INotebook
private _notebook: INotebook | undefined
) { ) {
super(); super();
...@@ -44,14 +41,7 @@ export class NotebookEditorModel extends EditorModel { ...@@ -44,14 +41,7 @@ export class NotebookEditorModel extends EditorModel {
} }
getNotebook(): INotebook { getNotebook(): INotebook {
if (this._notebook) { return 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!;
} }
insertCell(cell: ICell, index: number) { insertCell(cell: ICell, index: number) {
...@@ -100,8 +90,7 @@ export class NotebookEditorInput extends EditorInput { ...@@ -100,8 +90,7 @@ export class NotebookEditorInput extends EditorInput {
public resource: URI, public resource: URI,
public name: string, public name: string,
public readonly viewType: string | undefined, public readonly viewType: string | undefined,
@INotebookService private readonly notebookService: INotebookService, @INotebookService private readonly notebookService: INotebookService
@ITextModelService private readonly textModelResolverService: ITextModelService
) { ) {
super(); super();
} }
...@@ -128,21 +117,15 @@ export class NotebookEditorInput extends EditorInput { ...@@ -128,21 +117,15 @@ export class NotebookEditorInput extends EditorInput {
return undefined; return undefined;
} }
resolve(): Promise<NotebookEditorModel> { async resolve(): Promise<NotebookEditorModel> {
if (!this.promise) { if (!this.promise) {
this.promise = this.textModelResolverService.createModelReference(this.resource) await this.notebookService.canResolve(this.viewType!);
.then(async ref => {
const textModel = ref.object.textEditorModel; this.promise = this.notebookService.resolveNotebook(this.viewType!, this.resource).then(notebook => {
this.textModel = new NotebookEditorModel(notebook!);
let notebook: INotebook | undefined = undefined; this.textModel.onDidChangeDirty(() => this._onDidChangeDirty.fire());
if (this.viewType !== undefined) { return this.textModel;
notebook = await this.notebookService.resolveNotebook(this.viewType, this.resource); });
}
this.textModel = new NotebookEditorModel(textModel, notebook);
this.textModel.onDidChangeDirty(() => this._onDidChangeDirty.fire());
return this.textModel;
});
} }
return this.promise; return this.promise;
......
...@@ -11,6 +11,7 @@ import { NotebookProviderInfo } from 'vs/workbench/contrib/notebook/common/noteb ...@@ -11,6 +11,7 @@ import { NotebookProviderInfo } from 'vs/workbench/contrib/notebook/common/noteb
import { NotebookExtensionDescription } from 'vs/workbench/api/common/extHost.protocol'; import { NotebookExtensionDescription } from 'vs/workbench/api/common/extHost.protocol';
import { Emitter, Event } from 'vs/base/common/event'; import { Emitter, Event } from 'vs/base/common/event';
import { INotebook, ICell, INotebookMimeTypeSelector } from 'vs/workbench/contrib/notebook/common/notebookCommon'; 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 { function MODEL_ID(resource: URI): string {
return resource.toString(); return resource.toString();
...@@ -30,6 +31,7 @@ export interface IMainNotebookController { ...@@ -30,6 +31,7 @@ export interface IMainNotebookController {
export interface INotebookService { export interface INotebookService {
_serviceBrand: undefined; _serviceBrand: undefined;
canResolve(viewType: string): Promise<void>;
onDidChangeActiveEditor: Event<{ viewType: string, uri: URI }>; onDidChangeActiveEditor: Event<{ viewType: string, uri: URI }>;
registerNotebookController(viewType: string, extensionData: NotebookExtensionDescription, controller: IMainNotebookController): void; registerNotebookController(viewType: string, extensionData: NotebookExtensionDescription, controller: IMainNotebookController): void;
unregisterNotebookProvider(viewType: string): void; unregisterNotebookProvider(viewType: string): void;
...@@ -97,8 +99,11 @@ export class NotebookService extends Disposable implements INotebookService { ...@@ -97,8 +99,11 @@ export class NotebookService extends Disposable implements INotebookService {
private readonly _models: { [modelId: string]: ModelData; }; private readonly _models: { [modelId: string]: ModelData; };
private _onDidChangeActiveEditor = new Emitter<{ viewType: string, uri: URI }>(); private _onDidChangeActiveEditor = new Emitter<{ viewType: string, uri: URI }>();
onDidChangeActiveEditor: Event<{ viewType: string, uri: URI }> = this._onDidChangeActiveEditor.event; onDidChangeActiveEditor: Event<{ viewType: string, uri: URI }> = this._onDidChangeActiveEditor.event;
private _resolvePool = new Map<string, () => void>();
constructor() { constructor(
@IExtensionService private readonly extensionService: IExtensionService
) {
super(); super();
this._models = {}; this._models = {};
...@@ -119,8 +124,27 @@ export class NotebookService extends Disposable implements INotebookService { ...@@ -119,8 +124,27 @@ export class NotebookService extends Disposable implements INotebookService {
} }
async canResolve(viewType: string): Promise<void> {
if (this._notebookProviders.has(viewType)) {
return;
}
this.extensionService.activateByEvent(`onNotebookEditor:${viewType}`);
let resolve: () => void;
const promise = new Promise<void>(r => { resolve = r; });
this._resolvePool.set(viewType, resolve!);
return promise;
}
registerNotebookController(viewType: string, extensionData: NotebookExtensionDescription, controller: IMainNotebookController) { registerNotebookController(viewType: string, extensionData: NotebookExtensionDescription, controller: IMainNotebookController) {
this._notebookProviders.set(viewType, { extensionData, controller }); this._notebookProviders.set(viewType, { extensionData, controller });
let resolve = this._resolvePool.get(viewType);
if (resolve) {
resolve();
this._resolvePool.delete(viewType);
}
} }
unregisterNotebookProvider(viewType: string): void { unregisterNotebookProvider(viewType: string): void {
......
...@@ -5,7 +5,7 @@ ...@@ -5,7 +5,7 @@
import { IInstantiationService, ServicesAccessor } from 'vs/platform/instantiation/common/instantiation'; import { IInstantiationService, ServicesAccessor } from 'vs/platform/instantiation/common/instantiation';
import { IResourceInput, ITextEditorOptions, IEditorOptions, EditorActivation } from 'vs/platform/editor/common/editor'; 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 { ResourceEditorInput } from 'vs/workbench/common/editor/resourceEditorInput';
import { Registry } from 'vs/platform/registry/common/platform'; import { Registry } from 'vs/platform/registry/common/platform';
import { ResourceMap } from 'vs/base/common/map'; import { ResourceMap } from 'vs/base/common/map';
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册