Skip to content

Commit

Permalink
custom in metadata is optional based on ipynb ext
Browse files Browse the repository at this point in the history
  • Loading branch information
DonJayamanne committed Mar 20, 2024
1 parent 5dcb7eb commit 186a98d
Show file tree
Hide file tree
Showing 11 changed files with 1,780 additions and 1,617 deletions.
4 changes: 2 additions & 2 deletions src/interactive-window/editor-integration/cellFactory.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import { NotebookCellData, NotebookCellKind, NotebookDocument, Range, TextDocume
import { CellMatcher } from './cellMatcher';
import { ICellRange, IJupyterSettings } from '../../platform/common/types';
import { noop } from '../../platform/common/utils/misc';
import { parseForComments, generateMarkdownFromCodeLines } from '../../platform/common/utils';
import { parseForComments, generateMarkdownFromCodeLines, useCustomMetadata } from '../../platform/common/utils';
import { splitLines } from '../../platform/common/helpers';
import { isSysInfoCell } from '../systemInfoCell';
import { getCellMetadata } from '../../platform/common/utils';
Expand Down Expand Up @@ -145,7 +145,7 @@ export function generateCellsFromNotebookDocument(notebookDocument: NotebookDocu
if (cell.kind === NotebookCellKind.Code) {
cellData.outputs = [...cell.outputs];
}
cellData.metadata = { custom: getCellMetadata(cell) };
cellData.metadata = useCustomMetadata() ? { custom: getCellMetadata(cell) } : getCellMetadata(cell);
return cellData;
});
}
1,713 changes: 868 additions & 845 deletions src/kernels/execution/cellExecutionMessageHandler.unit.test.ts

Large diffs are not rendered by default.

549 changes: 290 additions & 259 deletions src/notebooks/controllers/preferredKernelConnectionService.unit.test.ts

Large diffs are not rendered by default.

121 changes: 76 additions & 45 deletions src/platform/common/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,9 @@ import {
TextDocument,
Uri,
WorkspaceEdit,
extensions,
workspace,
type NotebookCell,
type NotebookCellData
type NotebookCell
} from 'vscode';
import {
InteractiveWindowView,
Expand Down Expand Up @@ -168,47 +168,71 @@ export type NotebookMetadata = nbformat.INotebookMetadata & {
};

export function getNotebookMetadata(document: NotebookDocument | NotebookData): NotebookMetadata | undefined {
// eslint-disable-next-line @typescript-eslint/no-explicit-any
const notebookContent: undefined | Partial<nbformat.INotebookContent> = document.metadata?.custom as any;
// Create a clone.
return JSON.parse(JSON.stringify(notebookContent?.metadata || {}));
if (useCustomMetadata()) {
// eslint-disable-next-line @typescript-eslint/no-explicit-any
const notebookContent: undefined | Partial<nbformat.INotebookContent> = document.metadata?.custom as any;
// Create a clone.
return JSON.parse(JSON.stringify(notebookContent?.metadata || {}));
} else {
// eslint-disable-next-line @typescript-eslint/no-explicit-any
const notebookContent: undefined | Partial<nbformat.INotebookContent> = document.metadata as any;
// Create a clone.
return JSON.parse(JSON.stringify(notebookContent?.metadata || {}));
}
}

export function getNotebookFormat(document: NotebookDocument): {
nbformat: number | undefined;
nbformat_minor: number | undefined;
} {
// eslint-disable-next-line @typescript-eslint/no-explicit-any
const notebookContent: undefined | Partial<nbformat.INotebookContent> = document.metadata?.custom as any;
// Create a clone.
return {
nbformat: notebookContent?.nbformat,
nbformat_minor: notebookContent?.nbformat_minor
};
if (useCustomMetadata()) {
// eslint-disable-next-line @typescript-eslint/no-explicit-any
const notebookContent: undefined | Partial<nbformat.INotebookContent> = document.metadata?.custom as any;
// Create a clone.
return {
nbformat: notebookContent?.nbformat,
nbformat_minor: notebookContent?.nbformat_minor
};
} else {
// eslint-disable-next-line @typescript-eslint/no-explicit-any
const notebookContent: undefined | Partial<nbformat.INotebookContent> = document.metadata as any;
// Create a clone.
return {
nbformat: notebookContent?.nbformat,
nbformat_minor: notebookContent?.nbformat_minor
};
}
}

export async function updateNotebookMetadata(document: NotebookDocument, metadata: NotebookMetadata) {
const edit = new WorkspaceEdit();
// Create a clone.
const docMetadata = JSON.parse(
JSON.stringify(
(document.metadata as {
custom?: Exclude<Partial<nbformat.INotebookContent>, 'cells'>;
}) || { custom: {} }
)
);

docMetadata.custom = docMetadata.custom || {};
docMetadata.custom.metadata = metadata;

edit.set(document.uri, [
NotebookEdit.updateNotebookMetadata(
sortObjectPropertiesRecursively({
...(document.metadata || {}),
custom: docMetadata.custom
})
)
]);
if (useCustomMetadata()) {
// Create a clone.
const docMetadata: {
custom?: Exclude<Partial<nbformat.INotebookContent>, 'cells'>;
} = JSON.parse(JSON.stringify(document.metadata || { custom: {} }));

docMetadata.custom = docMetadata.custom || {};
docMetadata.custom.metadata = metadata;

edit.set(document.uri, [
NotebookEdit.updateNotebookMetadata(
sortObjectPropertiesRecursively({
...(document.metadata || {}),
custom: docMetadata.custom
})
)
]);
} else {
edit.set(document.uri, [
NotebookEdit.updateNotebookMetadata(
sortObjectPropertiesRecursively({
...(document.metadata || {}),
metadata
})
)
]);
}
await workspace.applyEdit(edit);
}

Expand Down Expand Up @@ -430,21 +454,28 @@ type JupyterCellMetadata = Pick<nbformat.IRawCell, 'id' | 'metadata' | 'attachme
// eslint-disable-next-line @typescript-eslint/no-explicit-any
Record<string, any>;

export function getCellMetadata(cell: NotebookCell | NotebookCellData): JupyterCellMetadata {
const metadata: JupyterCellMetadata = cell.metadata?.custom || {};
const cellMetadata = metadata as nbformat.IRawCell;
// metadata property is never optional.
cellMetadata.metadata = cellMetadata.metadata || {};
export function getCellMetadata(cell: NotebookCell): JupyterCellMetadata {
if (useCustomMetadata()) {
const metadata: JupyterCellMetadata = cell.metadata.custom || {};
const cellMetadata = metadata as nbformat.IRawCell;
// metadata property is never optional.
cellMetadata.metadata = cellMetadata.metadata || {};

return metadata;
return metadata;
} else {
const metadata: JupyterCellMetadata = cell.metadata.metadata || {};
// metadata property is never optional.
metadata.metadata = metadata.metadata || {};
return metadata;
}
}

// function useCustomMetadata() {
// if (extensions.getExtension('vscode.ipynb')?.exports.dropCustomMetadata) {
// return false;
// }
// return true;
// }
export function useCustomMetadata() {
if (extensions.getExtension('vscode.ipynb')?.exports.dropCustomMetadata) {
return false;
}
return true;
}

/**
* Sort the JSON to minimize unnecessary SCM changes.
Expand Down
Loading

0 comments on commit 186a98d

Please sign in to comment.