mirror of
https://github.com/microsoft/vscode.git
synced 2025-12-24 20:26:08 +00:00
Fix unnecessary changes
This commit is contained in:
@@ -8,7 +8,7 @@ import * as detectIndent from 'detect-indent';
|
||||
import * as vscode from 'vscode';
|
||||
import { defaultNotebookFormat } from './constants';
|
||||
import { getPreferredLanguage, jupyterNotebookModelToNotebookData } from './deserializers';
|
||||
import { createJupyterCellFromNotebookCell, pruneCell } from './serializers';
|
||||
import { createJupyterCellFromNotebookCell, pruneCell, sortObjectPropertiesRecursively } from './serializers';
|
||||
import * as fnv from '@enonic/fnv-plus';
|
||||
|
||||
export class NotebookSerializer implements vscode.NotebookSerializer {
|
||||
@@ -81,7 +81,7 @@ export class NotebookSerializer implements vscode.NotebookSerializer {
|
||||
const notebookContent: Partial<nbformat.INotebookContent> = data.metadata?.custom || {};
|
||||
notebookContent.cells = notebookContent.cells || [];
|
||||
notebookContent.nbformat = notebookContent.nbformat || 4;
|
||||
notebookContent.nbformat_minor = notebookContent.nbformat_minor || 2;
|
||||
notebookContent.nbformat_minor = notebookContent.nbformat_minor ?? 2;
|
||||
notebookContent.metadata = notebookContent.metadata || { orig_nbformat: 4 };
|
||||
|
||||
notebookContent.cells = data.cells
|
||||
@@ -91,6 +91,6 @@ export class NotebookSerializer implements vscode.NotebookSerializer {
|
||||
const indentAmount = data.metadata && 'indentAmount' in data.metadata && typeof data.metadata.indentAmount === 'string' ?
|
||||
data.metadata.indentAmount :
|
||||
' ';
|
||||
return JSON.stringify(notebookContent, undefined, indentAmount);
|
||||
return JSON.stringify(sortObjectPropertiesRecursively(notebookContent), undefined, indentAmount);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -30,6 +30,29 @@ export function createJupyterCellFromNotebookCell(
|
||||
return cell;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Sort the JSON to minimize unnecessary SCM changes.
|
||||
* Jupyter notbeooks/labs sorts the JSON keys in alphabetical order.
|
||||
* https://github.com/microsoft/vscode-python/issues/13155
|
||||
*/
|
||||
export function sortObjectPropertiesRecursively(obj: any): any {
|
||||
if (Array.isArray(obj)) {
|
||||
return obj.map(sortObjectPropertiesRecursively);
|
||||
}
|
||||
if (obj !== undefined && obj !== null && typeof obj === 'object' && Object.keys(obj).length > 0) {
|
||||
return (
|
||||
Object.keys(obj)
|
||||
.sort()
|
||||
.reduce<Record<string, any>>((sortedObj, prop) => {
|
||||
sortedObj[prop] = sortObjectPropertiesRecursively(obj[prop]);
|
||||
return sortedObj;
|
||||
}, {}) as any
|
||||
);
|
||||
}
|
||||
return obj;
|
||||
}
|
||||
|
||||
function createCodeCellFromNotebookCell(cell: NotebookCellData): nbformat.ICodeCell {
|
||||
const cellMetadata = cell.metadata?.custom as CellMetadata | undefined;
|
||||
const codeCell: any = {
|
||||
|
||||
Reference in New Issue
Block a user