Skip to content

Commit bdcfb12

Browse files
committed
Add suppress error iff auto formatting on save config
1 parent ecaec67 commit bdcfb12

File tree

6 files changed

+154
-23
lines changed

6 files changed

+154
-23
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
**Jupyterlab extension**
66

77
- Add "Go to cell" option in dialog when formatting fails;
8+
- Add "suppressFormatterErrorsIFFAutoFormatOnSave" config;
89

910
## 2.2.1 2023-05-21
1011

docs/changelog.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
**Jupyterlab extension**
66

77
- Add "Go to cell" option in dialog when formatting fails;
8+
- Add "suppressFormatterErrorsIFFAutoFormatOnSave" config;
89

910
## 2.2.1 2023-05-21
1011

schema/settings.json

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -313,6 +313,10 @@
313313
"suppressFormatterErrors": {
314314
"additionalProperties": false,
315315
"type": "boolean"
316+
},
317+
"suppressFormatterErrorsIFFAutoFormatOnSave": {
318+
"additionalProperties": false,
319+
"type": "boolean"
316320
}
317321
},
318322
"properties": {
@@ -415,6 +419,12 @@
415419
"description": "Whether to suppress all errors reported by formatter while formatting. Useful when you have format on save mode on.",
416420
"$ref": "#/definitions/suppressFormatterErrors",
417421
"default": false
422+
},
423+
"suppressFormatterErrorsIFFAutoFormatOnSave": {
424+
"title": "Suppress formatter errors if and only if auto saving.",
425+
"description": "Whether to suppress all errors reported by formatter while formatting (if and only if auto saving). Useful when you have format on save mode on and still want to see error when manually formatting.",
426+
"$ref": "#/definitions/suppressFormatterErrorsIFFAutoFormatOnSave",
427+
"default": false
418428
}
419429
},
420430
"additionalProperties": false,

src/formatter.ts

Lines changed: 49 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,10 @@ import { IEditorTracker } from '@jupyterlab/fileeditor';
55
import { Widget } from '@lumino/widgets';
66
import { showErrorMessage, Dialog, showDialog } from '@jupyterlab/apputils';
77

8+
type Context = {
9+
saving: boolean;
10+
};
11+
812
class JupyterlabCodeFormatter {
913
working = false;
1014
protected client: JupyterlabCodeFormatterClient;
@@ -46,23 +50,30 @@ export class JupyterlabNotebookCodeFormatter extends JupyterlabCodeFormatter {
4650
}
4751

4852
public async formatAction(config: any, formatter?: string) {
49-
return this.formatCells(true, config, formatter);
53+
return this.formatCells(true, config, { saving: false }, formatter);
5054
}
5155

5256
public async formatSelectedCodeCells(
5357
config: any,
5458
formatter?: string,
5559
notebook?: Notebook
5660
) {
57-
return this.formatCells(true, config, formatter, notebook);
61+
return this.formatCells(
62+
true,
63+
config,
64+
{ saving: false },
65+
formatter,
66+
notebook
67+
);
5868
}
5969

6070
public async formatAllCodeCells(
6171
config: any,
72+
context: Context,
6273
formatter?: string,
6374
notebook?: Notebook
6475
) {
65-
return this.formatCells(false, config, formatter, notebook);
76+
return this.formatCells(false, config, context, formatter, notebook);
6677
}
6778

6879
private getCodeCells(selectedOnly = true, notebook?: Notebook): CodeCell[] {
@@ -87,7 +98,7 @@ export class JupyterlabNotebookCodeFormatter extends JupyterlabCodeFormatter {
8798
}
8899

89100
const metadata =
90-
this.notebookTracker.currentWidget.content.model!.sharedModel.metadata;;
101+
this.notebookTracker.currentWidget.content.model!.sharedModel.metadata;
91102

92103
if (!metadata) {
93104
return null;
@@ -142,43 +153,54 @@ export class JupyterlabNotebookCodeFormatter extends JupyterlabCodeFormatter {
142153
private async applyFormatters(
143154
selectedCells: CodeCell[],
144155
formattersToUse: string[],
145-
config: any
156+
config: any,
157+
context: Context
146158
) {
147159
for (const formatterToUse of formattersToUse) {
148160
if (formatterToUse === 'noop' || formatterToUse === 'skip') {
149161
continue;
150162
}
151-
const currentTexts = selectedCells.map(cell => cell.model.sharedModel.source);
163+
const currentTexts = selectedCells.map(
164+
cell => cell.model.sharedModel.source
165+
);
152166
const formattedTexts = await this.formatCode(
153167
currentTexts,
154168
formatterToUse,
155169
config[formatterToUse],
156170
true,
157171
config.cacheFormatters
158172
);
173+
console.log(config.suppressFormatterErrorsIFFAutoFormatOnSave, context.saving);
159174

160-
const showErrors = !(config.suppressFormatterErrors ?? false);
175+
const showErrors =
176+
!(config.suppressFormatterErrors ?? false) &&
177+
!(
178+
(config.suppressFormatterErrorsIFFAutoFormatOnSave ?? false) &&
179+
context.saving
180+
);
161181
for (let i = 0; i < selectedCells.length; ++i) {
162182
const cell = selectedCells[i];
163183
const currentText = currentTexts[i];
164184
const formattedText = formattedTexts.code[i];
165-
const cellValueHasNotChanged = cell.model.sharedModel.source === currentText;
185+
const cellValueHasNotChanged =
186+
cell.model.sharedModel.source === currentText;
166187
if (cellValueHasNotChanged) {
167188
if (formattedText.error) {
168189
if (showErrors) {
169-
const result = await showDialog(
170-
{
171-
title: 'Jupyterlab Code Formatter Error',
172-
body: formattedText.error,
173-
buttons: [
174-
Dialog.createButton({label: 'Go to cell', actions: ['revealError']}),
175-
Dialog.okButton({ label: 'Dismiss' }),
176-
]
177-
}
178-
)
190+
const result = await showDialog({
191+
title: 'Jupyterlab Code Formatter Error',
192+
body: formattedText.error,
193+
buttons: [
194+
Dialog.createButton({
195+
label: 'Go to cell',
196+
actions: ['revealError']
197+
}),
198+
Dialog.okButton({ label: 'Dismiss' })
199+
]
200+
});
179201
if (result.button.actions.indexOf('revealError') !== -1) {
180-
this.notebookTracker.currentWidget!.content.scrollToCell(cell)
181-
break
202+
this.notebookTracker.currentWidget!.content.scrollToCell(cell);
203+
break;
182204
}
183205
}
184206
} else {
@@ -199,6 +221,7 @@ export class JupyterlabNotebookCodeFormatter extends JupyterlabCodeFormatter {
199221
private async formatCells(
200222
selectedOnly: boolean,
201223
config: any,
224+
context: Context,
202225
formatter?: string,
203226
notebook?: Notebook
204227
) {
@@ -214,7 +237,12 @@ export class JupyterlabNotebookCodeFormatter extends JupyterlabCodeFormatter {
214237
}
215238

216239
const formattersToUse = await this.getFormattersToUse(config, formatter);
217-
await this.applyFormatters(selectedCells, formattersToUse, config);
240+
await this.applyFormatters(
241+
selectedCells,
242+
formattersToUse,
243+
config,
244+
context
245+
);
218246
} catch (error) {
219247
await showErrorMessage('Jupyterlab Code Formatter Error', error);
220248
}

src/index.ts

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,7 @@ class JupyterLabCodeFormatter
8080
onClick: async () => {
8181
await this.notebookCodeFormatter.formatAllCodeCells(
8282
this.config,
83+
{ saving: false },
8384
undefined,
8485
nb.content
8586
);
@@ -103,7 +104,12 @@ class JupyterLabCodeFormatter
103104
state: DocumentRegistry.SaveState
104105
) {
105106
if (state === 'started' && this.config.formatOnSave) {
106-
await this.notebookCodeFormatter.formatAllCodeCells(this.config);
107+
await this.notebookCodeFormatter.formatAllCodeCells(
108+
this.config,
109+
{ saving: true },
110+
undefined,
111+
undefined
112+
);
107113
}
108114
}
109115

@@ -143,7 +149,9 @@ class JupyterLabCodeFormatter
143149
});
144150
this.app.commands.addCommand(Constants.FORMAT_ALL_COMMAND, {
145151
execute: async () => {
146-
await this.notebookCodeFormatter.formatAllCodeCells(this.config);
152+
await this.notebookCodeFormatter.formatAllCodeCells(this.config, {
153+
saving: false
154+
});
147155
},
148156
iconClass: Constants.ICON_FORMAT_ALL,
149157
iconLabel: 'Format notebook'

test_snippets/test_with_errors.ipynb

Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
{
2+
"cells": [
3+
{
4+
"cell_type": "code",
5+
"execution_count": null,
6+
"id": "95cc4d6e-9fa2-493c-af98-83b1b88df0c6",
7+
"metadata": {},
8+
"outputs": [],
9+
"source": [
10+
"ddd ="
11+
]
12+
},
13+
{
14+
"cell_type": "code",
15+
"execution_count": null,
16+
"id": "4e6159ec-e287-4a22-b76e-9fc5f012a6d4",
17+
"metadata": {},
18+
"outputs": [],
19+
"source": [
20+
"hello = 42"
21+
]
22+
},
23+
{
24+
"cell_type": "code",
25+
"execution_count": null,
26+
"id": "41a699a0-0480-4d36-ae81-d06a6cc074b4",
27+
"metadata": {},
28+
"outputs": [],
29+
"source": [
30+
"fff ="
31+
]
32+
},
33+
{
34+
"cell_type": "code",
35+
"execution_count": null,
36+
"id": "804c67fe-6c33-4590-9616-57db0497b1eb",
37+
"metadata": {},
38+
"outputs": [],
39+
"source": []
40+
},
41+
{
42+
"cell_type": "code",
43+
"execution_count": null,
44+
"id": "411369a5-160e-472f-befa-25c82b836a61",
45+
"metadata": {},
46+
"outputs": [],
47+
"source": [
48+
"hello = 42"
49+
]
50+
},
51+
{
52+
"cell_type": "code",
53+
"execution_count": null,
54+
"id": "2c20f1d6-7b8b-4083-819d-d0daef945d23",
55+
"metadata": {},
56+
"outputs": [],
57+
"source": [
58+
"fff2 ="
59+
]
60+
}
61+
],
62+
"metadata": {
63+
"kernelspec": {
64+
"display_name": "Python 3 (ipykernel)",
65+
"language": "python",
66+
"name": "python3"
67+
},
68+
"language_info": {
69+
"codemirror_mode": {
70+
"name": "ipython",
71+
"version": 3
72+
},
73+
"file_extension": ".py",
74+
"mimetype": "text/x-python",
75+
"name": "python",
76+
"nbconvert_exporter": "python",
77+
"pygments_lexer": "ipython3",
78+
"version": "3.10.11"
79+
}
80+
},
81+
"nbformat": 4,
82+
"nbformat_minor": 5
83+
}

0 commit comments

Comments
 (0)