diff --git a/schema/settings.json b/schema/settings.json index 0f5431a..a4119e8 100644 --- a/schema/settings.json +++ b/schema/settings.json @@ -226,6 +226,10 @@ }, "additionalProperties": false, "type": "object" + }, + "formatOnSave": { + "additionalProperties": false, + "type": "boolean" } }, "properties": { @@ -291,6 +295,12 @@ "description": "Config to be passed into styler's style_text function call.", "$ref": "#/definitions/styler", "default": {} + }, + "formatOnSave": { + "title": "Auto format config", + "description": "Auto format code when save the notebook.", + "$ref": "#/definitions/formatOnSave", + "default": false } }, "additionalProperties": false, diff --git a/src/index.ts b/src/index.ts index 72fa978..183c717 100644 --- a/src/index.ts +++ b/src/index.ts @@ -96,11 +96,20 @@ class JupyterLabCodeFormatter this.app.commands.label(Constants.FORMAT_ALL_COMMAND), button ); + + context.saveState.connect(this.onSave, this); + return new DisposableDelegate(() => { button.dispose(); }); } + private async onSave(context: DocumentRegistry.IContext, state: DocumentRegistry.SaveState) { + if (state == 'started' && this.config.formatOnSave) { + await this.notebookCodeFormatter.formatAllCodeCells(this.config); + } + } + private setupWidgetExtension() { this.app.docRegistry.addWidgetExtension('Notebook', this); }