testNotebookEditor.ts 9.7 KB
Newer Older
1 2 3 4 5 6
/*---------------------------------------------------------------------------------------------
 *  Copyright (c) Microsoft Corporation. All rights reserved.
 *  Licensed under the MIT License. See License.txt in the project root for license information.
 *--------------------------------------------------------------------------------------------*/

import { URI } from 'vs/base/common/uri';
R
rebornix 已提交
7
import { CellKind, IOutput, CellUri, NotebookCellMetadata, INotebookEditorModel } from 'vs/workbench/contrib/notebook/common/notebookCommon';
R
rebornix 已提交
8
import { NotebookViewModel, IModelDecorationsChangeAccessor, CellViewModel } from 'vs/workbench/contrib/notebook/browser/viewModel/notebookViewModel';
9
import { IInstantiationService } from 'vs/platform/instantiation/common/instantiation';
R
rebornix 已提交
10
import { INotebookEditor, NotebookLayoutInfo, ICellViewModel, ICellRange, INotebookEditorMouseEvent, INotebookEditorContribution } from 'vs/workbench/contrib/notebook/browser/notebookBrowser';
11
import { IMouseWheelEvent } from 'vs/base/browser/mouseEvent';
R
rebornix 已提交
12
import { OutputRenderer } from 'vs/workbench/contrib/notebook/browser/view/output/outputRenderer';
13
import { BareFontInfo } from 'vs/editor/common/config/fontInfo';
R
rebornix 已提交
14
import { Range } from 'vs/editor/common/core/range';
R
rebornix 已提交
15 16
import { IBulkEditService } from 'vs/editor/browser/services/bulkEditService';
import { IUndoRedoService } from 'vs/platform/undoRedo/common/undoRedo';
17 18
import { NotebookTextModel } from 'vs/workbench/contrib/notebook/common/model/notebookTextModel';
import { NotebookCellTextModel } from 'vs/workbench/contrib/notebook/common/model/notebookCellTextModel';
R
rebornix 已提交
19
import { NotebookEventDispatcher } from 'vs/workbench/contrib/notebook/browser/viewModel/eventDispatcher';
R
rebornix 已提交
20
import { Webview } from 'vs/workbench/contrib/webview/browser/webview';
R
rebornix 已提交
21
import { IDisposable } from 'vs/base/common/lifecycle';
R
rebornix 已提交
22
import { Emitter, Event } from 'vs/base/common/event';
23
import { EditorModel } from 'vs/workbench/common/editor';
R
rebornix 已提交
24
export class TestCell extends NotebookCellTextModel {
25 26
	constructor(
		public viewType: string,
R
rebornix 已提交
27
		handle: number,
28
		public source: string[],
R
rebornix 已提交
29 30
		language: string,
		cellKind: CellKind,
31 32
		outputs: IOutput[]
	) {
R
rebornix 已提交
33
		super(CellUri.generate(URI.parse('test:///fake/notebook'), handle), handle, source, language, cellKind, outputs, undefined);
34 35 36 37 38
	}
}

export class TestNotebookEditor implements INotebookEditor {

R
rebornix 已提交
39 40 41 42
	get viewModel() {
		return undefined;
	}

43
	constructor(
R
rebornix 已提交
44
	) { }
R
rebornix 已提交
45 46 47
	getDomNode(): HTMLElement {
		throw new Error('Method not implemented.');
	}
R
rebornix 已提交
48 49 50 51 52 53

	private _onDidChangeModel = new Emitter<void>();
	onDidChangeModel: Event<void> = this._onDidChangeModel.event;
	getContribution<T extends INotebookEditorContribution>(id: string): T {
		throw new Error('Method not implemented.');
	}
R
rebornix 已提交
54 55 56 57 58 59
	onMouseUp(listener: (e: INotebookEditorMouseEvent) => void): IDisposable {
		throw new Error('Method not implemented.');
	}
	onMouseDown(listener: (e: INotebookEditorMouseEvent) => void): IDisposable {
		throw new Error('Method not implemented.');
	}
R
rebornix 已提交
60 61 62 63 64

	setHiddenAreas(_ranges: ICellRange[]): boolean {
		throw new Error('Method not implemented.');
	}

R
rebornix 已提交
65 66 67
	getInnerWebview(): Webview | undefined {
		throw new Error('Method not implemented.');
	}
68

69 70 71 72 73 74 75 76 77 78 79 80
	cancelNotebookCellExecution(cell: ICellViewModel): void {
		throw new Error('Method not implemented.');
	}

	executeNotebook(): Promise<void> {
		throw new Error('Method not implemented.');
	}

	cancelNotebookExecution(): void {
		throw new Error('Method not implemented.');
	}

81 82 83 84
	executeNotebookCell(cell: ICellViewModel): Promise<void> {
		throw new Error('Method not implemented.');
	}

85 86 87 88 89 90
	isNotebookEditor = true;

	postMessage(message: any): void {
		throw new Error('Method not implemented.');
	}

91 92 93 94
	setCellSelection(cell: CellViewModel, selection: Range): void {
		throw new Error('Method not implemented.');
	}

R
rebornix 已提交
95 96 97
	selectElement(cell: CellViewModel): void {
		throw new Error('Method not implemented.');
	}
R
rebornix 已提交
98

99
	moveCellDown(cell: CellViewModel): Promise<boolean> {
100 101 102
		throw new Error('Method not implemented.');
	}

103
	moveCellUp(cell: CellViewModel): Promise<boolean> {
104 105 106
		throw new Error('Method not implemented.');
	}

107
	moveCell(cell: ICellViewModel, relativeToCell: ICellViewModel, direction: 'above' | 'below'): Promise<boolean> {
108 109 110
		throw new Error('Method not implemented.');
	}

K
kieferrm 已提交
111 112 113 114 115 116 117 118
	splitNotebookCell(cell: ICellViewModel): CellViewModel[] | null {
		throw new Error('Method not implemented.');
	}

	joinNotebookCells(cell: ICellViewModel, direction: 'above' | 'below', constraint?: CellKind): Promise<ICellViewModel | null> {
		throw new Error('Method not implemented.');
	}

119
	setSelection(cell: CellViewModel, selection: Range): void {
R
rebornix 已提交
120 121
		throw new Error('Method not implemented.');
	}
R
rebornix 已提交
122 123 124 125 126 127 128 129 130
	revealRangeInView(cell: CellViewModel, range: Range): void {
		throw new Error('Method not implemented.');
	}
	revealRangeInCenter(cell: CellViewModel, range: Range): void {
		throw new Error('Method not implemented.');
	}
	revealRangeInCenterIfOutsideViewport(cell: CellViewModel, range: Range): void {
		throw new Error('Method not implemented.');
	}
131

R
rebornix 已提交
132 133
	revealLineInView(cell: CellViewModel, line: number): void {
		throw new Error('Method not implemented.');
R
rebornix 已提交
134 135 136
	}
	getLayoutInfo(): NotebookLayoutInfo {
		throw new Error('Method not implemented.');
R
rebornix 已提交
137 138 139 140 141 142
	}
	revealLineInCenterIfOutsideViewport(cell: CellViewModel, line: number): void {
		throw new Error('Method not implemented.');
	}
	revealLineInCenter(cell: CellViewModel, line: number): void {
		throw new Error('Method not implemented.');
R
rebornix 已提交
143 144 145 146 147 148 149 150 151 152
	}
	focus(): void {
		throw new Error('Method not implemented.');
	}
	showFind(): void {
		throw new Error('Method not implemented.');
	}
	hideFind(): void {
		throw new Error('Method not implemented.');
	}
R
rebornix 已提交
153
	revealInView(cell: CellViewModel): void {
R
rebornix 已提交
154 155
		throw new Error('Method not implemented.');
	}
R
rebornix 已提交
156
	revealInCenter(cell: CellViewModel): void {
R
rebornix 已提交
157 158
		throw new Error('Method not implemented.');
	}
R
rebornix 已提交
159
	revealInCenterIfOutsideViewport(cell: CellViewModel): void {
R
rebornix 已提交
160
		throw new Error('Method not implemented.');
161
	}
162
	insertNotebookCell(cell: CellViewModel, type: CellKind, direction: 'above' | 'below'): CellViewModel {
R
rebornix 已提交
163
		throw new Error('Method not implemented.');
164
	}
165 166
	deleteNotebookCell(cell: CellViewModel): Promise<boolean> {
		throw new Error('Method not implemented.');
167 168 169 170 171 172 173 174 175 176 177 178 179 180
	}
	editNotebookCell(cell: CellViewModel): void {
		// throw new Error('Method not implemented.');
	}
	saveNotebookCell(cell: CellViewModel): void {
		// throw new Error('Method not implemented.');
	}
	focusNotebookCell(cell: CellViewModel, focusEditor: boolean): void {
		// throw new Error('Method not implemented.');
	}
	getActiveCell(): CellViewModel | undefined {
		// throw new Error('Method not implemented.');
		return;
	}
R
rebornix 已提交
181
	async layoutNotebookCell(cell: CellViewModel, height: number): Promise<void> {
182
		// throw new Error('Method not implemented.');
R
rebornix 已提交
183
		return;
184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201
	}
	createInset(cell: CellViewModel, output: IOutput, shadowContent: string, offset: number): void {
		// throw new Error('Method not implemented.');
	}
	removeInset(output: IOutput): void {
		// throw new Error('Method not implemented.');
	}
	triggerScroll(event: IMouseWheelEvent): void {
		// throw new Error('Method not implemented.');
	}
	getFontInfo(): BareFontInfo | undefined {
		return BareFontInfo.createFromRawSettings({
			fontFamily: 'Monaco',
		}, 1, true);
	}
	getOutputRenderer(): OutputRenderer {
		throw new Error('Method not implemented.');
	}
R
rebornix 已提交
202 203 204 205

	changeDecorations(callback: (changeAccessor: IModelDecorationsChangeAccessor) => any): any {
		throw new Error('Method not implemented.');
	}
206 207
}

R
rebornix 已提交
208 209 210 211
// export function createTestCellViewModel(instantiationService: IInstantiationService, viewType: string, notebookHandle: number, cellhandle: number, source: string[], language: string, cellKind: CellKind, outputs: IOutput[]) {
// 	const mockCell = new TestCell(viewType, cellhandle, source, language, cellKind, outputs);
// 	return createCellViewModel(instantiationService, viewType, notebookHandle, mockCell);
// }
212

213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260
export class NotebookEditorTestModel extends EditorModel implements INotebookEditorModel {
	private _dirty = false;

	protected readonly _onDidChangeDirty = this._register(new Emitter<void>());
	readonly onDidChangeDirty = this._onDidChangeDirty.event;

	private readonly _onDidChangeContent = this._register(new Emitter<void>());
	readonly onDidChangeContent: Event<void> = this._onDidChangeContent.event;


	get notebook() {
		return this._notebook;
	}

	constructor(
		private _notebook: NotebookTextModel
	) {
		super();

		if (_notebook && _notebook.onDidChangeCells) {
			this._register(_notebook.onDidChangeContent(() => {
				this._dirty = true;
				this._onDidChangeDirty.fire();
				this._onDidChangeContent.fire();
			}));
		}
	}

	isDirty() {
		return this._dirty;
	}

	getNotebook(): NotebookTextModel {
		return this._notebook;
	}

	async save(): Promise<boolean> {
		if (this._notebook) {
			this._dirty = false;
			this._onDidChangeDirty.fire();
			// todo, flush all states
			return true;
		}

		return false;
	}
}

R
rebornix 已提交
261
export function withTestNotebook(instantiationService: IInstantiationService, blukEditService: IBulkEditService, undoRedoService: IUndoRedoService, cells: [string[], string, CellKind, IOutput[], NotebookCellMetadata][], callback: (editor: TestNotebookEditor, viewModel: NotebookViewModel, textModel: NotebookTextModel) => void) {
262
	const viewType = 'notebook';
R
rebornix 已提交
263
	const editor = new TestNotebookEditor();
264
	const notebook = new NotebookTextModel(0, viewType, URI.parse('test'));
265
	notebook.cells = cells.map((cell, index) => {
266
		return new NotebookCellTextModel(notebook.uri, index, cell[0], cell[1], cell[2], cell[3], cell[4]);
267
	});
268
	const model = new NotebookEditorTestModel(notebook);
R
rebornix 已提交
269
	const eventDispatcher = new NotebookEventDispatcher();
270
	const viewModel = new NotebookViewModel(viewType, model.notebook, eventDispatcher, null, instantiationService, blukEditService, undoRedoService);
271

R
rebornix 已提交
272
	callback(editor, viewModel, notebook);
273 274 275 276

	viewModel.dispose();
	return;
}