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