on master help sign in
Python highlight improvements: type, parameter etc
abef250Paul Scott
runtime/queries/python/highlights.scm+54−20
@@ -1,44 +1,78 @@
1−; Identifier naming conventions
2−
3−((identifier) @constant
4− (#match? @constant "^[A-Z_]*$"))
5−
6−((identifier) @constructor
7− (#match? @constructor "^[A-Z]"))
8−
9−; Types
10−
11−((identifier) @type
12− (#match?
13− @type
14− "^(bool|bytes|dict|float|frozenset|int|list|set|str|tuple)$"))
15−
16−(type (identifier)) @type
17−
18; Builtin functions
1; Builtin functions
19
2
20((call
3((call
21 function: (identifier) @function.builtin)
4 function: (identifier) @function.builtin)
22 (#match?
5 (#match?
23 @function.builtin
6 @function.builtin
24− "^(abs|all|any|ascii|bin|breakpoint|bytearray|callable|chr|classmethod|compile|complex|delattr|dir|divmod|enumerate|eval|exec|filter|format|getattr|globals|hasattr|hash|help|hex|id|input|isinstance|issubclass|iter|len|locals|map|max|memoryview|min|next|object|oct|open|ord|pow|print|property|range|repr|reversed|round|setattr|slice|sorted|staticmethod|sum|super|type|vars|zip|__import__)$"))
7+ "^(abs|all|any|ascii|bin|bool|breakpoint|bytearray|bytes|callable|chr|classmethod|compile|complex|delattr|dict|dir|divmod|enumerate|eval|exec|filter|float|format|frozenset|getattr|globals|hasattr|hash|help|hex|id|input|int|isinstance|issubclass|iter|len|list|locals|map|max|memoryview|min|next|object|oct|open|ord|pow|print|property|range|repr|reversed|round|set|setattr|slice|sorted|staticmethod|str|sum|super|tuple|type|vars|zip|__import__)$"))
25
8
26; Function calls
9; Function calls
27
10
28−(decorator) @function
11+(call
12+ function: (attribute attribute: (identifier) @constructor)
13+ (#match? @constructor "^[A-Z]"))
14+(call
15+ function: (identifier) @constructor
16+ (#match? @constructor "^[A-Z]"))
29
17
30(call
18(call
31 function: (attribute attribute: (identifier) @function.method))
19 function: (attribute attribute: (identifier) @function.method))
20+
32(call
21(call
33 function: (identifier) @function)
22 function: (identifier) @function)
34
23
35; Function definitions
24; Function definitions
36
25
26+(function_definition
27+ name: (identifier) @constructor
28+ (#match? @constructor "^(__new__|__init__)$"))
29+
37(function_definition
30(function_definition
38 name: (identifier) @function)
31 name: (identifier) @function)
39
32
33+; Decorators
34+
35+(decorator) @function
36+(decorator (identifier) @function)
37+
38+; Parameters
39+
40+((identifier) @variable.builtin
41+ (#match? @variable.builtin "^(self|cls)$"))
42+
40−(identifier) @variable
43+(parameters (identifier) @variable.parameter)
44+(parameters (typed_parameter (identifier) @variable.parameter))
45+
46+; Types
47+
48+((identifier) @type.builtin
49+ (#match?
50+ @type.builtin
51+ "^(bool|bytes|dict|float|frozenset|int|list|set|str|tuple)$"))
52+
53+; In type hints make everything types to catch non-conforming identifiers
54+; (e.g., datetime.datetime) and None
55+(type [(identifier) (none)] @type)
56+; Handle [] . and | nesting 4 levels deep
57+(type
58+ (_ [(identifier) (none)]? @type
59+ (_ [(identifier) (none)]? @type
60+ (_ [(identifier) (none)]? @type
61+ (_ [(identifier) (none)]? @type)))))
62+
63+(class_definition name: (identifier) @type)
64+(class_definition superclasses: (argument_list (identifier) @type))
65+
66+; Variables
67+
68+((identifier) @constant
69+ (#match? @constant "^[A-Z_]{2,}$"))
70+
71+((identifier) @type
72+ (#match? @type "^[A-Z]"))
73+
41(attribute attribute: (identifier) @variable.other.member)
74(attribute attribute: (identifier) @variable.other.member)
75+(identifier) @variable
42
76
43; Literals
77; Literals
44
78