|
3 | 3 | 'use strict';
|
4 | 4 | import '../../../common/extensions';
|
5 | 5 |
|
6 |
| -import * as monacoEditor from 'monaco-editor/esm/vs/editor/editor.api'; |
7 | 6 | import { EndOfLine, Position, Range, TextDocument, TextDocumentContentChangeEvent, TextLine, Uri } from 'vscode';
|
8 | 7 | import * as vscodeLanguageClient from 'vscode-languageclient';
|
9 | 8 |
|
10 | 9 | import { PYTHON_LANGUAGE } from '../../../common/constants';
|
11 | 10 | import { Identifiers } from '../../constants';
|
| 11 | +import { IEditorContentChange } from '../interactiveWindowTypes'; |
12 | 12 | import { DefaultWordPattern, ensureValidWordDefinition, getWordAtText, regExpLeadsToEndlessLoop } from './wordHelper';
|
13 | 13 |
|
14 | 14 | class IntellisenseLine implements TextLine {
|
@@ -201,54 +201,57 @@ export class IntellisenseDocument implements TextDocument {
|
201 | 201 | }
|
202 | 202 |
|
203 | 203 | public loadAllCells(cells: { code: string; id: string }[]): TextDocumentContentChangeEvent[] {
|
204 |
| - let changes: TextDocumentContentChangeEvent[] = []; |
205 | 204 | if (!this.inEditMode) {
|
206 | 205 | this.inEditMode = true;
|
207 |
| - this._version += 1; |
| 206 | + return this.reloadCells(cells); |
| 207 | + } |
| 208 | + return []; |
| 209 | + } |
208 | 210 |
|
209 |
| - // Normalize all of the cells, removing \r and separating each |
210 |
| - // with a newline |
211 |
| - const normalized = cells.map(c => { |
212 |
| - return { |
213 |
| - id: c.id, |
214 |
| - code: `${c.code.replace(/\r/g, '')}\n` |
215 |
| - }; |
216 |
| - }); |
| 211 | + public reloadCells(cells: { code: string; id: string }[]): TextDocumentContentChangeEvent[] { |
| 212 | + this._version += 1; |
| 213 | + |
| 214 | + // Normalize all of the cells, removing \r and separating each |
| 215 | + // with a newline |
| 216 | + const normalized = cells.map(c => { |
| 217 | + return { |
| 218 | + id: c.id, |
| 219 | + code: `${c.code.replace(/\r/g, '')}\n` |
| 220 | + }; |
| 221 | + }); |
217 | 222 |
|
218 |
| - // Contents are easy, just load all of the code in a row |
219 |
| - this._contents = normalized |
220 |
| - .map(c => c.code) |
221 |
| - .reduce((p, c) => { |
222 |
| - return `${p}${c}`; |
223 |
| - }); |
224 |
| - |
225 |
| - // Cell ranges are slightly more complicated |
226 |
| - let prev: number = 0; |
227 |
| - this._cellRanges = normalized.map(c => { |
228 |
| - const result = { |
229 |
| - id: c.id, |
230 |
| - start: prev, |
231 |
| - fullEnd: prev + c.code.length, |
232 |
| - currentEnd: prev + c.code.length |
233 |
| - }; |
234 |
| - prev += c.code.length; |
235 |
| - return result; |
| 223 | + // Contents are easy, just load all of the code in a row |
| 224 | + this._contents = normalized |
| 225 | + .map(c => c.code) |
| 226 | + .reduce((p, c) => { |
| 227 | + return `${p}${c}`; |
236 | 228 | });
|
237 | 229 |
|
238 |
| - // Then create the lines. |
239 |
| - this._lines = this.createLines(); |
| 230 | + // Cell ranges are slightly more complicated |
| 231 | + let prev: number = 0; |
| 232 | + this._cellRanges = normalized.map(c => { |
| 233 | + const result = { |
| 234 | + id: c.id, |
| 235 | + start: prev, |
| 236 | + fullEnd: prev + c.code.length, |
| 237 | + currentEnd: prev + c.code.length |
| 238 | + }; |
| 239 | + prev += c.code.length; |
| 240 | + return result; |
| 241 | + }); |
240 | 242 |
|
241 |
| - // Return our changes |
242 |
| - changes = [ |
243 |
| - { |
244 |
| - range: this.createSerializableRange(new Position(0, 0), new Position(0, 0)), |
245 |
| - rangeOffset: 0, |
246 |
| - rangeLength: 0, // Adds are always zero |
247 |
| - text: this._contents |
248 |
| - } |
249 |
| - ]; |
250 |
| - } |
251 |
| - return changes; |
| 243 | + // Then create the lines. |
| 244 | + this._lines = this.createLines(); |
| 245 | + |
| 246 | + // Return our changes |
| 247 | + return [ |
| 248 | + { |
| 249 | + range: this.createSerializableRange(new Position(0, 0), new Position(0, 0)), |
| 250 | + rangeOffset: 0, |
| 251 | + rangeLength: 0, // Adds are always zero |
| 252 | + text: this._contents |
| 253 | + } |
| 254 | + ]; |
252 | 255 | }
|
253 | 256 |
|
254 | 257 | public addCell(fullCode: string, currentCode: string, id: string): TextDocumentContentChangeEvent[] {
|
@@ -293,16 +296,33 @@ export class IntellisenseDocument implements TextDocument {
|
293 | 296 | ];
|
294 | 297 | }
|
295 | 298 |
|
296 |
| - public insertCell(id: string, code: string, codeCellAbove: string | undefined): TextDocumentContentChangeEvent[] { |
| 299 | + public reloadCell(id: string, code: string): TextDocumentContentChangeEvent[] { |
| 300 | + this._version += 1; |
| 301 | + |
| 302 | + // Make sure to put a newline between this code and the next code |
| 303 | + const newCode = `${code.replace(/\r/g, '')}\n`; |
| 304 | + |
| 305 | + // Figure where this goes |
| 306 | + const index = this._cellRanges.findIndex(r => r.id === id); |
| 307 | + if (index >= 0) { |
| 308 | + const start = this.positionAt(this._cellRanges[index].start); |
| 309 | + const end = this.positionAt(this._cellRanges[index].currentEnd); |
| 310 | + return this.removeRange(newCode, start, end, index); |
| 311 | + } |
| 312 | + |
| 313 | + return []; |
| 314 | + } |
| 315 | + |
| 316 | + public insertCell(id: string, code: string, codeCellAboveOrIndex: string | undefined | number): TextDocumentContentChangeEvent[] { |
297 | 317 | // This should only happen once for each cell.
|
298 | 318 | this._version += 1;
|
299 | 319 |
|
300 | 320 | // Make sure to put a newline between this code and the next code
|
301 | 321 | const newCode = `${code.replace(/\r/g, '')}\n`;
|
302 | 322 |
|
303 | 323 | // Figure where this goes
|
304 |
| - const aboveIndex = this._cellRanges.findIndex(r => r.id === codeCellAbove); |
305 |
| - const insertIndex = aboveIndex + 1; |
| 324 | + const aboveIndex = this._cellRanges.findIndex(r => r.id === codeCellAboveOrIndex); |
| 325 | + const insertIndex = typeof codeCellAboveOrIndex === 'number' ? codeCellAboveOrIndex : aboveIndex + 1; |
306 | 326 |
|
307 | 327 | // Compute where we start from.
|
308 | 328 | const fromOffset = insertIndex < this._cellRanges.length ? this._cellRanges[insertIndex].start : this._contents.length;
|
@@ -356,7 +376,7 @@ export class IntellisenseDocument implements TextDocument {
|
356 | 376 | return [];
|
357 | 377 | }
|
358 | 378 |
|
359 |
| - public edit(editorChanges: monacoEditor.editor.IModelContentChange[], id: string): TextDocumentContentChangeEvent[] { |
| 379 | + public editCell(editorChanges: IEditorContentChange[], id: string): TextDocumentContentChangeEvent[] { |
360 | 380 | this._version += 1;
|
361 | 381 |
|
362 | 382 | // Convert the range to local (and remove 1 based)
|
|
0 commit comments