nvim-treesitter/queries/python/highlights.scm

363 lines
8.1 KiB
Scheme
Raw Permalink Normal View History

;; From tree-sitter-python licensed under MIT License
; Copyright (c) 2016 Max Brunsfeld
; Variables
(identifier) @variable
2021-11-01 22:35:31 +01:00
; Reset highlighting in f-string interpolations
(interpolation) @none
;; Identifier naming conventions
((identifier) @type
(#lua-match? @type "^[A-Z].*[a-z]"))
((identifier) @constant
(#lua-match? @constant "^[A-Z][A-Z_0-9]*$"))
((identifier) @constant.builtin
(#lua-match? @constant.builtin "^__[a-zA-Z0-9_]*__$"))
((identifier) @constant.builtin
2021-05-15 12:29:55 +02:00
(#any-of? @constant.builtin
;; https://docs.python.org/3/library/constants.html
"NotImplemented"
"Ellipsis"
"quit"
"exit"
"copyright"
"credits"
"license"))
"_" @constant.builtin ; match wildcard
((attribute
attribute: (identifier) @field)
(#lua-match? @field "^[%l_].*$"))
2022-11-21 15:06:35 +02:00
((assignment
left: (identifier) @type.definition
(type (identifier) @_annotation))
(#eq? @_annotation "TypeAlias"))
((assignment
left: (identifier) @type.definition
right: (call
function: (identifier) @_func))
(#any-of? @_func "TypeVar" "NewType"))
; Function calls
(call
function: (identifier) @function.call)
(call
function: (attribute
attribute: (identifier) @method.call))
((call
function: (identifier) @constructor)
2023-04-21 04:06:20 -04:00
(#lua-match? @constructor "^%u"))
((call
function: (attribute
attribute: (identifier) @constructor))
2023-04-21 04:06:20 -04:00
(#lua-match? @constructor "^%u"))
2022-11-21 15:06:35 +02:00
;; Decorators
((decorator "@" @attribute)
(#set! "priority" 101))
(decorator
(identifier) @attribute)
(decorator
(attribute
attribute: (identifier) @attribute))
(decorator
(call (identifier) @attribute))
(decorator
(call (attribute
attribute: (identifier) @attribute)))
((decorator
(identifier) @attribute.builtin)
(#any-of? @attribute.builtin "classmethod" "property"))
;; Builtin functions
((call
function: (identifier) @function.builtin)
(#any-of? @function.builtin
2021-05-15 12:29:55 +02:00
"abs" "all" "any" "ascii" "bin" "bool" "breakpoint" "bytearray" "bytes" "callable" "chr" "classmethod"
"compile" "complex" "delattr" "dict" "dir" "divmod" "enumerate" "eval" "exec" "filter" "float" "format"
2021-05-15 12:29:55 +02:00
"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__"))
;; Function definitions
(function_definition
name: (identifier) @function)
(type (identifier) @type)
(type
(subscript
(identifier) @type)) ; type subscript: Tuple[int]
((call
2020-09-07 18:14:54 +02:00
function: (identifier) @_isinstance
arguments: (argument_list
(_)
(identifier) @type))
2020-09-07 18:14:54 +02:00
(#eq? @_isinstance "isinstance"))
2020-11-03 18:50:26 +01:00
;; Normal parameters
(parameters
(identifier) @parameter)
2020-11-03 18:50:26 +01:00
;; Lambda parameters
(lambda_parameters
(identifier) @parameter)
(lambda_parameters
2020-11-03 18:50:26 +01:00
(tuple_pattern
(identifier) @parameter))
; Default parameters
(keyword_argument
name: (identifier) @parameter)
; Naming parameters on call-site
(default_parameter
name: (identifier) @parameter)
(typed_parameter
(identifier) @parameter)
(typed_default_parameter
name: (identifier) @parameter)
; Variadic parameters *args, **kwargs
(parameters
2020-11-03 18:50:26 +01:00
(list_splat_pattern ; *args
(identifier) @parameter))
(parameters
2020-11-03 18:50:26 +01:00
(dictionary_splat_pattern ; **kwargs
(identifier) @parameter))
(lambda_parameters
(list_splat_pattern
(identifier) @parameter))
(lambda_parameters
(dictionary_splat_pattern
(identifier) @parameter))
2020-11-03 18:50:26 +01:00
;; Literals
(none) @constant.builtin
[(true) (false)] @boolean
((identifier) @variable.builtin
(#eq? @variable.builtin "self"))
((identifier) @variable.builtin
(#eq? @variable.builtin "cls"))
(integer) @number
(float) @float
2022-09-26 10:19:02 +01:00
(comment) @comment @spell
((module . (comment) @preproc)
2023-04-21 04:06:20 -04:00
(#lua-match? @preproc "^#!/"))
(string) @string
[
(escape_sequence)
(escape_interpolation)
] @string.escape
; doc-strings
(module . (expression_statement (string) @string.documentation @spell))
(class_definition
body:
(block
. (expression_statement (string) @string.documentation @spell)))
(function_definition
body:
(block
. (expression_statement (string) @string.documentation @spell)))
; Tokens
[
"-"
"-="
2020-06-24 16:42:55 +02:00
":="
"!="
"*"
"**"
"**="
"*="
"/"
"//"
"//="
"/="
"&"
"&="
"%"
"%="
"^"
"^="
"+"
"+="
"<"
"<<"
"<<="
"<="
"<>"
"="
"=="
">"
">="
">>"
">>="
"@"
"@="
"|"
"|="
"~"
"->"
] @operator
; Keywords
[
"and"
"in"
"is"
"not"
"or"
"is not"
"not in"
"del"
] @keyword.operator
[
"def"
"lambda"
] @keyword.function
[
"assert"
"class"
"exec"
"global"
"nonlocal"
"pass"
"print"
"with"
"as"
"type"
] @keyword
[
"async"
"await"
] @keyword.coroutine
[
"return"
"yield"
] @keyword.return
(yield "from" @keyword.return)
2022-11-21 15:06:35 +02:00
(future_import_statement
"from" @include
"__future__" @constant.builtin)
(import_from_statement "from" @include)
"import" @include
(aliased_import "as" @include)
["if" "elif" "else" "match" "case"] @conditional
["for" "while" "break" "continue"] @repeat
[
"try"
"except"
"except*"
"raise"
"finally"
] @exception
2022-04-30 20:10:55 -05:00
(raise_statement "from" @exception)
(try_statement
(else_clause
"else" @exception))
["(" ")" "[" "]" "{" "}"] @punctuation.bracket
(interpolation
"{" @punctuation.special
"}" @punctuation.special)
(type_conversion) @function.macro
2021-11-12 13:26:38 -05:00
["," "." ":" ";" (ellipsis)] @punctuation.delimiter
;; Class definitions
2021-07-11 15:06:52 -05:00
(class_definition name: (identifier) @type)
(class_definition
body: (block
(function_definition
name: (identifier) @method)))
(class_definition
superclasses: (argument_list
(identifier) @type))
((class_definition
body: (block
(expression_statement
(assignment
2020-11-28 22:25:27 +01:00
left: (identifier) @field))))
(#lua-match? @field "^%l.*$"))
2020-11-28 22:25:27 +01:00
((class_definition
body: (block
(expression_statement
(assignment
2021-11-01 10:46:24 -05:00
left: (_
2020-11-28 22:25:27 +01:00
(identifier) @field)))))
(#lua-match? @field "^%l.*$"))
((class_definition
(block
(function_definition
name: (identifier) @constructor)))
2021-05-15 12:29:55 +02:00
(#any-of? @constructor "__new__" "__init__"))
((identifier) @type.builtin
(#any-of? @type.builtin
;; https://docs.python.org/3/library/exceptions.html
"BaseException" "Exception" "ArithmeticError" "BufferError" "LookupError" "AssertionError" "AttributeError"
"EOFError" "FloatingPointError" "GeneratorExit" "ImportError" "ModuleNotFoundError" "IndexError" "KeyError"
"KeyboardInterrupt" "MemoryError" "NameError" "NotImplementedError" "OSError" "OverflowError" "RecursionError"
"ReferenceError" "RuntimeError" "StopIteration" "StopAsyncIteration" "SyntaxError" "IndentationError" "TabError"
"SystemError" "SystemExit" "TypeError" "UnboundLocalError" "UnicodeError" "UnicodeEncodeError" "UnicodeDecodeError"
"UnicodeTranslateError" "ValueError" "ZeroDivisionError" "EnvironmentError" "IOError" "WindowsError"
"BlockingIOError" "ChildProcessError" "ConnectionError" "BrokenPipeError" "ConnectionAbortedError"
"ConnectionRefusedError" "ConnectionResetError" "FileExistsError" "FileNotFoundError" "InterruptedError"
"IsADirectoryError" "NotADirectoryError" "PermissionError" "ProcessLookupError" "TimeoutError" "Warning"
"UserWarning" "DeprecationWarning" "PendingDeprecationWarning" "SyntaxWarning" "RuntimeWarning"
"FutureWarning" "ImportWarning" "UnicodeWarning" "BytesWarning" "ResourceWarning"
;; https://docs.python.org/3/library/stdtypes.html
"bool" "int" "float" "complex" "list" "tuple" "range" "str"
"bytes" "bytearray" "memoryview" "set" "frozenset" "dict" "type" "object"))
;; Regex from the `re` module
(call
function: (attribute
object: (identifier) @_re)
arguments: (argument_list . (string (string_content) @string.regex))
(#eq? @_re "re"))