Skip to content

Commit 2b521f0

Browse files
committed
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

+136-2
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)

0 commit comments

Comments
 (0)