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; 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; Literals
 96
 97[
 98  (true)
 99  (false)
100] @boolean
101
102[
103  (none)
104  (ellipsis)
105] @constant.builtin
106
107[
108  (integer)
109  (float)
110] @number
111
112; Self references
113
114[
115  (parameters (identifier) @variable.special)
116  (attribute (identifier) @variable.special)
117  (#any-of? @variable.special "self" "cls")
118]
119
120[
121  "."
122  ","
123  ":"
124] @punctuation.delimiter
125
126[
127  "("
128  ")"
129  "["
130  "]"
131  "{"
132  "}"
133] @punctuation.bracket
134
135(interpolation
136  "{" @punctuation.special
137  "}" @punctuation.special) @embedded
138
139; Docstrings.
140(module
141  .(expression_statement (string) @string.doc)+)
142
143(class_definition
144  body: (block .(expression_statement (string) @string.doc)+))
145
146(function_definition
147  "async"?
148  "def"
149  name: (_)
150  (parameters)?
151  body: (block .(expression_statement (string) @string.doc)+))
152
153(class_definition
154  body: (block
155    . (comment) @comment*
156    . (expression_statement (string) @string.doc)+))
157
158(module
159  . (comment) @comment*
160  . (expression_statement (string) @string.doc)+)
161
162(module
163  [
164    (expression_statement (assignment))
165    (type_alias_statement)
166  ]
167  . (expression_statement (string) @string.doc)+)
168
169(class_definition
170  body: (block
171    (expression_statement (assignment))
172    . (expression_statement (string) @string.doc)+))
173
174(class_definition
175  body: (block
176    (function_definition
177      name: (identifier) @function.method.constructor
178      (#eq? @function.method.constructor "__init__")
179      body: (block
180        (expression_statement (assignment))
181        . (expression_statement (string) @string.doc)+))))
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  "<="
207  "<>"
208  "="
209  ":="
210  "=="
211  ">"
212  ">="
213  ">>"
214  "|"
215  "~"
216] @operator
217
218[
219  "and"
220  "in"
221  "is"
222  "not"
223  "or"
224  "is not"
225  "not in"
226] @keyword.operator
227
228[
229  "as"
230  "assert"
231  "async"
232  "await"
233  "break"
234  "class"
235  "continue"
236  "def"
237  "del"
238  "elif"
239  "else"
240  "except"
241  "except*"
242  "exec"
243  "finally"
244  "for"
245  "from"
246  "global"
247  "if"
248  "import"
249  "lambda"
250  "nonlocal"
251  "pass"
252  "print"
253  "raise"
254  "return"
255  "try"
256  "while"
257  "with"
258  "yield"
259  "match"
260  "case"
261] @keyword
262
263; Definition keywords def, class, async def, lambda
264[
265  "async"
266  "def"
267  "class"
268  "lambda"
269] @keyword.definition
270
271(decorator (identifier) @attribute.builtin
272  (#any-of? @attribute.builtin "classmethod" "staticmethod" "property"))
273
274; Builtin types as identifiers
275[
276  (call
277    function: (identifier) @type.builtin)
278  (type (identifier) @type.builtin)
279  (generic_type (identifier) @type.builtin)
280  ; also check if type binary operator left identifier for union types
281  (type
282    (binary_operator
283      left: (identifier) @type.builtin))
284  (#any-of? @type.builtin "bool" "bytearray" "bytes" "complex" "dict" "float" "frozenset" "int" "list" "memoryview" "object" "range" "set" "slice" "str" "tuple")
285]