notebook.contribution.ts 9.8 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.
 *--------------------------------------------------------------------------------------------*/

R
rebornix 已提交
6 7
import * as nls from 'vs/nls';
import { IConfigurationRegistry, Extensions } from 'vs/platform/configuration/common/configurationRegistry';
8
import { IEditorOptions, ITextEditorOptions } from 'vs/platform/editor/common/editor';
P
Peng Lyu 已提交
9
import { SyncDescriptor } from 'vs/platform/instantiation/common/descriptors';
10 11 12
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 已提交
13 14
import { Registry } from 'vs/platform/registry/common/platform';
import { EditorDescriptor, Extensions as EditorExtensions, IEditorRegistry } from 'vs/workbench/browser/editor';
15
import { Extensions as WorkbenchExtensions, IWorkbenchContribution, IWorkbenchContributionsRegistry } from 'vs/workbench/common/contributions';
16 17
import { IEditorInput, IEditorInputFactoryRegistry, Extensions as EditorInputExtensions, IEditorInputFactory, EditorInput } from 'vs/workbench/common/editor';
import { NotebookEditor, NotebookEditorOptions } from 'vs/workbench/contrib/notebook/browser/notebookEditor';
18
import { NotebookEditorInput } from 'vs/workbench/contrib/notebook/browser/notebookEditorInput';
R
rebornix 已提交
19
import { INotebookService, NotebookService } from 'vs/workbench/contrib/notebook/browser/notebookService';
20 21
import { IEditorGroup } from 'vs/workbench/services/editor/common/editorGroupsService';
import { IEditorService, IOpenEditorOverride } from 'vs/workbench/services/editor/common/editorService';
22 23 24 25 26 27
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';
28 29
import { assertType } from 'vs/base/common/types';
import { parse } from 'vs/base/common/marshalling';
30
import { CellUri, CellKind } from 'vs/workbench/contrib/notebook/common/notebookCommon';
31
import { ResourceMap } from 'vs/base/common/map';
R
rebornix 已提交
32 33 34

// Output renderers registration

R
rebornix 已提交
35 36 37
import 'vs/workbench/contrib/notebook/browser/view/output/transforms/streamTransform';
import 'vs/workbench/contrib/notebook/browser/view/output/transforms/errorTransform';
import 'vs/workbench/contrib/notebook/browser/view/output/transforms/richTransform';
R
rebornix 已提交
38

39
// Actions
R
rebornix 已提交
40
import 'vs/workbench/contrib/notebook/browser/contrib/notebookActions';
41
import { basename } from 'vs/base/common/resources';
J
Johannes Rieken 已提交
42
import { NotebookProviderInfo } from 'vs/workbench/contrib/notebook/common/notebookProvider';
P
Peng Lyu 已提交
43 44 45 46 47 48 49 50 51 52 53 54

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

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 83
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;
			}
			return instantiationService.createInstance(NotebookEditorInput, resource, name, viewType);
		}
	}
);

J
Johannes Rieken 已提交
84 85 86 87
function getFirstNotebookInfo(notebookService: INotebookService, uri: URI): NotebookProviderInfo | undefined {
	return notebookService.getContributedNotebookProviders(uri)[0];
}

88
export class NotebookContribution implements IWorkbenchContribution {
89
	private _resourceMapping = new ResourceMap<NotebookEditorInput>();
P
Peng Lyu 已提交
90 91 92

	constructor(
		@IEditorService private readonly editorService: IEditorService,
R
rebornix 已提交
93
		@INotebookService private readonly notebookService: INotebookService,
P
Peng Lyu 已提交
94
		@IInstantiationService private readonly instantiationService: IInstantiationService
95

P
Peng Lyu 已提交
96
	) {
R
rebornix 已提交
97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116
		this.editorService.overrideOpenEditor({
			getEditorOverrides: (editor: IEditorInput, options: IEditorOptions | undefined, group: IEditorGroup | undefined) => {
				let resource = editor.resource;
				if (!resource) {
					return [];
				}

				const infos = notebookService.getContributedNotebookProviders(resource);

				return infos.map(info => {
					return {
						label: info.displayName,
						id: info.id,
						active: editor instanceof NotebookEditorInput && editor.viewType === info.id,
						detail: info.providerDisplayName
					};
				});
			},
			open: (editor, options, group, id) => this.onEditorOpening(editor, options, group, id)
		});
R
rebornix 已提交
117

R
rebornix 已提交
118 119 120
		this.editorService.onDidActiveEditorChange(() => {
			if (this.editorService.activeEditor && this.editorService.activeEditor! instanceof NotebookEditorInput) {
				let editorInput = this.editorService.activeEditor! as NotebookEditorInput;
J
Johannes Rieken 已提交
121
				this.notebookService.updateActiveNotebookDocument(editorInput.viewType!, editorInput.resource!);
R
rebornix 已提交
122 123
			}
		});
P
Peng Lyu 已提交
124 125
	}

R
rebornix 已提交
126
	private onEditorOpening(originalInput: IEditorInput, options: IEditorOptions | ITextEditorOptions | undefined, group: IEditorGroup, id: string | undefined): IOpenEditorOverride | undefined {
127 128 129 130
		let resource = originalInput.resource;
		if (!resource) {
			return undefined;
		}
131

J
Johannes Rieken 已提交
132 133
		let info: NotebookProviderInfo | undefined;
		const data = CellUri.parse(resource);
R
rebornix 已提交
134 135 136 137 138 139 140 141 142 143 144
		if (data) {
			const infos = this.notebookService.getContributedNotebookProviders(data.notebook);

			if (infos.length) {
				const info = id === undefined ? infos[0] : (infos.find(info => info.id === id) || infos[0]);
				// cell-uri -> open (container) notebook
				const name = basename(data.notebook);
				const input = this.instantiationService.createInstance(NotebookEditorInput, data.notebook, name, info.id);
				this._resourceMapping.set(resource, input);
				return { override: this.editorService.openEditor(input, new NotebookEditorOptions({ ...options, forceReload: true, cellOptions: { resource, options } }), group) };
			}
145 146
		}

R
rebornix 已提交
147 148 149
		const infos = this.notebookService.getContributedNotebookProviders(resource);
		info = id === undefined ? infos[0] : infos.find(info => info.id === id);

J
Johannes Rieken 已提交
150
		if (!info) {
R
rebornix 已提交
151
			return undefined;
R
rebornix 已提交
152 153
		}

154 155
		if (this._resourceMapping.has(resource)) {
			const input = this._resourceMapping.get(resource);
P
Peng Lyu 已提交
156

R
rebornix 已提交
157
			if (!input!.isDisposed()) {
158
				return { override: this.editorService.openEditor(input!, new NotebookEditorOptions(options || {}).with({ ignoreOverrides: true }), group) };
R
rebornix 已提交
159
			}
P
Peng Lyu 已提交
160 161
		}

J
Johannes Rieken 已提交
162
		const input = this.instantiationService.createInstance(NotebookEditorInput, resource, originalInput.getName(), info.id);
163
		this._resourceMapping.set(resource, input);
164

R
rebornix 已提交
165
		return { override: this.editorService.openEditor(input, options, group) };
P
Peng Lyu 已提交
166 167 168
	}
}

169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186
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> {
J
Johannes Rieken 已提交
187 188 189 190
		const existing = this._modelService.getModel(resource);
		if (existing) {
			return existing;
		}
J
Johannes Rieken 已提交
191 192
		const data = CellUri.parse(resource);
		// const data = parseCellUri(resource);
193 194 195
		if (!data) {
			return null;
		}
J
Johannes Rieken 已提交
196 197 198 199 200
		const info = getFirstNotebookInfo(this._notebookService, data.notebook);
		if (!info) {
			return null;
		}
		const notebook = await this._notebookService.resolveNotebook(info.id, data.notebook);
201 202 203 204
		if (!notebook) {
			return null;
		}
		for (let cell of notebook.cells) {
205
			if (cell.uri.toString() === resource.toString()) {
206 207
				const bufferFactory = cell.resolveTextBufferFactory();
				const language = cell.cellKind === CellKind.Markdown ? this._modeService.create('markdown') : (cell.language ? this._modeService.create(cell.language) : this._modeService.createByFilepathOrFirstLine(resource, cell.source[0]));
208
				return this._modelService.createModel(
R
rebornix 已提交
209
					bufferFactory,
210
					language,
211 212 213 214 215 216 217 218 219
					resource
				);
			}
		}

		return null;
	}
}

220 221
const workbenchContributionsRegistry = Registry.as<IWorkbenchContributionsRegistry>(WorkbenchExtensions.Workbench);
workbenchContributionsRegistry.registerWorkbenchContribution(NotebookContribution, LifecyclePhase.Starting);
222
workbenchContributionsRegistry.registerWorkbenchContribution(CellContentProvider, LifecyclePhase.Starting);
R
rebornix 已提交
223 224

registerSingleton(INotebookService, NotebookService);
R
rebornix 已提交
225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242

const configurationRegistry = Registry.as<IConfigurationRegistry>(Extensions.Configuration);
configurationRegistry.registerConfiguration({
	id: 'notebook',
	order: 100,
	title: nls.localize('notebookConfigurationTitle', "Notebook"),
	type: 'object',
	properties: {
		'notebook.displayOrder': {
			markdownDescription: nls.localize('notebook.displayOrder.description', "Priority list for output mime types"),
			type: ['array'],
			items: {
				type: 'string'
			},
			default: []
		}
	}
});