1; Identifier naming conventions; these "soft conventions" should stay at the top of the file as they're often overridden
2(identifier) @variable
3(attribute attribute: (identifier) @property)
4
5; CamelCase for classes
6((identifier) @type.class
7 (#match? @type.class "^_*[A-Z][A-Za-z0-9_]*$"))
8
9; ALL_CAPS for constants:
10((identifier) @constant
11 (#match? @constant "^_*[A-Z][A-Z0-9_]*$"))
12
13(type (identifier) @type)
14(generic_type (identifier) @type)
15(comment) @comment
16(string) @string
17(escape_sequence) @string.escape
18
19; Type alias
20(type_alias_statement "type" @keyword)
21
22; TypeVar with constraints in type parameters
23(type
24 (tuple (identifier) @type)
25)
26
27; Forward references
28(type
29 (string) @type
30)
31
32
33; Function calls
34
35(call
36 function: (attribute attribute: (identifier) @function.method.call))
37(call
38 function: (identifier) @function.call)
39
40(decorator "@" @punctuation.special)
41(decorator
42 "@" @punctuation.special
43 [
44 (identifier) @function.decorator
45 (attribute attribute: (identifier) @function.decorator)
46 (call function: (identifier) @function.decorator.call)
47 (call (attribute attribute: (identifier) @function.decorator.call))
48 ])
49
50; Function and class definitions
51
52(function_definition
53 name: (identifier) @function.definition)
54
55; Function arguments
56(function_definition
57 parameters: (parameters
58 [
59 (identifier) @variable.parameter; Simple parameters
60 (typed_parameter
61 (identifier) @variable.parameter) ; Typed parameters
62 (default_parameter
63 name: (identifier) @variable.parameter) ; Default parameters
64 (typed_default_parameter
65 name: (identifier) @variable.parameter) ; Typed default parameters
66 ]))
67
68; Keyword arguments
69(call
70 arguments: (argument_list
71 (keyword_argument
72 name: (identifier) @function.kwargs)))
73
74; Class definitions and calling: needs to come after the regex matching above
75
76(class_definition
77 name: (identifier) @type.class.definition)
78
79(class_definition
80 superclasses: (argument_list
81 (identifier) @type.class.inheritance))
82
83(call
84 function: (identifier) @type.class.call
85 (#match? @type.class.call "^_*[A-Z][A-Za-z0-9_]*$"))
86
87; Builtins
88
89((call
90 function: (identifier) @function.builtin)
91 (#any-of?
92 @function.builtin
93 "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__"))
94
95((identifier) @type.builtin
96 (#any-of? @type.builtin "int" "float" "complex" "bool" "list" "tuple" "range" "str" "bytes" "bytearray" "memoryview" "set" "frozenset" "dict"))
97
98; Literals
99
100[
101 (true)
102 (false)
103] @boolean
104
105[
106 (none)
107 (ellipsis)
108] @constant.builtin
109
110[
111 (integer)
112 (float)
113] @number
114
115; Self references
116
117[
118 (parameters (identifier) @variable.special)
119 (attribute (identifier) @variable.special)
120 (#any-of? @variable.special "self" "cls")
121]
122
123[
124 "."
125 ","
126 ":"
127] @punctuation.delimiter
128
129[
130 "("
131 ")"
132 "["
133 "]"
134 "{"
135 "}"
136] @punctuation.bracket
137
138(interpolation
139 "{" @punctuation.special
140 "}" @punctuation.special) @embedded
141
142; Docstrings.
143(module
144 .(expression_statement (string) @string.doc)+)
145
146(class_definition
147 body: (block .(expression_statement (string) @string.doc)+))
148
149(function_definition
150 "async"?
151 "def"
152 name: (_)
153 (parameters)?
154 body: (block .(expression_statement (string) @string.doc)+))
155
156(class_definition
157 body: (block
158 . (comment) @comment*
159 . (expression_statement (string) @string.doc)+))
160
161(module
162 . (comment) @comment*
163 . (expression_statement (string) @string.doc)+)
164
165(module
166 [
167 (expression_statement (assignment))
168 (type_alias_statement)
169 ]
170 . (expression_statement (string) @string.doc)+)
171
172(class_definition
173 body: (block
174 (expression_statement (assignment))
175 . (expression_statement (string) @string.doc)+))
176
177(class_definition
178 body: (block
179 (function_definition
180 name: (identifier) @function.method.constructor
181 (#eq? @function.method.constructor "__init__")
182 body: (block
183 (expression_statement (assignment))
184 . (expression_statement (string) @string.doc)+))))
185
186
187[
188 "-"
189 "-="
190 "!="
191 "*"
192 "**"
193 "**="
194 "*="
195 "/"
196 "//"
197 "//="
198 "/="
199 "&"
200 "%"
201 "%="
202 "@"
203 "^"
204 "+"
205 "->"
206 "+="
207 "<"
208 "<<"
209 "<="
210 "<>"
211 "="
212 ":="
213 "=="
214 ">"
215 ">="
216 ">>"
217 "|"
218 "~"
219] @operator
220
221[
222 "and"
223 "in"
224 "is"
225 "not"
226 "or"
227 "is not"
228 "not in"
229] @keyword.operator
230
231[
232 "as"
233 "assert"
234 "async"
235 "await"
236 "break"
237 "class"
238 "continue"
239 "def"
240 "del"
241 "elif"
242 "else"
243 "except"
244 "except*"
245 "exec"
246 "finally"
247 "for"
248 "from"
249 "global"
250 "if"
251 "import"
252 "lambda"
253 "nonlocal"
254 "pass"
255 "print"
256 "raise"
257 "return"
258 "try"
259 "while"
260 "with"
261 "yield"
262 "match"
263 "case"
264] @keyword
265
266; Definition keywords def, class, async def, lambda
267[
268 "async"
269 "def"
270 "class"
271 "lambda"
272] @keyword.definition
273
274((identifier) @attribute.builtin
275 (#any-of? @attribute.builtin "classmethod" "staticmethod" "property"))
276
277; Builtin types as identifiers
278[
279 (call
280 function: (identifier) @type.builtin)
281 (call
282 arguments: (argument_list
283 (identifier) @type.builtin))
284 (call
285 arguments: (argument_list
286 (keyword_argument
287 value: (identifier) @type.builtin)))
288 (type (identifier) @type.builtin)
289 ; also check if type binary operator left identifier for union types
290 (type
291 (binary_operator
292 left: (identifier) @type.builtin))
293 (#any-of? @type.builtin "bool" "bytearray" "bytes" "complex" "dict" "float" "frozenset" "int" "list" "memoryview" "object" "range" "set" "slice" "str" "tuple")
294] @type.builtin