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

6
import { IEditorOptions, ITextEditorOptions } from 'vs/platform/editor/common/editor';
P
Peng Lyu 已提交
7
import { SyncDescriptor } from 'vs/platform/instantiation/common/descriptors';
8 9 10
import { registerSingleton } from 'vs/platform/instantiation/common/extensions';
import { IInstantiationService } from 'vs/platform/instantiation/common/instantiation';
import { LifecyclePhase } from 'vs/platform/lifecycle/common/lifecycle';
P
Peng Lyu 已提交
11 12
import { Registry } from 'vs/platform/registry/common/platform';
import { EditorDescriptor, Extensions as EditorExtensions, IEditorRegistry } from 'vs/workbench/browser/editor';
13
import { Extensions as WorkbenchExtensions, IWorkbenchContribution, IWorkbenchContributionsRegistry } from 'vs/workbench/common/contributions';
14
import { IEditorInput, IEditorInputFactoryRegistry, Extensions as EditorInputExtensions, IEditorInputFactory, EditorInput } from 'vs/workbench/common/editor';
15 16
import { NotebookEditor } from 'vs/workbench/contrib/notebook/browser/notebookEditor';
import { NotebookEditorInput } from 'vs/workbench/contrib/notebook/browser/notebookEditorInput';
17
import { INotebookService, NotebookService, parseCellUri } from 'vs/workbench/contrib/notebook/browser/notebookService';
18 19
import { IEditorGroup } from 'vs/workbench/services/editor/common/editorGroupsService';
import { IEditorService, IOpenEditorOverride } from 'vs/workbench/services/editor/common/editorService';
20 21 22 23 24 25
import { ITextModelContentProvider, ITextModelService } from 'vs/editor/common/services/resolverService';
import { ITextModel } from 'vs/editor/common/model';
import { URI } from 'vs/base/common/uri';
import { IModelService } from 'vs/editor/common/services/modelService';
import { IModeService } from 'vs/editor/common/services/modeService';
import { IDisposable } from 'vs/base/common/lifecycle';
26 27
import { assertType } from 'vs/base/common/types';
import { parse } from 'vs/base/common/marshalling';
R
rebornix 已提交
28 29 30 31 32 33 34

// Output renderers registration

import 'vs/workbench/contrib/notebook/browser/output/transforms/streamTransform';
import 'vs/workbench/contrib/notebook/browser/output/transforms/errorTransform';
import 'vs/workbench/contrib/notebook/browser/output/transforms/richTransform';

35 36
// Actions
import 'vs/workbench/contrib/notebook/browser/notebookActions';
P
Peng Lyu 已提交
37 38 39 40 41 42 43 44 45 46 47 48

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

49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82
Registry.as<IEditorInputFactoryRegistry>(EditorInputExtensions.EditorInputFactories).registerEditorInputFactory(
	NotebookEditorInput.ID,
	class implements IEditorInputFactory {
		canSerialize(): boolean {
			return true;
		}
		serialize(input: EditorInput): string {
			assertType(input instanceof NotebookEditorInput);
			return JSON.stringify({
				resource: input.resource,
				name: input.name,
				viewType: input.viewType,
			});
		}
		deserialize(instantiationService: IInstantiationService, raw: string) {
			type Data = { resource: URI, name: string, viewType: string };
			const data = <Data>parse(raw);
			if (!data) {
				return undefined;
			}
			const { resource, name, viewType } = data;
			if (!data || !URI.isUri(resource) || typeof name !== 'string' || typeof viewType !== 'string') {
				return undefined;
			}
			// TODO@joh,peng this is disabled because the note-editor isn't fit for being
			// restorted (as it seems)
			if ('true') {
				return undefined;
			}
			return instantiationService.createInstance(NotebookEditorInput, resource, name, viewType);
		}
	}
);

83
export class NotebookContribution implements IWorkbenchContribution {
P
Peng Lyu 已提交
84
	private _resourceMapping: Map<string, NotebookEditorInput> = new Map<string, NotebookEditorInput>();
P
Peng Lyu 已提交
85 86 87

	constructor(
		@IEditorService private readonly editorService: IEditorService,
R
rebornix 已提交
88
		@INotebookService private readonly notebookService: INotebookService,
P
Peng Lyu 已提交
89
		@IInstantiationService private readonly instantiationService: IInstantiationService
90

P
Peng Lyu 已提交
91
	) {
P
Peng Lyu 已提交
92
		this.editorService.overrideOpenEditor((editor, options, group) => this.onEditorOpening(editor, options, group));
R
rebornix 已提交
93

R
rebornix 已提交
94 95 96
		this.editorService.onDidActiveEditorChange(() => {
			if (this.editorService.activeEditor && this.editorService.activeEditor! instanceof NotebookEditorInput) {
				let editorInput = this.editorService.activeEditor! as NotebookEditorInput;
J
Johannes Rieken 已提交
97
				this.notebookService.updateActiveNotebookDocument(editorInput.viewType!, editorInput.resource!);
R
rebornix 已提交
98 99
			}
		});
P
Peng Lyu 已提交
100 101
	}

102
	private onEditorOpening(editor: IEditorInput, options: IEditorOptions | ITextEditorOptions | undefined, group: IEditorGroup): IOpenEditorOverride | undefined {
J
Johannes Rieken 已提交
103
		const resource = editor.resource;
R
rebornix 已提交
104 105 106
		let viewType: string | undefined = undefined;

		if (resource) {
J
Johannes Rieken 已提交
107
			let notebookProviders = this.notebookService.getContributedNotebookProviders(resource);
108

R
rebornix 已提交
109 110 111
			if (notebookProviders.length > 0) {
				viewType = notebookProviders[0].id;
			}
112 113
		}

R
rebornix 已提交
114
		if (viewType === undefined) {
R
rebornix 已提交
115
			return undefined;
R
rebornix 已提交
116 117 118 119
		}

		if (this._resourceMapping.has(resource!.path)) {
			const input = this._resourceMapping.get(resource!.path);
P
Peng Lyu 已提交
120

R
rebornix 已提交
121 122 123
			if (!input!.isDisposed()) {
				return { override: this.editorService.openEditor(input!, { ...options, ignoreOverrides: true }, group) };
			}
P
Peng Lyu 已提交
124 125
		}

126
		const input = this.instantiationService.createInstance(NotebookEditorInput, editor.resource!, editor.getName(), viewType);
R
rebornix 已提交
127
		this._resourceMapping.set(resource!.path, input);
128

R
rebornix 已提交
129
		return { override: this.editorService.openEditor(input, options, group) };
P
Peng Lyu 已提交
130 131 132
	}
}

133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159
class CellContentProvider implements ITextModelContentProvider {

	private readonly _registration: IDisposable;

	constructor(
		@ITextModelService textModelService: ITextModelService,
		@IModelService private readonly _modelService: IModelService,
		@IModeService private readonly _modeService: IModeService,
		@INotebookService private readonly _notebookService: INotebookService,
	) {
		this._registration = textModelService.registerTextModelContentProvider('vscode-notebook', this);
	}

	dispose(): void {
		this._registration.dispose();
	}

	async provideTextContent(resource: URI): Promise<ITextModel | null> {
		const data = parseCellUri(resource);
		if (!data) {
			return null;
		}
		const notebook = await this._notebookService.resolveNotebook(data.viewType, data.notebook);
		if (!notebook) {
			return null;
		}
		for (let cell of notebook.cells) {
160
			if (cell.uri.toString() === resource.toString()) {
161 162 163 164 165 166 167 168 169 170 171 172
				return this._modelService.createModel(
					cell.source.join('\n'),
					this._modeService.createByFilepathOrFirstLine(resource, cell.source[0]),
					resource
				);
			}
		}

		return null;
	}
}

173 174
const workbenchContributionsRegistry = Registry.as<IWorkbenchContributionsRegistry>(WorkbenchExtensions.Workbench);
workbenchContributionsRegistry.registerWorkbenchContribution(NotebookContribution, LifecyclePhase.Starting);
175
workbenchContributionsRegistry.registerWorkbenchContribution(CellContentProvider, LifecyclePhase.Starting);
R
rebornix 已提交
176 177

registerSingleton(INotebookService, NotebookService);