Optimize indentation detection

This commit is contained in:
Don Jayamanne
2021-08-17 14:51:53 -07:00
parent 0882f1e571
commit 09f68d545d
3 changed files with 42 additions and 4 deletions

View File

@@ -4,7 +4,6 @@
*--------------------------------------------------------------------------------------------*/
import type { nbformat } from '@jupyterlab/coreutils';
import * as detectIndent from 'detect-indent';
import * as vscode from 'vscode';
import { defaultNotebookFormat } from './constants';
import { getPreferredLanguage, jupyterNotebookModelToNotebookData } from './deserializers';
@@ -41,7 +40,7 @@ export class NotebookSerializer implements vscode.NotebookSerializer {
}
// Then compute indent from the contents
const indentAmount = contents ? detectIndent(contents).indent : ' ';
const indentAmount = contents ? detectIndent(contents) : ' ';
const preferredCellLanguage = getPreferredLanguage(json.metadata);
// Ensure we always have a blank cell.
@@ -94,3 +93,17 @@ export class NotebookSerializer implements vscode.NotebookSerializer {
return JSON.stringify(notebookContent, undefined, indentAmount);
}
}
export function detectIndent(jsonString: string) {
// ipynb is a JSON string of Object, hence first character will always `{`.
// Lets just take the distance between the first `{` and the next non-white space character`, ignoring \r & \n
if (!jsonString.startsWith('{')) {
return '';
}
// We're only interested in a small part of the string.
// The assumption is that we won't have an indentation of 10, just around 5 or so.
jsonString = jsonString.substring(1, 10).replace(/\r?\n/g, '');
// first index of non white space is the indentation.
const firstPositionOfNonWhiteSpace = jsonString.length - jsonString.trimStart().length;
return jsonString.substring(0, firstPositionOfNonWhiteSpace);
}