Remove code used to store metadata in custom property for notebooks (#227418)

* Remove code used to store metadata in custom property for notebooks

* oops
This commit is contained in:
Don Jayamanne
2024-09-03 17:24:35 +10:00
committed by GitHub
parent b58b0450d7
commit 9b2e9634aa
8 changed files with 1164 additions and 1441 deletions

View File

@@ -4,7 +4,6 @@
*--------------------------------------------------------------------------------------------*/
import type * as nbformat from '@jupyterlab/nbformat';
import { workspace } from 'vscode';
/**
* Metadata we store in VS Code cell output items.
@@ -65,7 +64,3 @@ export interface CellMetadata {
*/
execution_count?: number;
}
export function useCustomPropertyInMetadata() {
return !workspace.getConfiguration('jupyter', undefined).get<boolean>('experimental.dropCustomMetadata', true);
}

View File

@@ -5,7 +5,7 @@
import type * as nbformat from '@jupyterlab/nbformat';
import { extensions, NotebookCellData, NotebookCellExecutionSummary, NotebookCellKind, NotebookCellOutput, NotebookCellOutputItem, NotebookData } from 'vscode';
import { CellMetadata, CellOutputMetadata, useCustomPropertyInMetadata } from './common';
import { CellMetadata, CellOutputMetadata } from './common';
const jupyterLanguageToMonacoLanguageMapping = new Map([
['c#', 'csharp'],
@@ -154,51 +154,25 @@ function convertJupyterOutputToBuffer(mime: string, value: unknown): NotebookCel
function getNotebookCellMetadata(cell: nbformat.IBaseCell): {
[key: string]: any;
} {
if (useCustomPropertyInMetadata()) {
const cellMetadata: { [key: string]: any } = {};
// We put this only for VSC to display in diff view.
// Else we don't use this.
const custom: CellMetadata = {};
if (cell.cell_type === 'code' && typeof cell['execution_count'] === 'number') {
custom.execution_count = cell['execution_count'];
}
if (cell['metadata']) {
custom['metadata'] = JSON.parse(JSON.stringify(cell['metadata']));
}
if ('id' in cell && typeof cell.id === 'string') {
custom.id = cell.id;
}
cellMetadata.custom = custom;
if (cell['attachments']) {
cellMetadata.attachments = JSON.parse(JSON.stringify(cell['attachments']));
}
return cellMetadata;
} else {
// We put this only for VSC to display in diff view.
// Else we don't use this.
const cellMetadata: CellMetadata = {};
if (cell.cell_type === 'code' && typeof cell['execution_count'] === 'number') {
cellMetadata.execution_count = cell['execution_count'];
}
if (cell['metadata']) {
cellMetadata['metadata'] = JSON.parse(JSON.stringify(cell['metadata']));
}
if ('id' in cell && typeof cell.id === 'string') {
cellMetadata.id = cell.id;
}
if (cell['attachments']) {
cellMetadata.attachments = JSON.parse(JSON.stringify(cell['attachments']));
}
return cellMetadata;
// We put this only for VSC to display in diff view.
// Else we don't use this.
const cellMetadata: CellMetadata = {};
if (cell.cell_type === 'code' && typeof cell['execution_count'] === 'number') {
cellMetadata.execution_count = cell['execution_count'];
}
if (cell['metadata']) {
cellMetadata['metadata'] = JSON.parse(JSON.stringify(cell['metadata']));
}
if ('id' in cell && typeof cell.id === 'string') {
cellMetadata.id = cell.id;
}
if (cell['attachments']) {
cellMetadata.attachments = JSON.parse(JSON.stringify(cell['attachments']));
}
return cellMetadata;
}
function getOutputMetadata(output: nbformat.IOutput): CellOutputMetadata {
@@ -391,6 +365,6 @@ export function jupyterNotebookModelToNotebookData(
.filter((item): item is NotebookCellData => !!item);
const notebookData = new NotebookData(cells);
notebookData.metadata = useCustomPropertyInMetadata() ? { custom: notebookContentWithoutCells } : notebookContentWithoutCells;
notebookData.metadata = notebookContentWithoutCells;
return notebookData;
}

View File

@@ -8,7 +8,6 @@ import { NotebookSerializer } from './notebookSerializer';
import { activate as keepNotebookModelStoreInSync } from './notebookModelStoreSync';
import { notebookImagePasteSetup } from './notebookImagePaste';
import { AttachmentCleaner } from './notebookAttachmentCleaner';
import { useCustomPropertyInMetadata } from './common';
// From {nbformat.INotebookMetadata} in @jupyterlab/coreutils
type NotebookMetadata = {
@@ -34,11 +33,7 @@ export function activate(context: vscode.ExtensionContext) {
keepNotebookModelStoreInSync(context);
context.subscriptions.push(vscode.workspace.registerNotebookSerializer('jupyter-notebook', serializer, {
transientOutputs: false,
transientCellMetadata: useCustomPropertyInMetadata() ? {
breakpointMargin: true,
custom: false,
attachments: false
} : {
transientCellMetadata: {
breakpointMargin: true,
id: false,
metadata: false,
@@ -51,11 +46,7 @@ export function activate(context: vscode.ExtensionContext) {
context.subscriptions.push(vscode.workspace.registerNotebookSerializer('interactive', serializer, {
transientOutputs: false,
transientCellMetadata: useCustomPropertyInMetadata() ? {
breakpointMargin: true,
custom: false,
attachments: false
} : {
transientCellMetadata: {
breakpointMargin: true,
id: false,
metadata: false,
@@ -84,14 +75,7 @@ export function activate(context: vscode.ExtensionContext) {
const language = 'python';
const cell = new vscode.NotebookCellData(vscode.NotebookCellKind.Code, '', language);
const data = new vscode.NotebookData([cell]);
data.metadata = useCustomPropertyInMetadata() ? {
custom: {
cells: [],
metadata: {},
nbformat: 4,
nbformat_minor: 2
}
} : {
data.metadata = {
cells: [],
metadata: {},
nbformat: 4,
@@ -119,7 +103,7 @@ export function activate(context: vscode.ExtensionContext) {
return {
get dropCustomMetadata() {
return !useCustomPropertyInMetadata();
return true;
},
exportNotebook: (notebook: vscode.NotebookData): string => {
return exportNotebook(notebook, serializer);
@@ -131,26 +115,13 @@ export function activate(context: vscode.ExtensionContext) {
}
const edit = new vscode.WorkspaceEdit();
if (useCustomPropertyInMetadata()) {
edit.set(resource, [vscode.NotebookEdit.updateNotebookMetadata({
...document.metadata,
custom: {
...(document.metadata.custom ?? {}),
metadata: <NotebookMetadata>{
...(document.metadata.custom?.metadata ?? {}),
...metadata
},
}
})]);
} else {
edit.set(resource, [vscode.NotebookEdit.updateNotebookMetadata({
...document.metadata,
metadata: <NotebookMetadata>{
...(document.metadata.metadata ?? {}),
...metadata
},
})]);
}
edit.set(resource, [vscode.NotebookEdit.updateNotebookMetadata({
...document.metadata,
metadata: <NotebookMetadata>{
...(document.metadata.metadata ?? {}),
...metadata
},
})]);
return vscode.workspace.applyEdit(edit);
},
};

View File

@@ -5,7 +5,7 @@
import { Disposable, ExtensionContext, NotebookCellKind, NotebookDocument, NotebookDocumentChangeEvent, NotebookEdit, workspace, WorkspaceEdit, type NotebookCell, type NotebookDocumentWillSaveEvent } from 'vscode';
import { getCellMetadata, getVSCodeCellLanguageId, removeVSCodeCellLanguageId, setVSCodeCellLanguageId, sortObjectPropertiesRecursively } from './serializers';
import { CellMetadata, useCustomPropertyInMetadata } from './common';
import { CellMetadata } from './common';
import { getNotebookMetadata } from './notebookSerializer';
import type * as nbformat from '@jupyterlab/nbformat';
@@ -108,17 +108,12 @@ function trackAndUpdateCellMetadata(notebook: NotebookDocument, updates: { cell:
pendingNotebookCellModelUpdates.set(notebook, pendingUpdates);
const edit = new WorkspaceEdit();
updates.forEach(({ cell, metadata }) => {
let newMetadata: any = {};
if (useCustomPropertyInMetadata()) {
newMetadata = { ...(cell.metadata), custom: metadata };
} else {
newMetadata = { ...cell.metadata, ...metadata };
if (!metadata.execution_count && newMetadata.execution_count) {
delete newMetadata.execution_count;
}
if (!metadata.attachments && newMetadata.attachments) {
delete newMetadata.attachments;
}
const newMetadata = { ...cell.metadata, ...metadata };
if (!metadata.execution_count && newMetadata.execution_count) {
delete newMetadata.execution_count;
}
if (!metadata.attachments && newMetadata.attachments) {
delete newMetadata.attachments;
}
edit.set(cell.notebook.uri, [NotebookEdit.updateCellMetadata(cell.index, sortObjectPropertiesRecursively(newMetadata))]);
});

View File

@@ -10,7 +10,6 @@ import { defaultNotebookFormat } from './constants';
import { getPreferredLanguage, jupyterNotebookModelToNotebookData } from './deserializers';
import { createJupyterCellFromNotebookCell, pruneCell, sortObjectPropertiesRecursively } from './serializers';
import * as fnv from '@enonic/fnv-plus';
import { useCustomPropertyInMetadata } from './common';
export class NotebookSerializer implements vscode.NotebookSerializer {
constructor(readonly context: vscode.ExtensionContext) {
@@ -93,7 +92,7 @@ export class NotebookSerializer implements vscode.NotebookSerializer {
}
export function getNotebookMetadata(document: vscode.NotebookDocument | vscode.NotebookData) {
const existingContent: Partial<nbformat.INotebookContent> = (useCustomPropertyInMetadata() ? document.metadata?.custom : document.metadata) || {};
const existingContent: Partial<nbformat.INotebookContent> = document.metadata || {};
const notebookContent: Partial<nbformat.INotebookContent> = {};
notebookContent.cells = existingContent.cells || [];
notebookContent.nbformat = existingContent.nbformat || defaultNotebookFormat.major;

View File

@@ -5,7 +5,7 @@
import type * as nbformat from '@jupyterlab/nbformat';
import { NotebookCell, NotebookCellData, NotebookCellKind, NotebookCellOutput } from 'vscode';
import { CellOutputMetadata, useCustomPropertyInMetadata, type CellMetadata } from './common';
import { CellOutputMetadata, type CellMetadata } from './common';
import { textMimeTypes } from './deserializers';
const textDecoder = new TextDecoder();
@@ -57,18 +57,6 @@ export function sortObjectPropertiesRecursively(obj: any): any {
export function getCellMetadata(options: { cell: NotebookCell | NotebookCellData } | { metadata?: { [key: string]: any } }): CellMetadata {
if ('cell' in options) {
const cell = options.cell;
if (useCustomPropertyInMetadata()) {
const metadata: CellMetadata = {
// it contains the cell id, and the cell metadata, along with other nb cell metadata
...(cell.metadata?.custom ?? {})
};
// promote the cell attachments to the top level
const attachments = cell.metadata?.custom?.attachments ?? cell.metadata?.attachments;
if (attachments) {
metadata.attachments = attachments;
}
return metadata;
}
const metadata = {
// it contains the cell id, and the cell metadata, along with other nb cell metadata
...(cell.metadata ?? {})
@@ -77,18 +65,6 @@ export function getCellMetadata(options: { cell: NotebookCell | NotebookCellData
return metadata;
} else {
const cell = options;
if (useCustomPropertyInMetadata()) {
const metadata: CellMetadata = {
// it contains the cell id, and the cell metadata, along with other nb cell metadata
...(cell.metadata?.custom ?? {})
};
// promote the cell attachments to the top level
const attachments = cell.metadata?.custom?.attachments ?? cell.metadata?.attachments;
if (attachments) {
metadata.attachments = attachments;
}
return metadata;
}
const metadata = {
// it contains the cell id, and the cell metadata, along with other nb cell metadata
...(cell.metadata ?? {})

File diff suppressed because it is too large Load Diff

File diff suppressed because it is too large Load Diff