Skip to content

Commit 43a6f63

Browse files
committed
editor/pybricksMicroPython: improve f-string highlighting
Fixes: pybricks/support#875
1 parent 5bef7e2 commit 43a6f63

File tree

2 files changed

+40
-3
lines changed

2 files changed

+40
-3
lines changed

CHANGELOG.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
## [Unreleased]
66

77
### Changed
8-
- Improved syntax highlighting for operators and numeric literals.
8+
- Improved syntax highlighting for f-strings, operators and numeric literals.
99

1010
## [2.1.0-beta.2] - 2022-12-26
1111

src/editor/pybricksMicroPython.ts

Lines changed: 39 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -229,9 +229,43 @@ export const language = <monaco.languages.IMonarchLanguage>{
229229
// Recognize strings, including those broken across lines with \ (but not without)
230230
strings: [
231231
[/'$/, 'string.escape', '@popall'],
232-
[/'/, 'string.escape', '@stringBody'],
232+
[/([fF]|[fF][rR]|[rR][fF])?'/, 'string.escape', '@fStringBody'],
233+
[/[rRuUbB]?'/, 'string.escape', '@stringBody'],
233234
[/"$/, 'string.escape', '@popall'],
234-
[/"/, 'string.escape', '@dblStringBody'],
235+
[/([fF]|[fF][rR]|[rR][fF])?"/, 'string.escape', '@dblFStringBody'],
236+
[/[rRuUbB]?"/, 'string.escape', '@dblStringBody'],
237+
],
238+
fStringBody: [
239+
[/([^\\'{}]|\{\{|\}\}(?!\}[^}]))+$/, 'string', '@popall'],
240+
[/([^\\'{}]|\{\{|\}\}(?!\}[^}]))+/, 'string'],
241+
[/\\./, 'string'],
242+
[/'/, 'string.escape', '@popall'],
243+
[/\\$/, 'string'],
244+
[
245+
/\{/,
246+
{
247+
token: 'delimiter.curly',
248+
next: '@fStringReplacement',
249+
nextEmbedded: pybricksMicroPythonId,
250+
},
251+
],
252+
[/\}/, 'delimiter.curly'],
253+
],
254+
dblFStringBody: [
255+
[/([^\\"{}]|\{\{|\}\}(?!\}[^}]))+$/, 'string', '@popall'],
256+
[/([^\\"{}]|\{\{|\}\}(?!\}[^}]))+/, 'string'],
257+
[/\\./, 'string'],
258+
[/"/, 'string.escape', '@popall'],
259+
[/\\$/, 'string'],
260+
[
261+
/\{/,
262+
{
263+
token: 'delimiter.curly',
264+
next: '@fStringReplacement',
265+
nextEmbedded: pybricksMicroPythonId,
266+
},
267+
],
268+
[/\}/, 'delimiter.curly'],
235269
],
236270
stringBody: [
237271
[/[^\\']+$/, 'string', '@popall'],
@@ -247,6 +281,9 @@ export const language = <monaco.languages.IMonarchLanguage>{
247281
[/"/, 'string.escape', '@popall'],
248282
[/\\$/, 'string'],
249283
],
284+
fStringReplacement: [
285+
[/\}/, { token: '@rematch', next: '@pop', nextEmbedded: '@pop' }],
286+
],
250287

251288
operators: [
252289
[

0 commit comments

Comments
 (0)