Skip to content

Commit 82f8def

Browse files
authored
Fixes #1525: notebook event is broken. (re-ordering cells) (#1526)
1 parent 04d7162 commit 82f8def

File tree

2 files changed

+65
-18
lines changed

2 files changed

+65
-18
lines changed

client/src/common/notebook.ts

Lines changed: 17 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -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
}

testbed/workspace/bug.ipynb

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
{
2+
"cells": [
3+
{
4+
"cell_type": "code",
5+
"execution_count": null,
6+
"metadata": {
7+
"vscode": {
8+
"languageId": "bat"
9+
}
10+
},
11+
"outputs": [],
12+
"source": [
13+
"REM Cell 1"
14+
]
15+
},
16+
{
17+
"cell_type": "code",
18+
"execution_count": null,
19+
"metadata": {
20+
"vscode": {
21+
"languageId": "bat"
22+
}
23+
},
24+
"outputs": [],
25+
"source": []
26+
},
27+
{
28+
"cell_type": "code",
29+
"execution_count": null,
30+
"metadata": {
31+
"vscode": {
32+
"languageId": "bat"
33+
}
34+
},
35+
"outputs": [],
36+
"source": [
37+
"REM Cell 2"
38+
]
39+
}
40+
],
41+
"metadata": {
42+
"language_info": {
43+
"name": "python"
44+
}
45+
},
46+
"nbformat": 4,
47+
"nbformat_minor": 2
48+
}

0 commit comments

Comments
 (0)