Skip to content

Commit f7105bd

Browse files
committed
Impplement operator highlighting
Fixes #7 Closes #9
1 parent 51ea0da commit f7105bd

File tree

4 files changed

+58
-32
lines changed

4 files changed

+58
-32
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,7 @@ let g:python_highlight_all = 1
6767
| `g:python_highlight_space_errors` | Highlight trailing spaces | `0` |
6868
| `g:python_highlight_doctests` | Highlight doc-tests | `0` |
6969
| `g:python_highlight_class_vars` | Highlight class variables `self` and `cls` | `0` |
70+
| `g:python_highlight_operators` | Highlight all operators | `0` |
7071
| `g:python_highlight_all` | Enable all highlight options above, except for previously set. | `0` |
7172
| `g:python_highlight_file_headers_as_comments` | Highlight shebang and coding headers as comments | `0` |
7273
| `g:python_slow_sync` | Disable for slow machines | `1` |

doc/python-syntax.txt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,9 @@ following command to your `~/.config/nvim/init.vim` or `~/.vimrc`: >
8686
`g:python_highlight_class_vars` (default `0`)
8787
Highlight class variables `self` and `cls`
8888

89+
`g:python_highlight_operators` (default `0`)
90+
Highlight all operators
91+
8992
`g:python_highlight_all` (default `0`)
9093
Enable all highlight options above, except for previously set.
9194

syntax/python.vim

Lines changed: 34 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,7 @@ if s:Enabled('g:python_highlight_all')
5353
call s:EnableByDefault('g:python_highlight_doctests')
5454
call s:EnableByDefault('g:python_print_as_function')
5555
call s:EnableByDefault('g:python_highlight_class_vars')
56+
call s:EnableByDefault('g:python_highlight_operators')
5657
endif
5758

5859
"
@@ -73,7 +74,6 @@ endif
7374
syn keyword pythonRepeat for while
7475
syn keyword pythonConditional if elif else
7576
syn keyword pythonException try except finally
76-
syn keyword pythonOperator and in is not or
7777
" The standard pyrex.vim unconditionally removes the pythonInclude group, so
7878
" we provide a dummy group here to avoid crashing pyrex.vim.
7979
syn keyword pythonInclude import
@@ -83,7 +83,6 @@ syn match pythonRaiseFromStatement '\<from\>'
8383
syn match pythonImport '^\s*\zsfrom\>'
8484

8585

86-
8786
if s:Python2Syntax()
8887
if !s:Enabled('g:python_print_as_function')
8988
syn keyword pythonStatement print
@@ -100,6 +99,16 @@ else
10099
syn cluster pythonExpression contains=pythonStatement,pythonRepeat,pythonConditional,pythonOperator,pythonNumber,pythonHexNumber,pythonOctNumber,pythonBinNumber,pythonFloat,pythonString,pythonBytes,pythonBoolean,pythonBuiltinObj,pythonBuiltinFunc
101100
endif
102101

102+
103+
"
104+
" Operators
105+
"
106+
syn keyword pythonOperator and in is not or
107+
if s:Enabled('g:python_highlight_operators')
108+
syn match pythonOperator '\V=\|-\|+\|*\|@\|/\|%\|&\||\|^\|~\|<\|>\|!='
109+
endif
110+
syn match pythonError '[$?]\|\([-+@%&|^~]\)\1\{1,}\|\([=*/<>]\)\2\{2,}\|\([-=+*@/%&|^~<>]\)\3\@![-+*@/%&|^~<>]\|[<!>]\+=\{2,}\|[<!>]\{2,}=\+' display
111+
103112
"
104113
" Decorators (new in Python 2.4)
105114
"
@@ -127,10 +136,7 @@ syn keyword pythonTodo TODO FIXME XXX contained
127136
" Errors
128137
"
129138

130-
syn match pythonError '\<\d\+\D\+\>' display
131-
syn match pythonError '[$?]' display
132-
syn match pythonError '[&|]\{2,}' display
133-
syn match pythonError '[=]\{3,}' display
139+
syn match pythonError '\<\d\+[^0-9[:space:]]\+\>' display
134140

135141
" Mixing spaces and tabs also may be used for pretty formatting multiline
136142
" statements
@@ -275,34 +281,34 @@ endif
275281
"
276282

277283
if s:Python2Syntax()
278-
syn match pythonHexError '\<0[xX]\x*[g-zG-Z]\+\x*[lL]\=\>' display
279-
syn match pythonOctError '\<0[oO]\=\o*\D\+\d*[lL]\=\>' display
280-
syn match pythonBinError '\<0[bB][01]*\D\+\d*[lL]\=\>' display
284+
syn match pythonHexError '\<0[xX]\x*[g-zG-Z]\+\x*[lL]\=\>' display
285+
syn match pythonOctError '\<0[oO]\=\o*\D\+\d*[lL]\=\>' display
286+
syn match pythonBinError '\<0[bB][01]*\D\+\d*[lL]\=\>' display
281287

282-
syn match pythonHexNumber '\<0[xX]\x\+[lL]\=\>' display
283-
syn match pythonOctNumber '\<0[oO]\o\+[lL]\=\>' display
284-
syn match pythonBinNumber '\<0[bB][01]\+[lL]\=\>' display
288+
syn match pythonHexNumber '\<0[xX]\x\+[lL]\=\>' display
289+
syn match pythonOctNumber '\<0[oO]\o\+[lL]\=\>' display
290+
syn match pythonBinNumber '\<0[bB][01]\+[lL]\=\>' display
285291

286292
syn match pythonNumberError '\<\d\+\D[lL]\=\>' display
287293
syn match pythonNumber '\<\d[lL]\=\>' display
288294
syn match pythonNumber '\<[0-9]\d\+[lL]\=\>' display
289295
syn match pythonNumber '\<\d\+[lLjJ]\>' display
290296

291-
syn match pythonOctError '\<0[oO]\=\o*[8-9]\d*[lL]\=\>' display
292-
syn match pythonBinError '\<0[bB][01]*[2-9]\d*[lL]\=\>' display
297+
syn match pythonOctError '\<0[oO]\=\o*[8-9]\d*[lL]\=\>' display
298+
syn match pythonBinError '\<0[bB][01]*[2-9]\d*[lL]\=\>' display
293299

294-
syn match pythonFloat '\.\d\+\%([eE][+-]\=\d\+\)\=[jJ]\=\>' display
295-
syn match pythonFloat '\<\d\+[eE][+-]\=\d\+[jJ]\=\>' display
296-
syn match pythonFloat '\<\d\+\.\d*\%([eE][+-]\=\d\+\)\=[jJ]\=' display
300+
syn match pythonFloat '\.\d\+\%([eE][+-]\=\d\+\)\=[jJ]\=\>' display
301+
syn match pythonFloat '\<\d\+[eE][+-]\=\d\+[jJ]\=\>' display
302+
syn match pythonFloat '\<\d\+\.\d*\%([eE][+-]\=\d\+\)\=[jJ]\=' display
297303
else
298-
syn match pythonOctError '\<0[oO]\=\o*\D\+\d*\>' display
304+
syn match pythonOctError '\<0[oO]\=\o*\D\+\d*\>' display
299305
" pythonHexError comes after pythonOctError so that 0xffffl is pythonHexError
300-
syn match pythonHexError '\<0[xX]\x*[g-zG-Z]\x*\>' display
301-
syn match pythonBinError '\<0[bB][01]*\D\+\d*\>' display
306+
syn match pythonHexError '\<0[xX]\x*[g-zG-Z]\x*\>' display
307+
syn match pythonBinError '\<0[bB][01]*\D\+\d*\>' display
302308

303-
syn match pythonHexNumber '\<0[xX][_0-9a-fA-F]*\x\>' display
304-
syn match pythonOctNumber '\<0[oO][_0-7]*\o\>' display
305-
syn match pythonBinNumber '\<0[bB][_01]*[01]\>' display
309+
syn match pythonHexNumber '\<0[xX][_0-9a-fA-F]*\x\>' display
310+
syn match pythonOctNumber '\<0[oO][_0-7]*\o\>' display
311+
syn match pythonBinNumber '\<0[bB][_01]*[01]\>' display
306312

307313
syn match pythonNumberError '\<\d[_0-9]*\D\>' display
308314
syn match pythonNumberError '\<0[_0-9]\+\>' display
@@ -314,12 +320,12 @@ else
314320
syn match pythonNumber '\<\d[jJ]\>' display
315321
syn match pythonNumber '\<[1-9][_0-9]*\d[jJ]\>' display
316322

317-
syn match pythonOctError '\<0[oO]\=\o*[8-9]\d*\>' display
318-
syn match pythonBinError '\<0[bB][01]*[2-9]\d*\>' display
323+
syn match pythonOctError '\<0[oO]\=\o*[8-9]\d*\>' display
324+
syn match pythonBinError '\<0[bB][01]*[2-9]\d*\>' display
319325

320-
syn match pythonFloat '\.\d\%([_0-9]*\d\)\=\%([eE][+-]\=\d\%([_0-9]*\d\)\=\)\=[jJ]\=\>' display
321-
syn match pythonFloat '\<\d\%([_0-9]*\d\)\=[eE][+-]\=\d\%([_0-9]*\d\)\=[jJ]\=\>' display
322-
syn match pythonFloat '\<\d\%([_0-9]*\d\)\=\.\d\%([_0-9]*\d\)\=\%([eE][+-]\=\d\%([_0-9]*\d\)\=\)\=[jJ]\=' display
326+
syn match pythonFloat '\.\d\%([_0-9]*\d\)\=\%([eE][+-]\=\d\%([_0-9]*\d\)\=\)\=[jJ]\=\>' display
327+
syn match pythonFloat '\<\d\%([_0-9]*\d\)\=[eE][+-]\=\d\%([_0-9]*\d\)\=[jJ]\=\>' display
328+
syn match pythonFloat '\<\d\%([_0-9]*\d\)\=\.\d\%([_0-9]*\d\)\=\%([eE][+-]\=\d\%([_0-9]*\d\)\=\)\=[jJ]\=' display
323329
endif
324330

325331
"

tests/test.py

Lines changed: 20 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
# Keywords.
1010

1111
with break continue del exec return pass print raise global assert lambda yield
12-
for while if elif else import as try except finally and in is not or
12+
for while if elif else import as try except finally
1313

1414
from test import var as name
1515

@@ -66,6 +66,23 @@ async def Test
6666
@ декоратор
6767
@ декоратор.décorateur
6868

69+
# Operators
70+
71+
and or in is not
72+
=
73+
- + * ** @ / // %
74+
& | ^ ~ << >>
75+
< <= == != >= >
76+
77+
# Erroneous operators
78+
79+
$ ?
80+
===
81+
-- ++ *** @@ /// %%
82+
&& || ^^ ~~ <<< >>>
83+
<== <<= !== !!= >== >>=
84+
%- +- -+
85+
6986
# Numbers
7087

7188
0 1 2 9 10 0x1f .3 12.34 0j 124j 34.2E-3 0b10 0o77 1023434 0x0
@@ -141,11 +158,10 @@ async def Test
141158
Test
142159
'''
143160

144-
# Erroneous symbols or bad variable names.
161+
# Erroneous variable names
145162

146-
$ ? 6xav
163+
6xav
147164

148-
&& || ===
149165

150166
# Indentation errors.
151167

0 commit comments

Comments
 (0)