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