From 4e68558997c87939adb7fb93e74332e9b462fd2d Mon Sep 17 00:00:00 2001 From: Anton Gogolev Date: Fri, 25 Oct 2024 09:16:39 +0200 Subject: [PATCH] Add support for Python multiline f-strings Initial support for f-strings was added in https://github.com/microsoft/monaco-editor/pull/4401, but it didn't support multiline f-strings, as reported in https://github.com/microsoft/monaco-editor/issues/4601 --- src/basic-languages/python/python.test.ts | 51 +++++++++++++++++++++++ src/basic-languages/python/python.ts | 6 ++- 2 files changed, 55 insertions(+), 2 deletions(-) diff --git a/src/basic-languages/python/python.test.ts b/src/basic-languages/python/python.test.ts index 019758a413..937000f4d1 100644 --- a/src/basic-languages/python/python.test.ts +++ b/src/basic-languages/python/python.test.ts @@ -216,6 +216,57 @@ testTokenization('python', [ ] } ], + + // https://github.com/microsoft/monaco-editor/issues/4601 + [ + { + line: `f'''str `, + tokens: [ + { startIndex: 0, type: 'string.escape.python' }, + { startIndex: 4, type: 'string.python' } + ] + }, + { + line: ` str "{var}" {{ {{ }} `, + tokens: [ + { startIndex: 0, type: 'string.python' }, + { startIndex: 6, type: 'identifier.python' }, + { startIndex: 11, type: 'string.python' } + ] + }, + { + line: `str'''`, + tokens: [ + { startIndex: 0, type: 'string.python' }, + { startIndex: 3, type: 'string.escape.python' } + ] + } + ], + [ + { + line: `f"""str `, + tokens: [ + { startIndex: 0, type: 'string.escape.python' }, + { startIndex: 4, type: 'string.python' } + ] + }, + { + line: ` str '{var}' {{ {{ }} `, + tokens: [ + { startIndex: 0, type: 'string.python' }, + { startIndex: 6, type: 'identifier.python' }, + { startIndex: 11, type: 'string.python' } + ] + }, + { + line: `str"""`, + tokens: [ + { startIndex: 0, type: 'string.python' }, + { startIndex: 3, type: 'string.escape.python' } + ] + } + ], + [ { line: 'f"{var:.3f}{var!r}{var=}"', diff --git a/src/basic-languages/python/python.ts b/src/basic-languages/python/python.ts index a209212c28..1cd0cb12ce 100644 --- a/src/basic-languages/python/python.ts +++ b/src/basic-languages/python/python.ts @@ -257,7 +257,8 @@ export const language = { [/"/, 'string.escape', '@dblStringBody'] ], fStringBody: [ - [/[^\\'\{\}]+$/, 'string', '@popall'], + [/\{\{/, 'string'], + [/\}\}/, 'string'], [/[^\\'\{\}]+/, 'string'], [/\{[^\}':!=]+/, 'identifier', '@fStringDetail'], [/\\./, 'string'], @@ -272,7 +273,8 @@ export const language = { [/\\$/, 'string'] ], fDblStringBody: [ - [/[^\\"\{\}]+$/, 'string', '@popall'], + [/\{\{/, 'string'], + [/\}\}/, 'string'], [/[^\\"\{\}]+/, 'string'], [/\{[^\}':!=]+/, 'identifier', '@fStringDetail'], [/\\./, 'string'],