Skip to content

Commit 77cfc49

Browse files
jparisechrisbra
authored andcommitted
runtime(python): highlight ellipsis literals
The ellipsis literal (`...`) can be used in multiple contexts: - Placeholders: `class Foo: ...` - Containers: `Tuple[int, ...]` - Assignments: `x = ...` This is a trickier pattern to match because we can't rely on keyword boundaries, so we instead look for exactly three dots (`...`). This does mean that we will match the `...` portion of `x...x`, which isn't valid Python syntax, but I think that's an acceptable trade-off that avoids making this pattern much more complex. Reference: - https://docs.python.org/3/library/constants.html#Ellipsis closes: #18107 Signed-off-by: Jon Parise <jon@indelible.org> Signed-off-by: Christian Brabandt <cb@256bit.org>
1 parent d7d6a6f commit 77cfc49

File tree

5 files changed

+110
-2
lines changed

5 files changed

+110
-2
lines changed

runtime/syntax/python.vim

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -313,6 +313,8 @@ if !exists("python_no_builtin_highlight")
313313
syn match pythonAttribute /\.\h\w*/hs=s+1
314314
\ contains=ALLBUT,pythonBuiltin,pythonClass,pythonFunction,pythonType,pythonAsync
315315
\ transparent
316+
" the ellipsis literal `...` can be used in multiple syntactic contexts
317+
syn match pythonEllipsis "\.\@1<!.\.\.\ze\.\@!" display
316318
endif
317319

318320
" From the 'Python Library Reference' class hierarchy at the bottom.
@@ -368,10 +370,12 @@ if !exists("python_no_doctest_highlight")
368370
if !exists("python_no_doctest_code_highlight")
369371
syn region pythonDoctest
370372
\ start="^\s*>>>\s" end="^\s*$"
371-
\ contained contains=ALLBUT,pythonDoctest,pythonClass,pythonFunction,pythonType,@Spell
373+
\ contained contains=ALLBUT,pythonDoctest,pythonEllipsis,pythonClass,pythonFunction,pythonType,@Spell
372374
syn region pythonDoctestValue
373375
\ start=+^\s*\%(>>>\s\|\.\.\.\s\|"""\|'''\)\@!\S\++ end="$"
374-
\ contained
376+
\ contained contains=pythonEllipsis
377+
syn match pythonEllipsis "\%(^\s*\)\@<!\.\@1<!\zs\.\.\.\ze\.\@!" display
378+
\ contained containedin=pythonDoctest
375379
else
376380
syn region pythonDoctest
377381
\ start="^\s*>>>" end="^\s*$"
@@ -414,6 +418,7 @@ if !exists("python_no_number_highlight")
414418
endif
415419
if !exists("python_no_builtin_highlight")
416420
hi def link pythonBuiltin Function
421+
hi def link pythonEllipsis pythonBuiltin
417422
endif
418423
if !exists("python_no_exception_highlight")
419424
hi def link pythonExceptions Structure
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
>#+0#0000e05#ffffff0| |E|l@1|i|p|s|i|s| |L|i|t|e|r|a|l| +0#0000000&@56
2+
|#+0#0000e05&| |h|t@1|p|s|:|/@1|d|o|c|s|.|p|y|t|h|o|n|.|o|r|g|/|3|/|l|i|b|r|a|r|y|/|c|o|n|s|t|a|n|t|s|.|h|t|m|l|#|E|l@1|i|p|s|i|s| +0#0000000&@15
3+
@75
4+
|#+0#0000e05&| |P|l|a|c|e|h|o|l|d|e|r|s| +0#0000000&@60
5+
|.+0#00e0e07&@2| +0#0000000&@71
6+
@8|.+0#00e0e07&@2| +0#0000000&@63
7+
|x| |=| |.+0#00e0e07&@2| +0#0000000&@67
8+
|y| |=| |.+0#00e0e07&@2| +0#0000000&|#+0#0000e05&| |C|o|m@1|e|n|t| +0#0000000&@57
9+
|c+0#af5f00255&|l|a|s@1| +0#0000000&|C+0#00e0003&|:+0#0000000&| |.+0#00e0e07&@2| +0#0000000&@62
10+
|l+0#af5f00255&|a|m|b|d|a|:+0#0000000&| |.+0#00e0e07&@2| +0#0000000&@63
11+
@75
12+
|#+0#0000e05&| |A|n@1|o|t|a|t|i|o|n|s| +0#0000000&@61
13+
|n|u|m|b|e|r|s|:| |T|u|p|l|e|[|i+0#00e0e07&|n|t|,+0#0000000&| |.+0#00e0e07&@2|]+0#0000000&| @50
14+
@75
15+
|#+0#0000e05&| |D|o|c|t|e|s|t|s| +0#0000000&@64
16+
|"+0#e000002&@2|A| |d|o|c|t|e|s|t| +0#0000000&@62
17+
@75
18+
|>+0#e000e06&@2| |c+0#af5f00255&|l|a|s@1| +0#e000e06&|A+0#00e0003&|:+0#e000e06&| +0#0000000&@62
19+
|.+0#e000e06&@2| @4|d+0#af5f00255&|e|f| +0#e000e06&|_+0#00e0e07&@1|i|n|i|t|_@1|(+0#0000000&|s+0#00e0e07&|e|l|f|)+0#0000000&|:+0#e000e06&| +0#0000000&@47
20+
@57|1|,|1| @10|T|o|p|
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
| +0&#ffffff0@74
2+
|#+0#0000e05&| |D|o|c|t|e|s|t|s| +0#0000000&@64
3+
|"+0#e000002&@2|A| |d|o|c|t|e|s|t| +0#0000000&@62
4+
@75
5+
|>+0#e000e06&@2| |c+0#af5f00255&|l|a|s@1| +0#e000e06&|A+0#00e0003&|:+0#e000e06&| +0#0000000&@62
6+
>.+0#e000e06&@2| @4|d+0#af5f00255&|e|f| +0#e000e06&|_+0#00e0e07&@1|i|n|i|t|_@1|(+0#0000000&|s+0#00e0e07&|e|l|f|)+0#0000000&|:+0#e000e06&| +0#0000000&@47
7+
|.+0#e000e06&@2| @12|.+0#00e0e07&@2| +0#0000000&@55
8+
|>+0#e000e06&@2| |c+0#af5f00255&|l|a|s@1| +0#e000e06&|B+0#00e0003&|:+0#e000e06&| |.+0#00e0e07&@2| +0#0000000&@58
9+
|>+0#e000e06&@2| |x| |=| |.+0#00e0e07&@2| +0#0000000&@63
10+
|>+0#e000e06&@2| |r+0#af5f00255&|a|i|s|e| +0#e000e06&|V+0#00e0003&|a|l|u|e|E|r@1|o|r|(+0#0000000&|'+0#e000002&|m|u|l|t|i|\+0#e000e06&|n| +0#e000002&@3|l|i|n|e|\+0#e000e06&|n|d+0#e000002&|e|t|a|i|l|'|)+0#0000000&| @27
11+
|T+0#e000e06&|r|a|c|e|b|a|c|k| |(|m|o|s|t| |r|e|c|e|n|t| |c|a|l@1| |l|a|s|t|)|:| +0#0000000&@40
12+
| +0#e000e06&@3|.+0#00e0e07&@2| +0#0000000&@67
13+
|V+0#00e0003&|a|l|u|e|E|r@1|o|r|:+0#e000e06&| |m|u|l|t|i| +0#0000000&@57
14+
| +0#e000e06&@3|l|i|n|e| +0#0000000&@66
15+
|d+0#e000e06&|e|t|a|i|l| +0#0000000&@68
16+
|>+0#e000e06&@2| |p+0#00e0e07&|r|i|n|t|(+0#e000e06&|l+0#00e0e07&|i|s|t|(+0#e000e06&|r+0#00e0e07&|a|n|g|e|(+0#0000000&|2+0#e000002&|0|)+0#0000000&|)+0#e000e06&@1| @1|#+0#0000e05&| |d|o|c|t|e|s|t|:| |+|E|L@1|I|P|S|I|S| +0#0000000&@26
17+
|[+0#e000e06&|0|,| |1|,| |.+0#00e0e07&@2|,+0#e000e06&| |1|8|,| |1|9|]| +0#0000000&@55
18+
|>+0#e000e06&@2| |e+0#00e0e07&|x|e|c|(+0#0000000&|s|)| +0#e000e06&@1|#+0#0000e05&|d|o|c|t|e|s|t|:| |+|E|L@1|I|P|S|I|S| +0#0000000&@42
19+
|-+0#e000e06&|3|.|2|1|7|1|6|0|3|4|2|7|2|e|-|0|.+0#00e0e07&@2|7+0#e000e06&| +0#0000000&@53
20+
@57|1|9|,|1| @9|5|4|%|
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
|-+0#e000e06#ffffff0|3|.|2|1|7|1|6|0|3|4|2|7|2|e|-|0|.+0#00e0e07&@2|7+0#e000e06&| +0#0000000&@53
2+
|"+0#e000002&@2| +0#0000000&@71
3+
@75
4+
|c+0#af5f00255&|l|a|s@1| +0#0000000&|C+0#00e0003&|:+0#0000000&| @66
5+
@8|"+0#e000002&@2| +0#0000000&@63
6+
| +0#e000e06&@7>>@2| |c+0#af5f00255&|l|a|s@1| +0#e000e06&|C+0#00e0003&|:+0#e000e06&| +0#0000000&@54
7+
| +0#e000e06&@7|.@2| @4|d+0#af5f00255&|e|f| +0#e000e06&|_+0#00e0e07&@1|i|n|i|t|_@1|(+0#0000000&|s+0#00e0e07&|e|l|f|)+0#0000000&|:+0#e000e06&| +0#0000000&@39
8+
| +0#e000e06&@7|.@2| @12|.+0#00e0e07&@2| +0#0000000&@47
9+
| +0#e000e06&@7|"+0#e000002&@2| +0#0000000&@63
10+
@75
11+
|#+0#0000e05&| |N|u|m|p|y| +0#0000000&@67
12+
|x|[|.+0#00e0e07&@2|,+0#0000000&| |0+0#e000002&|]+0#0000000&| @65
13+
|~+0#4040ff13&| @73
14+
|~| @73
15+
|~| @73
16+
|~| @73
17+
|~| @73
18+
|~| @73
19+
|~| @73
20+
| +0#0000000&@56|3|7|,|2|-|9| @7|B|o|t|
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
# Ellipsis Literal
2+
# https://docs.python.org/3/library/constants.html#Ellipsis
3+
4+
# Placeholders
5+
...
6+
...
7+
x = ...
8+
y = ... # Comment
9+
class C: ...
10+
lambda: ...
11+
12+
# Annotations
13+
numbers: Tuple[int, ...]
14+
15+
# Doctests
16+
"""A doctest
17+
18+
>>> class A:
19+
... def __init__(self):
20+
... ...
21+
>>> class B: ...
22+
>>> x = ...
23+
>>> raise ValueError('multi\n line\ndetail')
24+
Traceback (most recent call last):
25+
...
26+
ValueError: multi
27+
line
28+
detail
29+
>>> print(list(range(20))) # doctest: +ELLIPSIS
30+
[0, 1, ..., 18, 19]
31+
>>> exec(s) #doctest: +ELLIPSIS
32+
-3.21716034272e-0...7
33+
"""
34+
35+
class C:
36+
"""
37+
>>> class C:
38+
... def __init__(self):
39+
... ...
40+
"""
41+
42+
# Numpy
43+
x[..., 0]

0 commit comments

Comments
 (0)