@@ -530,7 +530,7 @@ class NotebookDocumentSyncFeatureProvider implements NotebookDocumentSyncFeature
530530 if ( cellMatches ) {
531531 // don't use cells from above since there might be more matching cells in the notebook
532532 // Since we had a matching cell above we will have matching cells now.
533- const matchingCells = this . mergeCells ( syncInfo , [ cell ] ) ;
533+ const matchingCells = this . mergeCells ( notebookDocument , syncInfo , [ cell ] ) ;
534534 if ( matchingCells !== undefined ) {
535535 const event = this . asNotebookDocumentChangeEvent ( notebookDocument , undefined , syncInfo , matchingCells ) ;
536536 if ( event !== undefined ) {
@@ -911,7 +911,6 @@ class NotebookDocumentSyncFeatureProvider implements NotebookDocumentSyncFeature
911911 }
912912 }
913913 }
914- const added : vscode . NotebookCell [ ] = [ ] ;
915914 if ( event . contentChanges !== undefined && event . contentChanges . length > 0 ) {
916915 if ( cells === undefined ) {
917916 cells = new Set ( syncInfo . uris ) ;
@@ -922,28 +921,23 @@ class NotebookDocumentSyncFeatureProvider implements NotebookDocumentSyncFeature
922921 }
923922 const filtered = this . filterCells ( notebookDocument , new Array ( ...item . addedCells ) , selector . cells ) ;
924923 for ( const cell of filtered ) {
925- if ( ! cells . has ( cell . document . uri . toString ( ) ) ) {
926- added . push ( cell ) ;
927- }
924+ cells . add ( cell . document . uri . toString ( ) ) ;
928925 }
929926 }
930927 }
931- if ( cells === undefined && added . length === 0 ) {
928+ if ( cells === undefined ) {
932929 return syncInfo . cells ;
933930 }
934931
932+ // Only return the cells that are still in the set, but keep the order
933+ // as present in the current notebook.
935934 const result : vscode . NotebookCell [ ] = [ ] ;
936- if ( cells !== undefined ) {
937- for ( const item of syncInfo . cells ) {
938- if ( cells . has ( item . document . uri . toString ( ) ) ) {
939- result . push ( item ) ;
940- }
935+ const current = notebookDocument . getCells ( ) ;
936+ for ( const cell of current ) {
937+ if ( cells . has ( cell . document . uri . toString ( ) ) ) {
938+ result . push ( cell ) ;
941939 }
942940 }
943- if ( added . length > 0 ) {
944- result . push ( ...added ) ;
945- }
946-
947941 return result ;
948942 }
949943
@@ -957,10 +951,15 @@ class NotebookDocumentSyncFeatureProvider implements NotebookDocumentSyncFeature
957951 return syncInfo !== undefined ? syncInfo . cells : this . getMatchingCells ( notebook ) ;
958952 }
959953
960- private mergeCells ( syncInfo : SyncInfo , cells : vscode . NotebookCell [ ] ) : vscode . NotebookCell [ ] {
961- const result = syncInfo . cells . slice ( ) ;
954+ private mergeCells ( notebookDocument : vscode . NotebookDocument , syncInfo : SyncInfo , cells : vscode . NotebookCell [ ] ) : vscode . NotebookCell [ ] {
955+ const result : vscode . NotebookCell [ ] = [ ] ;
956+ const merged = new Set ( syncInfo . uris ) ;
962957 for ( const cell of cells ) {
963- if ( ! syncInfo . uris . has ( cell . document . uri . toString ( ) ) ) {
958+ merged . add ( cell . document . uri . toString ( ) ) ;
959+ }
960+ // Keep the cell order of the current notebook.
961+ for ( const cell of notebookDocument . getCells ( ) ) {
962+ if ( merged . has ( cell . document . uri . toString ( ) ) ) {
964963 result . push ( cell ) ;
965964 }
966965 }
0 commit comments