Debian Codegraph @

runtime/queries/python/highlights.scm

raw symbols SCIP
1 ; -------
2 ; Punctuation
3 ; -------
4
5 ["," "." ":" ";" (ellipsis)] @punctuation.delimiter
6 ["(" ")" "[" "]" "{" "}"] @punctuation.bracket
7 (interpolation
8 "{" @punctuation.special
9 "}" @punctuation.special)
10
11 ; -------
12 ; Operators
13 ; -------
14
15 [
16 "-"
17 "-="
18 "!="
19 "*"
20 "**"
21 "**="
22 "*="
23 "/"
24 "//"
25 "//="
26 "/="
27 "&"
28 "&="
29 "%"
30 "%="
31 "^"
32 "^="
33 "+"
34 "->"
35 "+="
36 "<"
37 "<<"
38 "<<="
39 "<="
40 "<>"
41 "="
42 ":="
43 "=="
44 ">"
45 ">="
46 ">>"
47 ">>="
48 "|"
49 "|="
50 "~"
51 "@="
52 ] @operator
53
54 ; -------
55 ; Variables
56 ; -------
57
58 (identifier) @variable
59
60 ; - Member
61 (attribute attribute: (identifier) @variable.other.member)
62
63 ; - Parameter
64 (parameters (identifier) @variable.parameter)
65 (parameters (typed_parameter (identifier) @variable.parameter))
66 (parameters (default_parameter name: (identifier) @variable.parameter))
67 (parameters (typed_default_parameter name: (identifier) @variable.parameter))
68 (parameters
69 (list_splat_pattern ; *args
70 (identifier) @variable.parameter))
71 (parameters
72 (dictionary_splat_pattern ; **kwargs
73 (identifier) @variable.parameter))
74
75 (lambda_parameters
76 (identifier) @variable.parameter)
77
78 (keyword_argument
79 name: (identifier) @variable.parameter)
80
81 ; - Builtin
82 ((identifier) @variable.builtin
83 (#any-of? @variable.builtin "self" "cls"))
84
85 ; -------
86 ; Keywords
87 ; -------
88
89 [
90 "async"
91 "class"
92 "exec"
93 "global"
94 "nonlocal"
95 "print"
96 "type"
97 ] @keyword
98
99 ; Operators
100 [
101 "and"
102 "or"
103 "not in"
104 "in" ; Has to be before loop keywords because "in" is overloaded
105 "not"
106 "del"
107 "is not"
108 "is"
109 ] @keyword.operator
110
111 ; Control
112 [
113 "as"
114 "assert"
115 "await"
116 "from"
117 "pass"
118
119 "with"
120 ] @keyword.control
121
122 ; Conditionals
123 [
124 "if"
125 "elif"
126 "else"
127 "match"
128 "case"
129 ] @keyword.control.conditional
130
131 ; Exceptions
132 [
133 "raise"
134 "try"
135 "except"
136 "finally"
137 ] @keyword.control.exception
138 (raise_statement "from" @keyword.control.exception)
139
140 ; Functions
141 [
142 "def"
143 "lambda"
144 ] @keyword.function
145
146 ; Import
147 "import" @keyword.control.import
148
149 ; Loops
150 [
151 "while"
152 "for"
153 "break"
154 "continue"
155 ] @keyword.control.repeat
156
157 (for_statement "in" @keyword.control.repeat)
158 (for_in_clause "in" @keyword.control.repeat)
159
160 ; Return
161 [
162 "return"
163 "yield"
164 ] @keyword.control.return
165 (yield "from" @keyword.control.return)
166
167 ; -------
168 ; Imports
169 ; -------
170
171 (dotted_name
172 (identifier)* @namespace)
173
174 (aliased_import
175 alias: (identifier) @namespace)
176
177 ; - Builtins
178 (none) @constant.builtin ; Has to be before types
179
180 ; -------
181 ; Types
182 ; -------
183
184 ((identifier) @type
185 (#match? @type "^[A-Z]")) ; Has to be before constructor due to this being a more general match
186
187 ; In type hints make everything types to catch non-conforming identifiers
188 ; (e.g., datetime.datetime) and None
189 (type [(identifier) (none)] @type)
190 ; Handle [] . and | nesting 4 levels deep
191 (type
192 (_ [(identifier) (none)]? @type
193 (_ [(identifier) (none)]? @type
194 (_ [(identifier) (none)]? @type
195 (_ [(identifier) (none)]? @type)))))
196
197 ; Classes
198 (class_definition name: (identifier) @type)
199 (class_definition superclasses: (argument_list (identifier) @type))
200
201 ; -------
202 ; Functions
203 ; -------
204
205 (function_definition
206 name: (identifier) @function)
207
208 (call
209 function: (identifier) @function)
210
211 ; Decorators
212 (decorator) @function
213 (decorator (identifier) @function)
214 (decorator (attribute attribute: (identifier) @function))
215 (decorator (call
216 function: (attribute attribute: (identifier) @function)))
217
218 ; Methods
219 (call
220 function: (attribute attribute: (identifier) @function.method))
221
222 ; Builtin functions
223 ((call
224 function: (identifier) @function.builtin)
225 (#any-of?
226 @function.builtin
227 "abs" "all" "any" "ascii" "bin" "breakpoint" "bytearray" "callable" "chr"
228 "classmethod" "compile" "complex" "delattr" "dir" "divmod" "enumerate"
229 "eval" "exec" "filter" "format" "getattr" "globals" "hasattr" "hash" "help"
230 "hex" "id" "input" "isinstance" "issubclass" "iter" "len" "locals" "map"
231 "max" "memoryview" "min" "next" "object" "oct" "open" "ord" "pow" "print"
232 "property" "range" "repr" "reversed" "round" "setattr" "slice" "sorted"
233 "staticmethod" "sum" "super" "type" "vars" "zip" "__import__"))
234
235 ; Constructors
236 (call
237 function: (attribute attribute: (identifier) @constructor)
238 (#any-of?
239 @constructor
240 "__new__" "__init__"))
241
242 ((call
243 function: (identifier) @constructor)
244 (#any-of?
245 @constructor
246 "__new__" "__init__"))
247
248 (function_definition
249 name: (identifier) @constructor
250 (#any-of? @constructor "__new__" "__init__"))
251
252 (call
253 function: (attribute attribute: (identifier) @constructor)
254 (#match? @constructor "^[A-Z]"))
255 (call
256 function: (identifier) @constructor
257 (#match? @constructor "^[A-Z]"))
258
259 ; Builtin types
260 ((identifier) @type.builtin ; Has to be after functions due to broad matching
261 (#any-of?
262 @type.builtin
263 "bool" "bytes" "dict" "float" "frozenset" "int" "list" "set" "str" "tuple"))
264
265 ; Builtin error types
266 ((identifier) @type.builtin ; Has to be after constructors due to broad matching of constructor
267 (#any-of? @type.builtin
268 "BaseException" "Exception" "ArithmeticError" "BufferError" "LookupError"
269 "AssertionError" "AttributeError" "EOFError" "FloatingPointError" "GeneratorExit"
270 "ImportError" "ModuleNotFoundError" "IndexError" "KeyError" "KeyboardInterrupt"
271 "MemoryError" "NameError" "NotImplementedError" "OSError" "OverflowError"
272 "RecursionError" "ReferenceError" "RuntimeError" "StopIteration" "StopAsyncIteration"
273 "SyntaxError" "IndentationError" "TabError" "SystemError" "SystemExit" "TypeError"
274 "UnboundLocalError" "UnicodeError" "UnicodeEncodeError" "UnicodeDecodeError"
275 "UnicodeTranslateError" "ValueError" "ZeroDivisionError" "EnvironmentError"
276 "IOError" "WindowsError" "BlockingIOError" "ChildProcessError" "ConnectionError"
277 "BrokenPipeError" "ConnectionAbortedError" "ConnectionRefusedError"
278 "ConnectionResetError" "FileExistsError" "FileNotFoundError" "InterruptedError"
279 "IsADirectoryError" "NotADirectoryError" "PermissionError" "ProcessLookupError"
280 "TimeoutError" "Warning" "UserWarning" "DeprecationWarning" "PendingDeprecationWarning"
281 "SyntaxWarning" "RuntimeWarning" "FutureWarning" "ImportWarning" "UnicodeWarning"
282 "BytesWarning" "ResourceWarning"))
283
284 ; -------
285 ; Constants
286 ; -------
287
288 ((identifier) @constant
289 (#match? @constant "^_*[A-Z][A-Z\\d_]*$"))
290
291 (escape_sequence) @constant.character.escape
292 (escape_interpolation) @constant.character.escape ; the `{{` and `}}` literals in f-strings
293
294 [
295 (true)
296 (false)
297 ] @constant.builtin.boolean
298
299
300 ; - Numbers
301 (integer) @constant.numeric.integer
302 (float) @constant.numeric.float
303
304 ; -------
305 ; Other literals
306 ; -------
307
308 (comment) @comment
309 (string) @string
310
311 ; -------
312 ; Type aliases / type variables (after the generic identifier+type rules so they win)
313 ; -------
314
315 ; PEP 695: `type Alias = ...`
316 (type_alias_statement
317 . (type (identifier) @type.definition))
318
319 ; `X = TypeVar("X")` / `X = NewType("X", int)`
320 ((assignment
321 left: (identifier) @type.definition
322 right: (call
323 function: (identifier) @_func))
324 (#any-of? @_func "TypeVar" "NewType" "ParamSpec" "TypeVarTuple"))
325
326 ; `X: TypeAlias = ...`
327 ((assignment
328 left: (identifier) @type.definition
329 type: (type (identifier) @_ann))
330 (#eq? @_ann "TypeAlias"))