Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit 2b521f0

Browse files
committedMar 12, 2025
add rudimentary support for PEP 695
1 parent 7d0f2b2 commit 2b521f0

File tree

3 files changed

+440
-6
lines changed

3 files changed

+440
-6
lines changed
 

‎grammars/MagicPython.cson‎

Lines changed: 136 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,9 @@ repository:
3939
{
4040
include: "#import"
4141
}
42+
{
43+
include: "#type-declaration"
44+
}
4245
{
4346
include: "#class-declaration"
4447
}
@@ -1099,6 +1102,45 @@ repository:
10991102
]
11001103
}
11011104
]
1105+
"type-declaration":
1106+
name: "meta.type.python"
1107+
begin: '''
1108+
(?x)
1109+
\\s*(type)\\s+
1110+
(?=
1111+
[[:alpha:]_][[:word:]]* \\s* [=\\[]
1112+
)
1113+
1114+
'''
1115+
end: "((?=[=]))"
1116+
beginCaptures:
1117+
"1":
1118+
name: "storage.type.type.python"
1119+
patterns: [
1120+
{
1121+
include: "#type-name"
1122+
}
1123+
{
1124+
include: "#type-parameters"
1125+
}
1126+
]
1127+
"type-name":
1128+
patterns: [
1129+
{
1130+
include: "#illegal-object-name"
1131+
}
1132+
{
1133+
include: "#builtin-possible-callables"
1134+
}
1135+
{
1136+
name: "entity.name.type.type.python"
1137+
match: '''
1138+
(?x)
1139+
\\b ([[:alpha:]_]\\w*) \\b
1140+
1141+
'''
1142+
}
1143+
]
11021144
"class-declaration":
11031145
patterns: [
11041146
{
@@ -1107,7 +1149,7 @@ repository:
11071149
(?x)
11081150
\\s*(class)\\s+
11091151
(?=
1110-
[[:alpha:]_]\\w* \\s* (:|\\()
1152+
[[:alpha:]_]\\w* \\s* (:|[\\[(])
11111153
)
11121154
11131155
'''
@@ -1125,6 +1167,9 @@ repository:
11251167
{
11261168
include: "#class-inheritance"
11271169
}
1170+
{
1171+
include: "#type-parameters"
1172+
}
11281173
]
11291174
}
11301175
]
@@ -1355,7 +1400,7 @@ repository:
13551400
\\s*
13561401
(?:\\b(async) \\s+)? \\b(def)\\s+
13571402
(?=
1358-
[[:alpha:]_][[:word:]]* \\s* \\(
1403+
[[:alpha:]_][[:word:]]* \\s* [(\\[]
13591404
)
13601405
13611406
'''
@@ -1372,6 +1417,9 @@ repository:
13721417
{
13731418
include: "#function-def-name"
13741419
}
1420+
{
1421+
include: "#type-parameters"
1422+
}
13751423
{
13761424
include: "#parameters"
13771425
}
@@ -1399,6 +1447,67 @@ repository:
13991447
'''
14001448
}
14011449
]
1450+
"type-parameters":
1451+
name: "meta.function.parameters.type.python"
1452+
begin: "(\\[)"
1453+
end: "(\\])"
1454+
beginCaptures:
1455+
"1":
1456+
name: "punctuation.definition.parameters.begin.python"
1457+
endCaptures:
1458+
"1":
1459+
name: "punctuation.definition.parameters.end.python"
1460+
patterns: [
1461+
{
1462+
name: "keyword.operator.unpacking.parameter.python"
1463+
match: "(\\*\\*|\\*)"
1464+
}
1465+
{
1466+
include: "#lambda-incomplete"
1467+
}
1468+
{
1469+
include: "#illegal-names"
1470+
}
1471+
{
1472+
include: "#illegal-object-name"
1473+
}
1474+
{
1475+
match: '''
1476+
(?x)
1477+
([[:alpha:]_]\\w*)
1478+
\\s* (?: (,) | (?=[\\]#\\n=]))
1479+
1480+
'''
1481+
captures:
1482+
"1":
1483+
name: "variable.parameter.function.language.python"
1484+
"2":
1485+
name: "punctuation.separator.parameters.python"
1486+
}
1487+
{
1488+
include: "#comments"
1489+
}
1490+
{
1491+
include: "#type-loose-default"
1492+
}
1493+
{
1494+
include: "#type-annotated-parameter"
1495+
}
1496+
]
1497+
"type-loose-default":
1498+
begin: "(=)"
1499+
end: "(,)|(?=\\])"
1500+
beginCaptures:
1501+
"1":
1502+
name: "keyword.operator.python"
1503+
endCaptures:
1504+
"1":
1505+
name: "punctuation.separator.parameters.python"
1506+
patterns: [
1507+
{
1508+
include: "#expression"
1509+
}
1510+
]
14021511
parameters:
14031512
name: "meta.function.parameters.python"
14041513
begin: "(\\()"
@@ -1482,6 +1591,31 @@ repository:
14821591
include: "#expression"
14831592
}
14841593
]
1594+
"type-annotated-parameter":
1595+
begin: '''
1596+
(?x)
1597+
\\b
1598+
([[:alpha:]_]\\w*) \\s* (:)
1599+
1600+
'''
1601+
end: "(,)|(?=\\])"
1602+
beginCaptures:
1603+
"1":
1604+
name: "variable.parameter.function.language.python"
1605+
"2":
1606+
name: "punctuation.separator.annotation.python"
1607+
endCaptures:
1608+
"1":
1609+
name: "punctuation.separator.parameters.python"
1610+
patterns: [
1611+
{
1612+
include: "#expression"
1613+
}
1614+
{
1615+
name: "keyword.operator.assignment.python"
1616+
match: "=(?!=)"
1617+
}
1618+
]
14851619
"annotated-parameter":
14861620
begin: '''
14871621
(?x)

‎grammars/MagicPython.tmLanguage‎

Lines changed: 221 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,10 @@
5959
<key>include</key>
6060
<string>#import</string>
6161
</dict>
62+
<dict>
63+
<key>include</key>
64+
<string>#type-declaration</string>
65+
</dict>
6266
<dict>
6367
<key>include</key>
6468
<string>#class-declaration</string>
@@ -1675,6 +1679,61 @@ E.g. "arr[idx](args)"
16751679
</dict>
16761680
</array>
16771681
</dict>
1682+
<key>type-declaration</key>
1683+
<dict>
1684+
<key>name</key>
1685+
<string>meta.type.python</string>
1686+
<key>begin</key>
1687+
<string>(?x)
1688+
\s*(type)\s+
1689+
(?=
1690+
[[:alpha:]_][[:word:]]* \s* [=\[]
1691+
)
1692+
</string>
1693+
<key>end</key>
1694+
<string>((?=[=]))</string>
1695+
<key>beginCaptures</key>
1696+
<dict>
1697+
<key>1</key>
1698+
<dict>
1699+
<key>name</key>
1700+
<string>storage.type.type.python</string>
1701+
</dict>
1702+
</dict>
1703+
<key>patterns</key>
1704+
<array>
1705+
<dict>
1706+
<key>include</key>
1707+
<string>#type-name</string>
1708+
</dict>
1709+
<dict>
1710+
<key>include</key>
1711+
<string>#type-parameters</string>
1712+
</dict>
1713+
</array>
1714+
</dict>
1715+
<key>type-name</key>
1716+
<dict>
1717+
<key>patterns</key>
1718+
<array>
1719+
<dict>
1720+
<key>include</key>
1721+
<string>#illegal-object-name</string>
1722+
</dict>
1723+
<dict>
1724+
<key>include</key>
1725+
<string>#builtin-possible-callables</string>
1726+
</dict>
1727+
<dict>
1728+
<key>name</key>
1729+
<string>entity.name.type.type.python</string>
1730+
<key>match</key>
1731+
<string>(?x)
1732+
\b ([[:alpha:]_]\w*) \b
1733+
</string>
1734+
</dict>
1735+
</array>
1736+
</dict>
16781737
<key>class-declaration</key>
16791738
<dict>
16801739
<key>patterns</key>
@@ -1686,7 +1745,7 @@ E.g. "arr[idx](args)"
16861745
<string>(?x)
16871746
\s*(class)\s+
16881747
(?=
1689-
[[:alpha:]_]\w* \s* (:|\()
1748+
[[:alpha:]_]\w* \s* (:|[\[(])
16901749
)
16911750
</string>
16921751
<key>end</key>
@@ -1717,6 +1776,10 @@ E.g. "arr[idx](args)"
17171776
<key>include</key>
17181777
<string>#class-inheritance</string>
17191778
</dict>
1779+
<dict>
1780+
<key>include</key>
1781+
<string>#type-parameters</string>
1782+
</dict>
17201783
</array>
17211784
</dict>
17221785
</array>
@@ -2103,7 +2166,7 @@ correctly identify the "in" as a control flow keyword.
21032166
\s*
21042167
(?:\b(async) \s+)? \b(def)\s+
21052168
(?=
2106-
[[:alpha:]_][[:word:]]* \s* \(
2169+
[[:alpha:]_][[:word:]]* \s* [(\[]
21072170
)
21082171
</string>
21092172
<key>end</key>
@@ -2135,6 +2198,10 @@ correctly identify the "in" as a control flow keyword.
21352198
<key>include</key>
21362199
<string>#function-def-name</string>
21372200
</dict>
2201+
<dict>
2202+
<key>include</key>
2203+
<string>#type-parameters</string>
2204+
</dict>
21382205
<dict>
21392206
<key>include</key>
21402207
<string>#parameters</string>
@@ -2171,6 +2238,114 @@ correctly identify the "in" as a control flow keyword.
21712238
</dict>
21722239
</array>
21732240
</dict>
2241+
<key>type-parameters</key>
2242+
<dict>
2243+
<key>name</key>
2244+
<string>meta.function.parameters.type.python</string>
2245+
<key>begin</key>
2246+
<string>(\[)</string>
2247+
<key>end</key>
2248+
<string>(\])</string>
2249+
<key>beginCaptures</key>
2250+
<dict>
2251+
<key>1</key>
2252+
<dict>
2253+
<key>name</key>
2254+
<string>punctuation.definition.parameters.begin.python</string>
2255+
</dict>
2256+
</dict>
2257+
<key>endCaptures</key>
2258+
<dict>
2259+
<key>1</key>
2260+
<dict>
2261+
<key>name</key>
2262+
<string>punctuation.definition.parameters.end.python</string>
2263+
</dict>
2264+
</dict>
2265+
<key>patterns</key>
2266+
<array>
2267+
<dict>
2268+
<key>name</key>
2269+
<string>keyword.operator.unpacking.parameter.python</string>
2270+
<key>match</key>
2271+
<string>(\*\*|\*)</string>
2272+
</dict>
2273+
<dict>
2274+
<key>include</key>
2275+
<string>#lambda-incomplete</string>
2276+
</dict>
2277+
<dict>
2278+
<key>include</key>
2279+
<string>#illegal-names</string>
2280+
</dict>
2281+
<dict>
2282+
<key>include</key>
2283+
<string>#illegal-object-name</string>
2284+
</dict>
2285+
<dict>
2286+
<key>match</key>
2287+
<string>(?x)
2288+
([[:alpha:]_]\w*)
2289+
\s* (?: (,) | (?=[\]#\n=]))
2290+
</string>
2291+
<key>captures</key>
2292+
<dict>
2293+
<key>1</key>
2294+
<dict>
2295+
<key>name</key>
2296+
<string>variable.parameter.function.language.python</string>
2297+
</dict>
2298+
<key>2</key>
2299+
<dict>
2300+
<key>name</key>
2301+
<string>punctuation.separator.parameters.python</string>
2302+
</dict>
2303+
</dict>
2304+
</dict>
2305+
<dict>
2306+
<key>include</key>
2307+
<string>#comments</string>
2308+
</dict>
2309+
<dict>
2310+
<key>include</key>
2311+
<string>#type-loose-default</string>
2312+
</dict>
2313+
<dict>
2314+
<key>include</key>
2315+
<string>#type-annotated-parameter</string>
2316+
</dict>
2317+
</array>
2318+
</dict>
2319+
<key>type-loose-default</key>
2320+
<dict>
2321+
<key>begin</key>
2322+
<string>(=)</string>
2323+
<key>end</key>
2324+
<string>(,)|(?=\])</string>
2325+
<key>beginCaptures</key>
2326+
<dict>
2327+
<key>1</key>
2328+
<dict>
2329+
<key>name</key>
2330+
<string>keyword.operator.python</string>
2331+
</dict>
2332+
</dict>
2333+
<key>endCaptures</key>
2334+
<dict>
2335+
<key>1</key>
2336+
<dict>
2337+
<key>name</key>
2338+
<string>punctuation.separator.parameters.python</string>
2339+
</dict>
2340+
</dict>
2341+
<key>patterns</key>
2342+
<array>
2343+
<dict>
2344+
<key>include</key>
2345+
<string>#expression</string>
2346+
</dict>
2347+
</array>
2348+
</dict>
21742349
<key>parameters</key>
21752350
<dict>
21762351
<key>name</key>
@@ -2319,6 +2494,50 @@ correctly identify the "in" as a control flow keyword.
23192494
</dict>
23202495
</array>
23212496
</dict>
2497+
<key>type-annotated-parameter</key>
2498+
<dict>
2499+
<key>begin</key>
2500+
<string>(?x)
2501+
\b
2502+
([[:alpha:]_]\w*) \s* (:)
2503+
</string>
2504+
<key>end</key>
2505+
<string>(,)|(?=\])</string>
2506+
<key>beginCaptures</key>
2507+
<dict>
2508+
<key>1</key>
2509+
<dict>
2510+
<key>name</key>
2511+
<string>variable.parameter.function.language.python</string>
2512+
</dict>
2513+
<key>2</key>
2514+
<dict>
2515+
<key>name</key>
2516+
<string>punctuation.separator.annotation.python</string>
2517+
</dict>
2518+
</dict>
2519+
<key>endCaptures</key>
2520+
<dict>
2521+
<key>1</key>
2522+
<dict>
2523+
<key>name</key>
2524+
<string>punctuation.separator.parameters.python</string>
2525+
</dict>
2526+
</dict>
2527+
<key>patterns</key>
2528+
<array>
2529+
<dict>
2530+
<key>include</key>
2531+
<string>#expression</string>
2532+
</dict>
2533+
<dict>
2534+
<key>name</key>
2535+
<string>keyword.operator.assignment.python</string>
2536+
<key>match</key>
2537+
<string>=(?!=)</string>
2538+
</dict>
2539+
</array>
2540+
</dict>
23222541
<key>annotated-parameter</key>
23232542
<dict>
23242543
<key>begin</key>

‎grammars/src/MagicPython.syntax.yaml‎

Lines changed: 83 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -142,6 +142,7 @@ repository:
142142
statement:
143143
patterns:
144144
- include: '#import'
145+
- include: '#type-declaration'
145146
- include: '#class-declaration'
146147
- include: '#function-declaration'
147148
- include: '#generator'
@@ -831,14 +832,39 @@ repository:
831832
match: \b(?<!\.)as\b
832833
- include: '#expression'
833834

835+
type-declaration:
836+
name: meta.type.python
837+
begin: |
838+
(?x)
839+
\s*(type)\s+
840+
(?=
841+
[[:alpha:]_][[:word:]]* \s* [=\[]
842+
)
843+
844+
end: ((?=[=]))
845+
beginCaptures:
846+
'1': {name: storage.type.type.python}
847+
patterns:
848+
- include: '#type-name'
849+
- include: '#type-parameters'
850+
851+
type-name:
852+
patterns:
853+
- include: '#illegal-object-name'
854+
- include: '#builtin-possible-callables'
855+
- name: entity.name.type.type.python
856+
match: |
857+
(?x)
858+
\b ([[:alpha:]_]\w*) \b
859+
834860
class-declaration:
835861
patterns:
836862
- name: meta.class.python
837863
begin: |
838864
(?x)
839865
\s*(class)\s+
840866
(?=
841-
[[:alpha:]_]\w* \s* (:|\()
867+
[[:alpha:]_]\w* \s* (:|[\[(])
842868
)
843869
end: (:)
844870
beginCaptures:
@@ -848,6 +874,7 @@ repository:
848874
patterns:
849875
- include: '#class-name'
850876
- include: '#class-inheritance'
877+
- include: '#type-parameters'
851878

852879
class-name:
853880
patterns:
@@ -992,7 +1019,7 @@ repository:
9921019
\s*
9931020
(?:\b(async) \s+)? \b(def)\s+
9941021
(?=
995-
[[:alpha:]_][[:word:]]* \s* \(
1022+
[[:alpha:]_][[:word:]]* \s* [(\[]
9961023
)
9971024
9981025
end: (:|(?=[#'"\n]))
@@ -1005,6 +1032,7 @@ repository:
10051032

10061033
patterns:
10071034
- include: '#function-def-name'
1035+
- include: '#type-parameters'
10081036
- include: '#parameters'
10091037
- include: '#line-continuation'
10101038
- include: '#return-annotation'
@@ -1018,6 +1046,43 @@ repository:
10181046
(?x)
10191047
\b ([[:alpha:]_]\w*) \b
10201048
1049+
type-parameters:
1050+
name: meta.function.parameters.type.python
1051+
begin: (\[)
1052+
end: (\])
1053+
beginCaptures:
1054+
'1': {name: punctuation.definition.parameters.begin.python}
1055+
endCaptures:
1056+
'1': {name: punctuation.definition.parameters.end.python}
1057+
1058+
patterns:
1059+
- name: keyword.operator.unpacking.parameter.python
1060+
match: (\*\*|\*)
1061+
- include: '#lambda-incomplete'
1062+
- include: '#illegal-names'
1063+
- include: '#illegal-object-name'
1064+
- match: |
1065+
(?x)
1066+
([[:alpha:]_]\w*)
1067+
\s* (?: (,) | (?=[\]#\n=]))
1068+
captures:
1069+
'1': {name: variable.parameter.function.language.python}
1070+
'2': {name: punctuation.separator.parameters.python}
1071+
1072+
- include: '#comments'
1073+
- include: '#type-loose-default'
1074+
- include: '#type-annotated-parameter'
1075+
1076+
type-loose-default:
1077+
begin: (=)
1078+
end: (,)|(?=\])
1079+
beginCaptures:
1080+
'1': {name: keyword.operator.python}
1081+
endCaptures:
1082+
'1': {name: punctuation.separator.parameters.python}
1083+
patterns:
1084+
- include: '#expression'
1085+
10211086
parameters:
10221087
name: meta.function.parameters.python
10231088
begin: (\()
@@ -1068,6 +1133,22 @@ repository:
10681133
patterns:
10691134
- include: '#expression'
10701135

1136+
type-annotated-parameter:
1137+
begin: |
1138+
(?x)
1139+
\b
1140+
([[:alpha:]_]\w*) \s* (:)
1141+
end: (,)|(?=\])
1142+
beginCaptures:
1143+
'1': {name: variable.parameter.function.language.python}
1144+
'2': {name: punctuation.separator.annotation.python}
1145+
endCaptures:
1146+
'1': {name: punctuation.separator.parameters.python}
1147+
patterns:
1148+
- include: '#expression'
1149+
- name: keyword.operator.assignment.python
1150+
match: =(?!=)
1151+
10711152
annotated-parameter:
10721153
begin: |
10731154
(?x)

0 commit comments

Comments
 (0)
Please sign in to comment.