diff --git a/grammars/MagicPython.cson b/grammars/MagicPython.cson
index d837d6a..d1f43df 100644
--- a/grammars/MagicPython.cson
+++ b/grammars/MagicPython.cson
@@ -39,6 +39,9 @@ repository:
{
include: "#import"
}
+ {
+ include: "#type-declaration"
+ }
{
include: "#class-declaration"
}
@@ -1099,6 +1102,45 @@ repository:
]
}
]
+ "type-declaration":
+ name: "meta.type.python"
+ begin: '''
+ (?x)
+ \\s*(type)\\s+
+ (?=
+ [[:alpha:]_][[:word:]]* \\s* [=\\[]
+ )
+
+ '''
+ end: "((?=[=]))"
+ beginCaptures:
+ "1":
+ name: "storage.type.type.python"
+ patterns: [
+ {
+ include: "#type-name"
+ }
+ {
+ include: "#type-parameters"
+ }
+ ]
+ "type-name":
+ patterns: [
+ {
+ include: "#illegal-object-name"
+ }
+ {
+ include: "#builtin-possible-callables"
+ }
+ {
+ name: "entity.name.type.type.python"
+ match: '''
+ (?x)
+ \\b ([[:alpha:]_]\\w*) \\b
+
+ '''
+ }
+ ]
"class-declaration":
patterns: [
{
@@ -1107,7 +1149,7 @@ repository:
(?x)
\\s*(class)\\s+
(?=
- [[:alpha:]_]\\w* \\s* (:|\\()
+ [[:alpha:]_]\\w* \\s* (:|[\\[(])
)
'''
@@ -1125,6 +1167,9 @@ repository:
{
include: "#class-inheritance"
}
+ {
+ include: "#type-parameters"
+ }
]
}
]
@@ -1355,7 +1400,7 @@ repository:
\\s*
(?:\\b(async) \\s+)? \\b(def)\\s+
(?=
- [[:alpha:]_][[:word:]]* \\s* \\(
+ [[:alpha:]_][[:word:]]* \\s* [(\\[]
)
'''
@@ -1372,6 +1417,9 @@ repository:
{
include: "#function-def-name"
}
+ {
+ include: "#type-parameters"
+ }
{
include: "#parameters"
}
@@ -1399,6 +1447,67 @@ repository:
'''
}
]
+ "type-parameters":
+ name: "meta.function.parameters.type.python"
+ begin: "(\\[)"
+ end: "(\\])"
+ beginCaptures:
+ "1":
+ name: "punctuation.definition.parameters.begin.python"
+ endCaptures:
+ "1":
+ name: "punctuation.definition.parameters.end.python"
+ patterns: [
+ {
+ name: "keyword.operator.unpacking.parameter.python"
+ match: "(\\*\\*|\\*)"
+ }
+ {
+ include: "#lambda-incomplete"
+ }
+ {
+ include: "#illegal-names"
+ }
+ {
+ include: "#illegal-object-name"
+ }
+ {
+ match: '''
+ (?x)
+ ([[:alpha:]_]\\w*)
+ \\s* (?: (,) | (?=[\\]#\\n=]))
+
+ '''
+ captures:
+ "1":
+ name: "variable.parameter.function.language.python"
+ "2":
+ name: "punctuation.separator.parameters.python"
+ }
+ {
+ include: "#comments"
+ }
+ {
+ include: "#type-loose-default"
+ }
+ {
+ include: "#type-annotated-parameter"
+ }
+ ]
+ "type-loose-default":
+ begin: "(=)"
+ end: "(,)|(?=\\])"
+ beginCaptures:
+ "1":
+ name: "keyword.operator.python"
+ endCaptures:
+ "1":
+ name: "punctuation.separator.parameters.python"
+ patterns: [
+ {
+ include: "#expression"
+ }
+ ]
parameters:
name: "meta.function.parameters.python"
begin: "(\\()"
@@ -1482,6 +1591,31 @@ repository:
include: "#expression"
}
]
+ "type-annotated-parameter":
+ begin: '''
+ (?x)
+ \\b
+ ([[:alpha:]_]\\w*) \\s* (:)
+
+ '''
+ end: "(,)|(?=\\])"
+ beginCaptures:
+ "1":
+ name: "variable.parameter.function.language.python"
+ "2":
+ name: "punctuation.separator.annotation.python"
+ endCaptures:
+ "1":
+ name: "punctuation.separator.parameters.python"
+ patterns: [
+ {
+ include: "#expression"
+ }
+ {
+ name: "keyword.operator.assignment.python"
+ match: "=(?!=)"
+ }
+ ]
"annotated-parameter":
begin: '''
(?x)
diff --git a/grammars/MagicPython.tmLanguage b/grammars/MagicPython.tmLanguage
index a1c3f8e..ab1bb16 100644
--- a/grammars/MagicPython.tmLanguage
+++ b/grammars/MagicPython.tmLanguage
@@ -59,6 +59,10 @@
include
#import
+
+ include
+ #type-declaration
+
include
#class-declaration
@@ -1675,6 +1679,61 @@ E.g. "arr[idx](args)"
+ type-declaration
+
+ name
+ meta.type.python
+ begin
+ (?x)
+ \s*(type)\s+
+ (?=
+ [[:alpha:]_][[:word:]]* \s* [=\[]
+ )
+
+ end
+ ((?=[=]))
+ beginCaptures
+
+ 1
+
+ name
+ storage.type.type.python
+
+
+ patterns
+
+
+ include
+ #type-name
+
+
+ include
+ #type-parameters
+
+
+
+ type-name
+
+ patterns
+
+
+ include
+ #illegal-object-name
+
+
+ include
+ #builtin-possible-callables
+
+
+ name
+ entity.name.type.type.python
+ match
+ (?x)
+ \b ([[:alpha:]_]\w*) \b
+
+
+
+
class-declaration
patterns
@@ -1686,7 +1745,7 @@ E.g. "arr[idx](args)"
(?x)
\s*(class)\s+
(?=
- [[:alpha:]_]\w* \s* (:|\()
+ [[:alpha:]_]\w* \s* (:|[\[(])
)
end
@@ -1717,6 +1776,10 @@ E.g. "arr[idx](args)"
include
#class-inheritance
+
+ include
+ #type-parameters
+
@@ -2103,7 +2166,7 @@ correctly identify the "in" as a control flow keyword.
\s*
(?:\b(async) \s+)? \b(def)\s+
(?=
- [[:alpha:]_][[:word:]]* \s* \(
+ [[:alpha:]_][[:word:]]* \s* [(\[]
)
end
@@ -2135,6 +2198,10 @@ correctly identify the "in" as a control flow keyword.
include
#function-def-name
+
+ include
+ #type-parameters
+
include
#parameters
@@ -2171,6 +2238,114 @@ correctly identify the "in" as a control flow keyword.
+ type-parameters
+
+ name
+ meta.function.parameters.type.python
+ begin
+ (\[)
+ end
+ (\])
+ beginCaptures
+
+ 1
+
+ name
+ punctuation.definition.parameters.begin.python
+
+
+ endCaptures
+
+ 1
+
+ name
+ punctuation.definition.parameters.end.python
+
+
+ patterns
+
+
+ name
+ keyword.operator.unpacking.parameter.python
+ match
+ (\*\*|\*)
+
+
+ include
+ #lambda-incomplete
+
+
+ include
+ #illegal-names
+
+
+ include
+ #illegal-object-name
+
+
+ match
+ (?x)
+ ([[:alpha:]_]\w*)
+ \s* (?: (,) | (?=[\]#\n=]))
+
+ captures
+
+ 1
+
+ name
+ variable.parameter.function.language.python
+
+ 2
+
+ name
+ punctuation.separator.parameters.python
+
+
+
+
+ include
+ #comments
+
+
+ include
+ #type-loose-default
+
+
+ include
+ #type-annotated-parameter
+
+
+
+ type-loose-default
+
+ begin
+ (=)
+ end
+ (,)|(?=\])
+ beginCaptures
+
+ 1
+
+ name
+ keyword.operator.python
+
+
+ endCaptures
+
+ 1
+
+ name
+ punctuation.separator.parameters.python
+
+
+ patterns
+
+
+ include
+ #expression
+
+
+
parameters
name
@@ -2319,6 +2494,50 @@ correctly identify the "in" as a control flow keyword.
+ type-annotated-parameter
+
+ begin
+ (?x)
+ \b
+ ([[:alpha:]_]\w*) \s* (:)
+
+ end
+ (,)|(?=\])
+ beginCaptures
+
+ 1
+
+ name
+ variable.parameter.function.language.python
+
+ 2
+
+ name
+ punctuation.separator.annotation.python
+
+
+ endCaptures
+
+ 1
+
+ name
+ punctuation.separator.parameters.python
+
+
+ patterns
+
+
+ include
+ #expression
+
+
+ name
+ keyword.operator.assignment.python
+ match
+ =(?!=)
+
+
+
annotated-parameter
begin
diff --git a/grammars/src/MagicPython.syntax.yaml b/grammars/src/MagicPython.syntax.yaml
index ebfccda..f6e7ab8 100644
--- a/grammars/src/MagicPython.syntax.yaml
+++ b/grammars/src/MagicPython.syntax.yaml
@@ -142,6 +142,7 @@ repository:
statement:
patterns:
- include: '#import'
+ - include: '#type-declaration'
- include: '#class-declaration'
- include: '#function-declaration'
- include: '#generator'
@@ -831,6 +832,31 @@ repository:
match: \b(?