feat(treesitter)!: new standard capture names

Problem: Sharing queries with upstream and Helix is difficult due to
different capture names.

Solution: Define and document a new set of standard captures that
matches tree-sitter "standard captures" (where defined) and is closer to
Helix' Atom-style nested groups.

This is a breaking change for colorschemes that defined highlights based
on the old captures. On the other hand, the default colorscheme now
defines links for all standard captures (not just those used in bundled
queries), improving the out-of-the-box experience.
This commit is contained in:
Christian Clason
2024-01-17 16:55:52 +01:00
parent fa9a85ae46
commit f5dc453109
27 changed files with 1434 additions and 827 deletions

View File

@@ -1,28 +1,23 @@
[
(function_definition)
(class_definition)
(while_statement)
(for_statement)
(if_statement)
(with_statement)
(try_statement)
(match_statement)
(import_from_statement)
(parameters)
(argument_list)
(parenthesized_expression)
(generator_expression)
(list_comprehension)
(set_comprehension)
(dictionary_comprehension)
(tuple)
(list)
(set)
(dictionary)
(string)
] @fold

View File

@@ -1,183 +1,223 @@
;; From tree-sitter-python licensed under MIT License
; From tree-sitter-python licensed under MIT License
; Copyright (c) 2016 Max Brunsfeld
; Variables
(identifier) @variable
; Reset highlighting in f-string interpolations
(interpolation) @none
;; Identifier naming conventions
; Identifier naming conventions
((identifier) @type
(#lua-match? @type "^[A-Z].*[a-z]"))
(#lua-match? @type "^[A-Z].*[a-z]"))
((identifier) @constant
(#lua-match? @constant "^[A-Z][A-Z_0-9]*$"))
(#lua-match? @constant "^[A-Z][A-Z_0-9]*$"))
((identifier) @constant.builtin
(#lua-match? @constant.builtin "^__[a-zA-Z0-9_]*__$"))
(#lua-match? @constant.builtin "^__[a-zA-Z0-9_]*__$"))
((identifier) @constant.builtin
(#any-of? @constant.builtin
;; https://docs.python.org/3/library/constants.html
"NotImplemented"
"Ellipsis"
"quit"
"exit"
"copyright"
"credits"
"license"))
; format-ignore
(#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_].*$"))
attribute: (identifier) @variable.member)
(#lua-match? @variable.member "^[%l_].*$"))
((assignment
left: (identifier) @type.definition
(type (identifier) @_annotation))
(#eq? @_annotation "TypeAlias"))
(type
(identifier) @_annotation))
(#eq? @_annotation "TypeAlias"))
((assignment
left: (identifier) @type.definition
right: (call
function: (identifier) @_func))
(#any-of? @_func "TypeVar" "NewType"))
right:
(call
function: (identifier) @_func))
(#any-of? @_func "TypeVar" "NewType"))
; Function calls
(call
function: (identifier) @function.call)
(call
function: (attribute
attribute: (identifier) @method.call))
function:
(attribute
attribute: (identifier) @function.method.call))
((call
function: (identifier) @constructor)
(#lua-match? @constructor "^%u"))
function: (identifier) @constructor)
(#lua-match? @constructor "^%u"))
((call
function: (attribute
attribute: (identifier) @constructor))
(#lua-match? @constructor "^%u"))
function:
(attribute
attribute: (identifier) @constructor))
(#lua-match? @constructor "^%u"))
;; Decorators
((decorator "@" @attribute)
(#set! "priority" 101))
; Decorators
((decorator
"@" @attribute)
(#set! "priority" 101))
(decorator
(identifier) @attribute)
(decorator
(attribute
attribute: (identifier) @attribute))
(decorator
(call (identifier) @attribute))
(call
(identifier) @attribute))
(decorator
(call (attribute
attribute: (identifier) @attribute)))
(call
(attribute
attribute: (identifier) @attribute)))
((decorator
(identifier) @attribute.builtin)
(#any-of? @attribute.builtin "classmethod" "property"))
;; Builtin functions
(#any-of? @attribute.builtin "classmethod" "property"))
; Builtin functions
((call
function: (identifier) @function.builtin)
(#any-of? @function.builtin
"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__"))
;; Function definitions
(#any-of? @function.builtin "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__"))
; Function definitions
(function_definition
name: (identifier) @function)
(type (identifier) @type)
(type
(identifier) @type)
(type
(subscript
(identifier) @type)) ; type subscript: Tuple[int]
((call
function: (identifier) @_isinstance
arguments: (argument_list
(_)
(identifier) @type))
(#eq? @_isinstance "isinstance"))
arguments:
(argument_list
(_)
(identifier) @type))
(#eq? @_isinstance "isinstance"))
;; Normal parameters
; Normal parameters
(parameters
(identifier) @parameter)
;; Lambda parameters
(identifier) @variable.parameter)
; Lambda parameters
(lambda_parameters
(identifier) @parameter)
(identifier) @variable.parameter)
(lambda_parameters
(tuple_pattern
(identifier) @parameter))
(identifier) @variable.parameter))
; Default parameters
(keyword_argument
name: (identifier) @parameter)
name: (identifier) @variable.parameter)
; Naming parameters on call-site
(default_parameter
name: (identifier) @parameter)
name: (identifier) @variable.parameter)
(typed_parameter
(identifier) @parameter)
(identifier) @variable.parameter)
(typed_default_parameter
(identifier) @parameter)
name: (identifier) @variable.parameter)
; Variadic parameters *args, **kwargs
(parameters
(list_splat_pattern ; *args
(identifier) @parameter))
(identifier) @variable.parameter))
(parameters
(dictionary_splat_pattern ; **kwargs
(identifier) @parameter))
(identifier) @variable.parameter))
; Typed variadic parameters
(parameters
(typed_parameter
(list_splat_pattern ; *args: type
(identifier) @variable.parameter)))
;; Literals
(parameters
(typed_parameter
(dictionary_splat_pattern ; *kwargs: type
(identifier) @variable.parameter)))
; Lambda parameters
(lambda_parameters
(list_splat_pattern
(identifier) @variable.parameter))
(lambda_parameters
(dictionary_splat_pattern
(identifier) @variable.parameter))
; Literals
(none) @constant.builtin
[(true) (false)] @boolean
[
(true)
(false)
] @boolean
((identifier) @variable.builtin
(#eq? @variable.builtin "self"))
(#eq? @variable.builtin "self"))
((identifier) @variable.builtin
(#eq? @variable.builtin "cls"))
(#eq? @variable.builtin "cls"))
(integer) @number
(float) @float
(float) @number.float
(comment) @comment @spell
((module . (comment) @preproc)
(#lua-match? @preproc "^#!/"))
((module
.
(comment) @keyword.directive)
(#lua-match? @keyword.directive "^#!/"))
(string) @string
[
(escape_sequence)
(escape_interpolation)
] @string.escape
; doc-strings
(module . (expression_statement (string) @string.documentation @spell))
(module
.
(expression_statement
(string) @string.documentation @spell))
(class_definition
body:
(block
. (expression_statement (string) @string.documentation @spell)))
.
(expression_statement
(string) @string.documentation @spell)))
(function_definition
body:
(block
. (expression_statement (string) @string.documentation @spell)))
.
(expression_statement
(string) @string.documentation @spell)))
; Tokens
[
"-"
"-="
@@ -227,7 +267,6 @@
"or"
"is not"
"not in"
"del"
] @keyword.operator
@@ -258,19 +297,36 @@
"return"
"yield"
] @keyword.return
(yield "from" @keyword.return)
(yield
"from" @keyword.return)
(future_import_statement
"from" @include
"from" @keyword.import
"__future__" @constant.builtin)
(import_from_statement "from" @include)
"import" @include
(aliased_import "as" @include)
(import_from_statement
"from" @keyword.import)
["if" "elif" "else" "match" "case"] @conditional
"import" @keyword.import
["for" "while" "break" "continue"] @repeat
(aliased_import
"as" @keyword.import)
[
"if"
"elif"
"else"
"match"
"case"
] @keyword.conditional
[
"for"
"while"
"break"
"continue"
] @keyword.repeat
[
"try"
@@ -278,15 +334,23 @@
"except*"
"raise"
"finally"
] @exception
] @keyword.exception
(raise_statement "from" @exception)
(raise_statement
"from" @keyword.exception)
(try_statement
(else_clause
"else" @exception))
"else" @keyword.exception))
["(" ")" "[" "]" "{" "}"] @punctuation.bracket
[
"("
")"
"["
"]"
"{"
"}"
] @punctuation.bracket
(interpolation
"{" @punctuation.special
@@ -294,58 +358,80 @@
(type_conversion) @function.macro
["," "." ":" ";" (ellipsis)] @punctuation.delimiter
[
","
"."
":"
";"
(ellipsis)
] @punctuation.delimiter
;; Class definitions
(class_definition name: (identifier) @type)
; Class definitions
(class_definition
name: (identifier) @type)
(class_definition
body: (block
(function_definition
name: (identifier) @method)))
body:
(block
(function_definition
name: (identifier) @function.method)))
(class_definition
superclasses: (argument_list
(identifier) @type))
superclasses:
(argument_list
(identifier) @type))
((class_definition
body: (block
(expression_statement
(assignment
left: (identifier) @field))))
(#lua-match? @field "^%l.*$"))
body:
(block
(expression_statement
(assignment
left: (identifier) @variable.member))))
(#lua-match? @variable.member "^%l.*$"))
((class_definition
body: (block
(expression_statement
(assignment
left: (_
(identifier) @field)))))
(#lua-match? @field "^%l.*$"))
body:
(block
(expression_statement
(assignment
left:
(_
(identifier) @variable.member)))))
(#lua-match? @variable.member "^%l.*$"))
((class_definition
(block
(function_definition
name: (identifier) @constructor)))
(#any-of? @constructor "__new__" "__init__"))
(#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"))
; format-ignore
(#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"))
;; Error
(ERROR) @error
; Regex from the `re` module
(call
function:
(attribute
object: (identifier) @_re)
arguments:
(argument_list
.
(string
(string_content) @string.regexp))
(#eq? @_re "re"))