notebookBrowser.ts 20.0 KB
Newer Older
R
rebornix 已提交
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';
R
rebornix 已提交
7 8
import { IListEvent, IListMouseEvent } from 'vs/base/browser/ui/list/list';
import { IListOptions, IListStyles } from 'vs/base/browser/ui/list/listWidget';
9
import { ProgressBar } from 'vs/base/browser/ui/progressbar/progressbar';
10
import { ToolBar } from 'vs/base/browser/ui/toolbar/toolbar';
R
rebornix 已提交
11 12 13
import { Event } from 'vs/base/common/event';
import { DisposableStore, IDisposable } from 'vs/base/common/lifecycle';
import { ScrollEvent } from 'vs/base/common/scrollable';
R
rebornix 已提交
14
import { URI } from 'vs/base/common/uri';
R
Rob Lourens 已提交
15
import { ICodeEditor } from 'vs/editor/browser/editorBrowser';
16
import { BareFontInfo } from 'vs/editor/common/config/fontInfo';
K
kieferrm 已提交
17
import { IPosition } from 'vs/editor/common/core/position';
18
import { Range } from 'vs/editor/common/core/range';
19
import { FindMatch, IReadonlyTextBuffer, ITextModel } from 'vs/editor/common/model';
20
import { ContextKeyExpr, RawContextKey, IContextKeyService } from 'vs/platform/contextkey/common/contextkey';
21
import { OutputRenderer } from 'vs/workbench/contrib/notebook/browser/view/output/outputRenderer';
22
import { CellLanguageStatusBarItem, RunStateRenderer, TimerRenderer } from 'vs/workbench/contrib/notebook/browser/view/renderers/cellRenderer';
23
import { CellViewModel, IModelDecorationsChangeAccessor, NotebookViewModel } from 'vs/workbench/contrib/notebook/browser/viewModel/notebookViewModel';
R
rebornix 已提交
24
import { NotebookCellTextModel } from 'vs/workbench/contrib/notebook/common/model/notebookCellTextModel';
R
rebornix 已提交
25
import { CellKind, IProcessedOutput, IRenderOutput, NotebookCellMetadata, NotebookDocumentMetadata, INotebookKernelInfo, IEditor, INotebookKernelInfo2 } from 'vs/workbench/contrib/notebook/common/notebookCommon';
R
rebornix 已提交
26
import { Webview } from 'vs/workbench/contrib/webview/browser/webview';
R
rebornix 已提交
27
import { NotebookTextModel } from 'vs/workbench/contrib/notebook/common/model/notebookTextModel';
28
import { IMenu } from 'vs/platform/actions/common/actions';
R
rebornix 已提交
29
import { IEditorService } from 'vs/workbench/services/editor/common/editorService';
R
rebornix 已提交
30 31

export const KEYBINDING_CONTEXT_NOTEBOOK_FIND_WIDGET_FOCUSED = new RawContextKey<boolean>('notebookFindWidgetFocused', false);
R
rebornix 已提交
32

33 34 35
// Is Notebook
export const NOTEBOOK_IS_ACTIVE_EDITOR = ContextKeyExpr.equals('activeEditor', 'workbench.editor.notebook');

36
// Editor keys
37
export const NOTEBOOK_EDITOR_FOCUSED = new RawContextKey<boolean>('notebookEditorFocused', false);
R
rebornix 已提交
38
export const NOTEBOOK_CELL_LIST_FOCUSED = new RawContextKey<boolean>('notebookCellListFocused', false);
39
export const NOTEBOOK_OUTPUT_FOCUSED = new RawContextKey<boolean>('notebookOutputFocused', false);
40 41 42
export const NOTEBOOK_EDITOR_EDITABLE = new RawContextKey<boolean>('notebookEditable', true);
export const NOTEBOOK_EDITOR_RUNNABLE = new RawContextKey<boolean>('notebookRunnable', true);
export const NOTEBOOK_EDITOR_EXECUTING_NOTEBOOK = new RawContextKey<boolean>('notebookExecuting', false);
43

44 45 46 47 48 49 50
// Cell keys
export const NOTEBOOK_VIEW_TYPE = new RawContextKey<string>('notebookViewType', undefined);
export const NOTEBOOK_CELL_TYPE = new RawContextKey<string>('notebookCellType', undefined); // code, markdown
export const NOTEBOOK_CELL_EDITABLE = new RawContextKey<boolean>('notebookCellEditable', false); // bool
export const NOTEBOOK_CELL_RUNNABLE = new RawContextKey<boolean>('notebookCellRunnable', false); // bool
export const NOTEBOOK_CELL_MARKDOWN_EDIT_MODE = new RawContextKey<boolean>('notebookCellMarkdownEditMode', false); // bool
export const NOTEBOOK_CELL_RUN_STATE = new RawContextKey<string>('notebookCellRunState', undefined); // idle, running
R
rebornix 已提交
51
export const NOTEBOOK_CELL_HAS_OUTPUTS = new RawContextKey<boolean>('notebookCellHasOutputs', false); // bool
52
export const NOTEBOOK_CELL_INPUT_COLLAPSED = new RawContextKey<boolean>('notebookCellInputIsCollapsed', false); // bool
53
export const NOTEBOOK_CELL_OUTPUT_COLLAPSED = new RawContextKey<boolean>('notebookCellOutputIsCollapsed', false); // bool
54

R
rebornix 已提交
55 56 57 58
// Kernels

export const NOTEBOOK_HAS_MULTIPLE_KERNELS = new RawContextKey<boolean>('notebookHasMultipleKernels', false);

R
rebornix 已提交
59 60 61 62 63 64
export interface NotebookLayoutInfo {
	width: number;
	height: number;
	fontInfo: BareFontInfo;
}

R
rebornix 已提交
65 66 67 68 69 70
export interface NotebookLayoutChangeEvent {
	width?: boolean;
	height?: boolean;
	fontInfo?: boolean;
}

71 72 73 74 75 76 77
export enum CodeCellLayoutState {
	Uninitialized,
	Estimated,
	FromCache,
	Measured
}

R
rebornix 已提交
78 79 80 81 82
export interface CodeCellLayoutInfo {
	readonly fontInfo: BareFontInfo | null;
	readonly editorHeight: number;
	readonly editorWidth: number;
	readonly totalHeight: number;
83
	readonly outputContainerOffset: number;
R
rebornix 已提交
84 85
	readonly outputTotalHeight: number;
	readonly indicatorHeight: number;
R
rebornix 已提交
86
	readonly bottomToolbarOffset: number;
87
	readonly layoutState: CodeCellLayoutState;
R
rebornix 已提交
88 89 90 91 92 93
}

export interface CodeCellLayoutChangeEvent {
	editorHeight?: boolean;
	outputHeight?: boolean;
	totalHeight?: boolean;
R
rebornix 已提交
94 95
	outerWidth?: number;
	font?: BareFontInfo;
R
rebornix 已提交
96 97 98 99 100
}

export interface MarkdownCellLayoutInfo {
	readonly fontInfo: BareFontInfo | null;
	readonly editorWidth: number;
R
Rob Lourens 已提交
101
	readonly editorHeight: number;
R
rebornix 已提交
102
	readonly bottomToolbarOffset: number;
R
rebornix 已提交
103
	readonly totalHeight: number;
R
rebornix 已提交
104 105 106
}

export interface MarkdownCellLayoutChangeEvent {
R
rebornix 已提交
107 108
	font?: BareFontInfo;
	outerWidth?: number;
R
rebornix 已提交
109
	totalHeight?: number;
R
rebornix 已提交
110 111
}

R
rebornix 已提交
112
export interface ICellViewModel {
R
rebornix 已提交
113
	readonly model: NotebookCellTextModel;
R
rebornix 已提交
114
	readonly id: string;
115
	readonly textBuffer: IReadonlyTextBuffer;
R
Rob Lourens 已提交
116
	collapseState: CellCollapseState;
117
	outputCollapseState: CellCollapseState;
118
	dragging: boolean;
R
rebornix 已提交
119 120
	handle: number;
	uri: URI;
121
	language: string;
R
rebornix 已提交
122
	cellKind: CellKind;
123
	editState: CellEditState;
R
rebornix 已提交
124 125
	focusMode: CellFocusMode;
	getText(): string;
126
	getTextLength(): number;
127
	metadata: NotebookCellMetadata | undefined;
128 129 130
	textModel: ITextModel | undefined;
	hasModel(): this is IEditableCellViewModel;
	resolveTextModel(): Promise<ITextModel>;
R
rebornix 已提交
131
	getEvaluatedMetadata(documentMetadata: NotebookDocumentMetadata | undefined): NotebookCellMetadata;
K
kieferrm 已提交
132
	getSelectionsStartPosition(): IPosition[] | undefined;
R
rebornix 已提交
133
	getCellDecorations(): INotebookCellDecorationOptions[];
R
rebornix 已提交
134 135
}

136 137 138 139
export interface IEditableCellViewModel extends ICellViewModel {
	textModel: ITextModel;
}

R
rebornix 已提交
140 141 142 143 144
export interface INotebookEditorMouseEvent {
	readonly event: MouseEvent;
	readonly target: CellViewModel;
}

R
rebornix 已提交
145 146 147 148 149 150 151 152
export interface INotebookEditorContribution {
	/**
	 * Dispose this contribution.
	 */
	dispose(): void;
	/**
	 * Store view state.
	 */
R
rebornix 已提交
153
	saveViewState?(): unknown;
R
rebornix 已提交
154 155 156
	/**
	 * Restore view state.
	 */
R
rebornix 已提交
157
	restoreViewState?(state: unknown): void;
R
rebornix 已提交
158 159
}

R
rebornix 已提交
160 161 162 163 164 165 166 167 168 169
export interface INotebookCellDecorationOptions {
	className?: string;
	outputClassName?: string;
}

export interface INotebookDeltaDecoration {
	handle: number;
	options: INotebookCellDecorationOptions;
}

R
rebornix 已提交
170
export interface INotebookEditor extends IEditor {
R
rebornix 已提交
171

172 173
	cursorNavigationMode: boolean;

R
rebornix 已提交
174 175 176
	/**
	 * Notebook view model attached to the current editor
	 */
R
rebornix 已提交
177
	viewModel: NotebookViewModel | undefined;
R
rebornix 已提交
178

R
rebornix 已提交
179 180 181 182
	/**
	 * An event emitted when the model of this editor has changed.
	 * @event
	 */
R
rebornix 已提交
183 184
	readonly onDidChangeModel: Event<NotebookTextModel | undefined>;
	readonly onDidFocusEditorWidget: Event<void>;
185
	isNotebookEditor: boolean;
R
rebornix 已提交
186 187
	activeKernel: INotebookKernelInfo | INotebookKernelInfo2 | undefined;
	multipleKernelsAvailable: boolean;
R
rebornix 已提交
188
	readonly onDidChangeAvailableKernels: Event<void>;
R
rebornix 已提交
189
	readonly onDidChangeKernel: Event<void>;
190

191 192
	isDisposed: boolean;

R
rebornix 已提交
193
	getId(): string;
R
rebornix 已提交
194
	getDomNode(): HTMLElement;
R
rebornix 已提交
195 196
	getInnerWebview(): Webview | undefined;

R
rebornix 已提交
197 198 199
	/**
	 * Focus the notebook editor cell list
	 */
R
rebornix 已提交
200
	focus(): void;
R
rebornix 已提交
201

R
rebornix 已提交
202 203
	hasFocus(): boolean;

R
rebornix 已提交
204 205 206
	/**
	 * Select & focus cell
	 */
R
rebornix 已提交
207
	selectElement(cell: ICellViewModel): void;
R
rebornix 已提交
208

R
rebornix 已提交
209 210 211
	/**
	 * Layout info for the notebook editor
	 */
R
rebornix 已提交
212
	getLayoutInfo(): NotebookLayoutInfo;
R
rebornix 已提交
213 214 215
	/**
	 * Fetch the output renderers for notebook outputs.
	 */
R
rebornix 已提交
216
	getOutputRenderer(): OutputRenderer;
R
rebornix 已提交
217 218 219 220

	/**
	 * Insert a new cell around `cell`
	 */
R
rebornix 已提交
221
	insertNotebookCell(cell: ICellViewModel | undefined, type: CellKind, direction?: 'above' | 'below', initialText?: string, ui?: boolean): CellViewModel | null;
R
rebornix 已提交
222

K
kieferrm 已提交
223 224 225
	/**
	 * Split a given cell into multiple cells of the same type using the selection start positions.
	 */
226
	splitNotebookCell(cell: ICellViewModel): Promise<CellViewModel[] | null>;
K
kieferrm 已提交
227 228 229 230 231 232

	/**
	 * Joins the given cell either with the cell above or the one below depending on the given direction.
	 */
	joinNotebookCells(cell: ICellViewModel, direction: 'above' | 'below', constraint?: CellKind): Promise<ICellViewModel | null>;

R
rebornix 已提交
233 234 235
	/**
	 * Delete a cell from the notebook
	 */
236
	deleteNotebookCell(cell: ICellViewModel): Promise<boolean>;
R
rebornix 已提交
237

238 239 240
	/**
	 * Move a cell up one spot
	 */
241
	moveCellUp(cell: ICellViewModel): Promise<ICellViewModel | null>;
242 243 244 245

	/**
	 * Move a cell down one spot
	 */
246
	moveCellDown(cell: ICellViewModel): Promise<ICellViewModel | null>;
247

R
Rob Lourens 已提交
248
	/**
R
rebornix 已提交
249
	 * @deprecated Note that this method doesn't support batch operations, use #moveCellToIdx instead.
R
Rob Lourens 已提交
250 251
	 * Move a cell above or below another cell
	 */
252
	moveCell(cell: ICellViewModel, relativeToCell: ICellViewModel, direction: 'above' | 'below'): Promise<ICellViewModel | null>;
R
Rob Lourens 已提交
253

R
rebornix 已提交
254 255 256 257 258
	/**
	 * Move a cell to a specific position
	 */
	moveCellToIdx(cell: ICellViewModel, index: number): Promise<ICellViewModel | null>;

R
rebornix 已提交
259 260 261
	/**
	 * Focus the container of a cell (the monaco editor inside is not focused).
	 */
C
Christopher Maynard 已提交
262
	focusNotebookCell(cell: ICellViewModel, focus: 'editor' | 'container' | 'output'): void;
R
rebornix 已提交
263

264 265 266 267 268
	/**
	 * Execute the given notebook cell
	 */
	executeNotebookCell(cell: ICellViewModel): Promise<void>;

269 270 271 272 273 274 275 276 277 278 279 280 281 282 283
	/**
	 * Cancel the cell execution
	 */
	cancelNotebookCellExecution(cell: ICellViewModel): void;

	/**
	 * Executes all notebook cells in order
	 */
	executeNotebook(): Promise<void>;

	/**
	 * Cancel the notebook execution
	 */
	cancelNotebookExecution(): void;

R
rebornix 已提交
284 285 286
	/**
	 * Get current active cell
	 */
R
rebornix 已提交
287
	getActiveCell(): ICellViewModel | undefined;
R
rebornix 已提交
288 289 290 291

	/**
	 * Layout the cell with a new height
	 */
292
	layoutNotebookCell(cell: ICellViewModel, height: number): Promise<void>;
R
rebornix 已提交
293 294 295 296

	/**
	 * Render the output in webview layer
	 */
297
	createInset(cell: ICellViewModel, output: IProcessedOutput, shadowContent: string, offset: number): Promise<void>;
R
rebornix 已提交
298 299 300 301

	/**
	 * Remove the output from the webview layer
	 */
302
	removeInset(output: IProcessedOutput): void;
R
rebornix 已提交
303

304 305 306 307 308
	/**
	 * Hide the inset in the webview layer without removing it
	 */
	hideInset(output: IProcessedOutput): void;

309 310 311
	/**
	 * Send message to the webview for outputs.
	 */
312
	postMessage(forRendererId: string | undefined, message: any): void;
313

314 315 316 317 318 319 320 321 322 323 324 325 326 327
	/**
	 * Toggle class name on the notebook editor root DOM node.
	 */
	toggleClassName(className: string): void;

	/**
	 * Remove class name on the notebook editor root DOM node.
	 */
	addClassName(className: string): void;

	/**
	 * Remove class name on the notebook editor root DOM node.
	 */
	removeClassName(className: string): void;
328

R
rebornix 已提交
329 330
	deltaCellOutputContainerClassNames(cellId: string, added: string[], removed: string[]): void;

R
rebornix 已提交
331 332 333
	/**
	 * Trigger the editor to scroll from scroll event programmatically
	 */
334
	triggerScroll(event: IMouseWheelEvent): void;
R
rebornix 已提交
335 336 337 338

	/**
	 * Reveal cell into viewport.
	 */
R
rebornix 已提交
339
	revealInView(cell: ICellViewModel): void;
R
rebornix 已提交
340 341 342 343

	/**
	 * Reveal cell into viewport center.
	 */
R
rebornix 已提交
344
	revealInCenter(cell: ICellViewModel): void;
R
rebornix 已提交
345 346 347 348

	/**
	 * Reveal cell into viewport center if cell is currently out of the viewport.
	 */
R
rebornix 已提交
349
	revealInCenterIfOutsideViewport(cell: ICellViewModel): void;
R
rebornix 已提交
350 351 352 353

	/**
	 * Reveal a line in notebook cell into viewport with minimal scrolling.
	 */
R
rebornix 已提交
354
	revealLineInViewAsync(cell: ICellViewModel, line: number): Promise<void>;
R
rebornix 已提交
355

R
rebornix 已提交
356 357 358
	/**
	 * Reveal a line in notebook cell into viewport center.
	 */
R
rebornix 已提交
359
	revealLineInCenterAsync(cell: ICellViewModel, line: number): Promise<void>;
R
rebornix 已提交
360 361 362 363

	/**
	 * Reveal a line in notebook cell into viewport center.
	 */
R
rebornix 已提交
364
	revealLineInCenterIfOutsideViewportAsync(cell: ICellViewModel, line: number): Promise<void>;
R
rebornix 已提交
365

R
rebornix 已提交
366 367 368
	/**
	 * Reveal a range in notebook cell into viewport with minimal scrolling.
	 */
R
rebornix 已提交
369
	revealRangeInViewAsync(cell: ICellViewModel, range: Range): Promise<void>;
R
rebornix 已提交
370 371 372 373

	/**
	 * Reveal a range in notebook cell into viewport center.
	 */
R
rebornix 已提交
374
	revealRangeInCenterAsync(cell: ICellViewModel, range: Range): Promise<void>;
R
rebornix 已提交
375 376 377 378

	/**
	 * Reveal a range in notebook cell into viewport center.
	 */
R
rebornix 已提交
379
	revealRangeInCenterIfOutsideViewportAsync(cell: ICellViewModel, range: Range): Promise<void>;
R
rebornix 已提交
380

R
rebornix 已提交
381 382 383 384 385
	/**
	 * Set hidden areas on cell text models.
	 */
	setHiddenAreas(_ranges: ICellRange[]): boolean;

R
rebornix 已提交
386
	setCellSelection(cell: ICellViewModel, selection: Range): void;
R
rebornix 已提交
387

R
rebornix 已提交
388 389 390 391
	/**
	 * Change the decorations on cells.
	 * The notebook is virtualized and this method should be called to create/delete editor decorations safely.
	 */
R
rebornix 已提交
392
	changeModelDecorations<T>(callback: (changeAccessor: IModelDecorationsChangeAccessor) => T): T | null;
R
rebornix 已提交
393

R
rebornix 已提交
394 395 396 397 398 399 400 401 402 403 404
	/**
	 * An event emitted on a "mouseup".
	 * @event
	 */
	onMouseUp(listener: (e: INotebookEditorMouseEvent) => void): IDisposable;

	/**
	 * An event emitted on a "mousedown".
	 * @event
	 */
	onMouseDown(listener: (e: INotebookEditorMouseEvent) => void): IDisposable;
R
rebornix 已提交
405 406 407 408 409 410 411

	/**
	 * Get a contribution of this editor.
	 * @id Unique identifier of the contribution.
	 * @return The contribution or null if contribution not found.
	 */
	getContribution<T extends INotebookEditorContribution>(id: string): T;
R
rebornix 已提交
412 413
}

R
rebornix 已提交
414
export interface INotebookCellList {
R
rebornix 已提交
415
	isDisposed: boolean
416
	readonly contextKeyService: IContextKeyService;
R
Rob Lourens 已提交
417
	elementAt(position: number): ICellViewModel | undefined;
418
	elementHeight(element: ICellViewModel): number;
R
rebornix 已提交
419 420 421 422 423 424 425 426
	onWillScroll: Event<ScrollEvent>;
	onDidChangeFocus: Event<IListEvent<ICellViewModel>>;
	onDidChangeContentHeight: Event<number>;
	scrollTop: number;
	scrollHeight: number;
	scrollLeft: number;
	length: number;
	rowsContainer: HTMLElement;
427 428
	readonly onDidRemoveOutput: Event<IProcessedOutput>;
	readonly onDidHideOutput: Event<IProcessedOutput>;
R
rebornix 已提交
429 430
	readonly onMouseUp: Event<IListMouseEvent<CellViewModel>>;
	readonly onMouseDown: Event<IListMouseEvent<CellViewModel>>;
R
rebornix 已提交
431 432 433
	detachViewModel(): void;
	attachViewModel(viewModel: NotebookViewModel): void;
	clear(): void;
434
	getViewIndex(cell: ICellViewModel): number | undefined;
R
rebornix 已提交
435 436 437 438 439 440
	focusElement(element: ICellViewModel): void;
	selectElement(element: ICellViewModel): void;
	getFocusedElements(): ICellViewModel[];
	revealElementInView(element: ICellViewModel): void;
	revealElementInCenterIfOutsideViewport(element: ICellViewModel): void;
	revealElementInCenter(element: ICellViewModel): void;
R
rebornix 已提交
441 442 443 444 445 446
	revealElementLineInViewAsync(element: ICellViewModel, line: number): Promise<void>;
	revealElementLineInCenterAsync(element: ICellViewModel, line: number): Promise<void>;
	revealElementLineInCenterIfOutsideViewportAsync(element: ICellViewModel, line: number): Promise<void>;
	revealElementRangeInViewAsync(element: ICellViewModel, range: Range): Promise<void>;
	revealElementRangeInCenterAsync(element: ICellViewModel, range: Range): Promise<void>;
	revealElementRangeInCenterIfOutsideViewportAsync(element: ICellViewModel, range: Range): Promise<void>;
R
rebornix 已提交
447
	setHiddenAreas(_ranges: ICellRange[], triggerViewUpdate: boolean): boolean;
R
rebornix 已提交
448 449 450 451 452 453 454 455 456 457 458
	domElementOfElement(element: ICellViewModel): HTMLElement | null;
	focusView(): void;
	getAbsoluteTopOfElement(element: ICellViewModel): number;
	triggerScrollFromMouseWheelEvent(browserEvent: IMouseWheelEvent): void;
	updateElementHeight2(element: ICellViewModel, size: number): void;
	domFocus(): void;
	setCellSelection(element: ICellViewModel, range: Range): void;
	style(styles: IListStyles): void;
	updateOptions(options: IListOptions<ICellViewModel>): void;
	layout(height?: number, width?: number): void;
	dispose(): void;
459 460 461 462 463

	// TODO resolve differences between List<CellViewModel> and INotebookCellList<ICellViewModel>
	getFocus(): number[];
	setFocus(indexes: number[]): void;
	setSelection(indexes: number[]): void;
R
rebornix 已提交
464 465
}

466
export interface BaseCellRenderTemplate {
R
Rob Lourens 已提交
467 468
	editorPart: HTMLElement;
	collapsedPart: HTMLElement;
469
	expandButton: HTMLElement;
470
	contextKeyService: IContextKeyService;
R
rebornix 已提交
471 472
	container: HTMLElement;
	cellContainer: HTMLElement;
473
	toolbar: ToolBar;
474
	betweenCellToolbar: ToolBar;
475
	focusIndicatorLeft: HTMLElement;
476
	disposables: DisposableStore;
477
	elementDisposables: DisposableStore;
R
rebornix 已提交
478
	bottomCellContainer: HTMLElement;
R
Rob Lourens 已提交
479
	currentRenderedCell?: ICellViewModel;
480 481
	statusBarContainer: HTMLElement;
	languageStatusBarItem: CellLanguageStatusBarItem;
482
	titleMenu: IMenu;
R
rebornix 已提交
483
	toJSON: () => object;
484 485 486
}

export interface MarkdownCellRenderTemplate extends BaseCellRenderTemplate {
487
	editorContainer: HTMLElement;
R
rebornix 已提交
488
	foldingIndicator: HTMLElement;
R
Rob Lourens 已提交
489
	currentEditor?: ICodeEditor;
490 491 492
}

export interface CodeCellRenderTemplate extends BaseCellRenderTemplate {
493
	cellRunState: RunStateRenderer;
R
Rob Lourens 已提交
494
	cellStatusMessageContainer: HTMLElement;
495 496 497 498
	runToolbar: ToolBar;
	runButtonContainer: HTMLElement;
	executionOrderLabel: HTMLElement;
	outputContainer: HTMLElement;
499
	focusSinkElement: HTMLElement;
R
Rob Lourens 已提交
500
	editor: ICodeEditor;
501
	progressBar: ProgressBar;
502
	timer: TimerRenderer;
503 504 505 506 507 508
	focusIndicatorRight: HTMLElement;
	focusIndicatorBottom: HTMLElement;
}

export function isCodeCellRenderTemplate(templateData: BaseCellRenderTemplate): templateData is CodeCellRenderTemplate {
	return !!(templateData as CodeCellRenderTemplate).runToolbar;
R
rebornix 已提交
509
}
R
rebornix 已提交
510 511 512 513 514 515 516

export interface IOutputTransformContribution {
	/**
	 * Dispose this contribution.
	 */
	dispose(): void;

517
	render(output: IProcessedOutput, container: HTMLElement, preferredMimeType: string | undefined): IRenderOutput;
R
rebornix 已提交
518
}
R
rebornix 已提交
519 520 521 522 523 524

export interface CellFindMatch {
	cell: CellViewModel;
	matches: FindMatch[];
}

R
rebornix 已提交
525 526 527 528 529 530 531 532 533
export enum CellRevealType {
	Line,
	Range
}

export enum CellRevealPosition {
	Top,
	Center
}
R
rebornix 已提交
534

535
export enum CellEditState {
R
rebornix 已提交
536 537 538 539 540
	/**
	 * Default state.
	 * For markdown cell, it's Markdown preview.
	 * For code cell, the browser focus should be on the container instead of the editor
	 */
541
	Preview,
R
rebornix 已提交
542 543 544 545 546 547 548


	/**
	 * Eding mode. Source for markdown or code is rendered in editors and the state will be persistent.
	 */
	Editing
}
R
rebornix 已提交
549

R
Rob Lourens 已提交
550 551 552 553 554
export enum CellCollapseState {
	Normal,
	Collapsed
}

R
rebornix 已提交
555 556 557 558 559 560 561 562 563 564 565
export enum CellFocusMode {
	Container,
	Editor
}

export enum CursorAtBoundary {
	None,
	Top,
	Bottom,
	Both
}
R
rebornix 已提交
566

567 568 569 570 571 572
export interface CellViewModelStateChangeEvent {
	metadataChanged?: boolean;
	selectionChanged?: boolean;
	focusModeChanged?: boolean;
	editStateChanged?: boolean;
	languageChanged?: boolean;
R
rebornix 已提交
573 574
	foldingStateChanged?: boolean;
	contentChanged?: boolean;
575
	outputIsHoveredChanged?: boolean;
576 577
}

R
rebornix 已提交
578
/**
R
rebornix 已提交
579
 * [start, end]
R
rebornix 已提交
580 581
 */
export interface ICellRange {
R
rebornix 已提交
582 583 584
	/**
	 * zero based index
	 */
R
rebornix 已提交
585
	start: number;
R
rebornix 已提交
586

R
rebornix 已提交
587
	/**
R
rebornix 已提交
588
	 * zero based index
R
rebornix 已提交
589
	 */
R
rebornix 已提交
590
	end: number;
R
rebornix 已提交
591 592 593 594 595 596 597 598 599 600 601 602 603 604
}


/**
 * @param _ranges
 */
export function reduceCellRanges(_ranges: ICellRange[]): ICellRange[] {
	if (!_ranges.length) {
		return [];
	}

	let ranges = _ranges.sort((a, b) => a.start - b.start);
	let result: ICellRange[] = [];
	let currentRangeStart = ranges[0].start;
R
rebornix 已提交
605
	let currentRangeEnd = ranges[0].end + 1;
R
rebornix 已提交
606 607 608 609 610

	for (let i = 0, len = ranges.length; i < len; i++) {
		let range = ranges[i];

		if (range.start > currentRangeEnd) {
R
rebornix 已提交
611
			result.push({ start: currentRangeStart, end: currentRangeEnd - 1 });
R
rebornix 已提交
612
			currentRangeStart = range.start;
R
rebornix 已提交
613 614 615
			currentRangeEnd = range.end + 1;
		} else if (range.end + 1 > currentRangeEnd) {
			currentRangeEnd = range.end + 1;
R
rebornix 已提交
616 617 618
		}
	}

R
rebornix 已提交
619
	result.push({ start: currentRangeStart, end: currentRangeEnd - 1 });
R
rebornix 已提交
620 621 622 623 624 625 626 627 628 629
	return result;
}

export function getVisibleCells(cells: CellViewModel[], hiddenRanges: ICellRange[]) {
	if (!hiddenRanges.length) {
		return cells;
	}

	let start = 0;
	let hiddenRangeIndex = 0;
R
rebornix 已提交
630
	let result: CellViewModel[] = [];
R
rebornix 已提交
631 632 633 634 635 636

	while (start < cells.length && hiddenRangeIndex < hiddenRanges.length) {
		if (start < hiddenRanges[hiddenRangeIndex].start) {
			result.push(...cells.slice(start, hiddenRanges[hiddenRangeIndex].start));
		}

R
rebornix 已提交
637
		start = hiddenRanges[hiddenRangeIndex].end + 1;
R
rebornix 已提交
638 639 640 641 642 643 644 645 646
		hiddenRangeIndex++;
	}

	if (start < cells.length) {
		result.push(...cells.slice(start));
	}

	return result;
}
R
rebornix 已提交
647 648 649 650 651 652

export function getActiveNotebookEditor(editorService: IEditorService): INotebookEditor | undefined {
	// TODO can `isNotebookEditor` be on INotebookEditor to avoid a circular dependency?
	const activeEditorPane = editorService.activeEditorPane as unknown as { isNotebookEditor?: boolean } | undefined;
	return activeEditorPane?.isNotebookEditor ? (editorService.activeEditorPane?.getControl() as INotebookEditor) : undefined;
}