commit 39af96c8efddd8afe04bce8e48aa446126002e0c
parent 82a54a772be64c75412ee02b67f2d9318d675387
Author: zeertzjq <zeertzjq@outlook.com>
Date: Mon, 11 Aug 2025 07:00:58 +0800
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
https://github.com/vim/vim/commit/a94a0555d911da0d3bcd7e91d76c8e8ef03d9b0e
Co-authored-by: Rob B <github@0x7e.net>
Diffstat:
1 file changed, 24 insertions(+), 5 deletions(-)
diff --git a/runtime/syntax/python.vim b/runtime/syntax/python.vim
@@ -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