vim-patch:a94a055: runtime(python): Highlight f-string replacement fields in Python

Highlight f-string replacement fields, including

- Comments
- Debugging flags
- Conversion fields
- Format specifications
- Delimiters

Syntax inside fields will be addressed in a separate commit.

related: vim/vim#10734
related: vim/vim#14033
closes: vim/vim#17784

a94a0555d9

Co-authored-by: Rob B <github@0x7e.net>
This commit is contained in:
zeertzjq
2025-08-11 07:00:58 +08:00
parent 82a54a772b
commit 39af96c8ef

View File

@@ -1,7 +1,7 @@
" Vim syntax file
" Language: Python
" Maintainer: Zvezdan Petkovic <zpetkovic@acm.org>
" Last Change: 2025 Jul 26
" Last Change: 2025 Aug 10
" Credits: Neil Schemenauer <nas@python.ca>
" Dmitry Vasiliev
" Rob B
@@ -165,25 +165,25 @@ syn region pythonFString
\ start=+\cF\z(['"]\)+
\ end="\z1"
\ skip="\\\\\|\\\z1"
\ contains=pythonEscape,pythonUnicodeEscape,@Spell
\ contains=pythonFStringField,pythonFStringSkip,pythonEscape,pythonUnicodeEscape,@Spell
syn region pythonFString
\ matchgroup=pythonTripleQuotes
\ start=+\cF\z('''\|"""\)+
\ end="\z1"
\ keepend
\ contains=pythonEscape,pythonUnicodeEscape,pythonSpaceError,pythonDoctest,@Spell
\ contains=pythonFStringField,pythonFStringSkip,pythonEscape,pythonUnicodeEscape,pythonSpaceError,pythonDoctest,@Spell
syn region pythonRawFString
\ matchgroup=pythonQuotes
\ start=+\c\%(FR\|RF\)\z(['"]\)+
\ end="\z1"
\ skip="\\\\\|\\\z1"
\ contains=@Spell
\ contains=pythonFStringField,pythonFStringSkip,@Spell
syn region pythonRawFString
\ matchgroup=pythonTripleQuotes
\ start=+\c\%(FR\|RF\)\z('''\|"""\)+
\ end="\z1"
\ keepend
\ contains=pythonSpaceError,pythonDoctest,@Spell
\ contains=pythonFStringField,pythonFStringSkip,pythonSpaceError,pythonDoctest,@Spell
" Bytes
syn region pythonBytes
@@ -209,6 +209,24 @@ syn region pythonRawBytes
\ end="\z1"
\ keepend
" F-string replacement fields
"
" - Matched parentheses, brackets and braces are ignored
" - A bare # is ignored to end of line
" - A bare = (surrounded by optional whitespace) enables debugging
" - A bare ! prefixes a conversion field
" - A bare : begins a format specification
" - Matched braces inside a format specification are ignored
"
syn region pythonFStringField
\ matchgroup=pythonFStringDelimiter
\ start=/{/
\ skip=/([^)]*)\|\[[^]]*]\|{[^}]*}\|#.*$/
\ end=/\%(\s*=\s*\)\=\%(!\a\)\=\%(:\%({[^}]*}\|[^}]*\)\+\)\=}/
\ contained
" Doubled braces and Unicode escapes are not replacement fields
syn match pythonFStringSkip /{{\|\\N{/ transparent contained contains=NONE
syn match pythonEscape +\\[abfnrtv'"\\]+ contained
syn match pythonEscape "\\\o\{1,3}" contained
syn match pythonEscape "\\x\x\{2}" contained
@@ -376,6 +394,7 @@ hi def link pythonQuotes String
hi def link pythonTripleQuotes pythonQuotes
hi def link pythonEscape Special
hi def link pythonUnicodeEscape pythonEscape
hi def link pythonFStringDelimiter Special
if !exists("python_no_number_highlight")
hi def link pythonNumber Number
endif