-
Notifications
You must be signed in to change notification settings - Fork 29.4k
/
notebookCellTextModel.ts
547 lines (456 loc) · 18.2 KB
/
notebookCellTextModel.ts
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
/*---------------------------------------------------------------------------------------------
* 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 '../../../../../base/common/event.js';
import { hash, StringSHA1 } from '../../../../../base/common/hash.js';
import { Disposable, DisposableStore, dispose } from '../../../../../base/common/lifecycle.js';
import { URI } from '../../../../../base/common/uri.js';
import * as UUID from '../../../../../base/common/uuid.js';
import { Range } from '../../../../../editor/common/core/range.js';
import * as model from '../../../../../editor/common/model.js';
import { PieceTreeTextBuffer } from '../../../../../editor/common/model/pieceTreeTextBuffer/pieceTreeTextBuffer.js';
import { createTextBuffer, TextModel } from '../../../../../editor/common/model/textModel.js';
import { PLAINTEXT_LANGUAGE_ID } from '../../../../../editor/common/languages/modesRegistry.js';
import { ILanguageService } from '../../../../../editor/common/languages/language.js';
import { NotebookCellOutputTextModel } from './notebookCellOutputTextModel.js';
import { CellInternalMetadataChangedEvent, CellKind, ICell, ICellDto2, ICellOutput, IOutputDto, IOutputItemDto, NotebookCellCollapseState, NotebookCellInternalMetadata, NotebookCellMetadata, NotebookCellOutputsSplice, TransientCellMetadata, TransientOptions } from '../notebookCommon.js';
import { ThrottledDelayer } from '../../../../../base/common/async.js';
import { ILanguageDetectionService } from '../../../../services/languageDetection/common/languageDetectionWorkerService.js';
import { toFormattedString } from '../../../../../base/common/jsonFormatter.js';
import { IModelContentChangedEvent } from '../../../../../editor/common/textModelEvents.js';
import { splitLines } from '../../../../../base/common/strings.js';
export class NotebookCellTextModel extends Disposable implements ICell {
private readonly _onDidChangeOutputs = this._register(new Emitter<NotebookCellOutputsSplice>());
readonly onDidChangeOutputs: Event<NotebookCellOutputsSplice> = this._onDidChangeOutputs.event;
private readonly _onDidChangeOutputItems = this._register(new Emitter<void>());
readonly onDidChangeOutputItems: Event<void> = this._onDidChangeOutputItems.event;
private readonly _onDidChangeContent = this._register(new Emitter<'content' | 'language' | 'mime' | { type: 'model'; event: IModelContentChangedEvent }>());
readonly onDidChangeContent: Event<'content' | 'language' | 'mime' | { type: 'model'; event: IModelContentChangedEvent }> = this._onDidChangeContent.event;
private readonly _onDidChangeMetadata = this._register(new Emitter<void>());
readonly onDidChangeMetadata: Event<void> = this._onDidChangeMetadata.event;
private readonly _onDidChangeInternalMetadata = this._register(new Emitter<CellInternalMetadataChangedEvent>());
readonly onDidChangeInternalMetadata: Event<CellInternalMetadataChangedEvent> = this._onDidChangeInternalMetadata.event;
private readonly _onDidChangeLanguage = this._register(new Emitter<string>());
readonly onDidChangeLanguage: Event<string> = this._onDidChangeLanguage.event;
private _outputs: NotebookCellOutputTextModel[];
get outputs(): ICellOutput[] {
return this._outputs;
}
private _metadata: NotebookCellMetadata;
get metadata() {
return this._metadata;
}
set metadata(newMetadata: NotebookCellMetadata) {
this._metadata = newMetadata;
this._hash = null;
this._onDidChangeMetadata.fire();
}
private _internalMetadata: NotebookCellInternalMetadata;
get internalMetadata() {
return this._internalMetadata;
}
set internalMetadata(newInternalMetadata: NotebookCellInternalMetadata) {
const lastRunSuccessChanged = this._internalMetadata.lastRunSuccess !== newInternalMetadata.lastRunSuccess;
newInternalMetadata = {
...newInternalMetadata,
...{ runStartTimeAdjustment: computeRunStartTimeAdjustment(this._internalMetadata, newInternalMetadata) }
};
this._internalMetadata = newInternalMetadata;
this._hash = null;
this._onDidChangeInternalMetadata.fire({ lastRunSuccessChanged });
}
get language() {
return this._language;
}
set language(newLanguage: string) {
if (this._textModel
// 1. the language update is from workspace edit, checking if it's the same as text model's mode
&& this._textModel.getLanguageId() === this._languageService.getLanguageIdByLanguageName(newLanguage)
// 2. the text model's mode might be the same as the `this.language`, even if the language friendly name is not the same, we should not trigger an update
&& this._textModel.getLanguageId() === this._languageService.getLanguageIdByLanguageName(this.language)) {
return;
}
this._hasLanguageSetExplicitly = true;
this._setLanguageInternal(newLanguage);
}
public get mime(): string | undefined {
return this._mime;
}
public set mime(newMime: string | undefined) {
if (this._mime === newMime) {
return;
}
this._mime = newMime;
this._hash = null;
this._onDidChangeContent.fire('mime');
}
private _textBuffer!: model.ITextBuffer;
get textBuffer() {
if (this._textBuffer) {
return this._textBuffer;
}
this._textBuffer = this._register(createTextBuffer(this._source, model.DefaultEndOfLine.LF).textBuffer);
this._register(this._textBuffer.onDidChangeContent(() => {
this._hash = null;
if (!this._textModel) {
this._onDidChangeContent.fire('content');
}
this.autoDetectLanguage();
}));
return this._textBuffer;
}
private _textBufferHash: string | null = null;
private _hash: number | null = null;
private _versionId: number = 1;
private _alternativeId: number = 1;
get alternativeId(): number {
return this._alternativeId;
}
private readonly _textModelDisposables = this._register(new DisposableStore());
private _textModel: TextModel | undefined = undefined;
get textModel(): TextModel | undefined {
return this._textModel;
}
set textModel(m: TextModel | undefined) {
if (this._textModel === m) {
return;
}
this._textModelDisposables.clear();
this._textModel = m;
if (this._textModel) {
this.setRegisteredLanguage(this._languageService, this._textModel.getLanguageId(), this.language);
// Listen to language changes on the model
this._textModelDisposables.add(this._textModel.onDidChangeLanguage((e) => this.setRegisteredLanguage(this._languageService, e.newLanguage, this.language)));
this._textModelDisposables.add(this._textModel.onWillDispose(() => this.textModel = undefined));
this._textModelDisposables.add(this._textModel.onDidChangeContent((e) => {
if (this._textModel) {
this._versionId = this._textModel.getVersionId();
this._alternativeId = this._textModel.getAlternativeVersionId();
}
this._textBufferHash = null;
this._onDidChangeContent.fire('content');
this._onDidChangeContent.fire({ type: 'model', event: e });
}));
this._textModel._overwriteVersionId(this._versionId);
this._textModel._overwriteAlternativeVersionId(this._versionId);
}
}
private setRegisteredLanguage(languageService: ILanguageService, newLanguage: string, currentLanguage: string) {
// The language defined in the cell might not be supported in the editor so the text model might be using the default fallback
// If so let's not modify the language
const isFallBackLanguage = (newLanguage === PLAINTEXT_LANGUAGE_ID || newLanguage === 'jupyter');
if (!languageService.isRegisteredLanguageId(currentLanguage) && isFallBackLanguage) {
// notify to display warning, but don't change the language
this._onDidChangeLanguage.fire(currentLanguage);
} else {
this.language = newLanguage;
}
}
private static readonly AUTO_DETECT_LANGUAGE_THROTTLE_DELAY = 600;
private readonly autoDetectLanguageThrottler = this._register(new ThrottledDelayer<void>(NotebookCellTextModel.AUTO_DETECT_LANGUAGE_THROTTLE_DELAY));
private _autoLanguageDetectionEnabled: boolean = false;
private _hasLanguageSetExplicitly: boolean = false;
get hasLanguageSetExplicitly(): boolean { return this._hasLanguageSetExplicitly; }
constructor(
readonly uri: URI,
public readonly handle: number,
private readonly _source: string,
private _language: string,
private _mime: string | undefined,
public readonly cellKind: CellKind,
outputs: IOutputDto[],
metadata: NotebookCellMetadata | undefined,
internalMetadata: NotebookCellInternalMetadata | undefined,
public readonly collapseState: NotebookCellCollapseState | undefined,
public readonly transientOptions: TransientOptions,
private readonly _languageService: ILanguageService,
private readonly _languageDetectionService: ILanguageDetectionService | undefined = undefined
) {
super();
this._outputs = outputs.map(op => new NotebookCellOutputTextModel(op));
this._metadata = metadata ?? {};
this._internalMetadata = internalMetadata ?? {};
}
enableAutoLanguageDetection() {
this._autoLanguageDetectionEnabled = true;
this.autoDetectLanguage();
}
async autoDetectLanguage(): Promise<void> {
if (this._autoLanguageDetectionEnabled) {
this.autoDetectLanguageThrottler.trigger(() => this._doAutoDetectLanguage());
}
}
private async _doAutoDetectLanguage(): Promise<void> {
if (this.hasLanguageSetExplicitly) {
return;
}
const newLanguage = await this._languageDetectionService?.detectLanguage(this.uri);
if (!newLanguage) {
return;
}
if (this._textModel
&& this._textModel.getLanguageId() === this._languageService.getLanguageIdByLanguageName(newLanguage)
&& this._textModel.getLanguageId() === this._languageService.getLanguageIdByLanguageName(this.language)) {
return;
}
this._setLanguageInternal(newLanguage);
}
private _setLanguageInternal(newLanguage: string) {
const newLanguageId = this._languageService.getLanguageIdByLanguageName(newLanguage);
if (newLanguageId === null) {
return;
}
if (this._textModel) {
const languageId = this._languageService.createById(newLanguageId);
this._textModel.setLanguage(languageId.languageId);
}
if (this._language === newLanguage) {
return;
}
this._language = newLanguage;
this._hash = null;
this._onDidChangeLanguage.fire(newLanguage);
this._onDidChangeContent.fire('language');
}
resetTextBuffer(textBuffer: model.ITextBuffer) {
this._textBuffer = textBuffer;
}
getValue(): string {
const fullRange = this.getFullModelRange();
const eol = this.textBuffer.getEOL();
if (eol === '\n') {
return this.textBuffer.getValueInRange(fullRange, model.EndOfLinePreference.LF);
} else {
return this.textBuffer.getValueInRange(fullRange, model.EndOfLinePreference.CRLF);
}
}
getTextBufferHash() {
if (this._textBufferHash !== null) {
return this._textBufferHash;
}
const shaComputer = new StringSHA1();
const snapshot = this.textBuffer.createSnapshot(false);
let text: string | null;
while ((text = snapshot.read())) {
shaComputer.update(text);
}
this._textBufferHash = shaComputer.digest();
return this._textBufferHash;
}
getHashValue(): number {
if (this._hash !== null) {
return this._hash;
}
this._hash = hash([hash(this.language), this.getTextBufferHash(), this._getPersisentMetadata(), this.transientOptions.transientOutputs ? [] : this._outputs.map(op => ({
outputs: op.outputs.map(output => ({
mime: output.mime,
data: Array.from(output.data.buffer)
})),
metadata: op.metadata
}))]);
return this._hash;
}
private _getPersisentMetadata() {
return getFormattedMetadataJSON(this.transientOptions.transientCellMetadata, this.metadata, this.language);
}
getTextLength(): number {
return this.textBuffer.getLength();
}
getFullModelRange() {
const lineCount = this.textBuffer.getLineCount();
return new Range(1, 1, lineCount, this.textBuffer.getLineLength(lineCount) + 1);
}
spliceNotebookCellOutputs(splice: NotebookCellOutputsSplice): void {
if (splice.deleteCount > 0 && splice.newOutputs.length > 0) {
const commonLen = Math.min(splice.deleteCount, splice.newOutputs.length);
// update
for (let i = 0; i < commonLen; i++) {
const currentOutput = this.outputs[splice.start + i];
const newOutput = splice.newOutputs[i];
this.replaceOutput(currentOutput.outputId, newOutput);
}
const removed = this.outputs.splice(splice.start + commonLen, splice.deleteCount - commonLen, ...splice.newOutputs.slice(commonLen));
removed.forEach(output => output.dispose());
this._onDidChangeOutputs.fire({ start: splice.start + commonLen, deleteCount: splice.deleteCount - commonLen, newOutputs: splice.newOutputs.slice(commonLen) });
} else {
const removed = this.outputs.splice(splice.start, splice.deleteCount, ...splice.newOutputs);
removed.forEach(output => output.dispose());
this._onDidChangeOutputs.fire(splice);
}
}
replaceOutput(outputId: string, newOutputItem: ICellOutput) {
const outputIndex = this.outputs.findIndex(output => output.outputId === outputId);
if (outputIndex < 0) {
return false;
}
const output = this.outputs[outputIndex];
// convert to dto and dispose the cell output model
output.replaceData({
outputs: newOutputItem.outputs,
outputId: newOutputItem.outputId,
metadata: newOutputItem.metadata
});
newOutputItem.dispose();
this._onDidChangeOutputItems.fire();
return true;
}
changeOutputItems(outputId: string, append: boolean, items: IOutputItemDto[]): boolean {
const outputIndex = this.outputs.findIndex(output => output.outputId === outputId);
if (outputIndex < 0) {
return false;
}
const output = this.outputs[outputIndex];
if (append) {
output.appendData(items);
} else {
output.replaceData({ outputId: outputId, outputs: items, metadata: output.metadata });
}
this._onDidChangeOutputItems.fire();
return true;
}
private _outputNotEqualFastCheck(left: ICellOutput[], right: ICellOutput[]) {
if (left.length !== right.length) {
return false;
}
for (let i = 0; i < this.outputs.length; i++) {
const l = left[i];
const r = right[i];
if (l.outputs.length !== r.outputs.length) {
return false;
}
for (let k = 0; k < l.outputs.length; k++) {
if (l.outputs[k].mime !== r.outputs[k].mime) {
return false;
}
if (l.outputs[k].data.byteLength !== r.outputs[k].data.byteLength) {
return false;
}
}
}
return true;
}
equal(b: NotebookCellTextModel): boolean {
if (this.language !== b.language) {
return false;
}
if (this.outputs.length !== b.outputs.length) {
return false;
}
if (this.getTextLength() !== b.getTextLength()) {
return false;
}
if (!this.transientOptions.transientOutputs) {
// compare outputs
if (!this._outputNotEqualFastCheck(this.outputs, b.outputs)) {
return false;
}
}
return this.getHashValue() === b.getHashValue();
}
/**
* Only compares
* - language
* - mime
* - cellKind
* - internal metadata (conditionally)
* - source
*/
fastEqual(b: ICellDto2, ignoreMetadata: boolean): boolean {
if (this.language !== b.language) {
return false;
}
if (this.mime !== b.mime) {
return false;
}
if (this.cellKind !== b.cellKind) {
return false;
}
if (!ignoreMetadata) {
if (this.internalMetadata?.executionOrder !== b.internalMetadata?.executionOrder
|| this.internalMetadata?.lastRunSuccess !== b.internalMetadata?.lastRunSuccess
|| this.internalMetadata?.runStartTime !== b.internalMetadata?.runStartTime
|| this.internalMetadata?.runStartTimeAdjustment !== b.internalMetadata?.runStartTimeAdjustment
|| this.internalMetadata?.runEndTime !== b.internalMetadata?.runEndTime) {
return false;
}
}
// Once we attach the cell text buffer to an editor, the source of truth is the text buffer instead of the original source
if (this._textBuffer) {
if (!NotebookCellTextModel.linesAreEqual(this.textBuffer.getLinesContent(), b.source)) {
return false;
}
} else if (this._source !== b.source) {
return false;
}
return true;
}
private static linesAreEqual(aLines: string[], b: string) {
const bLines = splitLines(b);
if (aLines.length !== bLines.length) {
return false;
}
for (let i = 0; i < aLines.length; i++) {
if (aLines[i] !== bLines[i]) {
return false;
}
}
return true;
}
override dispose() {
dispose(this._outputs);
// Manually release reference to previous text buffer to avoid large leaks
// in case someone leaks a CellTextModel reference
const emptyDisposedTextBuffer = new PieceTreeTextBuffer([], '', '\n', false, false, true, true);
emptyDisposedTextBuffer.dispose();
this._textBuffer = emptyDisposedTextBuffer;
super.dispose();
}
}
export function cloneNotebookCellTextModel(cell: NotebookCellTextModel) {
return {
source: cell.getValue(),
language: cell.language,
mime: cell.mime,
cellKind: cell.cellKind,
outputs: cell.outputs.map(output => ({
outputs: output.outputs,
/* paste should generate new outputId */ outputId: UUID.generateUuid()
})),
metadata: {}
};
}
function computeRunStartTimeAdjustment(oldMetadata: NotebookCellInternalMetadata, newMetadata: NotebookCellInternalMetadata): number | undefined {
if (oldMetadata.runStartTime !== newMetadata.runStartTime && typeof newMetadata.runStartTime === 'number') {
const offset = Date.now() - newMetadata.runStartTime;
return offset < 0 ? Math.abs(offset) : 0;
} else {
return newMetadata.runStartTimeAdjustment;
}
}
export function getFormattedMetadataJSON(transientCellMetadata: TransientCellMetadata | undefined, metadata: NotebookCellMetadata, language?: string) {
let filteredMetadata: { [key: string]: any } = {};
if (transientCellMetadata) {
const keys = new Set([...Object.keys(metadata)]);
for (const key of keys) {
if (!(transientCellMetadata[key as keyof NotebookCellMetadata])
) {
filteredMetadata[key] = metadata[key as keyof NotebookCellMetadata];
}
}
} else {
filteredMetadata = metadata;
}
const obj = {
language,
...filteredMetadata
};
// Give preference to the language we have been given.
// Metadata can contain `language` due to round-tripping of cell metadata.
// I.e. we add it here, and then from SCM when we revert the cell, we get this same metadata back with the `language` property.
if (language) {
obj.language = language;
}
const metadataSource = toFormattedString(obj, {});
return metadataSource;
}