testNotebookEditor.ts 6.7 KB
Newer Older
1 2 3 4 5 6 7 8 9
/*---------------------------------------------------------------------------------------------
 *  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 { Disposable } from 'vs/base/common/lifecycle';
import { URI } from 'vs/base/common/uri';
import { PieceTreeTextBufferFactory } from 'vs/editor/common/model/pieceTreeTextBuffer/pieceTreeTextBufferBuilder';
J
Johannes Rieken 已提交
10
import { CellKind, ICell, INotebook, IOutput, NotebookCellOutputsSplice, NotebookCellsSplice, CellUri } from 'vs/workbench/contrib/notebook/common/notebookCommon';
R
rebornix 已提交
11
import { NotebookViewModel, IModelDecorationsChangeAccessor } from 'vs/workbench/contrib/notebook/browser/viewModel/notebookViewModel';
12
import { IInstantiationService } from 'vs/platform/instantiation/common/instantiation';
R
rebornix 已提交
13
import { CellViewModel } from 'vs/workbench/contrib/notebook/browser/viewModel/notebookCellViewModel';
14
import { NotebookEditorModel } from 'vs/workbench/contrib/notebook/browser/notebookEditorInput';
R
rebornix 已提交
15
import { INotebookEditor, NotebookLayoutInfo } from 'vs/workbench/contrib/notebook/browser/notebookBrowser';
16
import { IMouseWheelEvent } from 'vs/base/browser/mouseEvent';
R
rebornix 已提交
17
import { OutputRenderer } from 'vs/workbench/contrib/notebook/browser/view/output/outputRenderer';
18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47
import { BareFontInfo } from 'vs/editor/common/config/fontInfo';

export class TestCell implements ICell {
	uri: URI;
	private _onDidChangeOutputs = new Emitter<NotebookCellOutputsSplice[]>();
	onDidChangeOutputs: Event<NotebookCellOutputsSplice[]> = this._onDidChangeOutputs.event;
	private _isDirty: boolean = false;
	private _outputs: IOutput[];
	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 已提交
48
		this.uri = CellUri.generate(URI.parse('test:///fake/notebook'), handle);
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 83 84 85
	}

	resolveTextBufferFactory(): PieceTreeTextBufferFactory {
		throw new Error('Method not implemented.');
	}
}

export class TestNotebook extends Disposable implements INotebook {
	private readonly _onDidChangeCells = new Emitter<NotebookCellsSplice[]>();
	get onDidChangeCells(): Event<NotebookCellsSplice[]> { return this._onDidChangeCells.event; }
	private _onDidChangeDirtyState = new Emitter<boolean>();
	onDidChangeDirtyState: Event<boolean> = this._onDidChangeDirtyState.event;
	private readonly _onWillDispose: Emitter<void> = this._register(new Emitter<void>());
	readonly onWillDispose: Event<void> = this._onWillDispose.event;
	cells: TestCell[];
	activeCell: TestCell | undefined;
	languages: string[] = [];
	renderers = new Set<number>();


	constructor(
		public handle: number,
		public viewType: string,
		public uri: URI
	) {
		super();

		this.cells = [];
	}

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

export class TestNotebookEditor implements INotebookEditor {

R
rebornix 已提交
86 87 88 89
	get viewModel() {
		return undefined;
	}

90
	constructor(
R
rebornix 已提交
91
	) { }
92

R
rebornix 已提交
93 94
	revealLineInView(cell: CellViewModel, line: number): void {
		throw new Error('Method not implemented.');
R
rebornix 已提交
95 96 97
	}
	getLayoutInfo(): NotebookLayoutInfo {
		throw new Error('Method not implemented.');
R
rebornix 已提交
98 99 100 101 102 103
	}
	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 已提交
104 105 106 107 108 109 110 111 112 113
	}
	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 已提交
114
	revealInView(cell: CellViewModel): void {
R
rebornix 已提交
115 116
		throw new Error('Method not implemented.');
	}
R
rebornix 已提交
117
	revealInCenter(cell: CellViewModel): void {
R
rebornix 已提交
118 119
		throw new Error('Method not implemented.');
	}
R
rebornix 已提交
120
	revealInCenterIfOutsideViewport(cell: CellViewModel): void {
R
rebornix 已提交
121
		throw new Error('Method not implemented.');
122 123 124 125 126 127 128 129 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 160 161
	}
	async insertEmptyNotebookCell(cell: CellViewModel, type: CellKind, direction: 'above' | 'below'): Promise<void> {
		// 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 已提交
162 163 164 165

	changeDecorations(callback: (changeAccessor: IModelDecorationsChangeAccessor) => any): any {
		throw new Error('Method not implemented.');
	}
166 167 168 169
}

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);
R
rebornix 已提交
170
	return instantiationService.createInstance(CellViewModel, viewType, notebookHandle, mockCell);
171 172 173 174
}

export function withTestNotebook(instantiationService: IInstantiationService, cells: [string[], string, CellKind, IOutput[]][], callback: (editor: TestNotebookEditor, viewModel: NotebookViewModel) => void) {
	const viewType = 'notebook';
R
rebornix 已提交
175
	const editor = new TestNotebookEditor();
176 177 178 179 180 181 182 183 184 185 186 187
	const notebook = new TestNotebook(0, viewType, URI.parse('test'));
	notebook.cells = cells.map((cell, index) => {
		return new TestCell(viewType, index, cell[0], cell[1], cell[2], cell[3]);
	});
	const model = new NotebookEditorModel(notebook);
	const viewModel = new NotebookViewModel(viewType, model, instantiationService);

	callback(editor, viewModel);

	viewModel.dispose();
	return;
}