@@ -27,7 +27,9 @@ import { JupyterNotebookView } from '../constants';
27
27
// tslint:disable-next-line: no-var-requires no-require-imports
28
28
const vscodeNotebookEnums = require ( 'vscode' ) as typeof import ( 'vscode-proposed' ) ;
29
29
// tslint:disable-next-line: no-require-imports
30
+ import { url } from 'inspector' ;
30
31
import cloneDeep = require( 'lodash/cloneDeep' ) ;
32
+ import { WorkspaceEdit } from 'vscode' ;
31
33
import { isUntitledFile } from '../../../common/utils/misc' ;
32
34
import { KernelConnectionMetadata } from '../../jupyter/kernels/types' ;
33
35
import { updateNotebookMetadata } from '../../notebookStorage/baseModel' ;
@@ -339,58 +341,59 @@ export function createIOutputFromCellOutputs(cellOutputs: CellOutput[]): nbforma
339
341
. map ( ( output ) => output ! ! ) ;
340
342
}
341
343
342
- export function clearCellForExecution ( editor : NotebookEditor , cell : NotebookCell ) {
343
- editor . edit ( ( edit ) => {
344
- const cellIndex = editor . document . cells . indexOf ( cell ) ;
345
- edit . replaceMetadata ( cellIndex , {
346
- ...cell . metadata ,
347
- statusMessage : undefined ,
348
- executionOrder : undefined ,
349
- lastRunDuration : undefined ,
350
- runStartTime : undefined
351
- } ) ;
352
- edit . replaceOutput ( cellIndex , [ ] ) ;
344
+ export function clearCellForExecution ( cell : NotebookCell ) {
345
+ const cellIndex = cell . notebook . cells . indexOf ( cell ) ;
346
+ new WorkspaceEdit ( ) . replaceCellMetadata ( cell . notebook . uri , cellIndex , {
347
+ ...cell . metadata ,
348
+ statusMessage : undefined ,
349
+ executionOrder : undefined ,
350
+ lastRunDuration : undefined ,
351
+ runStartTime : undefined
353
352
} ) ;
353
+ new WorkspaceEdit ( ) . replaceCellOutput ( cell . notebook . uri , cellIndex , [ ] ) ;
354
354
355
- updateCellExecutionTimes ( editor , cell ) ;
355
+ updateCellExecutionTimes ( cell ) ;
356
356
}
357
357
358
358
/**
359
359
* Store execution start and end times.
360
360
* Stored as ISO for portability.
361
361
*/
362
362
export function updateCellExecutionTimes (
363
- editor : NotebookEditor ,
364
363
cell : NotebookCell ,
365
- times ?: { startTime ?: number ; duration ?: number }
364
+ times ?: { startTime ?: number ; duration ?: number ; lastRunDuration ?: number }
366
365
) {
367
- editor . edit ( ( edit ) => {
368
- const cellIndex = editor . document . cells . indexOf ( cell ) ;
369
- if ( ! times || ! times . duration || ! times . startTime ) {
370
- const cellMetadata = cloneDeep ( cell . metadata ) ;
371
- let updated = false ;
372
- if ( cellMetadata . custom ?. metadata ?. vscode ?. start_execution_time ) {
373
- delete cellMetadata . custom . metadata . vscode . start_execution_time ;
374
- updated = true ;
375
- }
376
- if ( cellMetadata . custom ?. metadata ?. vscode ?. end_execution_time ) {
377
- delete cellMetadata . custom . metadata . vscode . end_execution_time ;
378
- updated = true ;
379
- }
380
- if ( updated ) {
381
- edit . replaceMetadata ( cellIndex , { ...cellMetadata } ) ;
382
- }
383
- return ;
366
+ const cellIndex = cell . notebook . cells . indexOf ( cell ) ;
367
+
368
+ if ( ! times || ! times . duration || ! times . startTime ) {
369
+ const cellMetadata = cloneDeep ( cell . metadata ) ;
370
+ let updated = false ;
371
+ if ( cellMetadata . custom ?. metadata ?. vscode ?. start_execution_time ) {
372
+ delete cellMetadata . custom . metadata . vscode . start_execution_time ;
373
+ updated = true ;
374
+ }
375
+ if ( cellMetadata . custom ?. metadata ?. vscode ?. end_execution_time ) {
376
+ delete cellMetadata . custom . metadata . vscode . end_execution_time ;
377
+ updated = true ;
384
378
}
379
+ if ( updated ) {
380
+ new WorkspaceEdit ( ) . replaceCellMetadata ( cell . notebook . uri , cellIndex , { ...cellMetadata } ) ;
381
+ }
382
+ return ;
383
+ }
385
384
386
- const startTimeISO = new Date ( times . startTime ) . toISOString ( ) ;
387
- const endTimeISO = new Date ( times . startTime + times . duration ) . toISOString ( ) ;
388
- const customMetadata = cloneDeep ( cell . metadata . custom || { } ) ;
389
- customMetadata . metadata = customMetadata . metadata || { } ;
390
- customMetadata . metadata . vscode = customMetadata . metadata . vscode || { } ;
391
- customMetadata . metadata . vscode . end_execution_time = endTimeISO ;
392
- customMetadata . metadata . vscode . start_execution_time = startTimeISO ;
393
- edit . replaceMetadata ( cellIndex , { ...cell . metadata , custom : customMetadata } ) ;
385
+ const startTimeISO = new Date ( times . startTime ) . toISOString ( ) ;
386
+ const endTimeISO = new Date ( times . startTime + times . duration ) . toISOString ( ) ;
387
+ const customMetadata = cloneDeep ( cell . metadata . custom || { } ) ;
388
+ customMetadata . metadata = customMetadata . metadata || { } ;
389
+ customMetadata . metadata . vscode = customMetadata . metadata . vscode || { } ;
390
+ customMetadata . metadata . vscode . end_execution_time = endTimeISO ;
391
+ customMetadata . metadata . vscode . start_execution_time = startTimeISO ;
392
+ const lastRunDuration = times . lastRunDuration ?? cell . metadata . lastRunDuration ;
393
+ new WorkspaceEdit ( ) . replaceCellMetadata ( cell . notebook . uri , cellIndex , {
394
+ ...cell . metadata ,
395
+ custom : customMetadata ,
396
+ lastRunDuration
394
397
} ) ;
395
398
}
396
399
@@ -662,11 +665,7 @@ export function getCellStatusMessageBasedOnFirstCellErrorOutput(outputs?: CellOu
662
665
/**
663
666
* Updates a notebook document as a result of trusting it.
664
667
*/
665
- export function updateVSCNotebookAfterTrustingNotebook (
666
- editor : NotebookEditor ,
667
- document : NotebookDocument ,
668
- originalCells : ICell [ ]
669
- ) {
668
+ export function updateVSCNotebookAfterTrustingNotebook ( document : NotebookDocument , originalCells : ICell [ ] ) {
670
669
const areAllCellsEditableAndRunnable = document . cells . every ( ( cell ) => {
671
670
if ( cell . cellKind === vscodeNotebookEnums . CellKind . Markdown ) {
672
671
return cell . metadata . editable ;
@@ -690,16 +689,23 @@ export function updateVSCNotebookAfterTrustingNotebook(
690
689
document . metadata . editable = true ;
691
690
document . metadata . runnable = true ;
692
691
693
- editor . edit ( ( edit ) => {
694
- document . cells . forEach ( ( cell , index ) => {
695
- if ( cell . cellKind === vscodeNotebookEnums . CellKind . Markdown ) {
696
- edit . replaceMetadata ( index , { ...cell . metadata , editable : true } ) ;
697
- } else {
698
- edit . replaceMetadata ( index , { ...cell . metadata , editable : true , runnable : true } ) ;
699
- // Restore the output once we trust the notebook.
700
- // tslint:disable-next-line: no-any
701
- edit . replaceOutput ( index , createVSCCellOutputsFromOutputs ( originalCells [ index ] . data . outputs as any ) ) ;
702
- }
703
- } ) ;
692
+ const workspaceEdit = new WorkspaceEdit ( ) ;
693
+ document . cells . forEach ( ( cell , index ) => {
694
+ if ( cell . cellKind === vscodeNotebookEnums . CellKind . Markdown ) {
695
+ workspaceEdit . replaceCellMetadata ( document . uri , index , { ...cell . metadata , editable : true } ) ;
696
+ } else {
697
+ workspaceEdit . replaceCellMetadata ( document . uri , index , {
698
+ ...cell . metadata ,
699
+ editable : true ,
700
+ runnable : true
701
+ } ) ;
702
+ // Restore the output once we trust the notebook.
703
+ // tslint:disable-next-line: no-any
704
+ workspaceEdit . replaceCellOutput (
705
+ document . uri ,
706
+ index ,
707
+ createVSCCellOutputsFromOutputs ( originalCells [ index ] . data . outputs as any )
708
+ ) ;
709
+ }
704
710
} ) ;
705
711
}
0 commit comments