Skip to content

Commit d4c8e8d

Browse files
clasonf4nb0y
andcommitted
vim-patch:a24f5be: runtime(python): highlight bytes in python
- Highlight bytes literals - Do not highlight Unicode escape sequences in bytes literals fixes: vim/vim#14033 fixes: vim/vim#17726 closes: vim/vim#17728 vim/vim@a24f5be Co-authored-by: Rob B <github@0x7e.net>
1 parent 3eb597c commit d4c8e8d

1 file changed

Lines changed: 35 additions & 7 deletions

File tree

runtime/syntax/python.vim

Lines changed: 35 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
11
" Vim syntax file
22
" Language: Python
33
" Maintainer: Zvezdan Petkovic <zpetkovic@acm.org>
4-
" Last Change: 2023 Feb 28
4+
" Last Change: 2025 Jul 14
55
" Credits: Neil Schemenauer <nas@python.ca>
66
" Dmitry Vasiliev
7+
" Rob B
78
"
89
" This version is a major rewrite by Zvezdan Petkovic.
910
"
@@ -144,24 +145,48 @@ syn keyword pythonTodo FIXME NOTE NOTES TODO XXX contained
144145
" Triple-quoted strings can contain doctests.
145146
syn region pythonString matchgroup=pythonQuotes
146147
\ start=+[uU]\=\z(['"]\)+ end="\z1" skip="\\\\\|\\\z1"
147-
\ contains=pythonEscape,@Spell
148+
\ contains=pythonEscape,pythonUnicodeEscape,@Spell
148149
syn region pythonString matchgroup=pythonTripleQuotes
149150
\ start=+[uU]\=\z('''\|"""\)+ skip=+\\["']+ end="\z1" keepend
150-
\ contains=pythonEscape,pythonSpaceError,pythonDoctest,@Spell
151+
\ contains=pythonEscape,pythonUnicodeEscape,pythonSpaceError,pythonDoctest,@Spell
151152
syn region pythonRawString matchgroup=pythonQuotes
152-
\ start=+[uU]\=[rR]\z(['"]\)+ end="\z1" skip="\\\\\|\\\z1"
153+
\ start=+[rR]\z(['"]\)+ end="\z1" skip="\\\\\|\\\z1"
153154
\ contains=@Spell
154155
syn region pythonRawString matchgroup=pythonTripleQuotes
155-
\ start=+[uU]\=[rR]\z('''\|"""\)+ end="\z1" keepend
156+
\ start=+[rR]\z('''\|"""\)+ end="\z1" keepend
156157
\ contains=pythonSpaceError,pythonDoctest,@Spell
157158

159+
" Bytes
160+
syn region pythonBytes
161+
\ matchgroup=pythonQuotes
162+
\ start=+\cB\z(['"]\)+
163+
\ end="\z1"
164+
\ skip="\\\\\|\\\z1"
165+
\ contains=pythonEscape
166+
syn region pythonBytes
167+
\ matchgroup=pythonTripleQuotes
168+
\ start=+\cB\z('''\|"""\)+
169+
\ end="\z1"
170+
\ keepend
171+
\ contains=pythonEscape
172+
syn region pythonRawBytes
173+
\ matchgroup=pythonQuotes
174+
\ start=+\c\%(BR\|RB\)\z(['"]\)+
175+
\ end="\z1"
176+
\ skip="\\\\\|\\\z1"
177+
syn region pythonRawBytes
178+
\ matchgroup=pythonTripleQuotes
179+
\ start=+\c\%(BR\|RB\)\z('''\|"""\)+
180+
\ end="\z1"
181+
\ keepend
182+
158183
syn match pythonEscape +\\[abfnrtv'"\\]+ contained
159184
syn match pythonEscape "\\\o\{1,3}" contained
160185
syn match pythonEscape "\\x\x\{2}" contained
161-
syn match pythonEscape "\%(\\u\x\{4}\|\\U\x\{8}\)" contained
186+
syn match pythonUnicodeEscape "\%(\\u\x\{4}\|\\U\x\{8}\)" contained
162187
" Python allows case-insensitive Unicode IDs: http://www.unicode.org/charts/
163188
" The specification: https://www.unicode.org/versions/Unicode16.0.0/core-spec/chapter-4/#G135165
164-
syn match pythonEscape "\\N{\a\+\%(\%(\s\a\+[[:alnum:]]*\)\|\%(-[[:alnum:]]\+\)\)*}" contained
189+
syn match pythonUnicodeEscape "\\N{\a\+\%(\%(\s\a\+[[:alnum:]]*\)\|\%(-[[:alnum:]]\+\)\)*}" contained
165190
syn match pythonEscape "\\$"
166191

167192
" It is very important to understand all details before changing the
@@ -313,9 +338,12 @@ hi def link pythonComment Comment
313338
hi def link pythonTodo Todo
314339
hi def link pythonString String
315340
hi def link pythonRawString String
341+
hi def link pythonBytes String
342+
hi def link pythonRawBytes String
316343
hi def link pythonQuotes String
317344
hi def link pythonTripleQuotes pythonQuotes
318345
hi def link pythonEscape Special
346+
hi def link pythonUnicodeEscape pythonEscape
319347
if !exists("python_no_number_highlight")
320348
hi def link pythonNumber Number
321349
endif

0 commit comments

Comments
 (0)