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
 40  "@" @punctuation.special
 41  [
 42    (identifier) @function.decorator
 43    (attribute attribute: (identifier) @function.decorator)
 44    (call function: (identifier) @function.decorator.call)
 45    (call (attribute attribute: (identifier) @function.decorator.call))
 46  ])
 47
 48; Function and class definitions
 49
 50(function_definition
 51  name: (identifier) @function.definition)
 52
 53; Class definitions and calling: needs to come after the regex matching above
 54
 55(class_definition
 56  name: (identifier) @type.class.definition)
 57
 58(call
 59  function: (identifier) @type.class.call
 60  (#match? @type.class.call "^_*[A-Z][A-Za-z0-9_]*$"))
 61
 62; Builtins
 63
 64((call
 65  function: (identifier) @function.builtin)
 66 (#match?
 67   @function.builtin
 68   "^(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__)$"))
 69
 70((identifier) @type.builtin
 71    (#any-of? @type.builtin "int" "float" "complex" "bool" "list" "tuple" "range" "str" "bytes" "bytearray" "memoryview" "set" "frozenset" "dict"))
 72    
 73; Literals
 74
 75[
 76  (none)
 77  (true)
 78  (false)
 79  (ellipsis)
 80] @constant.builtin
 81
 82[
 83  (integer)
 84  (float)
 85] @number
 86
 87; Self references
 88
 89[
 90  (parameters (identifier) @variable.special)
 91  (attribute (identifier) @variable.special)
 92  (#match? @variable.special "^self|cls$")
 93]
 94
 95[
 96  "("
 97  ")"
 98  "["
 99  "]"
100  "{"
101  "}"
102] @punctuation.bracket
103
104(interpolation
105  "{" @punctuation.special
106  "}" @punctuation.special) @embedded
107
108; Docstrings.
109(function_definition
110  "async"?
111  "def"
112  name: (_)
113  (parameters)?
114  body: (block . (expression_statement (string) @string.doc)))
115
116(class_definition
117  body: (block
118    . (comment) @comment*
119    . (expression_statement (string) @string.doc)))
120
121(module
122  . (comment) @comment*
123  . (expression_statement (string) @string.doc))
124
125(module
126  [
127    (expression_statement (assignment))
128    (type_alias_statement)
129  ]
130  . (expression_statement (string) @string.doc))
131
132(class_definition
133  body: (block
134    (expression_statement (assignment))
135    . (expression_statement (string) @string.doc)))
136
137(class_definition
138  body: (block
139    (function_definition
140      name: (identifier) @function.method.constructor
141      (#eq? @function.method.constructor "__init__")
142      body: (block
143        (expression_statement (assignment))
144        . (expression_statement (string) @string.doc)))))
145
146
147[
148  "-"
149  "-="
150  "!="
151  "*"
152  "**"
153  "**="
154  "*="
155  "/"
156  "//"
157  "//="
158  "/="
159  "&"
160  "%"
161  "%="
162  "^"
163  "+"
164  "->"
165  "+="
166  "<"
167  "<<"
168  "<="
169  "<>"
170  "="
171  ":="
172  "=="
173  ">"
174  ">="
175  ">>"
176  "|"
177  "~"
178] @operator
179
180[
181  "and"
182  "in"
183  "is"
184  "not"
185  "or"
186  "is not"
187  "not in"
188] @keyword.operator
189
190[
191  "as"
192  "assert"
193  "async"
194  "await"
195  "break"
196  "class"
197  "continue"
198  "def"
199  "del"
200  "elif"
201  "else"
202  "except"
203  "except*"
204  "exec"
205  "finally"
206  "for"
207  "from"
208  "global"
209  "if"
210  "import"
211  "lambda"
212  "nonlocal"
213  "pass"
214  "print"
215  "raise"
216  "return"
217  "try"
218  "while"
219  "with"
220  "yield"
221  "match"
222  "case"
223] @keyword