@@ -423,7 +423,7 @@ suite('Notebook API tests', () => {
423423 await vscode . commands . executeCommand ( 'vscode.openWith' , resource , 'notebookCoreTest' ) ;
424424
425425 await vscode . notebook . activeNotebookEditor ! . edit ( editBuilder => {
426- editBuilder . replaceOutput ( 0 , [ { outputKind : vscode . CellOutputKind . Rich , data : { foo : 'bar' } } ] ) ;
426+ editBuilder . replaceCellOutput ( 0 , [ { outputKind : vscode . CellOutputKind . Rich , data : { foo : 'bar' } } ] ) ;
427427 } ) ;
428428
429429 const document = vscode . notebook . activeNotebookEditor ?. document ! ;
@@ -442,7 +442,7 @@ suite('Notebook API tests', () => {
442442
443443 const outputChangeEvent = getEventOncePromise < vscode . NotebookCellOutputsChangeEvent > ( vscode . notebook . onDidChangeCellOutputs ) ;
444444 await vscode . notebook . activeNotebookEditor ! . edit ( editBuilder => {
445- editBuilder . replaceOutput ( 0 , [ { outputKind : vscode . CellOutputKind . Rich , data : { foo : 'bar' } } ] ) ;
445+ editBuilder . replaceCellOutput ( 0 , [ { outputKind : vscode . CellOutputKind . Rich , data : { foo : 'bar' } } ] ) ;
446446 } ) ;
447447
448448 const value = await outputChangeEvent ;
@@ -462,7 +462,7 @@ suite('Notebook API tests', () => {
462462 await vscode . commands . executeCommand ( 'vscode.openWith' , resource , 'notebookCoreTest' ) ;
463463
464464 await vscode . notebook . activeNotebookEditor ! . edit ( editBuilder => {
465- editBuilder . replaceMetadata ( 0 , { inputCollapsed : true , executionOrder : 17 } ) ;
465+ editBuilder . replaceCellMetadata ( 0 , { inputCollapsed : true , executionOrder : 17 } ) ;
466466 } ) ;
467467
468468 const document = vscode . notebook . activeNotebookEditor ?. document ! ;
@@ -483,7 +483,7 @@ suite('Notebook API tests', () => {
483483 const event = getEventOncePromise < vscode . NotebookCellMetadataChangeEvent > ( vscode . notebook . onDidChangeCellMetadata ) ;
484484
485485 await vscode . notebook . activeNotebookEditor ! . edit ( editBuilder => {
486- editBuilder . replaceMetadata ( 0 , { inputCollapsed : true , executionOrder : 17 } ) ;
486+ editBuilder . replaceCellMetadata ( 0 , { inputCollapsed : true , executionOrder : 17 } ) ;
487487 } ) ;
488488
489489 const data = await event ;
@@ -495,6 +495,134 @@ suite('Notebook API tests', () => {
495495 await saveFileAndCloseAll ( resource ) ;
496496 } ) ;
497497
498+ test ( 'workspace edit API (replaceCells)' , async function ( ) {
499+
500+ assertInitalState ( ) ;
501+ const resource = await createRandomFile ( '' , undefined , 'first' , '.vsctestnb' ) ;
502+ await vscode . commands . executeCommand ( 'vscode.openWith' , resource , 'notebookCoreTest' ) ;
503+
504+ const { document } = vscode . notebook . activeNotebookEditor ! ;
505+ assert . strictEqual ( document . cells . length , 1 ) ;
506+
507+ // inserting two new cells
508+ {
509+ const edit = new vscode . WorkspaceEdit ( ) ;
510+ edit . replaceCells ( document . uri , 0 , 0 , [ {
511+ cellKind : vscode . CellKind . Markdown ,
512+ language : 'markdown' ,
513+ metadata : undefined ,
514+ outputs : [ ] ,
515+ source : 'new_markdown'
516+ } , {
517+ cellKind : vscode . CellKind . Code ,
518+ language : 'fooLang' ,
519+ metadata : undefined ,
520+ outputs : [ ] ,
521+ source : 'new_code'
522+ } ] ) ;
523+
524+ const success = await vscode . workspace . applyEdit ( edit ) ;
525+ assert . strictEqual ( success , true ) ;
526+ }
527+
528+ assert . strictEqual ( document . cells . length , 3 ) ;
529+ assert . strictEqual ( document . cells [ 0 ] . document . getText ( ) , 'new_markdown' ) ;
530+ assert . strictEqual ( document . cells [ 1 ] . document . getText ( ) , 'new_code' ) ;
531+
532+ // deleting cell 1 and 3
533+ {
534+ const edit = new vscode . WorkspaceEdit ( ) ;
535+ edit . replaceCells ( document . uri , 0 , 1 , [ ] ) ;
536+ edit . replaceCells ( document . uri , 2 , 3 , [ ] ) ;
537+ const success = await vscode . workspace . applyEdit ( edit ) ;
538+ assert . strictEqual ( success , true ) ;
539+ }
540+
541+ assert . strictEqual ( document . cells . length , 1 ) ;
542+ assert . strictEqual ( document . cells [ 0 ] . document . getText ( ) , 'new_code' ) ;
543+
544+ // replacing all cells
545+ {
546+ const edit = new vscode . WorkspaceEdit ( ) ;
547+ edit . replaceCells ( document . uri , 0 , 1 , [ {
548+ cellKind : vscode . CellKind . Markdown ,
549+ language : 'markdown' ,
550+ metadata : undefined ,
551+ outputs : [ ] ,
552+ source : 'new2_markdown'
553+ } , {
554+ cellKind : vscode . CellKind . Code ,
555+ language : 'fooLang' ,
556+ metadata : undefined ,
557+ outputs : [ ] ,
558+ source : 'new2_code'
559+ } ] ) ;
560+ const success = await vscode . workspace . applyEdit ( edit ) ;
561+ assert . strictEqual ( success , true ) ;
562+ }
563+ assert . strictEqual ( document . cells . length , 2 ) ;
564+ assert . strictEqual ( document . cells [ 0 ] . document . getText ( ) , 'new2_markdown' ) ;
565+ assert . strictEqual ( document . cells [ 1 ] . document . getText ( ) , 'new2_code' ) ;
566+
567+ // remove all cells
568+ {
569+ const edit = new vscode . WorkspaceEdit ( ) ;
570+ edit . replaceCells ( document . uri , 0 , document . cells . length , [ ] ) ;
571+ const success = await vscode . workspace . applyEdit ( edit ) ;
572+ assert . strictEqual ( success , true ) ;
573+ }
574+ assert . strictEqual ( document . cells . length , 0 ) ;
575+
576+ await saveFileAndCloseAll ( resource ) ;
577+ } ) ;
578+
579+ test ( 'workspace edit API (replaceCells, event)' , async function ( ) {
580+
581+ assertInitalState ( ) ;
582+ const resource = await createRandomFile ( '' , undefined , 'first' , '.vsctestnb' ) ;
583+ await vscode . commands . executeCommand ( 'vscode.openWith' , resource , 'notebookCoreTest' ) ;
584+
585+ const { document } = vscode . notebook . activeNotebookEditor ! ;
586+ assert . strictEqual ( document . cells . length , 1 ) ;
587+
588+ const edit = new vscode . WorkspaceEdit ( ) ;
589+ edit . replaceCells ( document . uri , 0 , 0 , [ {
590+ cellKind : vscode . CellKind . Markdown ,
591+ language : 'markdown' ,
592+ metadata : undefined ,
593+ outputs : [ ] ,
594+ source : 'new_markdown'
595+ } , {
596+ cellKind : vscode . CellKind . Code ,
597+ language : 'fooLang' ,
598+ metadata : undefined ,
599+ outputs : [ ] ,
600+ source : 'new_code'
601+ } ] ) ;
602+
603+ const event = getEventOncePromise < vscode . NotebookCellsChangeEvent > ( vscode . notebook . onDidChangeNotebookCells ) ;
604+
605+ const success = await vscode . workspace . applyEdit ( edit ) ;
606+ assert . strictEqual ( success , true ) ;
607+
608+ const data = await event ;
609+
610+ // check document
611+ assert . strictEqual ( document . cells . length , 3 ) ;
612+ assert . strictEqual ( document . cells [ 0 ] . document . getText ( ) , 'new_markdown' ) ;
613+ assert . strictEqual ( document . cells [ 1 ] . document . getText ( ) , 'new_code' ) ;
614+
615+ // check event data
616+ assert . strictEqual ( data . document === document , true ) ;
617+ assert . strictEqual ( data . changes . length , 1 ) ;
618+ assert . strictEqual ( data . changes [ 0 ] . deletedCount , 0 ) ;
619+ assert . strictEqual ( data . changes [ 0 ] . deletedItems . length , 0 ) ;
620+ assert . strictEqual ( data . changes [ 0 ] . items . length , 2 ) ;
621+ assert . strictEqual ( data . changes [ 0 ] . items [ 0 ] , document . cells [ 0 ] ) ;
622+ assert . strictEqual ( data . changes [ 0 ] . items [ 1 ] , document . cells [ 1 ] ) ;
623+ await saveFileAndCloseAll ( resource ) ;
624+ } ) ;
625+
498626 test ( 'initialzation should not emit cell change events.' , async function ( ) {
499627 assertInitalState ( ) ;
500628 const resource = await createRandomFile ( '' , undefined , 'first' , '.vsctestnb' ) ;
0 commit comments