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