-
Notifications
You must be signed in to change notification settings - Fork 9
/
notebookConcatDocument.ts
559 lines (498 loc) · 21 KB
/
notebookConcatDocument.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
548
549
550
551
552
553
554
555
556
557
558
559
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.
'use strict';
import * as vscode from 'vscode';
import * as protocol from 'vscode-languageclient/node';
import * as path from 'path';
import * as shajs from 'sha.js';
import {
InteractiveInputScheme,
InteractiveScheme,
isInteractiveCell,
NotebookScheme,
PYTHON_LANGUAGE,
splitLines
} from './common/utils';
import {
DefaultWordPattern,
ensureValidWordDefinition,
getWordAtText,
regExpLeadsToEndlessLoop
} from './common/wordHelper';
import { NotebookConcatLine } from './notebookConcatLine';
import { RefreshNotebookEvent } from './common/types';
interface ICellRange {
uri: vscode.Uri;
fragment: number;
startOffset: number;
endOffset: number;
startLine: number;
}
const NotebookConcatPrefix = '_NotebookConcat_';
export class NotebookConcatDocument implements vscode.TextDocument, vscode.Disposable {
public get uri(): vscode.Uri {
return this.concatUri;
}
public get fileName(): string {
return this.uri.fsPath;
}
public get isUntitled(): boolean {
return true;
}
public get languageId(): string {
return PYTHON_LANGUAGE;
}
public get version(): number {
return this._version;
}
public get isDirty(): boolean {
return true;
}
public get isClosed(): boolean {
return this._closed;
}
public get eol(): vscode.EndOfLine {
return vscode.EndOfLine.LF;
}
public get lineCount(): number {
return this._lines.length;
}
public get notebook(): vscode.NotebookDocument | undefined {
// This represents a python file, so notebook should be undefined
return undefined;
}
public get concatUri(): vscode.Uri {
return this._concatUri || vscode.Uri.parse('');
}
public get notebookUri(): vscode.Uri {
return this._notebookUri || vscode.Uri.parse('');
}
private _interactiveWindow = false;
private _concatUri: vscode.Uri | undefined;
private _notebookUri: vscode.Uri | undefined;
private _version = 1;
private _closed = true;
private _lines: NotebookConcatLine[] = [];
private _contents: string = '';
private _cellRanges: ICellRange[] = [];
public handleChange(e: protocol.TextDocumentEdit): protocol.DidChangeTextDocumentParams | undefined {
this._version++;
const changes: protocol.TextDocumentContentChangeEvent[] = [];
const cellIndex = this._cellRanges.findIndex((c) => c.uri.toString() === e.textDocument.uri);
const cell = cellIndex >= 0 ? this._cellRanges[cellIndex] : undefined;
if (cell) {
e.edits.forEach((edit) => {
const normalized = edit.newText.replace(/\r/g, '');
const position = this.positionAt(cell.startOffset);
const from = new vscode.Position(position.line + edit.range.start.line, edit.range.start.character);
const to = new vscode.Position(position.line + edit.range.end.line, edit.range.end.character);
changes.push(...this.changeRange(normalized, from, to, cellIndex));
});
return this.toDidChangeTextDocumentParams(changes);
}
}
public handleOpen(e: protocol.TextDocumentItem): protocol.DidChangeTextDocumentParams | undefined {
const cellUri = vscode.Uri.parse(e.uri);
// Make sure we don't already have this cell open
if (this._cellRanges.find((c) => c.uri.toString() == e.uri)) {
// Can't open twice
return undefined;
}
this._version = Math.max(e.version, this._version + 1);
this._closed = false;
// Setup uri and such if first open
this.initialize(cellUri);
// Make sure to put a newline between this code and the next code
const newCode = `${e.text.replace(/\r/g, '')}\n`;
// Compute 'fragment' portion of URI. It's the tentative cell index
const fragment =
cellUri.scheme === InteractiveInputScheme ? -1 : parseInt(cellUri.fragment.substring(2) || '0');
// That fragment determines order in the list.
const insertIndex = this.computeInsertionIndex(fragment);
// Compute where we start from.
const fromOffset =
insertIndex < this._cellRanges.length && insertIndex >= 0
? this._cellRanges[insertIndex].startOffset
: this._contents.length;
// Split our text between the text and the cells above
const before = this._contents.substring(0, fromOffset);
const after = this._contents.substring(fromOffset);
const fromPosition = this.positionAt(fromOffset);
// Update our entire contents and recompute our lines
this._contents = `${before}${newCode}${after}`;
this._lines = this.createLines();
// Move all the other cell ranges down
for (let i = insertIndex; i <= this._cellRanges.length - 1; i += 1) {
this._cellRanges[i].startOffset += newCode.length;
this._cellRanges[i].endOffset += newCode.length;
}
const startOffset = fromOffset;
const endOffset = fromOffset + newCode.length;
this._cellRanges.splice(insertIndex, 0, {
uri: cellUri,
fragment,
startOffset,
endOffset,
startLine: this._lines.find((l) => l.offset === startOffset)?.lineNumber || 0
});
const changes: protocol.TextDocumentContentChangeEvent[] = [
{
range: this.createSerializableRange(fromPosition, fromPosition),
rangeOffset: fromOffset,
rangeLength: 0, // Opens are always zero
text: newCode
} as any
];
return this.toDidChangeTextDocumentParams(changes);
}
public handleClose(e: protocol.TextDocumentIdentifier): protocol.DidChangeTextDocumentParams | undefined {
const index = this._cellRanges.findIndex((c) => c.uri.toString() === e.uri);
// Setup uri and such if a reopen.
this.initialize(vscode.Uri.parse(e.uri));
// Ignore unless in notebook mode. For interactive, cells are still there.
if (index >= 0 && !this._interactiveWindow) {
this._version += 1;
const found = this._cellRanges[index];
const foundLength = found.endOffset - found.startOffset;
const from = new vscode.Position(this.getLineFromOffset(found.startOffset), 0);
const to = this.positionAt(found.endOffset);
// Remove from the cell ranges.
for (let i = index + 1; i <= this._cellRanges.length - 1; i += 1) {
this._cellRanges[i].startOffset -= foundLength;
this._cellRanges[i].endOffset -= foundLength;
}
this._cellRanges.splice(index, 1);
// Recreate the contents
const before = this._contents.substring(0, found.startOffset);
const after = this._contents.substring(found.endOffset);
this._contents = `${before}${after}`;
this._lines = this.createLines();
const changes: protocol.TextDocumentContentChangeEvent[] = [
{
range: this.createSerializableRange(from, to),
rangeOffset: found.startOffset,
rangeLength: foundLength,
text: ''
} as any
];
// If we closed the last cell, mark as closed
if (this._cellRanges.length == 0) {
this._closed = true;
}
return this.toDidChangeTextDocumentParams(changes);
}
}
public handleRefresh(e: RefreshNotebookEvent): protocol.DidChangeTextDocumentParams | undefined {
// Delete all cells and start over. This should only happen for non interactive (you can't move interactive cells at the moment)
if (!this._interactiveWindow) {
// Track our old full range
const from = new vscode.Position(0, 0);
const to = this.positionAt(this._contents.length);
const oldLength = this._contents.length;
const oldContents = this._contents;
const normalizedCellText = e.cells.map((c) => c.textDocument.text.replace(/\r/g, ''));
const newContents = `${normalizedCellText.join('\n')}\n`;
if (newContents != oldContents) {
this._version++;
this._cellRanges = [];
this._contents = newContents;
this._lines = this.createLines();
let startOffset = 0;
e.cells.forEach((c, i) => {
const cellText = normalizedCellText[i];
const cellUri = vscode.Uri.parse(c.textDocument.uri);
this._cellRanges.push({
uri: cellUri,
startOffset,
startLine: this._lines.find((l) => l.offset === startOffset)?.lineNumber || 0,
fragment:
cellUri.scheme === InteractiveInputScheme
? -1
: parseInt(cellUri.fragment.substring(2) || '0'),
endOffset: startOffset + cellText.length + 1 // Account for \n between cells
});
startOffset = this._cellRanges[this._cellRanges.length - 1].endOffset;
});
// Create one big change
const changes: protocol.TextDocumentContentChangeEvent[] = [
{
range: this.createSerializableRange(from, to),
rangeOffset: 0,
rangeLength: oldLength,
text: this._contents
} as any
];
return this.toDidChangeTextDocumentParams(changes);
}
}
return undefined;
}
public dispose() {
// Do nothing for now.
}
public contains(cellUri: vscode.Uri) {
return this._cellRanges.find((c) => c.uri.toString() === cellUri.toString()) !== undefined;
}
public save(): Promise<boolean> {
return Promise.resolve(false);
}
public lineAt(position: vscode.Position | number): vscode.TextLine {
if (typeof position === 'number') {
return this._lines[position as number];
} else {
return this._lines[position.line];
}
}
public offsetAt(position: vscode.Position | vscode.Location): number {
return this.convertToOffset(position);
}
public cellOffsetAt(offset: number): number {
const positionAt = this.positionAt(offset);
const locationAt = this.locationAt(positionAt);
const cell = this._cellRanges.find((c) => c.uri.toString() === locationAt.uri.toString());
if (cell) {
return offset - cell.startOffset;
}
return offset;
}
public positionAt(offsetOrPosition: number | vscode.Position | vscode.Location): vscode.Position {
if (typeof offsetOrPosition !== 'number') {
offsetOrPosition = this.offsetAt(offsetOrPosition);
}
let line = 0;
let ch = 0;
while (line + 1 < this._lines.length && this._lines[line + 1].offset <= offsetOrPosition) {
line += 1;
}
if (line < this._lines.length) {
ch = offsetOrPosition - this._lines[line].offset;
}
return new vscode.Position(line, ch);
}
public rangeOf(cellUri: vscode.Uri) {
const range = this._cellRanges.find((c) => c.uri.toString() === cellUri.toString());
if (range) {
const startPosition = this.positionAt(range.startOffset);
const endPosition = this.positionAt(range.endOffset);
return new vscode.Range(startPosition, endPosition);
}
}
public getText(range?: vscode.Range | undefined): string {
if (!range) {
return this._contents;
} else {
const startOffset = this.convertToOffset(range.start);
const endOffset = this.convertToOffset(range.end);
return this._contents.substring(startOffset, endOffset - startOffset);
}
}
public getCells(): vscode.Uri[] {
return this._cellRanges.map((c) => c.uri);
}
public locationAt(positionOrRange: vscode.Range | vscode.Position): vscode.Location {
if (positionOrRange instanceof vscode.Position) {
positionOrRange = new vscode.Range(positionOrRange, positionOrRange);
}
const startOffset = this.convertToOffset(positionOrRange.start);
const endOffset = this.convertToOffset(positionOrRange.end);
// Find cell with that contains the range
const cell = this._cellRanges.find((c) => startOffset >= c.startOffset && endOffset < c.endOffset);
// Find the start and end lines that contain the start and end offset
const startLine = this._lines.find((l) => startOffset >= l.offset && startOffset < l.endOffset);
const endLine = this._lines.find((l) => endOffset >= l.offset && endOffset < l.endOffset);
// Range is range within this location
const range =
startLine && endLine && cell
? new vscode.Range(
new vscode.Position(startLine.lineNumber - cell.startLine, startOffset - startLine.offset),
new vscode.Position(endLine.lineNumber - cell.startLine, endOffset - endLine.offset)
)
: new vscode.Range(new vscode.Position(0, 0), new vscode.Position(0, 0));
return {
uri: cell?.uri || this._cellRanges[0].uri,
range
};
}
public getWordRangeAtPosition(position: vscode.Position, regexp?: RegExp | undefined): vscode.Range | undefined {
if (!regexp) {
// use default when custom-regexp isn't provided
regexp = DefaultWordPattern;
} else if (regExpLeadsToEndlessLoop(regexp)) {
// use default when custom-regexp is bad
console.warn(
`[getWordRangeAtPosition]: ignoring custom regexp '${regexp.source}' because it matches the empty string.`
);
regexp = DefaultWordPattern;
}
const wordAtText = getWordAtText(
position.character + 1,
ensureValidWordDefinition(regexp),
this._lines[position.line].text,
0
);
if (wordAtText) {
return new vscode.Range(position.line, wordAtText.startColumn - 1, position.line, wordAtText.endColumn - 1);
}
return undefined;
}
public validateRange(range: vscode.Range): vscode.Range {
return range;
}
public validatePosition(position: vscode.Position): vscode.Position {
return position;
}
public get textDocumentItem(): protocol.TextDocumentItem {
return {
uri: this.concatUri.toString(),
languageId: this.languageId,
version: this.version,
text: this.getText()
};
}
public get textDocumentId(): protocol.VersionedTextDocumentIdentifier {
return {
uri: this.concatUri.toString(),
version: this.version
};
}
private toDidChangeTextDocumentParams(
changes: protocol.TextDocumentContentChangeEvent[]
): protocol.DidChangeTextDocumentParams {
return {
textDocument: {
version: this.version,
uri: this.concatUri.toString()
},
contentChanges: changes
};
}
private getLineFromOffset(offset: number) {
let lineCounter = 0;
for (let i = 0; i < offset; i += 1) {
if (this._contents[i] === '\n') {
lineCounter += 1;
}
}
return lineCounter;
}
private changeRange(
newText: string,
from: vscode.Position,
to: vscode.Position,
cellIndex: number
): protocol.TextDocumentContentChangeEvent[] {
const fromOffset = this.convertToOffset(from);
const toOffset = this.convertToOffset(to);
// Recreate our contents, and then recompute all of our lines
const before = this._contents.substring(0, fromOffset);
const after = this._contents.substring(toOffset);
this._contents = `${before}${newText}${after}`;
this._lines = this.createLines();
// Update ranges after this. All should move by the diff in length, although the current one
// should stay at the same start point.
const lengthDiff = newText.length - (toOffset - fromOffset);
for (let i = cellIndex; i < this._cellRanges.length; i += 1) {
if (i !== cellIndex) {
this._cellRanges[i].startOffset += lengthDiff;
}
this._cellRanges[i].endOffset += lengthDiff;
}
return [
{
range: this.createSerializableRange(from, to),
rangeOffset: fromOffset,
rangeLength: toOffset - fromOffset,
text: newText
} as any
];
}
private createLines(): NotebookConcatLine[] {
const split = splitLines(this._contents, { trim: false, removeEmptyEntries: false });
let prevLine: NotebookConcatLine | undefined;
return split.map((s, i) => {
const nextLine = this.createTextLine(s, i, prevLine);
prevLine = nextLine;
return nextLine;
});
}
private createTextLine(
contents: string,
lineNumber: number,
prevLine: NotebookConcatLine | undefined
): NotebookConcatLine {
return new NotebookConcatLine(
contents,
lineNumber,
prevLine ? prevLine.offset + prevLine.rangeIncludingLineBreak.end.character : 0
);
}
private convertToOffset(posOrLocation: vscode.Position | vscode.Location): number {
if (posOrLocation instanceof vscode.Location) {
const cell = this._cellRanges.find(
(c) => c.uri.toString() == (<vscode.Location>posOrLocation).uri.toString()
);
posOrLocation = cell
? new vscode.Position(
this.convertToPosition(cell.startOffset).line + posOrLocation.range.start.line,
posOrLocation.range.start.character
)
: posOrLocation.range.start;
}
if (posOrLocation.line < this._lines.length) {
return this._lines[posOrLocation.line].offset + posOrLocation.character;
}
return this._contents.length;
}
private convertToPosition(offset: number): vscode.Position {
let lineIndex = this._lines.findIndex((l) => l.offset > offset) - 1;
if (lineIndex < 0 && offset <= this._contents.length) {
lineIndex = this._lines.length - 1;
}
if (lineIndex >= 0) {
const offsetInLine = offset - this._lines[lineIndex].offset;
const lineRange = this._lines[lineIndex].rangeIncludingLineBreak;
return new vscode.Position(lineRange.start.line, offsetInLine);
}
return new vscode.Position(0, 0);
}
private createSerializableRange(start: vscode.Position, end: vscode.Position): vscode.Range {
// This funciton is necessary so that the Range can be passed back
// over a remote connection without including all of the extra fields that
// VS code puts into a Range object.
const result = {
start: {
line: start.line,
character: start.character
},
end: {
line: end.line,
character: end.character
}
};
return result as vscode.Range;
}
private computeInsertionIndex(fragment: number): number {
// Remember if last cell is already the input box
const inputBoxPresent = this._cellRanges[this._cellRanges.length - 1]?.uri.scheme === InteractiveInputScheme;
const totalLength = inputBoxPresent ? this._cellRanges.length - 1 : this._cellRanges.length;
// Find index based on fragment
const index =
fragment == -1 ? this._cellRanges.length : this._cellRanges.findIndex((c) => c.fragment > fragment);
return index < 0 ? totalLength : index;
}
private initialize(cellUri: vscode.Uri) {
if (!this._concatUri?.fsPath) {
this._interactiveWindow = isInteractiveCell(cellUri);
const dir = path.dirname(cellUri.fsPath);
// Path has to match no matter how many times we open it.
const concatFilePath = path.join(
dir,
`${NotebookConcatPrefix}${shajs('sha1').update(cellUri.fsPath).digest('hex').substring(0, 12)}.py`
);
this._concatUri = vscode.Uri.file(concatFilePath);
this._notebookUri = vscode.Uri.parse(
`${this._interactiveWindow ? InteractiveScheme : NotebookScheme}://${cellUri.fsPath}`
);
}
}
}