highlights.scm

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