1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
|
/*---------------------------------------------------------------------------------------------
* Copyright (c) Microsoft Corporation. All rights reserved.
* Licensed under the MIT License. See License.txt in the project root for license information.
*--------------------------------------------------------------------------------------------*/
import { Emitter, Event } from 'vs/base/common/event';
import { Disposable, dispose, IDisposable, IReference } from 'vs/base/common/lifecycle';
import { Mimes } from 'vs/base/common/mime';
import { ICodeEditor } from 'vs/editor/browser/editorBrowser';
import { ICodeEditorService } from 'vs/editor/browser/services/codeEditorService';
import { IPosition } from 'vs/editor/common/core/position';
import { Range } from 'vs/editor/common/core/range';
import { Selection } from 'vs/editor/common/core/selection';
import * as editorCommon from 'vs/editor/common/editorCommon';
import * as model from 'vs/editor/common/model';
import { SearchParams } from 'vs/editor/common/model/textModelSearch';
import { IResolvedTextEditorModel, ITextModelService } from 'vs/editor/common/services/resolverService';
import { IConfigurationService } from 'vs/platform/configuration/common/configuration';
import { IUndoRedoService } from 'vs/platform/undoRedo/common/undoRedo';
import { IWordWrapTransientState, readTransientState, writeTransientState } from 'vs/workbench/contrib/codeEditor/browser/toggleWordWrap';
import { CellEditState, CellFocusMode, CursorAtBoundary, IEditableCellViewModel, INotebookCellDecorationOptions } from 'vs/workbench/contrib/notebook/browser/notebookBrowser';
import { CellViewModelStateChangeEvent } from 'vs/workbench/contrib/notebook/browser/notebookViewEvents';
import { ViewContext } from 'vs/workbench/contrib/notebook/browser/viewModel/viewContext';
import { NotebookCellTextModel } from 'vs/workbench/contrib/notebook/common/model/notebookCellTextModel';
import { CellKind, INotebookCellStatusBarItem, INotebookSearchOptions } from 'vs/workbench/contrib/notebook/common/notebookCommon';
import { NotebookOptionsChangeEvent } from 'vs/workbench/contrib/notebook/common/notebookOptions';
export abstract class BaseCellViewModel extends Disposable {
protected readonly _onDidChangeEditorAttachState = this._register(new Emitter<void>());
// Do not merge this event with `onDidChangeState` as we are using `Event.once(onDidChangeEditorAttachState)` elsewhere.
readonly onDidChangeEditorAttachState = this._onDidChangeEditorAttachState.event;
protected readonly _onDidChangeState = this._register(new Emitter<CellViewModelStateChangeEvent>());
public readonly onDidChangeState: Event<CellViewModelStateChangeEvent> = this._onDidChangeState.event;
get handle() {
return this.model.handle;
}
get uri() {
return this.model.uri;
}
get lineCount() {
return this.model.textBuffer.getLineCount();
}
get metadata() {
return this.model.metadata;
}
get internalMetadata() {
return this.model.internalMetadata;
}
get language() {
return this.model.language;
}
get mime(): string {
if (typeof this.model.mime === 'string') {
return this.model.mime;
}
switch (this.language) {
case 'markdown':
return Mimes.markdown;
default:
return Mimes.text;
}
}
abstract cellKind: CellKind;
private _editState: CellEditState = CellEditState.Preview;
private _lineNumbers: 'on' | 'off' | 'inherit' = 'inherit';
get lineNumbers(): 'on' | 'off' | 'inherit' {
return this._lineNumbers;
}
set lineNumbers(lineNumbers: 'on' | 'off' | 'inherit') {
if (lineNumbers === this._lineNumbers) {
return;
}
this._lineNumbers = lineNumbers;
this._onDidChangeState.fire({ cellLineNumberChanged: true });
}
private _focusMode: CellFocusMode = CellFocusMode.Container;
get focusMode() {
return this._focusMode;
}
set focusMode(newMode: CellFocusMode) {
if (this._focusMode !== newMode) {
this._focusMode = newMode;
this._onDidChangeState.fire({ focusModeChanged: true });
}
}
protected _textEditor?: ICodeEditor;
get editorAttached(): boolean {
return !!this._textEditor;
}
private _editorListeners: IDisposable[] = [];
private _editorViewStates: editorCommon.ICodeEditorViewState | null = null;
private _editorTransientState: IWordWrapTransientState | null = null;
private _resolvedCellDecorations = new Map<string, INotebookCellDecorationOptions>();
private readonly _cellDecorationsChanged = this._register(new Emitter<{ added: INotebookCellDecorationOptions[]; removed: INotebookCellDecorationOptions[] }>());
onCellDecorationsChanged: Event<{ added: INotebookCellDecorationOptions[]; removed: INotebookCellDecorationOptions[] }> = this._cellDecorationsChanged.event;
private _resolvedDecorations = new Map<string, {
id?: string;
options: model.IModelDeltaDecoration;
}>();
private _lastDecorationId: number = 0;
private _cellStatusBarItems = new Map<string, INotebookCellStatusBarItem>();
private readonly _onDidChangeCellStatusBarItems = this._register(new Emitter<void>());
readonly onDidChangeCellStatusBarItems: Event<void> = this._onDidChangeCellStatusBarItems.event;
private _lastStatusBarId: number = 0;
get textModel(): model.ITextModel | undefined {
return this.model.textModel;
}
hasModel(): this is IEditableCellViewModel {
return !!this.textModel;
}
private _dragging: boolean = false;
get dragging(): boolean {
return this._dragging;
}
set dragging(v: boolean) {
this._dragging = v;
this._onDidChangeState.fire({ dragStateChanged: true });
}
protected _textModelRef: IReference<IResolvedTextEditorModel> | undefined;
private _inputCollapsed: boolean = false;
get isInputCollapsed(): boolean {
return this._inputCollapsed;
}
set isInputCollapsed(v: boolean) {
this._inputCollapsed = v;
this._onDidChangeState.fire({ inputCollapsedChanged: true });
}
private _outputCollapsed: boolean = false;
get isOutputCollapsed(): boolean {
return this._outputCollapsed;
}
set isOutputCollapsed(v: boolean) {
this._outputCollapsed = v;
this._onDidChangeState.fire({ outputCollapsedChanged: true });
}
constructor(
readonly viewType: string,
readonly model: NotebookCellTextModel,
public id: string,
private readonly _viewContext: ViewContext,
private readonly _configurationService: IConfigurationService,
private readonly _modelService: ITextModelService,
private readonly _undoRedoService: IUndoRedoService,
private readonly _codeEditorService: ICodeEditorService,
// private readonly _keymapService: INotebookKeymapService
) {
super();
this._register(model.onDidChangeMetadata(() => {
this._onDidChangeState.fire({ metadataChanged: true });
}));
this._register(model.onDidChangeInternalMetadata(e => {
this._onDidChangeState.fire({ internalMetadataChanged: true });
if (e.lastRunSuccessChanged) {
// Statusbar visibility may change
this.layoutChange({});
}
}));
this._register(this._configurationService.onDidChangeConfiguration(e => {
if (e.affectsConfiguration('notebook.lineNumbers')) {
this.lineNumbers = 'inherit';
}
}));
if (this.model.collapseState?.inputCollapsed) {
this._inputCollapsed = true;
}
if (this.model.collapseState?.outputCollapsed) {
this._outputCollapsed = true;
}
}
abstract updateOptions(e: NotebookOptionsChangeEvent): void;
abstract hasDynamicHeight(): boolean;
abstract getHeight(lineHeight: number): number;
abstract onDeselect(): void;
abstract layoutChange(change: any): void;
assertTextModelAttached(): boolean {
if (this.textModel && this._textEditor && this._textEditor.getModel() === this.textModel) {
return true;
}
return false;
}
// private handleKeyDown(e: IKeyboardEvent) {
// if (this.viewType === IPYNB_VIEW_TYPE && isWindows && e.ctrlKey && e.keyCode === KeyCode.Enter) {
// this._keymapService.promptKeymapRecommendation();
// }
// }
attachTextEditor(editor: ICodeEditor) {
if (!editor.hasModel()) {
throw new Error('Invalid editor: model is missing');
}
if (this._textEditor === editor) {
if (this._editorListeners.length === 0) {
this._editorListeners.push(this._textEditor.onDidChangeCursorSelection(() => { this._onDidChangeState.fire({ selectionChanged: true }); }));
// this._editorListeners.push(this._textEditor.onKeyDown(e => this.handleKeyDown(e)));
this._onDidChangeState.fire({ selectionChanged: true });
}
return;
}
this._textEditor = editor;
if (this._editorViewStates) {
this._restoreViewState(this._editorViewStates);
}
if (this._editorTransientState) {
writeTransientState(editor.getModel(), this._editorTransientState, this._codeEditorService);
}
this._resolvedDecorations.forEach((value, key) => {
if (key.startsWith('_lazy_')) {
// lazy ones
const ret = this._textEditor!.deltaDecorations([], [value.options]);
this._resolvedDecorations.get(key)!.id = ret[0];
}
else {
const ret = this._textEditor!.deltaDecorations([], [value.options]);
this._resolvedDecorations.get(key)!.id = ret[0];
}
});
this._editorListeners.push(this._textEditor.onDidChangeCursorSelection(() => { this._onDidChangeState.fire({ selectionChanged: true }); }));
// this._editorListeners.push(this._textEditor.onKeyDown(e => this.handleKeyDown(e)));
this._onDidChangeState.fire({ selectionChanged: true });
this._onDidChangeEditorAttachState.fire();
}
detachTextEditor() {
this.saveViewState();
this.saveTransientState();
// decorations need to be cleared first as editors can be resued.
this._resolvedDecorations.forEach(value => {
const resolvedid = value.id;
if (resolvedid) {
this._textEditor?.deltaDecorations([resolvedid], []);
}
});
this._textEditor = undefined;
dispose(this._editorListeners);
this._editorListeners = [];
this._onDidChangeEditorAttachState.fire();
if (this._textModelRef) {
this._textModelRef.dispose();
this._textModelRef = undefined;
}
}
getText(): string {
return this.model.getValue();
}
getTextLength(): number {
return this.model.getTextLength();
}
private saveViewState(): void {
if (!this._textEditor) {
return;
}
this._editorViewStates = this._textEditor.saveViewState();
}
private saveTransientState() {
if (!this._textEditor || !this._textEditor.hasModel()) {
return;
}
this._editorTransientState = readTransientState(this._textEditor.getModel(), this._codeEditorService);
}
saveEditorViewState() {
if (this._textEditor) {
this._editorViewStates = this._textEditor.saveViewState();
}
return this._editorViewStates;
}
restoreEditorViewState(editorViewStates: editorCommon.ICodeEditorViewState | null, totalHeight?: number) {
this._editorViewStates = editorViewStates;
}
private _restoreViewState(state: editorCommon.ICodeEditorViewState | null): void {
if (state) {
this._textEditor?.restoreViewState(state);
}
}
addModelDecoration(decoration: model.IModelDeltaDecoration): string {
if (!this._textEditor) {
const id = ++this._lastDecorationId;
const decorationId = `_lazy_${this.id};${id}`;
this._resolvedDecorations.set(decorationId, { options: decoration });
return decorationId;
}
const result = this._textEditor.deltaDecorations([], [decoration]);
this._resolvedDecorations.set(result[0], { id: result[0], options: decoration });
return result[0];
}
removeModelDecoration(decorationId: string) {
const realDecorationId = this._resolvedDecorations.get(decorationId);
if (this._textEditor && realDecorationId && realDecorationId.id !== undefined) {
this._textEditor.deltaDecorations([realDecorationId.id!], []);
}
// lastly, remove all the cache
this._resolvedDecorations.delete(decorationId);
}
deltaModelDecorations(oldDecorations: string[], newDecorations: model.IModelDeltaDecoration[]): string[] {
oldDecorations.forEach(id => {
this.removeModelDecoration(id);
});
const ret = newDecorations.map(option => {
return this.addModelDecoration(option);
});
return ret;
}
private _removeCellDecoration(decorationId: string) {
const options = this._resolvedCellDecorations.get(decorationId);
if (options) {
this._cellDecorationsChanged.fire({ added: [], removed: [options] });
this._resolvedCellDecorations.delete(decorationId);
}
}
private _addCellDecoration(options: INotebookCellDecorationOptions): string {
const id = ++this._lastDecorationId;
const decorationId = `_cell_${this.id};${id}`;
this._resolvedCellDecorations.set(decorationId, options);
this._cellDecorationsChanged.fire({ added: [options], removed: [] });
return decorationId;
}
getCellDecorations() {
return [...this._resolvedCellDecorations.values()];
}
getCellDecorationRange(decorationId: string): Range | null {
if (this._textEditor) {
// (this._textEditor as CodeEditorWidget).decora
return this._textEditor.getModel()?.getDecorationRange(decorationId) ?? null;
}
return null;
}
deltaCellDecorations(oldDecorations: string[], newDecorations: INotebookCellDecorationOptions[]): string[] {
oldDecorations.forEach(id => {
this._removeCellDecoration(id);
});
const ret = newDecorations.map(option => {
return this._addCellDecoration(option);
});
return ret;
}
deltaCellStatusBarItems(oldItems: string[], newItems: INotebookCellStatusBarItem[]): string[] {
oldItems.forEach(id => {
const item = this._cellStatusBarItems.get(id);
if (item) {
this._cellStatusBarItems.delete(id);
}
});
const newIds = newItems.map(item => {
const id = ++this._lastStatusBarId;
const itemId = `_cell_${this.id};${id}`;
this._cellStatusBarItems.set(itemId, item);
return itemId;
});
this._onDidChangeCellStatusBarItems.fire();
return newIds;
}
getCellStatusBarItems(): INotebookCellStatusBarItem[] {
return Array.from(this._cellStatusBarItems.values());
}
revealRangeInCenter(range: Range) {
this._textEditor?.revealRangeInCenter(range, editorCommon.ScrollType.Immediate);
}
setSelection(range: Range) {
this._textEditor?.setSelection(range);
}
setSelections(selections: Selection[]) {
if (selections.length) {
this._textEditor?.setSelections(selections);
}
}
getSelections() {
return this._textEditor?.getSelections() || [];
}
getSelectionsStartPosition(): IPosition[] | undefined {
if (this._textEditor) {
const selections = this._textEditor.getSelections();
return selections?.map(s => s.getStartPosition());
} else {
const selections = this._editorViewStates?.cursorState;
return selections?.map(s => s.selectionStart);
}
}
getLineScrollTopOffset(line: number): number {
if (!this._textEditor) {
return 0;
}
const editorPadding = this._viewContext.notebookOptions.computeEditorPadding(this.internalMetadata, this.uri);
return this._textEditor.getTopForLineNumber(line) + editorPadding.top;
}
getPositionScrollTopOffset(line: number, column: number): number {
if (!this._textEditor) {
return 0;
}
const editorPadding = this._viewContext.notebookOptions.computeEditorPadding(this.internalMetadata, this.uri);
return this._textEditor.getTopForPosition(line, column) + editorPadding.top;
}
cursorAtBoundary(): CursorAtBoundary {
if (!this._textEditor) {
return CursorAtBoundary.None;
}
if (!this.textModel) {
return CursorAtBoundary.None;
}
// only validate primary cursor
const selection = this._textEditor.getSelection();
// only validate empty cursor
if (!selection || !selection.isEmpty()) {
return CursorAtBoundary.None;
}
const firstViewLineTop = this._textEditor.getTopForPosition(1, 1);
const lastViewLineTop = this._textEditor.getTopForPosition(this.textModel!.getLineCount(), this.textModel!.getLineLength(this.textModel!.getLineCount()));
const selectionTop = this._textEditor.getTopForPosition(selection.startLineNumber, selection.startColumn);
if (selectionTop === lastViewLineTop) {
if (selectionTop === firstViewLineTop) {
return CursorAtBoundary.Both;
} else {
return CursorAtBoundary.Bottom;
}
} else {
if (selectionTop === firstViewLineTop) {
return CursorAtBoundary.Top;
} else {
return CursorAtBoundary.None;
}
}
}
private _editStateSource: string = '';
get editStateSource(): string {
return this._editStateSource;
}
updateEditState(newState: CellEditState, source: string) {
this._editStateSource = source;
if (newState === this._editState) {
return;
}
this._editState = newState;
this._onDidChangeState.fire({ editStateChanged: true });
if (this._editState === CellEditState.Preview) {
this.focusMode = CellFocusMode.Container;
}
}
getEditState() {
return this._editState;
}
get textBuffer() {
return this.model.textBuffer;
}
/**
* Text model is used for editing.
*/
async resolveTextModel(): Promise<model.ITextModel> {
if (!this._textModelRef || !this.textModel) {
this._textModelRef = await this._modelService.createModelReference(this.uri);
if (!this._textModelRef) {
throw new Error(`Cannot resolve text model for ${this.uri}`);
}
this._register(this.textModel!.onDidChangeContent(() => this.onDidChangeTextModelContent()));
}
return this.textModel!;
}
protected abstract onDidChangeTextModelContent(): void;
protected cellStartFind(value: string, options: INotebookSearchOptions): model.FindMatch[] | null {
let cellMatches: model.FindMatch[] = [];
if (this.assertTextModelAttached()) {
cellMatches = this.textModel!.findMatches(
value,
false,
options.regex || false,
options.caseSensitive || false,
options.wholeWord ? options.wordSeparators || null : null,
options.regex || false);
} else {
const lineCount = this.textBuffer.getLineCount();
const fullRange = new Range(1, 1, lineCount, this.textBuffer.getLineLength(lineCount) + 1);
const searchParams = new SearchParams(value, options.regex || false, options.caseSensitive || false, options.wholeWord ? options.wordSeparators || null : null,);
const searchData = searchParams.parseSearchRequest();
if (!searchData) {
return null;
}
cellMatches = this.textBuffer.findMatchesLineByLine(fullRange, searchData, options.regex || false, 1000);
}
return cellMatches;
}
override dispose() {
super.dispose();
dispose(this._editorListeners);
// Only remove the undo redo stack if we map this cell uri to itself
// If we are not in perCell mode, it will map to the full NotebookDocument and
// we don't want to remove that entire document undo / redo stack when a cell is deleted
if (this._undoRedoService.getUriComparisonKey(this.uri) === this.uri.toString()) {
this._undoRedoService.removeElements(this.uri);
}
if (this._textModelRef) {
this._textModelRef.dispose();
}
}
toJSON(): object {
return {
handle: this.handle
};
}
}
|