Skip to content

Commit 7e726ff

Browse files
committed
mpy/alerts: implement compiler error
This shows the output from mpy-cross and has a button to go to the error.
1 parent e5ab52b commit 7e726ff

10 files changed

Lines changed: 159 additions & 0 deletions

File tree

‎src/alerts.ts‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import alerts from './alerts/alerts';
66
import ble from './ble/alerts';
77
import explorer from './explorer/alerts';
88
import firmware from './firmware/alerts';
9+
import mpy from './mpy/alerts';
910
import type { CreateToast } from './toasterTypes';
1011

1112
/** This collects alerts from all of the subsystems of the app */
@@ -14,6 +15,7 @@ const alertDomains = {
1415
ble,
1516
explorer,
1617
firmware,
18+
mpy,
1719
};
1820

1921
/** Gets the type of available alert domains. */

‎src/editor/actions.ts‎

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -112,3 +112,12 @@ export const editorDidFailToActivateFile = createAction((uuid: UUID, error: Erro
112112
uuid,
113113
error,
114114
}));
115+
116+
/**
117+
* Requests to activate a file and show line.
118+
*/
119+
export const editorGoto = createAction((uuid: UUID, line: number) => ({
120+
type: 'editor.action.goto',
121+
uuid,
122+
line,
123+
}));

‎src/editor/sagas.ts‎

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,7 @@ import {
6060
editorDidOpenFile,
6161
editorGetValueRequest,
6262
editorGetValueResponse,
63+
editorGoto,
6364
editorOpenFile,
6465
} from './actions';
6566
import { EditorError } from './error';
@@ -265,6 +266,34 @@ function* handleEditorActivateFile(
265266
}
266267
}
267268

269+
function* handleEditorGoto(
270+
editor: monaco.editor.ICodeEditor,
271+
action: ReturnType<typeof editorGoto>,
272+
): Generator {
273+
yield* put(editorActivateFile(action.uuid));
274+
275+
const { didActivate, didFailToActivate } = yield* race({
276+
didActivate: take(editorDidActivateFile.when((a) => a.uuid === action.uuid)),
277+
didFailToActivate: take(
278+
editorDidFailToActivateFile.when((a) => a.uuid === action.uuid),
279+
),
280+
});
281+
282+
if (didFailToActivate) {
283+
return;
284+
}
285+
286+
defined(didActivate);
287+
288+
editor.revealLineInCenterIfOutsideViewport(action.line);
289+
editor.setSelection({
290+
startColumn: 1,
291+
startLineNumber: action.line,
292+
endColumn: Infinity,
293+
endLineNumber: action.line,
294+
});
295+
}
296+
268297
function* handleEditorDidCloseFile(
269298
activeFileHistory: ActiveFileHistoryManager,
270299
action: ReturnType<typeof editorDidCloseFile>,
@@ -356,6 +385,7 @@ function* handleDidCreateEditor(editor: monaco.editor.ICodeEditor): Generator {
356385
openFiles,
357386
activeFileHistory,
358387
);
388+
yield* takeEvery(editorGoto, handleEditorGoto, editor);
359389
yield* takeEvery(editorDidCloseFile, handleEditorDidCloseFile, activeFileHistory);
360390
yield* fork(monitorViewState, editor);
361391

‎src/fileStorage/hooks.ts‎

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,3 +25,18 @@ export function useFileStoragePath(uuid: UUID): string | undefined {
2525
const db = useContext(FileStorageContext);
2626
return useLiveQuery(() => db.metadata.get(uuid, (x) => x?.path));
2727
}
28+
29+
/**
30+
* Gets the file path for a file UUID.
31+
*
32+
* If the file is renamed, the returned value will be automatically updated.
33+
*/
34+
export function useFileStorageUuid(path: string): UUID | undefined {
35+
const db = useContext(FileStorageContext);
36+
return useLiveQuery(() =>
37+
db.metadata
38+
.where('path')
39+
.equals(path)
40+
.first((x) => x?.uuid),
41+
);
42+
}

‎src/hub/sagas.ts‎

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -206,6 +206,16 @@ function* handleDownloadAndRun(action: ReturnType<typeof downloadAndRun>): Gener
206206
});
207207

208208
if (didFailToCompile) {
209+
yield* put(
210+
alertsShowAlert(
211+
'mpy',
212+
'compilerError',
213+
{
214+
error: didFailToCompile.error,
215+
},
216+
'mpy.compilerError',
217+
),
218+
);
209219
return;
210220
}
211221

‎src/mpy/alerts/CompilerError.tsx‎

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
// SPDX-License-Identifier: MIT
2+
// Copyright (c) 2022 The Pybricks Authors
3+
4+
import { Button, Intent } from '@blueprintjs/core';
5+
import React, { useMemo } from 'react';
6+
import { useDispatch } from 'react-redux';
7+
import { editorGoto } from '../../editor/actions';
8+
import { useFileStorageUuid } from '../../fileStorage/hooks';
9+
import type { CreateToast } from '../../toasterTypes';
10+
import { useI18n } from './i18n';
11+
12+
type CompilerErrorProps = {
13+
error: string[];
14+
};
15+
16+
const CompilerError: React.VoidFunctionComponent<CompilerErrorProps> = ({ error }) => {
17+
const dispatch = useDispatch();
18+
const i18n = useI18n();
19+
20+
const [file, line] = useMemo(() => {
21+
for (const line of error) {
22+
const match = line.match(/^ {2}File "(.*)", line (\d+)/);
23+
24+
if (match) {
25+
return [match[1], Number(match[2])];
26+
}
27+
}
28+
29+
return [undefined, undefined];
30+
}, [error]);
31+
32+
const uuid = useFileStorageUuid(file ?? '');
33+
34+
return (
35+
<>
36+
<p>{i18n.translate('compilerError.message')}</p>
37+
<pre className="pb-mpy-alerts-compile-error">{error.join('\n')}</pre>
38+
{file && uuid && (
39+
<Button icon="code" onClick={() => dispatch(editorGoto(uuid, line))}>
40+
{i18n.translate('compilerError.gotoErrorButton')}
41+
</Button>
42+
)}
43+
</>
44+
);
45+
};
46+
47+
export const compilerError: CreateToast<CompilerErrorProps, 'dismiss' | 'gotoError'> = (
48+
onAction,
49+
props,
50+
) => {
51+
return {
52+
message: <CompilerError {...props} />,
53+
icon: 'error',
54+
intent: Intent.DANGER,
55+
timeout: 0,
56+
onDismiss: () => onAction('dismiss'),
57+
};
58+
};

‎src/mpy/alerts/i18n.ts‎

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
// SPDX-License-Identifier: MIT
2+
// Copyright (c) 2022 The Pybricks Authors
3+
4+
import { useI18n as useShopifyI18n } from '@shopify/react-i18n';
5+
import type { TypedI18n } from '../../i18n';
6+
import type translations from './translations/en.json';
7+
8+
export function useI18n(): TypedI18n<typeof translations> {
9+
// istanbul ignore next: babel-loader rewrites this line
10+
const [i18n] = useShopifyI18n();
11+
return i18n;
12+
}

‎src/mpy/alerts/index.scss‎

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
// SPDX-License-Identifier: MIT
2+
// Copyright (c) 2022 The Pybricks Authors
3+
4+
pre.pb-mpy-alerts-compile-error {
5+
white-space: pre-wrap;
6+
word-break: keep-all;
7+
}

‎src/mpy/alerts/index.ts‎

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
// SPDX-License-Identifier: MIT
2+
// Copyright (c) 2022 The Pybricks Authors
3+
4+
import './index.scss';
5+
import { compilerError } from './CompilerError';
6+
7+
// gathers all of the alert creation functions for passing up to the top level
8+
export default {
9+
compilerError,
10+
};
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
{
2+
"compilerError": {
3+
"message": "Failed to compile MicroPython file.",
4+
"gotoErrorButton": "Goto error"
5+
}
6+
}

0 commit comments

Comments
 (0)