notebook.contribution.ts 3.1 KB
Newer Older
P
Peng Lyu 已提交
1 2 3 4 5 6 7 8 9 10
/*---------------------------------------------------------------------------------------------
 *  Copyright (c) Microsoft Corporation. All rights reserved.
 *  Licensed under the MIT License. See License.txt in the project root for license information.
 *--------------------------------------------------------------------------------------------*/

import { SyncDescriptor } from 'vs/platform/instantiation/common/descriptors';
import { Registry } from 'vs/platform/registry/common/platform';
import { EditorDescriptor, Extensions as EditorExtensions, IEditorRegistry } from 'vs/workbench/browser/editor';
import { NotebookEditor } from 'vs/workbench/contrib/notebook/browser/notebookEditor';
import { NotebookEditorInput } from 'vs/workbench/contrib/notebook/browser/notebookEditorInput';
11
import { IEditorService, IOpenEditorOverride } from 'vs/workbench/services/editor/common/editorService';
P
Peng Lyu 已提交
12
import { IInstantiationService } from 'vs/platform/instantiation/common/instantiation';
13 14 15 16 17 18
import { Extensions as WorkbenchExtensions, IWorkbenchContribution, IWorkbenchContributionsRegistry } from 'vs/workbench/common/contributions';
import { IEditorInput } from 'vs/workbench/common/editor';
import { IEditorOptions, ITextEditorOptions } from 'vs/platform/editor/common/editor';
import { endsWith } from 'vs/base/common/strings';
import { IEditorGroup } from 'vs/workbench/services/editor/common/editorGroupsService';
import { LifecyclePhase } from 'vs/platform/lifecycle/common/lifecycle';
P
Peng Lyu 已提交
19 20 21 22 23 24 25 26 27 28 29 30

Registry.as<IEditorRegistry>(EditorExtensions.Editors).registerEditor(
	EditorDescriptor.create(
		NotebookEditor,
		NotebookEditor.ID,
		'Notebook Editor'
	),
	[
		new SyncDescriptor(NotebookEditorInput)
	]
);

31
export class NotebookContribution implements IWorkbenchContribution {
P
Peng Lyu 已提交
32
	private _resourceMapping: Map<string, NotebookEditorInput> = new Map<string, NotebookEditorInput>();
P
Peng Lyu 已提交
33 34 35 36

	constructor(
		@IEditorService private readonly editorService: IEditorService,
		@IInstantiationService private readonly instantiationService: IInstantiationService
37

P
Peng Lyu 已提交
38
	) {
P
Peng Lyu 已提交
39
		this.editorService.overrideOpenEditor((editor, options, group) => this.onEditorOpening(editor, options, group));
P
Peng Lyu 已提交
40 41
	}

42 43 44 45 46 47 48 49 50 51
	private onEditorOpening(editor: IEditorInput, options: IEditorOptions | ITextEditorOptions | undefined, group: IEditorGroup): IOpenEditorOverride | undefined {
		const resource = editor.getResource();

		if (
			!resource ||
			!endsWith(resource.path, '.ipynb')
		) {
			return undefined;
		}

P
Peng Lyu 已提交
52 53 54 55 56 57
		if (this._resourceMapping.has(resource.path)) {
			const input = this._resourceMapping.get(resource.path);

			return { override: this.editorService.openEditor(input!, options, group) };
		}

58
		const input = this.instantiationService.createInstance(NotebookEditorInput, editor);
P
Peng Lyu 已提交
59
		this._resourceMapping.set(resource.path, input);
60

P
Peng Lyu 已提交
61
		return { override: this.editorService.openEditor(input, { ...options, ignoreOverrides: true }, group) };
P
Peng Lyu 已提交
62 63 64
	}
}

65 66
const workbenchContributionsRegistry = Registry.as<IWorkbenchContributionsRegistry>(WorkbenchExtensions.Workbench);
workbenchContributionsRegistry.registerWorkbenchContribution(NotebookContribution, LifecyclePhase.Starting);