mirror of
https://github.com/microsoft/vscode.git
synced 2025-12-21 18:59:15 +00:00
Optimize indentation detection
This commit is contained in:
@@ -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);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user