@@ -70,8 +70,11 @@ async function saveFileAndCloseAll(resource: vscode.Uri) {
70
70
await documentClosed ;
71
71
}
72
72
73
- async function saveAllFilesAndCloseAll ( resource : vscode . Uri ) {
73
+ async function saveAllFilesAndCloseAll ( resource : vscode . Uri | undefined ) {
74
74
const documentClosed = new Promise ( ( resolve , _reject ) => {
75
+ if ( ! resource ) {
76
+ return resolve ( ) ;
77
+ }
75
78
const d = vscode . notebook . onDidCloseNotebookDocument ( e => {
76
79
if ( e . uri . toString ( ) === resource . toString ( ) ) {
77
80
d . dispose ( ) ;
@@ -393,23 +396,107 @@ suite('Notebook API tests', () => {
393
396
await saveFileAndCloseAll ( resource ) ;
394
397
} ) ;
395
398
396
- test ( 'edit API' , async function ( ) {
399
+ test ( 'edit API (replaceCells) ' , async function ( ) {
397
400
assertInitalState ( ) ;
398
401
const resource = await createRandomFile ( '' , undefined , 'first' , '.vsctestnb' ) ;
399
402
await vscode . commands . executeCommand ( 'vscode.openWith' , resource , 'notebookCoreTest' ) ;
400
403
401
404
const cellsChangeEvent = getEventOncePromise < vscode . NotebookCellsChangeEvent > ( vscode . notebook . onDidChangeNotebookCells ) ;
402
405
await vscode . notebook . activeNotebookEditor ! . edit ( editBuilder => {
403
- editBuilder . insert ( 1 , 'test 2' , 'javascript' , vscode . CellKind . Code , [ ] , undefined ) ;
406
+ editBuilder . replaceCells ( 1 , 0 , [ { cellKind : vscode . CellKind . Code , language : 'javascript' , source : 'test 2' , outputs : [ ] , metadata : undefined } ] ) ;
404
407
} ) ;
405
408
406
409
const cellChangeEventRet = await cellsChangeEvent ;
407
- assert . equal ( cellChangeEventRet . document , vscode . notebook . activeNotebookEditor ?. document ) ;
408
- assert . equal ( cellChangeEventRet . changes . length , 1 ) ;
409
- assert . deepEqual ( cellChangeEventRet . changes [ 0 ] . start , 1 ) ;
410
- assert . deepEqual ( cellChangeEventRet . changes [ 0 ] . deletedCount , 0 ) ;
411
- assert . equal ( cellChangeEventRet . changes [ 0 ] . items [ 0 ] , vscode . notebook . activeNotebookEditor ! . document . cells [ 1 ] ) ;
410
+ assert . strictEqual ( cellChangeEventRet . document === vscode . notebook . activeNotebookEditor ?. document , true ) ;
411
+ assert . strictEqual ( cellChangeEventRet . document . isDirty , true ) ;
412
+ assert . strictEqual ( cellChangeEventRet . changes . length , 1 ) ;
413
+ assert . strictEqual ( cellChangeEventRet . changes [ 0 ] . start , 1 ) ;
414
+ assert . strictEqual ( cellChangeEventRet . changes [ 0 ] . deletedCount , 0 ) ;
415
+ assert . strictEqual ( cellChangeEventRet . changes [ 0 ] . items [ 0 ] === vscode . notebook . activeNotebookEditor ! . document . cells [ 1 ] , true ) ;
416
+
417
+ await saveAllFilesAndCloseAll ( resource ) ;
418
+ } ) ;
419
+
420
+ test ( 'edit API (replaceOutput)' , async function ( ) {
421
+ // no output events yet...
422
+ this . skip ( ) ;
423
+
424
+ assertInitalState ( ) ;
425
+ const resource = await createRandomFile ( '' , undefined , 'first' , '.vsctestnb' ) ;
426
+ await vscode . commands . executeCommand ( 'vscode.openWith' , resource , 'notebookCoreTest' ) ;
427
+
428
+ await vscode . notebook . activeNotebookEditor ! . edit ( editBuilder => {
429
+ editBuilder . replaceOutput ( 0 , [ { outputKind : vscode . CellOutputKind . Rich , data : { foo : 'bar' } } ] ) ;
430
+ } ) ;
431
+
432
+ const document = vscode . notebook . activeNotebookEditor ?. document ! ;
433
+ assert . strictEqual ( document . isDirty , false ) ;
434
+ assert . strictEqual ( document . cells . length , 1 ) ;
435
+ assert . strictEqual ( document . cells [ 0 ] . outputs . length , 1 ) ;
436
+ assert . strictEqual ( document . cells [ 0 ] . outputs [ 0 ] . outputKind , vscode . CellOutputKind . Rich ) ;
437
+
438
+ await saveAllFilesAndCloseAll ( undefined ) ;
439
+ } ) ;
440
+
441
+ test ( 'edit API (replaceOutput, event)' , async function ( ) {
442
+ // no output events yet...
443
+ this . skip ( ) ;
444
+
445
+ assertInitalState ( ) ;
446
+ const resource = await createRandomFile ( '' , undefined , 'first' , '.vsctestnb' ) ;
447
+ await vscode . commands . executeCommand ( 'vscode.openWith' , resource , 'notebookCoreTest' ) ;
448
+
449
+ const outputChangeEvent = getEventOncePromise < vscode . NotebookCellOutputsChangeEvent > ( vscode . notebook . onDidChangeCellOutputs ) ;
450
+ await vscode . notebook . activeNotebookEditor ! . edit ( editBuilder => {
451
+ editBuilder . replaceOutput ( 0 , [ { outputKind : vscode . CellOutputKind . Rich , data : { foo : 'bar' } } ] ) ;
452
+ } ) ;
453
+
454
+ const value = await outputChangeEvent ;
455
+ assert . strictEqual ( value . document === vscode . notebook . activeNotebookEditor ?. document , true ) ;
456
+ assert . strictEqual ( value . cells . length , 1 ) ;
457
+ assert . strictEqual ( value . cells [ 0 ] . outputs . length , 1 ) ;
458
+ assert . strictEqual ( value . cells [ 0 ] . outputs [ 0 ] . outputKind , vscode . CellOutputKind . Rich ) ;
459
+
460
+ await saveAllFilesAndCloseAll ( undefined ) ;
461
+ } ) ;
462
+
463
+ test ( 'edit API (replaceMetadata)' , async function ( ) {
464
+
465
+ assertInitalState ( ) ;
466
+ const resource = await createRandomFile ( '' , undefined , 'first' , '.vsctestnb' ) ;
467
+ await vscode . commands . executeCommand ( 'vscode.openWith' , resource , 'notebookCoreTest' ) ;
468
+
469
+ await vscode . notebook . activeNotebookEditor ! . edit ( editBuilder => {
470
+ editBuilder . replaceMetadata ( 0 , { inputCollapsed : true , executionOrder : 17 } ) ;
471
+ } ) ;
472
+
473
+ const document = vscode . notebook . activeNotebookEditor ?. document ! ;
474
+ assert . strictEqual ( document . cells . length , 1 ) ;
475
+ assert . strictEqual ( document . cells [ 0 ] . metadata . executionOrder , 17 ) ;
476
+ assert . strictEqual ( document . cells [ 0 ] . metadata . inputCollapsed , true ) ;
477
+
478
+ assert . strictEqual ( document . isDirty , true ) ;
479
+ await saveFileAndCloseAll ( resource ) ;
480
+ } ) ;
481
+
482
+ test ( 'edit API (replaceMetadata, event)' , async function ( ) {
483
+
484
+ assertInitalState ( ) ;
485
+ const resource = await createRandomFile ( '' , undefined , 'first' , '.vsctestnb' ) ;
486
+ await vscode . commands . executeCommand ( 'vscode.openWith' , resource , 'notebookCoreTest' ) ;
487
+
488
+ const event = getEventOncePromise < vscode . NotebookCellMetadataChangeEvent > ( vscode . notebook . onDidChangeCellMetadata ) ;
489
+
490
+ await vscode . notebook . activeNotebookEditor ! . edit ( editBuilder => {
491
+ editBuilder . replaceMetadata ( 0 , { inputCollapsed : true , executionOrder : 17 } ) ;
492
+ } ) ;
493
+
494
+ const data = await event ;
495
+ assert . strictEqual ( data . document , vscode . notebook . activeNotebookEditor ?. document ) ;
496
+ assert . strictEqual ( data . cell . metadata . executionOrder , 17 ) ;
497
+ assert . strictEqual ( data . cell . metadata . inputCollapsed , true ) ;
412
498
499
+ assert . strictEqual ( data . document . isDirty , true ) ;
413
500
await saveFileAndCloseAll ( resource ) ;
414
501
} ) ;
415
502
0 commit comments