Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
microsoft
GitHub Repository: microsoft/vscode
Path: blob/main/src/vs/workbench/contrib/notebook/browser/notebookBrowser.ts
3296 views
1
/*---------------------------------------------------------------------------------------------
2
* Copyright (c) Microsoft Corporation. All rights reserved.
3
* Licensed under the MIT License. See License.txt in the project root for license information.
4
*--------------------------------------------------------------------------------------------*/
5
6
import { CodeWindow } from '../../../../base/browser/window.js';
7
import { CancellationToken } from '../../../../base/common/cancellation.js';
8
import { Event } from '../../../../base/common/event.js';
9
import { IDisposable } from '../../../../base/common/lifecycle.js';
10
import { URI } from '../../../../base/common/uri.js';
11
import { IEditorContributionDescription } from '../../../../editor/browser/editorExtensions.js';
12
import * as editorCommon from '../../../../editor/common/editorCommon.js';
13
import { FontInfo } from '../../../../editor/common/config/fontInfo.js';
14
import { IPosition } from '../../../../editor/common/core/position.js';
15
import { IRange, Range } from '../../../../editor/common/core/range.js';
16
import { Selection } from '../../../../editor/common/core/selection.js';
17
import { FindMatch, IModelDeltaDecoration, IReadonlyTextBuffer, ITextModel, TrackedRangeStickiness } from '../../../../editor/common/model.js';
18
import { MenuId } from '../../../../platform/actions/common/actions.js';
19
import { ITextEditorOptions, ITextResourceEditorInput } from '../../../../platform/editor/common/editor.js';
20
import { IConstructorSignature } from '../../../../platform/instantiation/common/instantiation.js';
21
import { IEditorPane, IEditorPaneWithSelection } from '../../../common/editor.js';
22
import { CellViewModelStateChangeEvent, NotebookCellStateChangedEvent, NotebookLayoutInfo } from './notebookViewEvents.js';
23
import { NotebookCellTextModel } from '../common/model/notebookCellTextModel.js';
24
import { NotebookTextModel } from '../common/model/notebookTextModel.js';
25
import { CellKind, ICellOutput, INotebookCellStatusBarItem, INotebookRendererInfo, INotebookFindOptions, IOrderedMimeType, NotebookCellInternalMetadata, NotebookCellMetadata, NOTEBOOK_EDITOR_ID, NOTEBOOK_DIFF_EDITOR_ID } from '../common/notebookCommon.js';
26
import { isCompositeNotebookEditorInput } from '../common/notebookEditorInput.js';
27
import { INotebookKernel } from '../common/notebookKernelService.js';
28
import { NotebookOptions } from './notebookOptions.js';
29
import { cellRangesToIndexes, ICellRange, reduceCellRanges } from '../common/notebookRange.js';
30
import { IWebviewElement } from '../../webview/browser/webview.js';
31
import { IEditorCommentsOptions, IEditorOptions } from '../../../../editor/common/config/editorOptions.js';
32
import { IContextKeyService } from '../../../../platform/contextkey/common/contextkey.js';
33
import { ICodeEditor } from '../../../../editor/browser/editorBrowser.js';
34
import { IObservable } from '../../../../base/common/observable.js';
35
import { INotebookTextDiffEditor } from './diff/notebookDiffEditorBrowser.js';
36
37
//#region Shared commands
38
export const EXPAND_CELL_INPUT_COMMAND_ID = 'notebook.cell.expandCellInput';
39
export const EXECUTE_CELL_COMMAND_ID = 'notebook.cell.execute';
40
export const DETECT_CELL_LANGUAGE = 'notebook.cell.detectLanguage';
41
export const CHANGE_CELL_LANGUAGE = 'notebook.cell.changeLanguage';
42
export const QUIT_EDIT_CELL_COMMAND_ID = 'notebook.cell.quitEdit';
43
export const EXPAND_CELL_OUTPUT_COMMAND_ID = 'notebook.cell.expandCellOutput';
44
45
46
//#endregion
47
48
//#region Notebook extensions
49
50
// Hardcoding viewType/extension ID for now. TODO these should be replaced once we can
51
// look them up in the marketplace dynamically.
52
export const IPYNB_VIEW_TYPE = 'jupyter-notebook';
53
export const JUPYTER_EXTENSION_ID = 'ms-toolsai.jupyter';
54
/** @deprecated use the notebookKernel<Type> "keyword" instead */
55
export const KERNEL_EXTENSIONS = new Map<string, string>([
56
[IPYNB_VIEW_TYPE, JUPYTER_EXTENSION_ID],
57
]);
58
// @TODO lramos15, place this in a similar spot to our normal recommendations.
59
export const KERNEL_RECOMMENDATIONS = new Map<string, Map<string, INotebookExtensionRecommendation>>();
60
KERNEL_RECOMMENDATIONS.set(IPYNB_VIEW_TYPE, new Map<string, INotebookExtensionRecommendation>());
61
KERNEL_RECOMMENDATIONS.get(IPYNB_VIEW_TYPE)?.set('python', {
62
extensionIds: [
63
'ms-python.python',
64
JUPYTER_EXTENSION_ID
65
],
66
displayName: 'Python + Jupyter',
67
});
68
69
export interface INotebookExtensionRecommendation {
70
readonly extensionIds: string[];
71
readonly displayName?: string;
72
}
73
74
//#endregion
75
76
//#region Output related types
77
78
// !! IMPORTANT !! ----------------------------------------------------------------------------------
79
// NOTE that you MUST update vs/workbench/contrib/notebook/browser/view/renderers/webviewPreloads.ts#L1986
80
// whenever changing the values of this const enum. The webviewPreloads-files manually inlines these values
81
// because it cannot have dependencies.
82
// !! IMPORTANT !! ----------------------------------------------------------------------------------
83
export const enum RenderOutputType {
84
Html = 0,
85
Extension = 1
86
}
87
88
export interface IRenderPlainHtmlOutput {
89
readonly type: RenderOutputType.Html;
90
readonly source: IDisplayOutputViewModel;
91
readonly htmlContent: string;
92
}
93
94
export interface IRenderOutputViaExtension {
95
readonly type: RenderOutputType.Extension;
96
readonly source: IDisplayOutputViewModel;
97
readonly mimeType: string;
98
readonly renderer: INotebookRendererInfo;
99
}
100
101
export type IInsetRenderOutput = IRenderPlainHtmlOutput | IRenderOutputViaExtension;
102
103
export interface ICellOutputViewModel extends IDisposable {
104
cellViewModel: IGenericCellViewModel;
105
/**
106
* When rendering an output, `model` should always be used as we convert legacy `text/error` output to `display_data` output under the hood.
107
*/
108
model: ICellOutput;
109
resolveMimeTypes(textModel: NotebookTextModel, kernelProvides: readonly string[] | undefined): [readonly IOrderedMimeType[], number];
110
pickedMimeType: IOrderedMimeType | undefined;
111
hasMultiMimeType(): boolean;
112
readonly onDidResetRenderer: Event<void>;
113
readonly visible: IObservable<boolean>;
114
setVisible(visible: boolean, force?: boolean): void;
115
resetRenderer(): void;
116
toRawJSON(): any;
117
}
118
119
export interface IDisplayOutputViewModel extends ICellOutputViewModel {
120
resolveMimeTypes(textModel: NotebookTextModel, kernelProvides: readonly string[] | undefined): [readonly IOrderedMimeType[], number];
121
}
122
123
124
//#endregion
125
126
//#region Shared types between the Notebook Editor and Notebook Diff Editor, they are mostly used for output rendering
127
128
export interface IGenericCellViewModel {
129
id: string;
130
handle: number;
131
uri: URI;
132
metadata: NotebookCellMetadata;
133
outputIsHovered: boolean;
134
outputIsFocused: boolean;
135
inputInOutputIsFocused: boolean;
136
outputsViewModels: ICellOutputViewModel[];
137
getOutputOffset(index: number): number;
138
updateOutputHeight(index: number, height: number, source?: string): void;
139
}
140
141
export interface IDisplayOutputLayoutUpdateRequest {
142
readonly cell: IGenericCellViewModel;
143
output: IDisplayOutputViewModel;
144
cellTop: number;
145
outputOffset: number;
146
forceDisplay: boolean;
147
}
148
149
export interface ICommonCellInfo {
150
readonly cellId: string;
151
readonly cellHandle: number;
152
readonly cellUri: URI;
153
readonly executionId?: string;
154
}
155
156
export enum ScrollToRevealBehavior {
157
fullCell,
158
firstLine
159
}
160
161
export interface IFocusNotebookCellOptions {
162
readonly skipReveal?: boolean;
163
readonly focusEditorLine?: number;
164
readonly revealBehavior?: ScrollToRevealBehavior | undefined;
165
readonly outputId?: string;
166
readonly altOutputId?: string;
167
readonly outputWebviewFocused?: boolean;
168
}
169
170
//#endregion
171
172
export enum CellLayoutState {
173
Uninitialized,
174
Estimated,
175
FromCache,
176
Measured
177
}
178
179
/** LayoutInfo of the parts that are shared between all cell types. */
180
export interface CellLayoutInfo {
181
readonly layoutState: CellLayoutState;
182
readonly fontInfo: FontInfo | null;
183
readonly chatHeight: number;
184
readonly editorWidth: number;
185
readonly editorHeight: number;
186
readonly statusBarHeight: number;
187
readonly commentOffset: number;
188
readonly commentHeight: number;
189
readonly bottomToolbarOffset: number;
190
readonly totalHeight: number;
191
}
192
193
export interface CellLayoutChangeEvent {
194
readonly font?: FontInfo;
195
readonly outerWidth?: number;
196
readonly commentHeight?: boolean;
197
}
198
199
export interface CodeCellLayoutInfo extends CellLayoutInfo {
200
readonly estimatedHasHorizontalScrolling: boolean;
201
readonly outputContainerOffset: number;
202
readonly outputTotalHeight: number;
203
readonly outputShowMoreContainerHeight: number;
204
readonly outputShowMoreContainerOffset: number;
205
readonly codeIndicatorHeight: number;
206
readonly outputIndicatorHeight: number;
207
}
208
209
export interface CodeCellLayoutChangeEvent extends CellLayoutChangeEvent {
210
readonly source?: string;
211
readonly chatHeight?: boolean;
212
readonly editorHeight?: boolean;
213
readonly outputHeight?: boolean;
214
readonly outputShowMoreContainerHeight?: number;
215
readonly totalHeight?: boolean;
216
}
217
218
export interface MarkupCellLayoutInfo extends CellLayoutInfo {
219
readonly previewHeight: number;
220
readonly foldHintHeight: number;
221
}
222
223
export enum CellLayoutContext {
224
Fold
225
}
226
227
export interface MarkupCellLayoutChangeEvent extends CellLayoutChangeEvent {
228
readonly editorHeight?: number;
229
readonly previewHeight?: number;
230
totalHeight?: number;
231
readonly context?: CellLayoutContext;
232
}
233
234
export interface ICommonCellViewModelLayoutChangeInfo {
235
readonly totalHeight?: boolean | number;
236
readonly outerWidth?: number;
237
readonly context?: CellLayoutContext;
238
}
239
export interface ICellViewModel extends IGenericCellViewModel {
240
readonly model: NotebookCellTextModel;
241
readonly id: string;
242
readonly textBuffer: IReadonlyTextBuffer;
243
readonly layoutInfo: CellLayoutInfo;
244
readonly onDidChangeLayout: Event<ICommonCellViewModelLayoutChangeInfo>;
245
readonly onDidChangeCellStatusBarItems: Event<void>;
246
readonly onCellDecorationsChanged: Event<{ added: INotebookCellDecorationOptions[]; removed: INotebookCellDecorationOptions[] }>;
247
readonly onDidChangeState: Event<CellViewModelStateChangeEvent>;
248
readonly onDidChangeEditorAttachState: Event<void>;
249
readonly editStateSource: string;
250
readonly editorAttached: boolean;
251
isInputCollapsed: boolean;
252
isOutputCollapsed: boolean;
253
dragging: boolean;
254
handle: number;
255
uri: URI;
256
language: string;
257
readonly mime: string;
258
cellKind: CellKind;
259
lineNumbers: 'on' | 'off' | 'inherit';
260
commentOptions: IEditorCommentsOptions;
261
chatHeight: number;
262
commentHeight: number;
263
focusMode: CellFocusMode;
264
focusedOutputId?: string | undefined;
265
outputIsHovered: boolean;
266
getText(): string;
267
getAlternativeId(): number;
268
getTextLength(): number;
269
getHeight(lineHeight: number): number;
270
metadata: NotebookCellMetadata;
271
internalMetadata: NotebookCellInternalMetadata;
272
textModel: ITextModel | undefined;
273
hasModel(): this is IEditableCellViewModel;
274
resolveTextModel(): Promise<ITextModel>;
275
getSelections(): Selection[];
276
setSelections(selections: Selection[]): void;
277
getSelectionsStartPosition(): IPosition[] | undefined;
278
getCellDecorations(): INotebookCellDecorationOptions[];
279
getCellStatusBarItems(): INotebookCellStatusBarItem[];
280
getEditState(): CellEditState;
281
updateEditState(state: CellEditState, source: string): void;
282
deltaModelDecorations(oldDecorations: readonly string[], newDecorations: readonly IModelDeltaDecoration[]): string[];
283
getCellDecorationRange(id: string): Range | null;
284
enableAutoLanguageDetection(): void;
285
}
286
287
export interface IEditableCellViewModel extends ICellViewModel {
288
textModel: ITextModel;
289
}
290
291
export interface INotebookEditorMouseEvent {
292
readonly event: MouseEvent;
293
readonly target: ICellViewModel;
294
}
295
296
export interface INotebookEditorContribution {
297
/**
298
* Dispose this contribution.
299
*/
300
dispose(): void;
301
/**
302
* Store view state.
303
*/
304
saveViewState?(): unknown;
305
/**
306
* Restore view state.
307
*/
308
restoreViewState?(state: unknown): void;
309
}
310
311
/**
312
* Vertical Lane in the overview ruler of the notebook editor.
313
*/
314
export enum NotebookOverviewRulerLane {
315
Left = 1,
316
Center = 2,
317
Right = 4,
318
Full = 7
319
}
320
321
export interface INotebookCellDecorationOptions {
322
className?: string;
323
gutterClassName?: string;
324
outputClassName?: string;
325
topClassName?: string;
326
overviewRuler?: {
327
color: string;
328
modelRanges: IRange[];
329
includeOutput: boolean;
330
position: NotebookOverviewRulerLane;
331
};
332
}
333
334
export interface INotebookViewZoneDecorationOptions {
335
overviewRuler?: {
336
color: string;
337
position: NotebookOverviewRulerLane;
338
};
339
}
340
341
export interface INotebookDeltaCellDecoration {
342
readonly handle: number;
343
readonly options: INotebookCellDecorationOptions;
344
}
345
346
export interface INotebookDeltaViewZoneDecoration {
347
readonly viewZoneId: string;
348
readonly options: INotebookViewZoneDecorationOptions;
349
}
350
351
export function isNotebookCellDecoration(obj: unknown): obj is INotebookDeltaCellDecoration {
352
return !!obj && typeof (obj as INotebookDeltaCellDecoration).handle === 'number';
353
}
354
355
export function isNotebookViewZoneDecoration(obj: unknown): obj is INotebookDeltaViewZoneDecoration {
356
return !!obj && typeof (obj as INotebookDeltaViewZoneDecoration).viewZoneId === 'string';
357
}
358
359
export type INotebookDeltaDecoration = INotebookDeltaCellDecoration | INotebookDeltaViewZoneDecoration;
360
361
export interface INotebookDeltaCellStatusBarItems {
362
readonly handle: number;
363
readonly items: readonly INotebookCellStatusBarItem[];
364
}
365
366
export const enum CellRevealType {
367
Default = 1,
368
Top = 2,
369
Center = 3,
370
CenterIfOutsideViewport = 4,
371
NearTopIfOutsideViewport = 5,
372
FirstLineIfOutsideViewport = 6
373
}
374
375
export enum CellRevealRangeType {
376
Default = 1,
377
Center = 2,
378
CenterIfOutsideViewport = 3,
379
}
380
381
export interface INotebookEditorOptions extends ITextEditorOptions {
382
readonly cellOptions?: ITextResourceEditorInput;
383
readonly cellRevealType?: CellRevealType;
384
readonly cellSelections?: ICellRange[];
385
readonly isReadOnly?: boolean;
386
readonly viewState?: INotebookEditorViewState;
387
readonly indexedCellOptions?: { index: number; selection?: IRange };
388
readonly label?: string;
389
}
390
391
export type INotebookEditorContributionCtor = IConstructorSignature<INotebookEditorContribution, [INotebookEditor]>;
392
393
export interface INotebookEditorContributionDescription {
394
id: string;
395
ctor: INotebookEditorContributionCtor;
396
}
397
398
export interface INotebookEditorCreationOptions {
399
readonly isReplHistory?: boolean;
400
readonly isReadOnly?: boolean;
401
readonly contributions?: INotebookEditorContributionDescription[];
402
readonly cellEditorContributions?: IEditorContributionDescription[];
403
readonly menuIds: {
404
notebookToolbar: MenuId;
405
cellTitleToolbar: MenuId;
406
cellDeleteToolbar: MenuId;
407
cellInsertToolbar: MenuId;
408
cellTopInsertToolbar: MenuId;
409
cellExecuteToolbar: MenuId;
410
cellExecutePrimary?: MenuId;
411
};
412
readonly options?: NotebookOptions;
413
readonly codeWindow?: CodeWindow;
414
}
415
416
export interface INotebookWebviewMessage {
417
readonly message: unknown;
418
}
419
420
//#region Notebook View Model
421
export interface INotebookEditorViewState {
422
editingCells: { [key: number]: boolean };
423
collapsedInputCells: { [key: number]: boolean };
424
collapsedOutputCells: { [key: number]: boolean };
425
cellLineNumberStates: { [key: number]: 'on' | 'off' };
426
editorViewStates: { [key: number]: editorCommon.ICodeEditorViewState | null };
427
hiddenFoldingRanges?: ICellRange[];
428
cellTotalHeights?: { [key: number]: number };
429
scrollPosition?: { left: number; top: number };
430
focus?: number;
431
editorFocused?: boolean;
432
contributionsState?: { [id: string]: unknown };
433
selectedKernelId?: string;
434
}
435
436
export interface ICellModelDecorations {
437
readonly ownerId: number;
438
readonly decorations: readonly string[];
439
}
440
441
export interface ICellModelDeltaDecorations {
442
readonly ownerId: number;
443
readonly decorations: readonly IModelDeltaDecoration[];
444
}
445
446
export interface IModelDecorationsChangeAccessor {
447
deltaDecorations(oldDecorations: ICellModelDecorations[], newDecorations: ICellModelDeltaDecorations[]): ICellModelDecorations[];
448
}
449
450
export interface INotebookViewZone {
451
/**
452
* Use 0 to place a view zone before the first cell
453
*/
454
afterModelPosition: number;
455
domNode: HTMLElement;
456
457
heightInPx: number;
458
}
459
460
export interface INotebookViewZoneChangeAccessor {
461
addZone(zone: INotebookViewZone): string;
462
removeZone(id: string): void;
463
layoutZone(id: string): void;
464
}
465
466
export interface INotebookCellOverlay {
467
cell: ICellViewModel;
468
domNode: HTMLElement;
469
}
470
471
export interface INotebookCellOverlayChangeAccessor {
472
addOverlay(overlay: INotebookCellOverlay): string;
473
removeOverlay(id: string): void;
474
layoutOverlay(id: string): void;
475
}
476
477
export type NotebookViewCellsSplice = [
478
number /* start */,
479
number /* delete count */,
480
ICellViewModel[]
481
];
482
483
export interface INotebookViewCellsUpdateEvent {
484
readonly synchronous: boolean;
485
readonly splices: readonly NotebookViewCellsSplice[];
486
}
487
488
export interface INotebookViewModel {
489
notebookDocument: NotebookTextModel;
490
readonly viewCells: ICellViewModel[];
491
layoutInfo: NotebookLayoutInfo | null;
492
viewType: string;
493
onDidChangeViewCells: Event<INotebookViewCellsUpdateEvent>;
494
onDidChangeSelection: Event<string>;
495
onDidFoldingStateChanged: Event<void>;
496
getNearestVisibleCellIndexUpwards(index: number): number;
497
getTrackedRange(id: string): ICellRange | null;
498
setTrackedRange(id: string | null, newRange: ICellRange | null, newStickiness: TrackedRangeStickiness): string | null;
499
getOverviewRulerDecorations(): INotebookDeltaViewZoneDecoration[];
500
getSelections(): ICellRange[];
501
getCellIndex(cell: ICellViewModel): number;
502
getMostRecentlyExecutedCell(): ICellViewModel | undefined;
503
deltaCellStatusBarItems(oldItems: string[], newItems: INotebookDeltaCellStatusBarItems[]): string[];
504
getFoldedLength(index: number): number;
505
getFoldingStartIndex(index: number): number;
506
replaceOne(cell: ICellViewModel, range: Range, text: string): Promise<void>;
507
replaceAll(matches: CellFindMatchWithIndex[], texts: string[]): Promise<void>;
508
}
509
//#endregion
510
511
export interface INotebookEditor {
512
//#region Eventing
513
readonly onDidChangeCellState: Event<NotebookCellStateChangedEvent>;
514
readonly onDidChangeViewCells: Event<INotebookViewCellsUpdateEvent>;
515
readonly onDidChangeVisibleRanges: Event<void>;
516
readonly onDidChangeSelection: Event<void>;
517
readonly onDidChangeFocus: Event<void>;
518
/**
519
* An event emitted when the model of this editor has changed.
520
*/
521
readonly onDidChangeModel: Event<NotebookTextModel | undefined>;
522
readonly onDidAttachViewModel: Event<void>;
523
readonly onDidFocusWidget: Event<void>;
524
readonly onDidBlurWidget: Event<void>;
525
readonly onDidScroll: Event<void>;
526
readonly onDidChangeLayout: Event<void>;
527
readonly onDidChangeActiveCell: Event<void>;
528
readonly onDidChangeActiveEditor: Event<INotebookEditor>;
529
readonly onDidChangeActiveKernel: Event<void>;
530
readonly onMouseUp: Event<INotebookEditorMouseEvent>;
531
readonly onMouseDown: Event<INotebookEditorMouseEvent>;
532
//#endregion
533
534
//#region readonly properties
535
readonly visibleRanges: ICellRange[];
536
readonly textModel?: NotebookTextModel;
537
readonly isVisible: boolean;
538
readonly isReadOnly: boolean;
539
readonly notebookOptions: NotebookOptions;
540
readonly isDisposed: boolean;
541
readonly activeKernel: INotebookKernel | undefined;
542
readonly scrollTop: number;
543
readonly scrollBottom: number;
544
readonly scopedContextKeyService: IContextKeyService;
545
/**
546
* Required for Composite Editor check. The interface should not be changed.
547
*/
548
readonly activeCodeEditor: ICodeEditor | undefined;
549
readonly codeEditors: [ICellViewModel, ICodeEditor][];
550
readonly activeCellAndCodeEditor: [ICellViewModel, ICodeEditor] | undefined;
551
//#endregion
552
553
getLength(): number;
554
getSelections(): ICellRange[];
555
setSelections(selections: ICellRange[]): void;
556
getFocus(): ICellRange;
557
setFocus(focus: ICellRange): void;
558
getId(): string;
559
560
getViewModel(): INotebookViewModel | undefined;
561
hasModel(): this is IActiveNotebookEditor;
562
dispose(): void;
563
getDomNode(): HTMLElement;
564
getInnerWebview(): IWebviewElement | undefined;
565
getSelectionViewModels(): ICellViewModel[];
566
getEditorViewState(): INotebookEditorViewState;
567
restoreListViewState(viewState: INotebookEditorViewState | undefined): void;
568
569
getBaseCellEditorOptions(language: string): IBaseCellEditorOptions;
570
571
/**
572
* Focus the active cell in notebook cell list
573
*/
574
focus(): void;
575
576
/**
577
* Focus the notebook cell list container
578
*/
579
focusContainer(clearSelection?: boolean): void;
580
581
hasEditorFocus(): boolean;
582
hasWebviewFocus(): boolean;
583
584
hasOutputTextSelection(): boolean;
585
setOptions(options: INotebookEditorOptions | undefined): Promise<void>;
586
587
/**
588
* Select & focus cell
589
*/
590
focusElement(cell: ICellViewModel): void;
591
592
/**
593
* Layout info for the notebook editor
594
*/
595
getLayoutInfo(): NotebookLayoutInfo;
596
597
getVisibleRangesPlusViewportAboveAndBelow(): ICellRange[];
598
599
/**
600
* Focus the container of a cell (the monaco editor inside is not focused).
601
*/
602
focusNotebookCell(cell: ICellViewModel, focus: 'editor' | 'container' | 'output', options?: IFocusNotebookCellOptions): Promise<void>;
603
604
/**
605
* Execute the given notebook cells
606
*/
607
executeNotebookCells(cells?: Iterable<ICellViewModel>): Promise<void>;
608
609
/**
610
* Cancel the given notebook cells
611
*/
612
cancelNotebookCells(cells?: Iterable<ICellViewModel>): Promise<void>;
613
614
/**
615
* Get current active cell
616
*/
617
getActiveCell(): ICellViewModel | undefined;
618
619
/**
620
* Layout the cell with a new height
621
*/
622
layoutNotebookCell(cell: ICellViewModel, height: number): Promise<void>;
623
624
/**
625
* Render the output in webview layer
626
*/
627
createOutput(cell: ICellViewModel, output: IInsetRenderOutput, offset: number, createWhenIdle: boolean): Promise<void>;
628
629
/**
630
* Update the output in webview layer with latest content. It will delegate to `createOutput` is the output is not rendered yet
631
*/
632
updateOutput(cell: ICellViewModel, output: IInsetRenderOutput, offset: number): Promise<void>;
633
634
/**
635
* Copy the image in the specific cell output to the clipboard
636
*/
637
copyOutputImage(cellOutput: ICellOutputViewModel): Promise<void>;
638
/**
639
* Select the contents of the first focused output of the cell.
640
* Implementation of Ctrl+A for an output item.
641
*/
642
selectOutputContent(cell: ICellViewModel): void;
643
/**
644
* Select the active input element of the first focused output of the cell.
645
* Implementation of Ctrl+A for an input element in an output item.
646
*/
647
selectInputContents(cell: ICellViewModel): void;
648
649
readonly onDidReceiveMessage: Event<INotebookWebviewMessage>;
650
651
/**
652
* Send message to the webview for outputs.
653
*/
654
postMessage(message: any): void;
655
656
/**
657
* Remove class name on the notebook editor root DOM node.
658
*/
659
addClassName(className: string): void;
660
661
/**
662
* Remove class name on the notebook editor root DOM node.
663
*/
664
removeClassName(className: string): void;
665
666
/**
667
* Set scrollTop value of the notebook editor.
668
*/
669
setScrollTop(scrollTop: number): void;
670
671
/**
672
* The range will be revealed with as little scrolling as possible.
673
*/
674
revealCellRangeInView(range: ICellRange): void;
675
676
/**
677
* Reveal cell into viewport.
678
*/
679
revealInView(cell: ICellViewModel): Promise<void>;
680
681
/**
682
* Reveal cell into the top of viewport.
683
*/
684
revealInViewAtTop(cell: ICellViewModel): void;
685
686
/**
687
* Reveal cell into viewport center.
688
*/
689
revealInCenter(cell: ICellViewModel): void;
690
691
/**
692
* Reveal cell into viewport center if cell is currently out of the viewport.
693
*/
694
revealInCenterIfOutsideViewport(cell: ICellViewModel): Promise<void>;
695
696
/**
697
* Reveal the first line of the cell into the view if the cell is outside of the viewport.
698
*/
699
revealFirstLineIfOutsideViewport(cell: ICellViewModel): Promise<void>;
700
701
/**
702
* Reveal a line in notebook cell into viewport with minimal scrolling.
703
*/
704
revealLineInViewAsync(cell: ICellViewModel, line: number): Promise<void>;
705
706
/**
707
* Reveal a line in notebook cell into viewport center.
708
*/
709
revealLineInCenterAsync(cell: ICellViewModel, line: number): Promise<void>;
710
711
/**
712
* Reveal a line in notebook cell into viewport center.
713
*/
714
revealLineInCenterIfOutsideViewportAsync(cell: ICellViewModel, line: number): Promise<void>;
715
716
/**
717
* Reveal a range in notebook cell into viewport with minimal scrolling.
718
*/
719
revealRangeInViewAsync(cell: ICellViewModel, range: Selection | Range): Promise<void>;
720
721
/**
722
* Reveal a range in notebook cell into viewport center.
723
*/
724
revealRangeInCenterAsync(cell: ICellViewModel, range: Selection | Range): Promise<void>;
725
726
/**
727
* Reveal a range in notebook cell into viewport center.
728
*/
729
revealRangeInCenterIfOutsideViewportAsync(cell: ICellViewModel, range: Selection | Range): Promise<void>;
730
731
/**
732
* Reveal a position with `offset` in a cell into viewport center.
733
*/
734
revealCellOffsetInCenter(cell: ICellViewModel, offset: number): void;
735
736
/**
737
* Reveal `offset` in the list view into viewport center if it is outside of the viewport.
738
*/
739
revealOffsetInCenterIfOutsideViewport(offset: number): void;
740
741
/**
742
* Convert the view range to model range
743
* @param startIndex Inclusive
744
* @param endIndex Exclusive
745
*/
746
getCellRangeFromViewRange(startIndex: number, endIndex: number): ICellRange | undefined;
747
748
/**
749
* Set hidden areas on cell text models.
750
*/
751
setHiddenAreas(_ranges: ICellRange[]): boolean;
752
753
/**
754
* Set selectiosn on the text editor attached to the cell
755
*/
756
757
setCellEditorSelection(cell: ICellViewModel, selection: Range): void;
758
759
/**
760
*Change the decorations on the notebook cell list
761
*/
762
763
deltaCellDecorations(oldDecorations: string[], newDecorations: INotebookDeltaDecoration[]): string[];
764
765
/**
766
* Change the decorations on cell editors.
767
* The notebook is virtualized and this method should be called to create/delete editor decorations safely.
768
*/
769
changeModelDecorations<T>(callback: (changeAccessor: IModelDecorationsChangeAccessor) => T): T | null;
770
771
changeViewZones(callback: (accessor: INotebookViewZoneChangeAccessor) => void): void;
772
773
changeCellOverlays(callback: (accessor: INotebookCellOverlayChangeAccessor) => void): void;
774
775
getViewZoneLayoutInfo(id: string): { top: number; height: number } | null;
776
777
/**
778
* Get a contribution of this editor.
779
* @id Unique identifier of the contribution.
780
* @return The contribution or null if contribution not found.
781
*/
782
getContribution<T extends INotebookEditorContribution>(id: string): T;
783
784
/**
785
* Get the view index of a cell at model `index`
786
*/
787
getViewIndexByModelIndex(index: number): number;
788
getCellsInRange(range?: ICellRange): ReadonlyArray<ICellViewModel>;
789
cellAt(index: number): ICellViewModel | undefined;
790
getCellByHandle(handle: number): ICellViewModel | undefined;
791
getCellIndex(cell: ICellViewModel): number | undefined;
792
getNextVisibleCellIndex(index: number): number | undefined;
793
getPreviousVisibleCellIndex(index: number): number | undefined;
794
find(query: string, options: INotebookFindOptions, token: CancellationToken, skipWarmup?: boolean, shouldGetSearchPreviewInfo?: boolean, ownerID?: string): Promise<CellFindMatchWithIndex[]>;
795
findHighlightCurrent(matchIndex: number, ownerID?: string): Promise<number>;
796
findUnHighlightCurrent(matchIndex: number, ownerID?: string): Promise<void>;
797
findStop(ownerID?: string): void;
798
showProgress(): void;
799
hideProgress(): void;
800
801
getAbsoluteTopOfElement(cell: ICellViewModel): number;
802
getAbsoluteBottomOfElement(cell: ICellViewModel): number;
803
getHeightOfElement(cell: ICellViewModel): number;
804
}
805
806
export interface IActiveNotebookEditor extends INotebookEditor {
807
getViewModel(): INotebookViewModel;
808
textModel: NotebookTextModel;
809
getFocus(): ICellRange;
810
cellAt(index: number): ICellViewModel;
811
getCellIndex(cell: ICellViewModel): number;
812
getNextVisibleCellIndex(index: number): number;
813
}
814
815
export interface INotebookEditorPane extends IEditorPaneWithSelection {
816
getControl(): INotebookEditor | undefined;
817
readonly onDidChangeModel: Event<void>;
818
textModel: NotebookTextModel | undefined;
819
}
820
821
export interface IBaseCellEditorOptions extends IDisposable {
822
readonly value: IEditorOptions;
823
readonly onDidChange: Event<void>;
824
}
825
826
/**
827
* A mix of public interface and internal one (used by internal rendering code, e.g., cellRenderer)
828
*/
829
export interface INotebookEditorDelegate extends INotebookEditor {
830
hasModel(): this is IActiveNotebookEditorDelegate;
831
832
readonly creationOptions: INotebookEditorCreationOptions;
833
readonly onDidChangeOptions: Event<void>;
834
readonly onDidChangeDecorations: Event<void>;
835
createMarkupPreview(cell: ICellViewModel): Promise<void>;
836
unhideMarkupPreviews(cells: readonly ICellViewModel[]): Promise<void>;
837
hideMarkupPreviews(cells: readonly ICellViewModel[]): Promise<void>;
838
839
/**
840
* Remove the output from the webview layer
841
*/
842
removeInset(output: IDisplayOutputViewModel): void;
843
844
/**
845
* Hide the inset in the webview layer without removing it
846
*/
847
hideInset(output: IDisplayOutputViewModel): void;
848
deltaCellContainerClassNames(cellId: string, added: string[], removed: string[], cellKind: CellKind): void;
849
}
850
851
export interface IActiveNotebookEditorDelegate extends INotebookEditorDelegate {
852
getViewModel(): INotebookViewModel;
853
textModel: NotebookTextModel;
854
getFocus(): ICellRange;
855
cellAt(index: number): ICellViewModel;
856
getCellIndex(cell: ICellViewModel): number;
857
getNextVisibleCellIndex(index: number): number;
858
}
859
860
export interface ISearchPreviewInfo {
861
line: string;
862
range: {
863
start: number;
864
end: number;
865
};
866
}
867
868
export interface CellWebviewFindMatch {
869
readonly index: number;
870
readonly searchPreviewInfo?: ISearchPreviewInfo;
871
}
872
873
export type CellContentFindMatch = FindMatch;
874
875
export interface CellFindMatch {
876
cell: ICellViewModel;
877
contentMatches: CellContentFindMatch[];
878
}
879
880
export interface CellFindMatchWithIndex {
881
cell: ICellViewModel;
882
index: number;
883
length: number;
884
getMatch(index: number): FindMatch | CellWebviewFindMatch;
885
contentMatches: FindMatch[];
886
webviewMatches: CellWebviewFindMatch[];
887
}
888
889
export enum CellEditState {
890
/**
891
* Default state.
892
* For markup cells, this is the renderer version of the markup.
893
* For code cell, the browser focus should be on the container instead of the editor
894
*/
895
Preview,
896
897
/**
898
* Editing mode. Source for markup or code is rendered in editors and the state will be persistent.
899
*/
900
Editing
901
}
902
903
export enum CellFocusMode {
904
Container,
905
Editor,
906
Output,
907
ChatInput
908
}
909
910
export enum CursorAtBoundary {
911
None,
912
Top,
913
Bottom,
914
Both
915
}
916
917
export enum CursorAtLineBoundary {
918
None,
919
Start,
920
End,
921
Both
922
}
923
924
export function getNotebookEditorFromEditorPane(editorPane?: IEditorPane): INotebookEditor | undefined {
925
if (!editorPane) {
926
return;
927
}
928
929
if (editorPane.getId() === NOTEBOOK_EDITOR_ID) {
930
return editorPane.getControl() as INotebookEditor | undefined;
931
}
932
933
if (editorPane.getId() === NOTEBOOK_DIFF_EDITOR_ID) {
934
return (editorPane.getControl() as INotebookTextDiffEditor).inlineNotebookEditor;
935
}
936
937
const input = editorPane.input;
938
939
const isCompositeNotebook = input && isCompositeNotebookEditorInput(input);
940
941
if (isCompositeNotebook) {
942
return (editorPane.getControl() as { notebookEditor: INotebookEditor | undefined } | undefined)?.notebookEditor;
943
}
944
945
return undefined;
946
}
947
948
/**
949
* ranges: model selections
950
* this will convert model selections to view indexes first, and then include the hidden ranges in the list view
951
*/
952
export function expandCellRangesWithHiddenCells(editor: INotebookEditor, ranges: ICellRange[]) {
953
// assuming ranges are sorted and no overlap
954
const indexes = cellRangesToIndexes(ranges);
955
const modelRanges: ICellRange[] = [];
956
indexes.forEach(index => {
957
const viewCell = editor.cellAt(index);
958
959
if (!viewCell) {
960
return;
961
}
962
963
const viewIndex = editor.getViewIndexByModelIndex(index);
964
if (viewIndex < 0) {
965
return;
966
}
967
968
const nextViewIndex = viewIndex + 1;
969
const range = editor.getCellRangeFromViewRange(viewIndex, nextViewIndex);
970
971
if (range) {
972
modelRanges.push(range);
973
}
974
});
975
976
return reduceCellRanges(modelRanges);
977
}
978
979
export function cellRangeToViewCells(editor: IActiveNotebookEditor, ranges: ICellRange[]) {
980
const cells: ICellViewModel[] = [];
981
reduceCellRanges(ranges).forEach(range => {
982
cells.push(...editor.getCellsInRange(range));
983
});
984
985
return cells;
986
}
987
988
//#region Cell Folding
989
export const enum CellFoldingState {
990
None,
991
Expanded,
992
Collapsed
993
}
994
995
export interface EditorFoldingStateDelegate {
996
getCellIndex(cell: ICellViewModel): number;
997
getFoldingState(index: number): CellFoldingState;
998
}
999
//#endregion
1000
1001