Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add python f-string and string-prefix highlight #14048

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
37 changes: 32 additions & 5 deletions runtime/syntax/python.vim
Original file line number Diff line number Diff line change
Expand Up @@ -143,25 +143,47 @@ syn keyword pythonTodo FIXME NOTE NOTES TODO XXX contained

" Triple-quoted strings can contain doctests.
syn region pythonString matchgroup=pythonQuotes
\ start=+[uU]\=\z(['"]\)+ end="\z1" skip="\\\\\|\\\z1"
\ start=+[uUbB]\=\z(['"]\)+ end="$\|\z1" skip="\\\\\|\\\z1"
\ contains=pythonEscape,@Spell
syn region pythonString matchgroup=pythonTripleQuotes
\ start=+[uU]\=\z('''\|"""\)+ end="\z1" keepend
\ start=+[uUbB]\=\z('''\|"""\)+ end="\z1" keepend
\ contains=pythonEscape,pythonSpaceError,pythonDoctest,@Spell
syn region pythonRawString matchgroup=pythonQuotes
\ start=+[uU]\=[rR]\z(['"]\)+ end="\z1" skip="\\\\\|\\\z1"
\ start=+\%([bB][rR]\=\|[rR][bB]\=\)\z(['"]\)+
\ end="$\|\z1" skip="\\\\\|\\\r\=$\|\\\z1"
\ contains=@Spell
syn region pythonRawString matchgroup=pythonTripleQuotes
\ start=+[uU]\=[rR]\z('''\|"""\)+ end="\z1" keepend
\ start=+\%([bB][rR]\=\|[rR][bB]\=\)\z('''\|"""\)+
\ end="\z1" keepend
\ contains=pythonSpaceError,pythonDoctest,@Spell
syn region pythonFString matchgroup=pythonQuotes
\ start=+[fF]\z(['"]\)+
\ end="$\|\z1" skip="\\\\\|\\\z1"
\ contains=pythonEscape,pythonFStringEscapedBrace,pythonFStringValue,@Spell
syn region pythonFString matchgroup=pythonTripleQuotes
\ start=+[fF]\z('''\|"""\)+
\ end="\z1" keepend
\ contains=pythonEscape,pythonSpaceError,pythonFStringEscapedBrace,pythonFStringValue,pythonDoctest,@Spell
syn region pythonRawFString matchgroup=pythonQuotes
\ start=+\%([fF][rR]\|[rR][fF]\)\z(['"]\)+
\ end="$\|\z1" skip="\\\\\|\\\r\=$\|\\\z1"
\ contains=pythonFStringEscapedBrace,pythonFStringValue,@Spell
syn region pythonRawFString matchgroup=pythonTripleQuotes
\ start=+\%([fF][rR]\|[rR][fF]\)\z('''\|"""\)+
\ end="\z1" keepend
\ contains=pythonSpaceError,pythonFStringEscapedBrace,pythonFStringValue,pythonDoctest,@Spell

syn match pythonEscape +\\[abfnrtv'"\\]+ contained
syn match pythonEscape "\\\o\{1,3}" contained
syn match pythonEscape "\\x\x\{2}" contained
syn match pythonEscape "\%(\\u\x\{4}\|\\U\x\{8}\)" contained
" Python allows case-insensitive Unicode IDs: http://www.unicode.org/charts/
syn match pythonEscape "\\N{\a\+\%(\s\a\+\)*}" contained
syn match pythonEscape "\\$"
syn match pythonEscape "\\\r\=$"

syn region pythonFStringValue matchgroup=pythonFStringBrace
\ start=+{+ end=+=\=\%(![rsa]\)\=\%(:[^{}]*\%({[^{}]*\%({[^{}]*}[^{}]*\)*}[^{}]*\)*\)\=}+ contains=TOP contained excludenl
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

excludenl shouldn't be needed here as the pattern doesn't match EOL.

syn match pythonFStringEscapedBrace "{{\|}}" contained

" It is very important to understand all details before changing the
" regular expressions below or their order.
Expand Down Expand Up @@ -312,9 +334,14 @@ hi def link pythonComment Comment
hi def link pythonTodo Todo
hi def link pythonString String
hi def link pythonRawString String
hi def link pythonFString String
hi def link pythonRawFString String
hi def link pythonQuotes String
hi def link pythonTripleQuotes pythonQuotes
hi def link pythonEscape Special
hi def link pythonFStringEscapedBrace Special
hi def link pythonFStringValue Normal
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This isn't usually specified as it is the default if no highlight group is linked but does no harm.

hi def link pythonFStringBrace Include
if !exists("python_no_number_highlight")
hi def link pythonNumber Number
endif
Expand Down