highlights.scm

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