testNotebookEditor.ts 10.1 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 { IMouseWheelEvent } from 'vs/base/browser/mouseEvent';
7 8 9 10
import { Emitter, Event } from 'vs/base/common/event';
import { IDisposable } from 'vs/base/common/lifecycle';
import { URI } from 'vs/base/common/uri';
import { IBulkEditService } from 'vs/editor/browser/services/bulkEditService';
11
import { BareFontInfo } from 'vs/editor/common/config/fontInfo';
R
rebornix 已提交
12
import { Range } from 'vs/editor/common/core/range';
13
import { IInstantiationService } from 'vs/platform/instantiation/common/instantiation';
R
rebornix 已提交
14
import { IUndoRedoService } from 'vs/platform/undoRedo/common/undoRedo';
15 16 17
import { EditorModel } from 'vs/workbench/common/editor';
import { ICellRange, ICellViewModel, INotebookEditor, INotebookEditorContribution, INotebookEditorMouseEvent, NotebookLayoutInfo } from 'vs/workbench/contrib/notebook/browser/notebookBrowser';
import { OutputRenderer } from 'vs/workbench/contrib/notebook/browser/view/output/outputRenderer';
R
rebornix 已提交
18
import { NotebookEventDispatcher } from 'vs/workbench/contrib/notebook/browser/viewModel/eventDispatcher';
19 20 21
import { CellViewModel, IModelDecorationsChangeAccessor, NotebookViewModel } from 'vs/workbench/contrib/notebook/browser/viewModel/notebookViewModel';
import { NotebookCellTextModel } from 'vs/workbench/contrib/notebook/common/model/notebookCellTextModel';
import { NotebookTextModel } from 'vs/workbench/contrib/notebook/common/model/notebookTextModel';
R
rebornix 已提交
22
import { CellKind, CellUri, INotebookEditorModel, IOutput, NotebookCellMetadata, INotebookKernelInfo } from 'vs/workbench/contrib/notebook/common/notebookCommon';
R
rebornix 已提交
23
import { Webview } from 'vs/workbench/contrib/webview/browser/webview';
24
import { ICompositeCodeEditor, IEditor } from 'vs/editor/common/editorCommon';
R
rebornix 已提交
25 26
import { generateUuid } from 'vs/base/common/uuid';

R
rebornix 已提交
27
export class TestCell extends NotebookCellTextModel {
28 29
	constructor(
		public viewType: string,
R
rebornix 已提交
30
		handle: number,
31
		public source: string[],
R
rebornix 已提交
32 33
		language: string,
		cellKind: CellKind,
34 35
		outputs: IOutput[]
	) {
R
rebornix 已提交
36
		super(CellUri.generate(URI.parse('test:///fake/notebook'), handle), handle, source, language, cellKind, outputs, undefined);
37 38 39 40 41
	}
}

export class TestNotebookEditor implements INotebookEditor {

R
rebornix 已提交
42 43 44 45
	get viewModel() {
		return undefined;
	}

46
	constructor(
R
rebornix 已提交
47
	) { }
R
rebornix 已提交
48 49
	activeKernel: INotebookKernelInfo | undefined;
	onDidChangeKernel: Event<void> = new Emitter<void>().event;
50 51
	onDidChangeActiveEditor: Event<ICompositeCodeEditor> = new Emitter<ICompositeCodeEditor>().event;
	activeCodeEditor: IEditor | undefined;
R
rebornix 已提交
52 53 54
	getDomNode(): HTMLElement {
		throw new Error('Method not implemented.');
	}
R
rebornix 已提交
55 56 57 58 59 60

	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 已提交
61 62 63 64 65 66
	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 已提交
67 68 69 70 71

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

R
rebornix 已提交
72 73 74
	getInnerWebview(): Webview | undefined {
		throw new Error('Method not implemented.');
	}
75

76 77 78 79 80 81 82 83 84 85 86 87
	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.');
	}

88 89 90 91
	executeNotebookCell(cell: ICellViewModel): Promise<void> {
		throw new Error('Method not implemented.');
	}

92 93 94 95 96 97
	isNotebookEditor = true;

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

98 99 100 101
	setCellSelection(cell: CellViewModel, selection: Range): void {
		throw new Error('Method not implemented.');
	}

R
rebornix 已提交
102 103 104
	selectElement(cell: CellViewModel): void {
		throw new Error('Method not implemented.');
	}
R
rebornix 已提交
105

106
	moveCellDown(cell: CellViewModel): Promise<boolean> {
107 108 109
		throw new Error('Method not implemented.');
	}

110
	moveCellUp(cell: CellViewModel): Promise<boolean> {
111 112 113
		throw new Error('Method not implemented.');
	}

114
	moveCell(cell: ICellViewModel, relativeToCell: ICellViewModel, direction: 'above' | 'below'): Promise<boolean> {
115 116 117
		throw new Error('Method not implemented.');
	}

118
	splitNotebookCell(cell: ICellViewModel): Promise<CellViewModel[] | null> {
K
kieferrm 已提交
119 120 121 122 123 124 125
		throw new Error('Method not implemented.');
	}

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

126
	setSelection(cell: CellViewModel, selection: Range): void {
R
rebornix 已提交
127 128
		throw new Error('Method not implemented.');
	}
R
rebornix 已提交
129 130 131 132 133 134 135 136 137
	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.');
	}
138

R
rebornix 已提交
139 140
	revealLineInView(cell: CellViewModel, line: number): void {
		throw new Error('Method not implemented.');
R
rebornix 已提交
141 142 143
	}
	getLayoutInfo(): NotebookLayoutInfo {
		throw new Error('Method not implemented.');
R
rebornix 已提交
144 145 146 147 148 149
	}
	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 已提交
150 151 152 153 154 155 156 157 158 159
	}
	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 已提交
160
	revealInView(cell: CellViewModel): void {
R
rebornix 已提交
161 162
		throw new Error('Method not implemented.');
	}
R
rebornix 已提交
163
	revealInCenter(cell: CellViewModel): void {
R
rebornix 已提交
164 165
		throw new Error('Method not implemented.');
	}
R
rebornix 已提交
166
	revealInCenterIfOutsideViewport(cell: CellViewModel): void {
R
rebornix 已提交
167
		throw new Error('Method not implemented.');
168
	}
169
	insertNotebookCell(cell: CellViewModel, type: CellKind, direction: 'above' | 'below'): CellViewModel {
R
rebornix 已提交
170
		throw new Error('Method not implemented.');
171
	}
172 173
	deleteNotebookCell(cell: CellViewModel): Promise<boolean> {
		throw new Error('Method not implemented.');
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.');
	}
C
Christopher Maynard 已提交
181
	focusNotebookCell(cell: CellViewModel, focusItem: 'editor' | 'container' | 'output'): void {
182 183 184 185 186 187
		// throw new Error('Method not implemented.');
	}
	getActiveCell(): CellViewModel | undefined {
		// throw new Error('Method not implemented.');
		return;
	}
R
rebornix 已提交
188
	async layoutNotebookCell(cell: CellViewModel, height: number): Promise<void> {
189
		// throw new Error('Method not implemented.');
R
rebornix 已提交
190
		return;
191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208
	}
	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 已提交
209 210 211 212

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

R
rebornix 已提交
215 216 217 218
// 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);
// }
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 261 262 263 264 265 266 267
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 已提交
268
export function withTestNotebook(instantiationService: IInstantiationService, blukEditService: IBulkEditService, undoRedoService: IUndoRedoService, cells: [string[], string, CellKind, IOutput[], NotebookCellMetadata][], callback: (editor: TestNotebookEditor, viewModel: NotebookViewModel, textModel: NotebookTextModel) => void) {
269
	const viewType = 'notebook';
R
rebornix 已提交
270
	const editor = new TestNotebookEditor();
R
rebornix 已提交
271
	const notebook = new NotebookTextModel(0, viewType, URI.parse('test'), generateUuid());
272
	notebook.cells = cells.map((cell, index) => {
273
		return new NotebookCellTextModel(notebook.uri, index, cell[0], cell[1], cell[2], cell[3], cell[4]);
274
	});
275
	const model = new NotebookEditorTestModel(notebook);
R
rebornix 已提交
276
	const eventDispatcher = new NotebookEventDispatcher();
277
	const viewModel = new NotebookViewModel(viewType, model.notebook, eventDispatcher, null, instantiationService, blukEditService, undoRedoService);
278

R
rebornix 已提交
279
	callback(editor, viewModel, notebook);
280 281 282 283

	viewModel.dispose();
	return;
}