Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
**Jupyterlab extension**

- Add "Go to cell" option in dialog when formatting fails;
- Add "suppressFormatterErrorsIFFAutoFormatOnSave" config;

## 2.2.1 2023-05-21

Expand Down
1 change: 1 addition & 0 deletions docs/changelog.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
**Jupyterlab extension**

- Add "Go to cell" option in dialog when formatting fails;
- Add "suppressFormatterErrorsIFFAutoFormatOnSave" config;

## 2.2.1 2023-05-21

Expand Down
10 changes: 10 additions & 0 deletions schema/settings.json
Original file line number Diff line number Diff line change
Expand Up @@ -313,6 +313,10 @@
"suppressFormatterErrors": {
"additionalProperties": false,
"type": "boolean"
},
"suppressFormatterErrorsIFFAutoFormatOnSave": {
"additionalProperties": false,
"type": "boolean"
}
},
"properties": {
Expand Down Expand Up @@ -415,6 +419,12 @@
"description": "Whether to suppress all errors reported by formatter while formatting. Useful when you have format on save mode on.",
"$ref": "#/definitions/suppressFormatterErrors",
"default": false
},
"suppressFormatterErrorsIFFAutoFormatOnSave": {
"title": "Suppress formatter errors if and only if auto saving.",
"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.",
"$ref": "#/definitions/suppressFormatterErrorsIFFAutoFormatOnSave",
"default": false
}
},
"additionalProperties": false,
Expand Down
70 changes: 49 additions & 21 deletions src/formatter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,10 @@ import { IEditorTracker } from '@jupyterlab/fileeditor';
import { Widget } from '@lumino/widgets';
import { showErrorMessage, Dialog, showDialog } from '@jupyterlab/apputils';

type Context = {
saving: boolean;
};

class JupyterlabCodeFormatter {
working = false;
protected client: JupyterlabCodeFormatterClient;
Expand Down Expand Up @@ -46,23 +50,30 @@ export class JupyterlabNotebookCodeFormatter extends JupyterlabCodeFormatter {
}

public async formatAction(config: any, formatter?: string) {
return this.formatCells(true, config, formatter);
return this.formatCells(true, config, { saving: false }, formatter);
}

public async formatSelectedCodeCells(
config: any,
formatter?: string,
notebook?: Notebook
) {
return this.formatCells(true, config, formatter, notebook);
return this.formatCells(
true,
config,
{ saving: false },
formatter,
notebook
);
}

public async formatAllCodeCells(
config: any,
context: Context,
formatter?: string,
notebook?: Notebook
) {
return this.formatCells(false, config, formatter, notebook);
return this.formatCells(false, config, context, formatter, notebook);
}

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

const metadata =
this.notebookTracker.currentWidget.content.model!.sharedModel.metadata;;
this.notebookTracker.currentWidget.content.model!.sharedModel.metadata;

if (!metadata) {
return null;
Expand Down Expand Up @@ -142,43 +153,54 @@ export class JupyterlabNotebookCodeFormatter extends JupyterlabCodeFormatter {
private async applyFormatters(
selectedCells: CodeCell[],
formattersToUse: string[],
config: any
config: any,
context: Context
) {
for (const formatterToUse of formattersToUse) {
if (formatterToUse === 'noop' || formatterToUse === 'skip') {
continue;
}
const currentTexts = selectedCells.map(cell => cell.model.sharedModel.source);
const currentTexts = selectedCells.map(
cell => cell.model.sharedModel.source
);
const formattedTexts = await this.formatCode(
currentTexts,
formatterToUse,
config[formatterToUse],
true,
config.cacheFormatters
);
console.log(config.suppressFormatterErrorsIFFAutoFormatOnSave, context.saving);

const showErrors = !(config.suppressFormatterErrors ?? false);
const showErrors =
!(config.suppressFormatterErrors ?? false) &&
!(
(config.suppressFormatterErrorsIFFAutoFormatOnSave ?? false) &&
context.saving
);
for (let i = 0; i < selectedCells.length; ++i) {
const cell = selectedCells[i];
const currentText = currentTexts[i];
const formattedText = formattedTexts.code[i];
const cellValueHasNotChanged = cell.model.sharedModel.source === currentText;
const cellValueHasNotChanged =
cell.model.sharedModel.source === currentText;
if (cellValueHasNotChanged) {
if (formattedText.error) {
if (showErrors) {
const result = await showDialog(
{
title: 'Jupyterlab Code Formatter Error',
body: formattedText.error,
buttons: [
Dialog.createButton({label: 'Go to cell', actions: ['revealError']}),
Dialog.okButton({ label: 'Dismiss' }),
]
}
)
const result = await showDialog({
title: 'Jupyterlab Code Formatter Error',
body: formattedText.error,
buttons: [
Dialog.createButton({
label: 'Go to cell',
actions: ['revealError']
}),
Dialog.okButton({ label: 'Dismiss' })
]
});
if (result.button.actions.indexOf('revealError') !== -1) {
this.notebookTracker.currentWidget!.content.scrollToCell(cell)
break
this.notebookTracker.currentWidget!.content.scrollToCell(cell);
break;
}
}
} else {
Expand All @@ -199,6 +221,7 @@ export class JupyterlabNotebookCodeFormatter extends JupyterlabCodeFormatter {
private async formatCells(
selectedOnly: boolean,
config: any,
context: Context,
formatter?: string,
notebook?: Notebook
) {
Expand All @@ -214,7 +237,12 @@ export class JupyterlabNotebookCodeFormatter extends JupyterlabCodeFormatter {
}

const formattersToUse = await this.getFormattersToUse(config, formatter);
await this.applyFormatters(selectedCells, formattersToUse, config);
await this.applyFormatters(
selectedCells,
formattersToUse,
config,
context
);
} catch (error) {
await showErrorMessage('Jupyterlab Code Formatter Error', error);
}
Expand Down
12 changes: 10 additions & 2 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,7 @@ class JupyterLabCodeFormatter
onClick: async () => {
await this.notebookCodeFormatter.formatAllCodeCells(
this.config,
{ saving: false },
undefined,
nb.content
);
Expand All @@ -103,7 +104,12 @@ class JupyterLabCodeFormatter
state: DocumentRegistry.SaveState
) {
if (state === 'started' && this.config.formatOnSave) {
await this.notebookCodeFormatter.formatAllCodeCells(this.config);
await this.notebookCodeFormatter.formatAllCodeCells(
this.config,
{ saving: true },
undefined,
undefined
);
}
}

Expand Down Expand Up @@ -143,7 +149,9 @@ class JupyterLabCodeFormatter
});
this.app.commands.addCommand(Constants.FORMAT_ALL_COMMAND, {
execute: async () => {
await this.notebookCodeFormatter.formatAllCodeCells(this.config);
await this.notebookCodeFormatter.formatAllCodeCells(this.config, {
saving: false
});
},
iconClass: Constants.ICON_FORMAT_ALL,
iconLabel: 'Format notebook'
Expand Down
83 changes: 83 additions & 0 deletions test_snippets/test_with_errors.ipynb
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
{
"cells": [
{
"cell_type": "code",
"execution_count": null,
"id": "95cc4d6e-9fa2-493c-af98-83b1b88df0c6",
"metadata": {},
"outputs": [],
"source": [
"ddd ="
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "4e6159ec-e287-4a22-b76e-9fc5f012a6d4",
"metadata": {},
"outputs": [],
"source": [
"hello = 42"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "41a699a0-0480-4d36-ae81-d06a6cc074b4",
"metadata": {},
"outputs": [],
"source": [
"fff ="
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "804c67fe-6c33-4590-9616-57db0497b1eb",
"metadata": {},
"outputs": [],
"source": []
},
{
"cell_type": "code",
"execution_count": null,
"id": "411369a5-160e-472f-befa-25c82b836a61",
"metadata": {},
"outputs": [],
"source": [
"hello = 42"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "2c20f1d6-7b8b-4083-819d-d0daef945d23",
"metadata": {},
"outputs": [],
"source": [
"fff2 ="
]
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3 (ipykernel)",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.10.11"
}
},
"nbformat": 4,
"nbformat_minor": 5
}