11" Vim syntax file
22" Language: Python
33" Maintainer: Zvezdan Petkovic <[email protected] >4- " Last Change: 2016 Sep 14
4+ " Last Change: 2016 Oct 29
55" Credits: Neil Schemenauer <[email protected] >66" Dmitry Vasiliev
77"
4646let s: cpo_save = &cpo
4747set cpo &vim
4848
49+ if exists (" python_no_doctest_highlight" )
50+ let python_no_doctest_code_highlight = 1
51+ endif
52+
53+ if exists (" python_highlight_all" )
54+ if exists (" python_no_builtin_highlight" )
55+ unlet python_no_builtin_highlight
56+ endif
57+ if exists (" python_no_doctest_code_highlight" )
58+ unlet python_no_doctest_code_highlight
59+ endif
60+ if exists (" python_no_doctest_highlight" )
61+ unlet python_no_doctest_highlight
62+ endif
63+ if exists (" python_no_exception_highlight" )
64+ unlet python_no_exception_highlight
65+ endif
66+ if exists (" python_no_number_highlight" )
67+ unlet python_no_number_highlight
68+ endif
69+ let python_space_error_highlight = 1
70+ endif
71+
4972" Keep Python keywords in alphabetical order inside groups for easy
5073" comparison with the table in the 'Python Language Reference'
5174" https://docs.python.org/2/reference/lexical_analysis.html#keywords,
@@ -81,30 +104,31 @@ syn keyword pythonInclude from import
81104syn keyword pythonAsync async await
82105
83106" Decorators (new in Python 2.4)
84- " Python 3.5 introduced the use of the same symbol for matrix
85- " multiplication. We now have to exclude the symbol from being
86- " highlighted when used in that context. Hence, the check that it's
87- " preceded by empty space only (possibly in a docstring/doctest) and
88- " followed by decorator name, optional parenthesized list of arguments,
89- " and the next line with either def, class, or another decorator.
90- syn match pythonDecorator
91- \ " \% (\% (^\s *\)\% (\% (>>>\|\.\.\.\)\s\+\)\=\)\z s@\% (\s *\h\% (\w\|\.\) *\s *\% ((\_\s\{ -}[^)]\_ .\{ -})\s *\)\=\% (#.*\)\= \n \s *\% (\.\.\.\s\+\)\=\% (@\s *\h\|\% (def\| class\)\s\+\)\)\@ ="
92- \ display nextgroup= pythonDecoratorName skipwhite
93-
94107" A dot must be allowed because of @MyClass.myfunc decorators.
95- " It must be preceded by a decorator symbol and on a separate line from
96- " a function/class it decorates.
97- syn match pythonDecoratorName
98- \ " \% (@\s *\)\@ <=\h\% (\w\|\.\) *\% (\s *\% ((\_\s\{ -}[^)]\_ .\{ -})\s *\)\=\% (#.*\)\= \n \)\@ ="
99- \ contained display nextgroup= pythonFunction skipnl
108+ syn match pythonDecorator " @" display contained
109+ syn match pythonDecoratorName " @\s *\h\% (\w\|\.\) *" display contains =pythonDecorator
110+
111+ " Python 3.5 introduced the use of the same symbol for matrix multiplication:
112+ " https://www.python.org/dev/peps/pep-0465/. We now have to exclude the
113+ " symbol from highlighting when used in that context.
114+ " Single line multiplication.
115+ syn match pythonMatrixMultiply
116+ \ " \% (\w\| [])]\)\s *@"
117+ \ contains= ALLBUT ,pythonDecoratorName,pythonDecorator,pythonFunction,pythonDoctestValue
118+ \ transparent
119+ " Multiplication continued on the next line after backslash.
120+ syn match pythonMatrixMultiply
121+ \ " [^\\ ]\\ \s *\n \% (\s *\.\.\.\s\)\=\s\+ @"
122+ \ contains= ALLBUT ,pythonDecoratorName,pythonDecorator,pythonFunction,pythonDoctestValue
123+ \ transparent
124+ " Multiplication in a parenthesized expression over multiple lines with @ at
125+ " the start of each continued line; very similar to decorators and complex.
126+ syn match pythonMatrixMultiply
127+ \ " ^\s *\% (\% (>>>\|\.\.\.\)\s\+\)\=\z s\% (\h\|\% (\h\| [[(]\) .\{ -}\% (\w\| [])]\)\)\s *\n \% (\s *\.\.\.\s\)\=\s\+ @\% (.\{ -}\n \% (\s *\.\.\.\s\)\=\s\+ @\) *"
128+ \ contains= ALLBUT ,pythonDecoratorName,pythonDecorator,pythonFunction,pythonDoctestValue
129+ \ transparent
100130
101- " The zero-length non-grouping match of def or class before the function
102- " name is extremely important in pythonFunction. Without it, everything
103- " is interpreted as a function inside the contained environment of
104- " doctests.
105- syn match pythonFunction
106- \ " \% (\% (^\s *\)\% (\% (>>>\|\.\.\.\)\s\+\)\=\% (def\| class\)\s\+\)\@ <=\h\w *"
107- \ contained
131+ syn match pythonFunction " \h\w *" display contained
108132
109133syn match pythonComment " #.*$" contains =pythonTodo,@Spell
110134syn keyword pythonTodo FIXME NOTE NOTES TODO XXX contained
@@ -131,25 +155,6 @@ syn match pythonEscape "\%(\\u\x\{4}\|\\U\x\{8}\)" contained
131155syn match pythonEscape " \\ N{\a\+\% (\s\a\+\) *}" contained
132156syn match pythonEscape " \\ $"
133157
134- if exists (" python_highlight_all" )
135- if exists (" python_no_builtin_highlight" )
136- unlet python_no_builtin_highlight
137- endif
138- if exists (" python_no_doctest_code_highlight" )
139- unlet python_no_doctest_code_highlight
140- endif
141- if exists (" python_no_doctest_highlight" )
142- unlet python_no_doctest_highlight
143- endif
144- if exists (" python_no_exception_highlight" )
145- unlet python_no_exception_highlight
146- endif
147- if exists (" python_no_number_highlight" )
148- unlet python_no_number_highlight
149- endif
150- let python_space_error_highlight = 1
151- endif
152-
153158" It is very important to understand all details before changing the
154159" regular expressions below or their order.
155160" The word boundaries are *not* the floating-point number boundaries
@@ -213,7 +218,9 @@ if !exists("python_no_builtin_highlight")
213218 " non-essential built-in functions; Python 2 only
214219 syn keyword pythonBuiltin apply buffer coerce intern
215220 " avoid highlighting attributes as builtins
216- syn match pythonAttribute / \.\h\w */ hs =s + 1 contains =ALLBUT,pythonBuiltin transparent
221+ syn match pythonAttribute / \.\h\w */ hs =s + 1
222+ \ contains= ALLBUT ,pythonBuiltin,pythonFunction,pythonAsync
223+ \ transparent
217224endif
218225
219226" From the 'Python Library Reference' class hierarchy at the bottom.
@@ -275,7 +282,7 @@ if !exists("python_no_doctest_highlight")
275282 if ! exists (" python_no_doctest_code_highlight" )
276283 syn region pythonDoctest
277284 \ start = " ^\s *>>>\s " end = " ^\s *$"
278- \ contained contains= ALLBUT ,pythonDoctest,@S pell
285+ \ contained contains= ALLBUT ,pythonDoctest,pythonFunction, @S pell
279286 syn region pythonDoctestValue
280287 \ start = + ^\s * \% (>>>\s\|\.\.\.\s\| " " " \| '''\)\@ !\S\+ + end=" $ "
281288 \ contained
@@ -287,7 +294,7 @@ if !exists("python_no_doctest_highlight")
287294endif
288295
289296" Sync at the beginning of class, function, or method definition.
290- syn sync match pythonSync grouphere NONE " ^\s * \ % (def\| class\)\s\+\h\w *\s *( "
297+ syn sync match pythonSync grouphere NONE " ^\% (def\| class\)\s\+\h\w *\s *[(:] "
291298
292299" The default highlight links. Can be overridden later.
293300hi def link pythonStatement Statement
0 commit comments