Skip to content

Commit 5411414

Browse files
authored
Auto format code when save the notebook (#195)
* Format all cells when save file * Handling formatOnSave setting
1 parent 3d8450a commit 5411414

File tree

2 files changed

+19
-0
lines changed

2 files changed

+19
-0
lines changed

schema/settings.json

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -226,6 +226,10 @@
226226
},
227227
"additionalProperties": false,
228228
"type": "object"
229+
},
230+
"formatOnSave": {
231+
"additionalProperties": false,
232+
"type": "boolean"
229233
}
230234
},
231235
"properties": {
@@ -291,6 +295,12 @@
291295
"description": "Config to be passed into styler's style_text function call.",
292296
"$ref": "#/definitions/styler",
293297
"default": {}
298+
},
299+
"formatOnSave": {
300+
"title": "Auto format config",
301+
"description": "Auto format code when save the notebook.",
302+
"$ref": "#/definitions/formatOnSave",
303+
"default": false
294304
}
295305
},
296306
"additionalProperties": false,

src/index.ts

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -96,11 +96,20 @@ class JupyterLabCodeFormatter
9696
this.app.commands.label(Constants.FORMAT_ALL_COMMAND),
9797
button
9898
);
99+
100+
context.saveState.connect(this.onSave, this);
101+
99102
return new DisposableDelegate(() => {
100103
button.dispose();
101104
});
102105
}
103106

107+
private async onSave(context: DocumentRegistry.IContext<INotebookModel>, state: DocumentRegistry.SaveState) {
108+
if (state == 'started' && this.config.formatOnSave) {
109+
await this.notebookCodeFormatter.formatAllCodeCells(this.config);
110+
}
111+
}
112+
104113
private setupWidgetExtension() {
105114
this.app.docRegistry.addWidgetExtension('Notebook', this);
106115
}

0 commit comments

Comments
 (0)