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
5245 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
readonly topMargin: number;
192
readonly bottomMargin: number;
193
readonly outlineWidth: number;
194
}
195
196
export interface CellLayoutChangeEvent {
197
readonly font?: FontInfo;
198
readonly outerWidth?: number;
199
readonly commentHeight?: boolean;
200
}
201
202
export interface CodeCellLayoutInfo extends CellLayoutInfo {
203
readonly estimatedHasHorizontalScrolling: boolean;
204
readonly outputContainerOffset: number;
205
readonly outputTotalHeight: number;
206
readonly outputShowMoreContainerHeight: number;
207
readonly outputShowMoreContainerOffset: number;
208
readonly codeIndicatorHeight: number;
209
readonly outputIndicatorHeight: number;
210
}
211
212
export interface CodeCellLayoutChangeEvent extends CellLayoutChangeEvent {
213
readonly source?: string;
214
readonly chatHeight?: boolean;
215
readonly editorHeight?: boolean;
216
readonly outputHeight?: boolean;
217
readonly outputShowMoreContainerHeight?: number;
218
readonly totalHeight?: boolean;
219
}
220
221
export interface MarkupCellLayoutInfo extends CellLayoutInfo {
222
readonly previewHeight: number;
223
readonly foldHintHeight: number;
224
}
225
226
export enum CellLayoutContext {
227
Fold
228
}
229
230
export interface MarkupCellLayoutChangeEvent extends CellLayoutChangeEvent {
231
readonly editorHeight?: number;
232
readonly previewHeight?: number;
233
totalHeight?: number;
234
readonly context?: CellLayoutContext;
235
}
236
237
export interface ICommonCellViewModelLayoutChangeInfo {
238
readonly totalHeight?: boolean | number;
239
readonly outerWidth?: number;
240
readonly context?: CellLayoutContext;
241
}
242
export interface ICellViewModel extends IGenericCellViewModel {
243
readonly model: NotebookCellTextModel;
244
readonly id: string;
245
readonly textBuffer: IReadonlyTextBuffer;
246
readonly layoutInfo: CellLayoutInfo;
247
readonly onDidChangeLayout: Event<ICommonCellViewModelLayoutChangeInfo>;
248
readonly onDidChangeCellStatusBarItems: Event<void>;
249
readonly onCellDecorationsChanged: Event<{ added: INotebookCellDecorationOptions[]; removed: INotebookCellDecorationOptions[] }>;
250
readonly onDidChangeState: Event<CellViewModelStateChangeEvent>;
251
readonly onDidChangeEditorAttachState: Event<void>;
252
readonly editStateSource: string;
253
readonly editorAttached: boolean;
254
isInputCollapsed: boolean;
255
isOutputCollapsed: boolean;
256
dragging: boolean;
257
handle: number;
258
uri: URI;
259
language: string;
260
readonly mime: string;
261
cellKind: CellKind;
262
lineNumbers: 'on' | 'off' | 'inherit';
263
commentOptions: IEditorCommentsOptions;
264
chatHeight: number;
265
commentHeight: number;
266
focusMode: CellFocusMode;
267
focusedOutputId?: string | undefined;
268
outputIsHovered: boolean;
269
getText(): string;
270
getAlternativeId(): number;
271
getTextLength(): number;
272
getHeight(lineHeight: number): number;
273
metadata: NotebookCellMetadata;
274
internalMetadata: NotebookCellInternalMetadata;
275
textModel: ITextModel | undefined;
276
hasModel(): this is IEditableCellViewModel;
277
resolveTextModel(): Promise<ITextModel>;
278
getSelections(): Selection[];
279
setSelections(selections: Selection[]): void;
280
getSelectionsStartPosition(): IPosition[] | undefined;
281
getCellDecorations(): INotebookCellDecorationOptions[];
282
getCellStatusBarItems(): INotebookCellStatusBarItem[];
283
getEditState(): CellEditState;
284
updateEditState(state: CellEditState, source: string): void;
285
deltaModelDecorations(oldDecorations: readonly string[], newDecorations: readonly IModelDeltaDecoration[]): string[];
286
getCellDecorationRange(id: string): Range | null;
287
enableAutoLanguageDetection(): void;
288
}
289
290
export interface IEditableCellViewModel extends ICellViewModel {
291
textModel: ITextModel;
292
}
293
294
export interface INotebookEditorMouseEvent {
295
readonly event: MouseEvent;
296
readonly target: ICellViewModel;
297
}
298
299
export interface INotebookEditorContribution {
300
/**
301
* Dispose this contribution.
302
*/
303
dispose(): void;
304
/**
305
* Store view state.
306
*/
307
saveViewState?(): unknown;
308
/**
309
* Restore view state.
310
*/
311
restoreViewState?(state: unknown): void;
312
}
313
314
/**
315
* Vertical Lane in the overview ruler of the notebook editor.
316
*/
317
export enum NotebookOverviewRulerLane {
318
Left = 1,
319
Center = 2,
320
Right = 4,
321
Full = 7
322
}
323
324
export interface INotebookCellDecorationOptions {
325
className?: string;
326
gutterClassName?: string;
327
outputClassName?: string;
328
topClassName?: string;
329
overviewRuler?: {
330
color: string;
331
modelRanges: IRange[];
332
includeOutput: boolean;
333
position: NotebookOverviewRulerLane;
334
};
335
}
336
337
export interface INotebookViewZoneDecorationOptions {
338
overviewRuler?: {
339
color: string;
340
position: NotebookOverviewRulerLane;
341
};
342
}
343
344
export interface INotebookDeltaCellDecoration {
345
readonly handle: number;
346
readonly options: INotebookCellDecorationOptions;
347
}
348
349
export interface INotebookDeltaViewZoneDecoration {
350
readonly viewZoneId: string;
351
readonly options: INotebookViewZoneDecorationOptions;
352
}
353
354
export function isNotebookCellDecoration(obj: unknown): obj is INotebookDeltaCellDecoration {
355
return !!obj && typeof (obj as INotebookDeltaCellDecoration).handle === 'number';
356
}
357
358
export function isNotebookViewZoneDecoration(obj: unknown): obj is INotebookDeltaViewZoneDecoration {
359
return !!obj && typeof (obj as INotebookDeltaViewZoneDecoration).viewZoneId === 'string';
360
}
361
362
export type INotebookDeltaDecoration = INotebookDeltaCellDecoration | INotebookDeltaViewZoneDecoration;
363
364
export interface INotebookDeltaCellStatusBarItems {
365
readonly handle: number;
366
readonly items: readonly INotebookCellStatusBarItem[];
367
}
368
369
export const enum CellRevealType {
370
Default = 1,
371
Top = 2,
372
Center = 3,
373
CenterIfOutsideViewport = 4,
374
NearTopIfOutsideViewport = 5,
375
FirstLineIfOutsideViewport = 6
376
}
377
378
export enum CellRevealRangeType {
379
Default = 1,
380
Center = 2,
381
CenterIfOutsideViewport = 3,
382
}
383
384
export interface INotebookEditorOptions extends ITextEditorOptions {
385
readonly cellOptions?: ITextResourceEditorInput;
386
readonly cellRevealType?: CellRevealType;
387
readonly cellSelections?: ICellRange[];
388
readonly isReadOnly?: boolean;
389
readonly viewState?: INotebookEditorViewState;
390
readonly indexedCellOptions?: { index: number; selection?: IRange };
391
readonly label?: string;
392
}
393
394
export type INotebookEditorContributionCtor = IConstructorSignature<INotebookEditorContribution, [INotebookEditor]>;
395
396
export interface INotebookEditorContributionDescription {
397
id: string;
398
ctor: INotebookEditorContributionCtor;
399
}
400
401
export interface INotebookEditorCreationOptions {
402
readonly isReplHistory?: boolean;
403
readonly isReadOnly?: boolean;
404
readonly contributions?: INotebookEditorContributionDescription[];
405
readonly cellEditorContributions?: IEditorContributionDescription[];
406
readonly menuIds: {
407
notebookToolbar: MenuId;
408
cellTitleToolbar: MenuId;
409
cellDeleteToolbar: MenuId;
410
cellInsertToolbar: MenuId;
411
cellTopInsertToolbar: MenuId;
412
cellExecuteToolbar: MenuId;
413
cellExecutePrimary?: MenuId;
414
};
415
readonly options?: NotebookOptions;
416
readonly codeWindow?: CodeWindow;
417
}
418
419
export interface INotebookWebviewMessage {
420
readonly message: unknown;
421
}
422
423
//#region Notebook View Model
424
export interface INotebookEditorViewState {
425
editingCells: { [key: number]: boolean };
426
collapsedInputCells: { [key: number]: boolean };
427
collapsedOutputCells: { [key: number]: boolean };
428
cellLineNumberStates: { [key: number]: 'on' | 'off' };
429
editorViewStates: { [key: number]: editorCommon.ICodeEditorViewState | null };
430
hiddenFoldingRanges?: ICellRange[];
431
cellTotalHeights?: { [key: number]: number };
432
scrollPosition?: { left: number; top: number };
433
focus?: number;
434
editorFocused?: boolean;
435
contributionsState?: { [id: string]: unknown };
436
selectedKernelId?: string;
437
}
438
439
export interface ICellModelDecorations {
440
readonly ownerId: number;
441
readonly decorations: readonly string[];
442
}
443
444
export interface ICellModelDeltaDecorations {
445
readonly ownerId: number;
446
readonly decorations: readonly IModelDeltaDecoration[];
447
}
448
449
export interface IModelDecorationsChangeAccessor {
450
deltaDecorations(oldDecorations: ICellModelDecorations[], newDecorations: ICellModelDeltaDecorations[]): ICellModelDecorations[];
451
}
452
453
export interface INotebookViewZone {
454
/**
455
* Use 0 to place a view zone before the first cell
456
*/
457
afterModelPosition: number;
458
domNode: HTMLElement;
459
460
heightInPx: number;
461
}
462
463
export interface INotebookViewZoneChangeAccessor {
464
addZone(zone: INotebookViewZone): string;
465
removeZone(id: string): void;
466
layoutZone(id: string): void;
467
}
468
469
export interface INotebookCellOverlay {
470
cell: ICellViewModel;
471
domNode: HTMLElement;
472
}
473
474
export interface INotebookCellOverlayChangeAccessor {
475
addOverlay(overlay: INotebookCellOverlay): string;
476
removeOverlay(id: string): void;
477
layoutOverlay(id: string): void;
478
}
479
480
export type NotebookViewCellsSplice = [
481
number /* start */,
482
number /* delete count */,
483
ICellViewModel[]
484
];
485
486
export interface INotebookViewCellsUpdateEvent {
487
readonly synchronous: boolean;
488
readonly splices: readonly NotebookViewCellsSplice[];
489
}
490
491
export interface INotebookViewModel {
492
notebookDocument: NotebookTextModel;
493
readonly viewCells: ICellViewModel[];
494
layoutInfo: NotebookLayoutInfo | null;
495
viewType: string;
496
readonly onDidChangeViewCells: Event<INotebookViewCellsUpdateEvent>;
497
readonly onDidChangeSelection: Event<string>;
498
readonly onDidFoldingStateChanged: Event<void>;
499
getNearestVisibleCellIndexUpwards(index: number): number;
500
getTrackedRange(id: string): ICellRange | null;
501
setTrackedRange(id: string | null, newRange: ICellRange | null, newStickiness: TrackedRangeStickiness): string | null;
502
getOverviewRulerDecorations(): INotebookDeltaViewZoneDecoration[];
503
getSelections(): ICellRange[];
504
getCellIndex(cell: ICellViewModel): number;
505
getMostRecentlyExecutedCell(): ICellViewModel | undefined;
506
deltaCellStatusBarItems(oldItems: string[], newItems: INotebookDeltaCellStatusBarItems[]): string[];
507
getFoldedLength(index: number): number;
508
getFoldingStartIndex(index: number): number;
509
replaceOne(cell: ICellViewModel, range: Range, text: string): Promise<void>;
510
replaceAll(matches: CellFindMatchWithIndex[], texts: string[]): Promise<void>;
511
}
512
//#endregion
513
514
export interface INotebookEditor {
515
//#region Eventing
516
readonly onDidChangeCellState: Event<NotebookCellStateChangedEvent>;
517
readonly onDidChangeViewCells: Event<INotebookViewCellsUpdateEvent>;
518
readonly onDidChangeVisibleRanges: Event<void>;
519
readonly onDidChangeSelection: Event<void>;
520
readonly onDidChangeFocus: Event<void>;
521
/**
522
* An event emitted when the model of this editor has changed.
523
*/
524
readonly onDidChangeModel: Event<NotebookTextModel | undefined>;
525
readonly onDidAttachViewModel: Event<void>;
526
readonly onDidFocusWidget: Event<void>;
527
readonly onDidBlurWidget: Event<void>;
528
readonly onDidScroll: Event<void>;
529
readonly onDidChangeLayout: Event<void>;
530
readonly onDidChangeActiveCell: Event<void>;
531
readonly onDidChangeActiveEditor: Event<INotebookEditor>;
532
readonly onDidChangeActiveKernel: Event<void>;
533
readonly onMouseUp: Event<INotebookEditorMouseEvent>;
534
readonly onMouseDown: Event<INotebookEditorMouseEvent>;
535
//#endregion
536
537
//#region readonly properties
538
readonly visibleRanges: ICellRange[];
539
readonly textModel?: NotebookTextModel;
540
readonly isVisible: boolean;
541
readonly isReadOnly: boolean;
542
readonly isReplHistory: boolean;
543
readonly notebookOptions: NotebookOptions;
544
readonly isDisposed: boolean;
545
readonly activeKernel: INotebookKernel | undefined;
546
readonly scrollTop: number;
547
readonly scrollBottom: number;
548
readonly scopedContextKeyService: IContextKeyService;
549
/**
550
* Required for Composite Editor check. The interface should not be changed.
551
*/
552
readonly activeCodeEditor: ICodeEditor | undefined;
553
readonly codeEditors: [ICellViewModel, ICodeEditor][];
554
readonly activeCellAndCodeEditor: [ICellViewModel, ICodeEditor] | undefined;
555
//#endregion
556
557
getLength(): number;
558
getSelections(): ICellRange[];
559
setSelections(selections: ICellRange[]): void;
560
getFocus(): ICellRange;
561
setFocus(focus: ICellRange): void;
562
getId(): string;
563
564
getViewModel(): INotebookViewModel | undefined;
565
hasModel(): this is IActiveNotebookEditor;
566
dispose(): void;
567
getDomNode(): HTMLElement;
568
getInnerWebview(): IWebviewElement | undefined;
569
getSelectionViewModels(): ICellViewModel[];
570
getEditorViewState(): INotebookEditorViewState;
571
restoreListViewState(viewState: INotebookEditorViewState | undefined): void;
572
573
getBaseCellEditorOptions(language: string): IBaseCellEditorOptions;
574
575
/**
576
* Focus the active cell in notebook cell list
577
*/
578
focus(): void;
579
580
/**
581
* Focus the notebook cell list container
582
*/
583
focusContainer(clearSelection?: boolean): void;
584
585
hasEditorFocus(): boolean;
586
hasWebviewFocus(): boolean;
587
588
hasOutputTextSelection(): boolean;
589
setOptions(options: INotebookEditorOptions | undefined): Promise<void>;
590
591
/**
592
* Select & focus cell
593
*/
594
focusElement(cell: ICellViewModel): void;
595
596
/**
597
* Layout info for the notebook editor
598
*/
599
getLayoutInfo(): NotebookLayoutInfo;
600
601
getVisibleRangesPlusViewportAboveAndBelow(): ICellRange[];
602
603
/**
604
* Focus the container of a cell (the monaco editor inside is not focused).
605
*/
606
focusNotebookCell(cell: ICellViewModel, focus: 'editor' | 'container' | 'output', options?: IFocusNotebookCellOptions): Promise<void>;
607
608
/**
609
* Execute the given notebook cells
610
*/
611
executeNotebookCells(cells?: Iterable<ICellViewModel>): Promise<void>;
612
613
/**
614
* Cancel the given notebook cells
615
*/
616
cancelNotebookCells(cells?: Iterable<ICellViewModel>): Promise<void>;
617
618
/**
619
* Get current active cell
620
*/
621
getActiveCell(): ICellViewModel | undefined;
622
623
/**
624
* Layout the cell with a new height
625
*/
626
layoutNotebookCell(cell: ICellViewModel, height: number): Promise<void>;
627
628
/**
629
* Render the output in webview layer
630
*/
631
createOutput(cell: ICellViewModel, output: IInsetRenderOutput, offset: number, createWhenIdle: boolean): Promise<void>;
632
633
/**
634
* Update the output in webview layer with latest content. It will delegate to `createOutput` is the output is not rendered yet
635
*/
636
updateOutput(cell: ICellViewModel, output: IInsetRenderOutput, offset: number): Promise<void>;
637
638
/**
639
* Copy the image in the specific cell output to the clipboard
640
*/
641
copyOutputImage(cellOutput: ICellOutputViewModel): Promise<void>;
642
/**
643
* Select the contents of the first focused output of the cell.
644
* Implementation of Ctrl+A for an output item.
645
*/
646
selectOutputContent(cell: ICellViewModel): void;
647
/**
648
* Select the active input element of the first focused output of the cell.
649
* Implementation of Ctrl+A for an input element in an output item.
650
*/
651
selectInputContents(cell: ICellViewModel): void;
652
653
readonly onDidReceiveMessage: Event<INotebookWebviewMessage>;
654
655
/**
656
* Send message to the webview for outputs.
657
*/
658
postMessage(message: any): void;
659
660
/**
661
* Remove class name on the notebook editor root DOM node.
662
*/
663
addClassName(className: string): void;
664
665
/**
666
* Remove class name on the notebook editor root DOM node.
667
*/
668
removeClassName(className: string): void;
669
670
/**
671
* Set scrollTop value of the notebook editor.
672
*/
673
setScrollTop(scrollTop: number): void;
674
675
/**
676
* The range will be revealed with as little scrolling as possible.
677
*/
678
revealCellRangeInView(range: ICellRange): void;
679
680
/**
681
* Reveal cell into viewport.
682
*/
683
revealInView(cell: ICellViewModel): Promise<void>;
684
685
/**
686
* Reveal cell into the top of viewport.
687
*/
688
revealInViewAtTop(cell: ICellViewModel): void;
689
690
/**
691
* Reveal cell into viewport center.
692
*/
693
revealInCenter(cell: ICellViewModel): void;
694
695
/**
696
* Reveal cell into viewport center if cell is currently out of the viewport.
697
*/
698
revealInCenterIfOutsideViewport(cell: ICellViewModel): Promise<void>;
699
700
/**
701
* Reveal the first line of the cell into the view if the cell is outside of the viewport.
702
*/
703
revealFirstLineIfOutsideViewport(cell: ICellViewModel): Promise<void>;
704
705
/**
706
* Reveal a line in notebook cell into viewport with minimal scrolling.
707
*/
708
revealLineInViewAsync(cell: ICellViewModel, line: number): Promise<void>;
709
710
/**
711
* Reveal a line in notebook cell into viewport center.
712
*/
713
revealLineInCenterAsync(cell: ICellViewModel, line: number): Promise<void>;
714
715
/**
716
* Reveal a line in notebook cell into viewport center.
717
*/
718
revealLineInCenterIfOutsideViewportAsync(cell: ICellViewModel, line: number): Promise<void>;
719
720
/**
721
* Reveal a range in notebook cell into viewport with minimal scrolling.
722
*/
723
revealRangeInViewAsync(cell: ICellViewModel, range: Selection | Range): Promise<void>;
724
725
/**
726
* Reveal a range in notebook cell into viewport center.
727
*/
728
revealRangeInCenterAsync(cell: ICellViewModel, range: Selection | Range): Promise<void>;
729
730
/**
731
* Reveal a range in notebook cell into viewport center.
732
*/
733
revealRangeInCenterIfOutsideViewportAsync(cell: ICellViewModel, range: Selection | Range): Promise<void>;
734
735
/**
736
* Reveal a position with `offset` in a cell into viewport center.
737
*/
738
revealCellOffsetInCenter(cell: ICellViewModel, offset: number): void;
739
740
/**
741
* Reveal `offset` in the list view into viewport center if it is outside of the viewport.
742
*/
743
revealOffsetInCenterIfOutsideViewport(offset: number): void;
744
745
/**
746
* Convert the view range to model range
747
* @param startIndex Inclusive
748
* @param endIndex Exclusive
749
*/
750
getCellRangeFromViewRange(startIndex: number, endIndex: number): ICellRange | undefined;
751
752
/**
753
* Set hidden areas on cell text models.
754
*/
755
setHiddenAreas(_ranges: ICellRange[]): boolean;
756
757
/**
758
* Set selectiosn on the text editor attached to the cell
759
*/
760
761
setCellEditorSelection(cell: ICellViewModel, selection: Range): void;
762
763
/**
764
*Change the decorations on the notebook cell list
765
*/
766
767
deltaCellDecorations(oldDecorations: string[], newDecorations: INotebookDeltaDecoration[]): string[];
768
769
/**
770
* Change the decorations on cell editors.
771
* The notebook is virtualized and this method should be called to create/delete editor decorations safely.
772
*/
773
changeModelDecorations<T>(callback: (changeAccessor: IModelDecorationsChangeAccessor) => T): T | null;
774
775
changeViewZones(callback: (accessor: INotebookViewZoneChangeAccessor) => void): void;
776
777
changeCellOverlays(callback: (accessor: INotebookCellOverlayChangeAccessor) => void): void;
778
779
getViewZoneLayoutInfo(id: string): { top: number; height: number } | null;
780
781
/**
782
* Get a contribution of this editor.
783
* @id Unique identifier of the contribution.
784
* @return The contribution or null if contribution not found.
785
*/
786
getContribution<T extends INotebookEditorContribution>(id: string): T;
787
788
/**
789
* Get the view index of a cell at model `index`
790
*/
791
getViewIndexByModelIndex(index: number): number;
792
getCellsInRange(range?: ICellRange): ReadonlyArray<ICellViewModel>;
793
cellAt(index: number): ICellViewModel | undefined;
794
getCellByHandle(handle: number): ICellViewModel | undefined;
795
getCellIndex(cell: ICellViewModel): number | undefined;
796
getNextVisibleCellIndex(index: number): number | undefined;
797
getPreviousVisibleCellIndex(index: number): number | undefined;
798
find(query: string, options: INotebookFindOptions, token: CancellationToken, skipWarmup?: boolean, shouldGetSearchPreviewInfo?: boolean, ownerID?: string): Promise<CellFindMatchWithIndex[]>;
799
findHighlightCurrent(matchIndex: number, ownerID?: string): Promise<number>;
800
findUnHighlightCurrent(matchIndex: number, ownerID?: string): Promise<void>;
801
findStop(ownerID?: string): void;
802
showProgress(): void;
803
hideProgress(): void;
804
805
getAbsoluteTopOfElement(cell: ICellViewModel): number;
806
getAbsoluteBottomOfElement(cell: ICellViewModel): number;
807
getHeightOfElement(cell: ICellViewModel): number;
808
}
809
810
export interface IActiveNotebookEditor extends INotebookEditor {
811
getViewModel(): INotebookViewModel;
812
textModel: NotebookTextModel;
813
getFocus(): ICellRange;
814
cellAt(index: number): ICellViewModel;
815
getCellIndex(cell: ICellViewModel): number;
816
getNextVisibleCellIndex(index: number): number;
817
}
818
819
export interface INotebookEditorPane extends IEditorPaneWithSelection {
820
getControl(): INotebookEditor | undefined;
821
readonly onDidChangeModel: Event<void>;
822
textModel: NotebookTextModel | undefined;
823
}
824
825
export interface IBaseCellEditorOptions extends IDisposable {
826
readonly value: IEditorOptions;
827
readonly onDidChange: Event<void>;
828
}
829
830
/**
831
* A mix of public interface and internal one (used by internal rendering code, e.g., cellRenderer)
832
*/
833
export interface INotebookEditorDelegate extends INotebookEditor {
834
hasModel(): this is IActiveNotebookEditorDelegate;
835
836
readonly creationOptions: INotebookEditorCreationOptions;
837
readonly onDidChangeOptions: Event<void>;
838
readonly onDidChangeDecorations: Event<void>;
839
createMarkupPreview(cell: ICellViewModel): Promise<void>;
840
unhideMarkupPreviews(cells: readonly ICellViewModel[]): Promise<void>;
841
hideMarkupPreviews(cells: readonly ICellViewModel[]): Promise<void>;
842
843
/**
844
* Remove the output from the webview layer
845
*/
846
removeInset(output: IDisplayOutputViewModel): void;
847
848
/**
849
* Hide the inset in the webview layer without removing it
850
*/
851
hideInset(output: IDisplayOutputViewModel): void;
852
deltaCellContainerClassNames(cellId: string, added: string[], removed: string[], cellKind: CellKind): void;
853
}
854
855
export interface IActiveNotebookEditorDelegate extends INotebookEditorDelegate {
856
getViewModel(): INotebookViewModel;
857
textModel: NotebookTextModel;
858
getFocus(): ICellRange;
859
cellAt(index: number): ICellViewModel;
860
getCellIndex(cell: ICellViewModel): number;
861
getNextVisibleCellIndex(index: number): number;
862
}
863
864
export interface ISearchPreviewInfo {
865
line: string;
866
range: {
867
start: number;
868
end: number;
869
};
870
}
871
872
export interface CellWebviewFindMatch {
873
readonly index: number;
874
readonly searchPreviewInfo?: ISearchPreviewInfo;
875
}
876
877
export type CellContentFindMatch = FindMatch;
878
879
export interface CellFindMatch {
880
cell: ICellViewModel;
881
contentMatches: CellContentFindMatch[];
882
}
883
884
export interface CellFindMatchWithIndex {
885
cell: ICellViewModel;
886
index: number;
887
length: number;
888
getMatch(index: number): FindMatch | CellWebviewFindMatch;
889
contentMatches: FindMatch[];
890
webviewMatches: CellWebviewFindMatch[];
891
}
892
893
export enum CellEditState {
894
/**
895
* Default state.
896
* For markup cells, this is the renderer version of the markup.
897
* For code cell, the browser focus should be on the container instead of the editor
898
*/
899
Preview,
900
901
/**
902
* Editing mode. Source for markup or code is rendered in editors and the state will be persistent.
903
*/
904
Editing
905
}
906
907
export enum CellFocusMode {
908
Container,
909
Editor,
910
Output,
911
ChatInput
912
}
913
914
export enum CursorAtBoundary {
915
None,
916
Top,
917
Bottom,
918
Both
919
}
920
921
export enum CursorAtLineBoundary {
922
None,
923
Start,
924
End,
925
Both
926
}
927
928
export function getNotebookEditorFromEditorPane(editorPane?: IEditorPane): INotebookEditor | undefined {
929
if (!editorPane) {
930
return;
931
}
932
933
if (editorPane.getId() === NOTEBOOK_EDITOR_ID) {
934
return editorPane.getControl() as INotebookEditor | undefined;
935
}
936
937
if (editorPane.getId() === NOTEBOOK_DIFF_EDITOR_ID) {
938
return (editorPane.getControl() as INotebookTextDiffEditor).inlineNotebookEditor;
939
}
940
941
const input = editorPane.input;
942
943
const isCompositeNotebook = input && isCompositeNotebookEditorInput(input);
944
945
if (isCompositeNotebook) {
946
return (editorPane.getControl() as { notebookEditor: INotebookEditor | undefined } | undefined)?.notebookEditor;
947
}
948
949
return undefined;
950
}
951
952
/**
953
* ranges: model selections
954
* this will convert model selections to view indexes first, and then include the hidden ranges in the list view
955
*/
956
export function expandCellRangesWithHiddenCells(editor: INotebookEditor, ranges: ICellRange[]) {
957
// assuming ranges are sorted and no overlap
958
const indexes = cellRangesToIndexes(ranges);
959
const modelRanges: ICellRange[] = [];
960
indexes.forEach(index => {
961
const viewCell = editor.cellAt(index);
962
963
if (!viewCell) {
964
return;
965
}
966
967
const viewIndex = editor.getViewIndexByModelIndex(index);
968
if (viewIndex < 0) {
969
return;
970
}
971
972
const nextViewIndex = viewIndex + 1;
973
const range = editor.getCellRangeFromViewRange(viewIndex, nextViewIndex);
974
975
if (range) {
976
modelRanges.push(range);
977
}
978
});
979
980
return reduceCellRanges(modelRanges);
981
}
982
983
export function cellRangeToViewCells(editor: IActiveNotebookEditor, ranges: ICellRange[]) {
984
const cells: ICellViewModel[] = [];
985
reduceCellRanges(ranges).forEach(range => {
986
cells.push(...editor.getCellsInRange(range));
987
});
988
989
return cells;
990
}
991
992
//#region Cell Folding
993
export const enum CellFoldingState {
994
None,
995
Expanded,
996
Collapsed
997
}
998
999
export interface EditorFoldingStateDelegate {
1000
getCellIndex(cell: ICellViewModel): number;
1001
getFoldingState(index: number): CellFoldingState;
1002
}
1003
//#endregion
1004
1005