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

import { Emitter, Event } from 'vs/base/common/event';
import { URI } from 'vs/base/common/uri';
import { PieceTreeTextBufferFactory } from 'vs/editor/common/model/pieceTreeTextBuffer/pieceTreeTextBufferBuilder';
9
import { CellKind, ICell, IOutput, NotebookCellOutputsSplice, CellUri, NotebookCellMetadata } from 'vs/workbench/contrib/notebook/common/notebookCommon';
R
rebornix 已提交
10
import { NotebookViewModel, IModelDecorationsChangeAccessor, CellViewModel } from 'vs/workbench/contrib/notebook/browser/viewModel/notebookViewModel';
11 12
import { IInstantiationService } from 'vs/platform/instantiation/common/instantiation';
import { NotebookEditorModel } from 'vs/workbench/contrib/notebook/browser/notebookEditorInput';
13
import { INotebookEditor, NotebookLayoutInfo, ICellViewModel } from 'vs/workbench/contrib/notebook/browser/notebookBrowser';
14
import { IMouseWheelEvent } from 'vs/base/browser/mouseEvent';
R
rebornix 已提交
15
import { OutputRenderer } from 'vs/workbench/contrib/notebook/browser/view/output/outputRenderer';
16
import { BareFontInfo } from 'vs/editor/common/config/fontInfo';
R
rebornix 已提交
17
import { Range } from 'vs/editor/common/core/range';
R
rebornix 已提交
18 19
import { IBulkEditService } from 'vs/editor/browser/services/bulkEditService';
import { IUndoRedoService } from 'vs/platform/undoRedo/common/undoRedo';
20 21
import { NotebookTextModel } from 'vs/workbench/contrib/notebook/common/model/notebookTextModel';
import { NotebookCellTextModel } from 'vs/workbench/contrib/notebook/common/model/notebookCellTextModel';
R
rebornix 已提交
22
import { NotebookEventDispatcher } from 'vs/workbench/contrib/notebook/browser/viewModel/eventDispatcher';
23 24 25

export class TestCell implements ICell {
	uri: URI;
26 27 28
	private _onDidChangeContent = new Emitter<void>();
	onDidChangeContent: Event<void> = this._onDidChangeContent.event;

29 30
	private _onDidChangeOutputs = new Emitter<NotebookCellOutputsSplice[]>();
	onDidChangeOutputs: Event<NotebookCellOutputsSplice[]> = this._onDidChangeOutputs.event;
31 32
	private _onDidChangeLanguage = new Emitter<string>();
	onDidChangeLanguage: Event<string> = this._onDidChangeLanguage.event;
33 34
	private _onDidChangeMetadata = new Emitter<void>();
	onDidChangeMetadata: Event<void> = this._onDidChangeMetadata.event;
35 36
	private _isDirty: boolean = false;
	private _outputs: IOutput[];
R
Rob Lourens 已提交
37 38 39 40 41

	get metadata(): NotebookCellMetadata {
		return { editable: true };
	}

42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63
	get outputs(): IOutput[] {
		return this._outputs;
	}

	get isDirty() {
		return this._isDirty;
	}

	set isDirty(newState: boolean) {
		this._isDirty = newState;

	}

	constructor(
		public viewType: string,
		public handle: number,
		public source: string[],
		public language: string,
		public cellKind: CellKind,
		outputs: IOutput[]
	) {
		this._outputs = outputs;
J
Johannes Rieken 已提交
64
		this.uri = CellUri.generate(URI.parse('test:///fake/notebook'), handle);
65
	}
66 67
	contentChange(): void {
		// throw new Error('Method not implemented.');
68 69
	}

70
	resolveTextBufferFactory(): PieceTreeTextBufferFactory {
71 72 73 74 75 76
		throw new Error('Method not implemented.');
	}
}

export class TestNotebookEditor implements INotebookEditor {

R
rebornix 已提交
77 78 79 80
	get viewModel() {
		return undefined;
	}

81
	constructor(
R
rebornix 已提交
82
	) { }
83

84 85 86 87
	executeNotebookCell(cell: ICellViewModel): Promise<void> {
		throw new Error('Method not implemented.');
	}

88 89 90 91 92 93
	isNotebookEditor = true;

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

94 95 96 97
	setCellSelection(cell: CellViewModel, selection: Range): void {
		throw new Error('Method not implemented.');
	}

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

102 103 104 105 106 107 108 109 110
	moveCellDown(cell: CellViewModel): void {
		throw new Error('Method not implemented.');
	}

	moveCellUp(cell: CellViewModel): void {
		throw new Error('Method not implemented.');
	}

	setSelection(cell: CellViewModel, selection: Range): void {
R
rebornix 已提交
111 112
		throw new Error('Method not implemented.');
	}
R
rebornix 已提交
113 114 115 116 117 118 119 120 121
	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.');
	}
122

R
rebornix 已提交
123 124
	revealLineInView(cell: CellViewModel, line: number): void {
		throw new Error('Method not implemented.');
R
rebornix 已提交
125 126 127
	}
	getLayoutInfo(): NotebookLayoutInfo {
		throw new Error('Method not implemented.');
R
rebornix 已提交
128 129 130 131 132 133
	}
	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 已提交
134 135 136 137 138 139 140 141 142 143
	}
	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 已提交
144
	revealInView(cell: CellViewModel): void {
R
rebornix 已提交
145 146
		throw new Error('Method not implemented.');
	}
R
rebornix 已提交
147
	revealInCenter(cell: CellViewModel): void {
R
rebornix 已提交
148 149
		throw new Error('Method not implemented.');
	}
R
rebornix 已提交
150
	revealInCenterIfOutsideViewport(cell: CellViewModel): void {
R
rebornix 已提交
151
		throw new Error('Method not implemented.');
152
	}
153
	async insertNotebookCell(cell: CellViewModel, type: CellKind, direction: 'above' | 'below'): Promise<void> {
154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191
		// throw new Error('Method not implemented.');
	}
	deleteNotebookCell(cell: CellViewModel): void {
		// throw new Error('Method not implemented.');
	}
	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;
	}
	layoutNotebookCell(cell: CellViewModel, height: number): void {
		// throw new Error('Method not implemented.');
	}
	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 已提交
192 193 194 195

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

R
rebornix 已提交
198 199 200 201
// 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);
// }
202

203
export function withTestNotebook(instantiationService: IInstantiationService, blukEditService: IBulkEditService, undoRedoService: IUndoRedoService, cells: [string[], string, CellKind, IOutput[], NotebookCellMetadata][], callback: (editor: TestNotebookEditor, viewModel: NotebookViewModel) => void) {
204
	const viewType = 'notebook';
R
rebornix 已提交
205
	const editor = new TestNotebookEditor();
206
	const notebook = new NotebookTextModel(0, viewType, URI.parse('test'));
207
	notebook.cells = cells.map((cell, index) => {
208
		return new NotebookCellTextModel(notebook.uri, index, cell[0], cell[1], cell[2], cell[3], cell[4]);
209 210
	});
	const model = new NotebookEditorModel(notebook);
R
rebornix 已提交
211
	const eventDispatcher = new NotebookEventDispatcher();
212
	const viewModel = new NotebookViewModel(viewType, model, eventDispatcher, null, instantiationService, blukEditService, undoRedoService);
213 214 215 216 217 218

	callback(editor, viewModel);

	viewModel.dispose();
	return;
}