notebook.contribution.ts 3.2 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 19
import { Extensions as WorkbenchExtensions, IWorkbenchContribution, IWorkbenchContributionsRegistry } from 'vs/workbench/common/contributions';
import { IDisposable } from 'vs/base/common/lifecycle';
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 已提交
20 21 22 23 24 25 26 27 28 29 30 31

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

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

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

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

44 45 46 47 48 49 50 51 52 53
	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 已提交
54 55 56 57 58 59
		if (this._resourceMapping.has(resource.path)) {
			const input = this._resourceMapping.get(resource.path);

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

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

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

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