notebook.contribution.ts 8.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';
J
Johannes Rieken 已提交
30
import { CellUri } 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
	) {
P
Peng Lyu 已提交
97
		this.editorService.overrideOpenEditor((editor, options, group) => this.onEditorOpening(editor, options, group));
R
rebornix 已提交
98

R
rebornix 已提交
99 100 101
		this.editorService.onDidActiveEditorChange(() => {
			if (this.editorService.activeEditor && this.editorService.activeEditor! instanceof NotebookEditorInput) {
				let editorInput = this.editorService.activeEditor! as NotebookEditorInput;
J
Johannes Rieken 已提交
102
				this.notebookService.updateActiveNotebookDocument(editorInput.viewType!, editorInput.resource!);
R
rebornix 已提交
103 104
			}
		});
P
Peng Lyu 已提交
105 106
	}

107 108 109 110 111
	private onEditorOpening(originalInput: IEditorInput, options: IEditorOptions | ITextEditorOptions | undefined, group: IEditorGroup): IOpenEditorOverride | undefined {
		let resource = originalInput.resource;
		if (!resource) {
			return undefined;
		}
112

J
Johannes Rieken 已提交
113 114 115
		let info: NotebookProviderInfo | undefined;
		const data = CellUri.parse(resource);
		if (data && (info = getFirstNotebookInfo(this.notebookService, data.notebook))) {
116 117
			// cell-uri -> open (container) notebook
			const name = basename(data.notebook);
J
Johannes Rieken 已提交
118
			const input = this.instantiationService.createInstance(NotebookEditorInput, data.notebook, name, info.id);
119
			this._resourceMapping.set(resource, input);
120
			return { override: this.editorService.openEditor(input, new NotebookEditorOptions({ ...options, forceReload: true, cellOptions: { resource, options } }), group) };
121 122
		}

J
Johannes Rieken 已提交
123 124
		info = getFirstNotebookInfo(this.notebookService, resource);
		if (!info) {
R
rebornix 已提交
125
			return undefined;
R
rebornix 已提交
126 127
		}

128 129
		if (this._resourceMapping.has(resource)) {
			const input = this._resourceMapping.get(resource);
P
Peng Lyu 已提交
130

R
rebornix 已提交
131
			if (!input!.isDisposed()) {
132
				return { override: this.editorService.openEditor(input!, new NotebookEditorOptions(options || {}).with({ ignoreOverrides: true }), group) };
R
rebornix 已提交
133
			}
P
Peng Lyu 已提交
134 135
		}

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

R
rebornix 已提交
139
		return { override: this.editorService.openEditor(input, options, group) };
P
Peng Lyu 已提交
140 141 142
	}
}

143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160
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 已提交
161 162 163 164
		const existing = this._modelService.getModel(resource);
		if (existing) {
			return existing;
		}
J
Johannes Rieken 已提交
165 166
		const data = CellUri.parse(resource);
		// const data = parseCellUri(resource);
167 168 169
		if (!data) {
			return null;
		}
J
Johannes Rieken 已提交
170 171 172 173 174
		const info = getFirstNotebookInfo(this._notebookService, data.notebook);
		if (!info) {
			return null;
		}
		const notebook = await this._notebookService.resolveNotebook(info.id, data.notebook);
175 176 177 178
		if (!notebook) {
			return null;
		}
		for (let cell of notebook.cells) {
179
			if (cell.uri.toString() === resource.toString()) {
R
rebornix 已提交
180
				let bufferFactory = cell.resolveTextBufferFactory();
181
				return this._modelService.createModel(
R
rebornix 已提交
182
					bufferFactory,
R
rebornix 已提交
183
					cell.language ? this._modeService.create(cell.language) : this._modeService.createByFilepathOrFirstLine(resource, cell.source[0]),
184 185 186 187 188 189 190 191 192
					resource
				);
			}
		}

		return null;
	}
}

193 194
const workbenchContributionsRegistry = Registry.as<IWorkbenchContributionsRegistry>(WorkbenchExtensions.Workbench);
workbenchContributionsRegistry.registerWorkbenchContribution(NotebookContribution, LifecyclePhase.Starting);
195
workbenchContributionsRegistry.registerWorkbenchContribution(CellContentProvider, LifecyclePhase.Starting);
R
rebornix 已提交
196 197

registerSingleton(INotebookService, NotebookService);
R
rebornix 已提交
198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215

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: []
		}
	}
});