diff --git a/CHANGELOG.md b/CHANGELOG.md index 6725ef6f..8b07abae 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -5,7 +5,7 @@ ## [Unreleased] ### Changed -- Improved syntax highlighting for operators and numeric literals. +- Improved syntax highlighting for f-strings, operators and numeric literals. ## [2.1.0-beta.2] - 2022-12-26 diff --git a/src/editor/pybricksMicroPython.ts b/src/editor/pybricksMicroPython.ts index ac5e86f8..ffdb1340 100644 --- a/src/editor/pybricksMicroPython.ts +++ b/src/editor/pybricksMicroPython.ts @@ -229,9 +229,43 @@ export const language = { // Recognize strings, including those broken across lines with \ (but not without) strings: [ [/'$/, 'string.escape', '@popall'], - [/'/, 'string.escape', '@stringBody'], + [/([fF]|[fF][rR]|[rR][fF])?'/, 'string.escape', '@fStringBody'], + [/[rRuUbB]?'/, 'string.escape', '@stringBody'], [/"$/, 'string.escape', '@popall'], - [/"/, 'string.escape', '@dblStringBody'], + [/([fF]|[fF][rR]|[rR][fF])?"/, 'string.escape', '@dblFStringBody'], + [/[rRuUbB]?"/, 'string.escape', '@dblStringBody'], + ], + fStringBody: [ + [/([^\\'{}]|\{\{|\}\}(?!\}[^}]))+$/, 'string', '@popall'], + [/([^\\'{}]|\{\{|\}\}(?!\}[^}]))+/, 'string'], + [/\\./, 'string'], + [/'/, 'string.escape', '@popall'], + [/\\$/, 'string'], + [ + /\{/, + { + token: 'delimiter.curly', + next: '@fStringReplacement', + nextEmbedded: pybricksMicroPythonId, + }, + ], + [/\}/, 'delimiter.curly'], + ], + dblFStringBody: [ + [/([^\\"{}]|\{\{|\}\}(?!\}[^}]))+$/, 'string', '@popall'], + [/([^\\"{}]|\{\{|\}\}(?!\}[^}]))+/, 'string'], + [/\\./, 'string'], + [/"/, 'string.escape', '@popall'], + [/\\$/, 'string'], + [ + /\{/, + { + token: 'delimiter.curly', + next: '@fStringReplacement', + nextEmbedded: pybricksMicroPythonId, + }, + ], + [/\}/, 'delimiter.curly'], ], stringBody: [ [/[^\\']+$/, 'string', '@popall'], @@ -247,6 +281,9 @@ export const language = { [/"/, 'string.escape', '@popall'], [/\\$/, 'string'], ], + fStringReplacement: [ + [/\}/, { token: '@rematch', next: '@pop', nextEmbedded: '@pop' }], + ], operators: [ [