adopt tests to new notebook change event

This commit is contained in:
Johannes
2022-03-29 11:58:54 +02:00
parent bf0b4ac976
commit b1faab40de
6 changed files with 85 additions and 73 deletions

View File

@@ -10,7 +10,8 @@
},
"enabledApiProposals": [
"notebookEditor",
"notebookEditorEdit"
"notebookEditorEdit",
"notebookDocumentEvents"
],
"activationEvents": [
"*"
@@ -31,9 +32,9 @@
"commands": [
{
"command": "ipynb.newUntitledIpynb",
"title": "New Jupyter Notebook",
"title": "New Jupyter Notebook",
"shortTitle": "Jupyter Notebook",
"category": "Create"
"category": "Create"
},
{
"command": "ipynb.openIpynbInNotebookEditor",
@@ -60,9 +61,9 @@
}
],
"commandPalette": [
{
"command": "ipynb.newUntitledIpynb"
},
{
"command": "ipynb.newUntitledIpynb"
},
{
"command": "ipynb.openIpynbInNotebookEditor",
"when": "false"

View File

@@ -3,7 +3,7 @@
* Licensed under the MIT License. See License.txt in the project root for license information.
*--------------------------------------------------------------------------------------------*/
import { ExtensionContext, NotebookCellsChangeEvent, NotebookDocument, notebooks, workspace, WorkspaceEdit } from 'vscode';
import { ExtensionContext, NotebookDocument, NotebookDocumentChangeEvent, workspace, WorkspaceEdit } from 'vscode';
import { v4 as uuid } from 'uuid';
import { getCellMetadata } from './serializers';
import { CellMetadata } from './common';
@@ -15,21 +15,21 @@ import * as nbformat from '@jupyterlab/nbformat';
* Details of the spec can be found here https://jupyter.org/enhancement-proposals/62-cell-id/cell-id.html#
*/
export function ensureAllNewCellsHaveCellIds(context: ExtensionContext) {
notebooks.onDidChangeNotebookCells(onDidChangeNotebookCells, undefined, context.subscriptions);
workspace.onDidChangeNotebookDocument(onDidChangeNotebookCells, undefined, context.subscriptions);
}
function onDidChangeNotebookCells(e: NotebookCellsChangeEvent) {
const nbMetadata = getNotebookMetadata(e.document);
function onDidChangeNotebookCells(e: NotebookDocumentChangeEvent) {
const nbMetadata = getNotebookMetadata(e.notebook);
if (!isCellIdRequired(nbMetadata)) {
return;
}
e.changes.forEach(change => {
change.items.forEach(cell => {
e.contentChanges.forEach(change => {
change.addedCells.forEach(cell => {
const cellMetadata = getCellMetadata(cell);
if (cellMetadata?.id) {
return;
}
const id = generateCellId(e.document);
const id = generateCellId(e.notebook);
const edit = new WorkspaceEdit();
// Don't edit the metadata directly, always get a clone (prevents accidental singletons and directly editing the objects).
const updatedMetadata: CellMetadata = { ...JSON.parse(JSON.stringify(cellMetadata || {})) };

View File

@@ -11,5 +11,6 @@
"../../src/vscode-dts/vscode.d.ts",
"../../src/vscode-dts/vscode.proposed.notebookEditor.d.ts",
"../../src/vscode-dts/vscode.proposed.notebookEditorEdit.d.ts",
"../../src/vscode-dts/vscode.proposed.notebookDocumentEvents.d.ts",
]
}