Skip to content

Commit 082a78a

Browse files
committed
Fix f-string boundaries and add more tests
1 parent e82ed75 commit 082a78a

File tree

2 files changed

+5
-2
lines changed

2 files changed

+5
-2
lines changed

syntax/python.vim

+1-1
Original file line numberDiff line numberDiff line change
@@ -264,7 +264,7 @@ if s:Enabled('g:python_highlight_string_format')
264264
syn match pythonStrFormat '{\%(\%([^[:cntrl:][:space:][:punct:][:digit:]]\|_\)\%([^[:cntrl:][:punct:][:space:]]\|_\)*\|\d\+\)\=\%(\.\%([^[:cntrl:][:space:][:punct:][:digit:]]\|_\)\%([^[:cntrl:][:punct:][:space:]]\|_\)*\|\[\%(\d\+\|[^!:\}]\+\)\]\)*\%(![rsa]\)\=\%(:\%({\%(\%([^[:cntrl:][:space:][:punct:][:digit:]]\|_\)\%([^[:cntrl:][:punct:][:space:]]\|_\)*\|\d\+\)}\|\%([^}]\=[<>=^]\)\=[ +-]\=#\=0\=\d*,\=\%(\.\d\+\)\=[bcdeEfFgGnosxX%]\=\)\=\)\=}' contained containedin=pythonString,pythonUniString,pythonUniRawString,pythonRawString
265265
else
266266
syn match pythonStrFormat "{\%(\%([^[:cntrl:][:space:][:punct:][:digit:]]\|_\)\%([^[:cntrl:][:punct:][:space:]]\|_\)*\|\d\+\)\=\%(\.\%([^[:cntrl:][:space:][:punct:][:digit:]]\|_\)\%([^[:cntrl:][:punct:][:space:]]\|_\)*\|\[\%(\d\+\|[^!:\}]\+\)\]\)*\%(![rsa]\)\=\%(:\%({\%(\%([^[:cntrl:][:space:][:punct:][:digit:]]\|_\)\%([^[:cntrl:][:punct:][:space:]]\|_\)*\|\d\+\)}\|\%([^}]\=[<>=^]\)\=[ +-]\=#\=0\=\d*,\=\%(\.\d\+\)\=[bcdeEfFgGnosxX%]\=\)\=\)\=}" contained containedin=pythonString,pythonRawString
267-
syn region pythonStrInterpRegion matchgroup=pythonStrFormat start="\({\)\@<!{\({\)\@!"he=e,rs=e end="\%(![rsa]\)\=\%(:\%({\%(\%([^[:cntrl:][:space:][:punct:][:digit:]]\|_\)\%([^[:cntrl:][:punct:][:space:]]\|_\)*\|\d\+\)}\|\%([^}]\=[<>=^]\)\=[ +-]\=#\=0\=\d*,\=\%(\.\d\+\)\=[bcdeEfFgGnosxX%]\=\)\=\)\=\(}\)\@<!}\(}\)\@!"hs=s,re=s extend contained containedin=pythonFString,pythonRawFString contains=pythonStrInterpRegion,@pythonExpression
267+
syn region pythonStrInterpRegion matchgroup=pythonStrFormat start="{" end="\%(![rsa]\)\=\%(:\%({\%(\%([^[:cntrl:][:space:][:punct:][:digit:]]\|_\)\%([^[:cntrl:][:punct:][:space:]]\|_\)*\|\d\+\)}\|\%([^}]\=[<>=^]\)\=[ +-]\=#\=0\=\d*,\=\%(\.\d\+\)\=[bcdeEfFgGnosxX%]\=\)\=\)\=}" extend contained containedin=pythonFString,pythonRawFString contains=pythonStrInterpRegion,@pythonExpression
268268
syn match pythonStrFormat "{{\|}}" contained containedin=pythonFString,pythonRawFString
269269
endif
270270
endif

tests/test.py

+4-1
Original file line numberDiff line numberDiff line change
@@ -244,8 +244,11 @@ async def Test
244244
f"{var}...{arr[123]} normal {var['{'] // 0xff} \"xzcb\" 'xzcb' {var['}'] + 1} text"
245245
f"{expr1 if True or False else expr2} {None} wow {','.join(c.lower() for c in 'asdf')}"
246246
f"hello {expr:.2f} yes {(lambda: 0b1)():#03x} lol {var!r}"
247-
f'brackets: {{ 1 + 2 }} and {{{{ 3 + 4 }}}}'
247+
f'brackets: {{{ 1 + 2 }}} and {{{{ 3 + 4 }}}}'
248248
fr'this {that}'
249+
f"{f'{1+1}'}"
250+
'{{ }}'
251+
f"{"{test}"}" # FIXME: syntax error that should not be highlighted
249252

250253
# Doctests.
251254

0 commit comments

Comments
 (0)