Skip to content

Commit 94b25d4

Browse files
committed
Highlight '# type:' comments (PEP 484)
1 parent 31ba9aa commit 94b25d4

File tree

8 files changed

+309
-41
lines changed

8 files changed

+309
-41
lines changed

grammars/MagicPython.cson

Lines changed: 64 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,70 @@ repository:
4949
include: "#docstring-module"
5050
}
5151
]
52+
comments:
53+
patterns: [
54+
{
55+
name: "comment.line.number-sign.python meta.typehint.comment.python"
56+
begin: '''
57+
(?x)
58+
(?:
59+
\\# \\s* (type:)
60+
\\s*+ (?# we want `\\s*+` which is possessive quantifier since
61+
we do not actually want to backtrack when matching
62+
whitespace here)
63+
(?! $ | \\#)
64+
)
65+
66+
'''
67+
end: "(?:$|(?=\\#))"
68+
beginCaptures:
69+
"1":
70+
name: "keyword.comment.typehint.notation.python"
71+
patterns: [
72+
{
73+
name: "comment.typehint.ignore.notation.python"
74+
match: '''
75+
(?x)
76+
\\G ignore \\s* ($|(?=\\#))
77+
78+
'''
79+
}
80+
{
81+
name: "comment.typehint.type.notation.python"
82+
match: '''
83+
(?x)
84+
(?<!\\.)\\b(
85+
bool | bytes | float | int | object | str
86+
| List | Dict | Iterable | Sequence
87+
| Any | None
88+
)\\b
89+
90+
'''
91+
}
92+
{
93+
name: "comment.typehint.puctuation.notation.python"
94+
match: "([\\[\\]\\(\\),\\.\\=\\*]|(->))"
95+
}
96+
{
97+
name: "comment.typehint.variable.notation.python"
98+
match: "([[:alpha:]_]\\w*)"
99+
}
100+
]
101+
}
102+
{
103+
name: "comment.line.number-sign.python"
104+
begin: "(\\#)"
105+
end: "($)"
106+
beginCaptures:
107+
"1":
108+
name: "punctuation.definition.comment.python"
109+
patterns: [
110+
{
111+
include: "#codetags"
112+
}
113+
]
114+
}
115+
]
52116
"docstring-block":
53117
begin: "(?<=:)"
54118
end: '''
@@ -1755,18 +1819,6 @@ repository:
17551819
include: "#regexp-escape-catchall"
17561820
}
17571821
]
1758-
comments:
1759-
name: "comment.line.number-sign.python"
1760-
begin: "(\\#)"
1761-
beginCaptures:
1762-
"1":
1763-
name: "punctuation.definition.comment.python"
1764-
end: "($)"
1765-
patterns: [
1766-
{
1767-
include: "#codetags"
1768-
}
1769-
]
17701822
"comments-string-single-three":
17711823
name: "comment.line.number-sign.python"
17721824
begin: "(\\#)"

grammars/MagicPython.syntax.yaml

Lines changed: 48 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -12,11 +12,6 @@ repository:
1212
$apply:
1313
- file: 'regexp-common.inc.syntax.yaml'
1414

15-
- file: 'comment.inc.syntax.yaml'
16-
vars:
17-
suffix: ''
18-
marker: ''
19-
2015
- file: 'comment.inc.syntax.yaml'
2116
vars:
2217
suffix: '-string-single-three'
@@ -79,6 +74,54 @@ repository:
7974
- include: '#docstring-block'
8075
- include: '#docstring-module'
8176

77+
comments:
78+
patterns:
79+
- name: comment.line.number-sign.python
80+
meta.typehint.comment.python
81+
82+
begin: |
83+
(?x)
84+
(?:
85+
\# \s* (type:)
86+
\s*+ (?# we want `\s*+` which is possessive quantifier since
87+
we do not actually want to backtrack when matching
88+
whitespace here)
89+
(?! $ | \#)
90+
)
91+
92+
end: (?:$|(?=\#))
93+
beginCaptures:
94+
'1': {name: keyword.comment.typehint.notation.python}
95+
96+
patterns:
97+
- name: comment.typehint.ignore.notation.python
98+
match: |
99+
(?x)
100+
\G ignore \s* ($|(?=\#))
101+
102+
- name: comment.typehint.type.notation.python
103+
match: |
104+
(?x)
105+
(?<!\.)\b(
106+
bool | bytes | float | int | object | str
107+
| List | Dict | Iterable | Sequence
108+
| Any | None
109+
)\b
110+
111+
- name: comment.typehint.puctuation.notation.python
112+
match: ([\[\]\(\),\.\=\*]|(->))
113+
114+
- name: comment.typehint.variable.notation.python
115+
match: ([[:alpha:]_]\w*)
116+
117+
- name: comment.line.number-sign.python
118+
begin: (\#)
119+
end: ($)
120+
beginCaptures:
121+
'1': {name: punctuation.definition.comment.python}
122+
patterns:
123+
- include: '#codetags'
124+
82125
docstring-block:
83126
begin: (?<=:)
84127
end: |

grammars/MagicPython.tmLanguage

Lines changed: 88 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,94 @@
7272
</dict>
7373
</array>
7474
</dict>
75+
<key>comments</key>
76+
<dict>
77+
<key>patterns</key>
78+
<array>
79+
<dict>
80+
<key>name</key>
81+
<string>comment.line.number-sign.python meta.typehint.comment.python</string>
82+
<key>begin</key>
83+
<string>(?x)
84+
(?:
85+
\# \s* (type:)
86+
\s*+ (?# we want `\s*+` which is possessive quantifier since
87+
we do not actually want to backtrack when matching
88+
whitespace here)
89+
(?! $ | \#)
90+
)
91+
</string>
92+
<key>end</key>
93+
<string>(?:$|(?=\#))</string>
94+
<key>beginCaptures</key>
95+
<dict>
96+
<key>1</key>
97+
<dict>
98+
<key>name</key>
99+
<string>keyword.comment.typehint.notation.python</string>
100+
</dict>
101+
</dict>
102+
<key>patterns</key>
103+
<array>
104+
<dict>
105+
<key>name</key>
106+
<string>comment.typehint.ignore.notation.python</string>
107+
<key>match</key>
108+
<string>(?x)
109+
\G ignore \s* ($|(?=\#))
110+
</string>
111+
</dict>
112+
<dict>
113+
<key>name</key>
114+
<string>comment.typehint.type.notation.python</string>
115+
<key>match</key>
116+
<string>(?x)
117+
(?&lt;!\.)\b(
118+
bool | bytes | float | int | object | str
119+
| List | Dict | Iterable | Sequence
120+
| Any | None
121+
)\b
122+
</string>
123+
</dict>
124+
<dict>
125+
<key>name</key>
126+
<string>comment.typehint.puctuation.notation.python</string>
127+
<key>match</key>
128+
<string>([\[\]\(\),\.\=\*]|(-&gt;))</string>
129+
</dict>
130+
<dict>
131+
<key>name</key>
132+
<string>comment.typehint.variable.notation.python</string>
133+
<key>match</key>
134+
<string>([[:alpha:]_]\w*)</string>
135+
</dict>
136+
</array>
137+
</dict>
138+
<dict>
139+
<key>name</key>
140+
<string>comment.line.number-sign.python</string>
141+
<key>begin</key>
142+
<string>(\#)</string>
143+
<key>end</key>
144+
<string>($)</string>
145+
<key>beginCaptures</key>
146+
<dict>
147+
<key>1</key>
148+
<dict>
149+
<key>name</key>
150+
<string>punctuation.definition.comment.python</string>
151+
</dict>
152+
</dict>
153+
<key>patterns</key>
154+
<array>
155+
<dict>
156+
<key>include</key>
157+
<string>#codetags</string>
158+
</dict>
159+
</array>
160+
</dict>
161+
</array>
162+
</dict>
75163
<key>docstring-block</key>
76164
<dict>
77165
<key>begin</key>
@@ -2777,30 +2865,6 @@ indirectly through syntactic constructs
27772865
</dict>
27782866
</array>
27792867
</dict>
2780-
<key>comments</key>
2781-
<dict>
2782-
<key>name</key>
2783-
<string>comment.line.number-sign.python</string>
2784-
<key>begin</key>
2785-
<string>(\#)</string>
2786-
<key>beginCaptures</key>
2787-
<dict>
2788-
<key>1</key>
2789-
<dict>
2790-
<key>name</key>
2791-
<string>punctuation.definition.comment.python</string>
2792-
</dict>
2793-
</dict>
2794-
<key>end</key>
2795-
<string>($)</string>
2796-
<key>patterns</key>
2797-
<array>
2798-
<dict>
2799-
<key>include</key>
2800-
<string>#codetags</string>
2801-
</dict>
2802-
</array>
2803-
</dict>
28042868
<key>comments-string-single-three</key>
28052869
<dict>
28062870
<key>name</key>

misc/scopes

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
comment.line.number-sign.python
22
comment.regexp
3+
comment.typehint.ignore.notation.python
4+
comment.typehint.puctuation.notation.python
5+
comment.typehint.type.notation.python
6+
comment.typehint.variable.notation.python
37
constant.character.escape.regexp
48
constant.character.format.python
59
constant.character.python
@@ -33,6 +37,7 @@ invalid.illegal.name.python
3337
invalid.illegal.newline.python
3438
invalid.illegal.prefix.python
3539
keyword.codetag.notation.python
40+
keyword.comment.typehint.notation.python
3641
keyword.control.flow.python
3742
keyword.invalid.illegal.name.python
3843
keyword.operator.assignment.python
@@ -60,6 +65,7 @@ meta.function.python
6065
meta.item-access.python
6166
meta.lambda-function.python
6267
meta.named.regexp
68+
meta.typehint.comment.python
6369
punctuation.character.set.begin.regexp
6470
punctuation.character.set.end.regexp
6571
punctuation.comment.begin.regexp

test/comments/typing1.py

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
# type: ignore # test
2+
# type: ignore
3+
# type:ignore
4+
# type: ignore 1
5+
# type: 1 ignore
6+
# type : ignore
7+
##type: ignore
8+
#.type: ignore
9+
10+
11+
12+
# : comment.line.number-sign.python, meta.typehint.comment.python, source.python
13+
type: : comment.line.number-sign.python, keyword.comment.typehint.notation.python, meta.typehint.comment.python, source.python
14+
: comment.line.number-sign.python, meta.typehint.comment.python, source.python
15+
ignore : comment.line.number-sign.python, comment.typehint.ignore.notation.python, meta.typehint.comment.python, source.python
16+
# : comment.line.number-sign.python, punctuation.definition.comment.python, source.python
17+
test : comment.line.number-sign.python, source.python
18+
# : comment.line.number-sign.python, meta.typehint.comment.python, source.python
19+
type: : comment.line.number-sign.python, keyword.comment.typehint.notation.python, meta.typehint.comment.python, source.python
20+
: comment.line.number-sign.python, meta.typehint.comment.python, source.python
21+
ignore : comment.line.number-sign.python, comment.typehint.ignore.notation.python, meta.typehint.comment.python, source.python
22+
# : comment.line.number-sign.python, meta.typehint.comment.python, source.python
23+
type: : comment.line.number-sign.python, keyword.comment.typehint.notation.python, meta.typehint.comment.python, source.python
24+
ignore : comment.line.number-sign.python, comment.typehint.ignore.notation.python, meta.typehint.comment.python, source.python
25+
# : comment.line.number-sign.python, meta.typehint.comment.python, source.python
26+
type: : comment.line.number-sign.python, keyword.comment.typehint.notation.python, meta.typehint.comment.python, source.python
27+
: comment.line.number-sign.python, meta.typehint.comment.python, source.python
28+
ignore : comment.line.number-sign.python, comment.typehint.variable.notation.python, meta.typehint.comment.python, source.python
29+
1 : comment.line.number-sign.python, meta.typehint.comment.python, source.python
30+
# : comment.line.number-sign.python, meta.typehint.comment.python, source.python
31+
type: : comment.line.number-sign.python, keyword.comment.typehint.notation.python, meta.typehint.comment.python, source.python
32+
: comment.line.number-sign.python, meta.typehint.comment.python, source.python
33+
1 : comment.line.number-sign.python, meta.typehint.comment.python, source.python
34+
ignore : comment.line.number-sign.python, comment.typehint.variable.notation.python, meta.typehint.comment.python, source.python
35+
# : comment.line.number-sign.python, punctuation.definition.comment.python, source.python
36+
type : ignore : comment.line.number-sign.python, source.python
37+
# : comment.line.number-sign.python, punctuation.definition.comment.python, source.python
38+
#type: ignore : comment.line.number-sign.python, source.python
39+
# : comment.line.number-sign.python, punctuation.definition.comment.python, source.python
40+
.type: ignore : comment.line.number-sign.python, source.python

test/comments/typing2.py

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
a = 1 # type:
2+
a = 1 # type: # 123
3+
4+
5+
6+
a : source.python
7+
= : keyword.operator.assignment.python, source.python
8+
: source.python
9+
1 : constant.numeric.dec.python, source.python
10+
: source.python
11+
# : comment.line.number-sign.python, punctuation.definition.comment.python, source.python
12+
type: : comment.line.number-sign.python, source.python
13+
a : source.python
14+
= : keyword.operator.assignment.python, source.python
15+
: source.python
16+
1 : constant.numeric.dec.python, source.python
17+
: source.python
18+
# : comment.line.number-sign.python, punctuation.definition.comment.python, source.python
19+
type: # 123 : comment.line.number-sign.python, source.python

test/comments/typing3.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
# abc type: def
2+
3+
4+
5+
# : comment.line.number-sign.python, punctuation.definition.comment.python, source.python
6+
abc type: def : comment.line.number-sign.python, source.python

0 commit comments

Comments
 (0)