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 " Vim syntax file
" Language: Python " Language: Python
" Maintainer: Zvezdan Petkovic <zpetkovic@acm.org> " Maintainer: Zvezdan Petkovic <zpetkovic@acm.org>
" Last Change: 2025 Jul 26 " Last Change: 2025 Aug 10
" Credits: Neil Schemenauer <nas@python.ca> " Credits: Neil Schemenauer <nas@python.ca>
" Dmitry Vasiliev " Dmitry Vasiliev
" Rob B " Rob B
@@ -165,25 +165,25 @@ syn region pythonFString
\ start=+\cF\z(['"]\)+ \ start=+\cF\z(['"]\)+
\ end="\z1" \ end="\z1"
\ skip="\\\\\|\\\z1" \ skip="\\\\\|\\\z1"
\ contains=pythonEscape,pythonUnicodeEscape,@Spell \ contains=pythonFStringField,pythonFStringSkip,pythonEscape,pythonUnicodeEscape,@Spell
syn region pythonFString syn region pythonFString
\ matchgroup=pythonTripleQuotes \ matchgroup=pythonTripleQuotes
\ start=+\cF\z('''\|"""\)+ \ start=+\cF\z('''\|"""\)+
\ end="\z1" \ end="\z1"
\ keepend \ keepend
\ contains=pythonEscape,pythonUnicodeEscape,pythonSpaceError,pythonDoctest,@Spell \ contains=pythonFStringField,pythonFStringSkip,pythonEscape,pythonUnicodeEscape,pythonSpaceError,pythonDoctest,@Spell
syn region pythonRawFString syn region pythonRawFString
\ matchgroup=pythonQuotes \ matchgroup=pythonQuotes
\ start=+\c\%(FR\|RF\)\z(['"]\)+ \ start=+\c\%(FR\|RF\)\z(['"]\)+
\ end="\z1" \ end="\z1"
\ skip="\\\\\|\\\z1" \ skip="\\\\\|\\\z1"
\ contains=@Spell \ contains=pythonFStringField,pythonFStringSkip,@Spell
syn region pythonRawFString syn region pythonRawFString
\ matchgroup=pythonTripleQuotes \ matchgroup=pythonTripleQuotes
\ start=+\c\%(FR\|RF\)\z('''\|"""\)+ \ start=+\c\%(FR\|RF\)\z('''\|"""\)+
\ end="\z1" \ end="\z1"
\ keepend \ keepend
\ contains=pythonSpaceError,pythonDoctest,@Spell \ contains=pythonFStringField,pythonFStringSkip,pythonSpaceError,pythonDoctest,@Spell
" Bytes " Bytes
syn region pythonBytes syn region pythonBytes
@@ -209,6 +209,24 @@ syn region pythonRawBytes
\ end="\z1" \ end="\z1"
\ keepend \ 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 +\\[abfnrtv'"\\]+ contained
syn match pythonEscape "\\\o\{1,3}" contained syn match pythonEscape "\\\o\{1,3}" contained
syn match pythonEscape "\\x\x\{2}" 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 pythonTripleQuotes pythonQuotes
hi def link pythonEscape Special hi def link pythonEscape Special
hi def link pythonUnicodeEscape pythonEscape hi def link pythonUnicodeEscape pythonEscape
hi def link pythonFStringDelimiter Special
if !exists("python_no_number_highlight") if !exists("python_no_number_highlight")
hi def link pythonNumber Number hi def link pythonNumber Number
endif endif