-
Notifications
You must be signed in to change notification settings - Fork 22
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
mpy/alerts: implement compiler error
This shows the output from mpy-cross and has a button to go to the error.
- Loading branch information
Showing
10 changed files
with
159 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
// SPDX-License-Identifier: MIT | ||
// Copyright (c) 2022 The Pybricks Authors | ||
|
||
import { Button, Intent } from '@blueprintjs/core'; | ||
import React, { useMemo } from 'react'; | ||
import { useDispatch } from 'react-redux'; | ||
import { editorGoto } from '../../editor/actions'; | ||
import { useFileStorageUuid } from '../../fileStorage/hooks'; | ||
import type { CreateToast } from '../../toasterTypes'; | ||
import { useI18n } from './i18n'; | ||
|
||
type CompilerErrorProps = { | ||
error: string[]; | ||
}; | ||
|
||
const CompilerError: React.VoidFunctionComponent<CompilerErrorProps> = ({ error }) => { | ||
const dispatch = useDispatch(); | ||
const i18n = useI18n(); | ||
|
||
const [file, line] = useMemo(() => { | ||
for (const line of error) { | ||
const match = line.match(/^ {2}File "(.*)", line (\d+)/); | ||
|
||
if (match) { | ||
return [match[1], Number(match[2])]; | ||
} | ||
} | ||
|
||
return [undefined, undefined]; | ||
}, [error]); | ||
|
||
const uuid = useFileStorageUuid(file ?? ''); | ||
|
||
return ( | ||
<> | ||
<p>{i18n.translate('compilerError.message')}</p> | ||
<pre className="pb-mpy-alerts-compile-error">{error.join('\n')}</pre> | ||
{file && uuid && ( | ||
<Button icon="code" onClick={() => dispatch(editorGoto(uuid, line))}> | ||
{i18n.translate('compilerError.gotoErrorButton')} | ||
</Button> | ||
)} | ||
</> | ||
); | ||
}; | ||
|
||
export const compilerError: CreateToast<CompilerErrorProps, 'dismiss' | 'gotoError'> = ( | ||
onAction, | ||
props, | ||
) => { | ||
return { | ||
message: <CompilerError {...props} />, | ||
icon: 'error', | ||
intent: Intent.DANGER, | ||
timeout: 0, | ||
onDismiss: () => onAction('dismiss'), | ||
}; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
// SPDX-License-Identifier: MIT | ||
// Copyright (c) 2022 The Pybricks Authors | ||
|
||
import { useI18n as useShopifyI18n } from '@shopify/react-i18n'; | ||
import type { TypedI18n } from '../../i18n'; | ||
import type translations from './translations/en.json'; | ||
|
||
export function useI18n(): TypedI18n<typeof translations> { | ||
// istanbul ignore next: babel-loader rewrites this line | ||
const [i18n] = useShopifyI18n(); | ||
return i18n; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
// SPDX-License-Identifier: MIT | ||
// Copyright (c) 2022 The Pybricks Authors | ||
|
||
pre.pb-mpy-alerts-compile-error { | ||
white-space: pre-wrap; | ||
word-break: keep-all; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
// SPDX-License-Identifier: MIT | ||
// Copyright (c) 2022 The Pybricks Authors | ||
|
||
import './index.scss'; | ||
import { compilerError } from './CompilerError'; | ||
|
||
// gathers all of the alert creation functions for passing up to the top level | ||
export default { | ||
compilerError, | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
{ | ||
"compilerError": { | ||
"message": "Failed to compile MicroPython file.", | ||
"gotoErrorButton": "Goto error" | ||
} | ||
} |