aboutsummaryrefslogtreecommitdiffstats
path: root/runtime/queries/python
diff options
context:
space:
mode:
authorChristian Clason <c.clason@uni-graz.at>2023-06-12 09:54:30 -0600
committerChristian Clason <c.clason@uni-graz.at>2025-05-12 18:43:40 +0200
commit692b051b09935653befdb8f7ba8afdb640adf17b (patch)
tree167162b6b129ae04f68c5735078521a72917c742 /runtime/queries/python
parentfeat(c-family): inherit injections (diff)
downloadnvim-treesitter-692b051b09935653befdb8f7ba8afdb640adf17b.tar
nvim-treesitter-692b051b09935653befdb8f7ba8afdb640adf17b.tar.gz
nvim-treesitter-692b051b09935653befdb8f7ba8afdb640adf17b.tar.bz2
nvim-treesitter-692b051b09935653befdb8f7ba8afdb640adf17b.tar.lz
nvim-treesitter-692b051b09935653befdb8f7ba8afdb640adf17b.tar.xz
nvim-treesitter-692b051b09935653befdb8f7ba8afdb640adf17b.tar.zst
nvim-treesitter-692b051b09935653befdb8f7ba8afdb640adf17b.zip
feat!: drop modules, general refactor and cleanup
Diffstat (limited to 'runtime/queries/python')
-rw-r--r--runtime/queries/python/folds.scm28
-rw-r--r--runtime/queries/python/highlights.scm443
-rw-r--r--runtime/queries/python/indents.scm213
-rw-r--r--runtime/queries/python/injections.scm18
-rw-r--r--runtime/queries/python/locals.scm124
5 files changed, 826 insertions, 0 deletions
diff --git a/runtime/queries/python/folds.scm b/runtime/queries/python/folds.scm
new file mode 100644
index 000000000..ecb9352d7
--- /dev/null
+++ b/runtime/queries/python/folds.scm
@@ -0,0 +1,28 @@
+[
+ (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
+
+[
+ (import_statement)
+ (import_from_statement)
+]+ @fold
diff --git a/runtime/queries/python/highlights.scm b/runtime/queries/python/highlights.scm
new file mode 100644
index 000000000..00250de1b
--- /dev/null
+++ b/runtime/queries/python/highlights.scm
@@ -0,0 +1,443 @@
+; 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) @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
+ (#any-of? @constant.builtin
+ ; https://docs.python.org/3/library/constants.html
+ "NotImplemented" "Ellipsis" "quit" "exit" "copyright" "credits" "license"))
+
+"_" @character.special ; match wildcard
+
+((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 definitions
+(function_definition
+ name: (identifier) @function)
+
+(type
+ (identifier) @type)
+
+(type
+ (subscript
+ (identifier) @type)) ; type subscript: Tuple[int]
+
+((call
+ function: (identifier) @_isinstance
+ arguments: (argument_list
+ (_)
+ (identifier) @type))
+ (#eq? @_isinstance "isinstance"))
+
+; Literals
+(none) @constant.builtin
+
+[
+ (true)
+ (false)
+] @boolean
+
+(integer) @number
+
+(float) @number.float
+
+(comment) @comment @spell
+
+((module
+ .
+ (comment) @keyword.directive @nospell)
+ (#lua-match? @keyword.directive "^#!/"))
+
+(string) @string
+
+[
+ (escape_sequence)
+ (escape_interpolation)
+] @string.escape
+
+; doc-strings
+(expression_statement
+ (string
+ (string_content) @spell) @string.documentation)
+
+; Tokens
+[
+ "-"
+ "-="
+ ":="
+ "!="
+ "*"
+ "**"
+ "**="
+ "*="
+ "/"
+ "//"
+ "//="
+ "/="
+ "&"
+ "&="
+ "%"
+ "%="
+ "^"
+ "^="
+ "+"
+ "+="
+ "<"
+ "<<"
+ "<<="
+ "<="
+ "<>"
+ "="
+ "=="
+ ">"
+ ">="
+ ">>"
+ ">>="
+ "@"
+ "@="
+ "|"
+ "|="
+ "~"
+ "->"
+] @operator
+
+; Keywords
+[
+ "and"
+ "in"
+ "is"
+ "not"
+ "or"
+ "is not"
+ "not in"
+ "del"
+] @keyword.operator
+
+[
+ "def"
+ "lambda"
+] @keyword.function
+
+[
+ "assert"
+ "exec"
+ "global"
+ "nonlocal"
+ "pass"
+ "print"
+ "with"
+ "as"
+] @keyword
+
+[
+ "type"
+ "class"
+] @keyword.type
+
+[
+ "async"
+ "await"
+] @keyword.coroutine
+
+[
+ "return"
+ "yield"
+] @keyword.return
+
+(yield
+ "from" @keyword.return)
+
+(future_import_statement
+ "from" @keyword.import
+ "__future__" @module.builtin)
+
+(import_from_statement
+ "from" @keyword.import)
+
+"import" @keyword.import
+
+(aliased_import
+ "as" @keyword.import)
+
+(wildcard_import
+ "*" @character.special)
+
+(import_statement
+ name: (dotted_name
+ (identifier) @module))
+
+(import_statement
+ name: (aliased_import
+ name: (dotted_name
+ (identifier) @module)
+ alias: (identifier) @module))
+
+(import_from_statement
+ module_name: (dotted_name
+ (identifier) @module))
+
+(import_from_statement
+ module_name: (relative_import
+ (dotted_name
+ (identifier) @module)))
+
+[
+ "if"
+ "elif"
+ "else"
+ "match"
+ "case"
+] @keyword.conditional
+
+[
+ "for"
+ "while"
+ "break"
+ "continue"
+] @keyword.repeat
+
+[
+ "try"
+ "except"
+ "except*"
+ "raise"
+ "finally"
+] @keyword.exception
+
+(raise_statement
+ "from" @keyword.exception)
+
+(try_statement
+ (else_clause
+ "else" @keyword.exception))
+
+[
+ "("
+ ")"
+ "["
+ "]"
+ "{"
+ "}"
+] @punctuation.bracket
+
+(interpolation
+ "{" @punctuation.special
+ "}" @punctuation.special)
+
+(type_conversion) @function.macro
+
+[
+ ","
+ "."
+ ":"
+ ";"
+ (ellipsis)
+] @punctuation.delimiter
+
+((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"))
+
+; Normal parameters
+(parameters
+ (identifier) @variable.parameter)
+
+; Lambda parameters
+(lambda_parameters
+ (identifier) @variable.parameter)
+
+(lambda_parameters
+ (tuple_pattern
+ (identifier) @variable.parameter))
+
+; Default parameters
+(keyword_argument
+ name: (identifier) @variable.parameter)
+
+; Naming parameters on call-site
+(default_parameter
+ name: (identifier) @variable.parameter)
+
+(typed_parameter
+ (identifier) @variable.parameter)
+
+(typed_default_parameter
+ name: (identifier) @variable.parameter)
+
+; Variadic parameters *args, **kwargs
+(parameters
+ (list_splat_pattern ; *args
+ (identifier) @variable.parameter))
+
+(parameters
+ (dictionary_splat_pattern ; **kwargs
+ (identifier) @variable.parameter))
+
+; Typed variadic parameters
+(parameters
+ (typed_parameter
+ (list_splat_pattern ; *args: type
+ (identifier) @variable.parameter)))
+
+(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))
+
+((identifier) @variable.builtin
+ (#eq? @variable.builtin "self"))
+
+((identifier) @variable.builtin
+ (#eq? @variable.builtin "cls"))
+
+; After @type.builtin bacause builtins (such as `type`) are valid as attribute name
+((attribute
+ attribute: (identifier) @variable.member)
+ (#lua-match? @variable.member "^[%l_].*$"))
+
+; Class definitions
+(class_definition
+ name: (identifier) @type)
+
+(class_definition
+ body: (block
+ (function_definition
+ name: (identifier) @function.method)))
+
+(class_definition
+ superclasses: (argument_list
+ (identifier) @type))
+
+((class_definition
+ body: (block
+ (expression_statement
+ (assignment
+ left: (identifier) @variable.member))))
+ (#lua-match? @variable.member "^[%l_].*$"))
+
+((class_definition
+ 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__"))
+
+; Function calls
+(call
+ function: (identifier) @function.call)
+
+(call
+ function: (attribute
+ attribute: (identifier) @function.method.call))
+
+((call
+ function: (identifier) @constructor)
+ (#lua-match? @constructor "^%u"))
+
+((call
+ function: (attribute
+ attribute: (identifier) @constructor))
+ (#lua-match? @constructor "^%u"))
+
+; 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__"))
+
+; Regex from the `re` module
+(call
+ function: (attribute
+ object: (identifier) @_re)
+ arguments: (argument_list
+ .
+ (string
+ (string_content) @string.regexp))
+ (#eq? @_re "re"))
+
+; 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" "staticmethod"))
diff --git a/runtime/queries/python/indents.scm b/runtime/queries/python/indents.scm
new file mode 100644
index 000000000..2414812a7
--- /dev/null
+++ b/runtime/queries/python/indents.scm
@@ -0,0 +1,213 @@
+[
+ (import_from_statement)
+ (generator_expression)
+ (list_comprehension)
+ (set_comprehension)
+ (dictionary_comprehension)
+ (tuple_pattern)
+ (list_pattern)
+ (binary_operator)
+ (lambda)
+ (concatenated_string)
+] @indent.begin
+
+((list) @indent.align
+ (#set! indent.open_delimiter "[")
+ (#set! indent.close_delimiter "]"))
+
+((dictionary) @indent.align
+ (#set! indent.open_delimiter "{")
+ (#set! indent.close_delimiter "}"))
+
+((set) @indent.align
+ (#set! indent.open_delimiter "{")
+ (#set! indent.close_delimiter "}"))
+
+((parenthesized_expression) @indent.align
+ (#set! indent.open_delimiter "(")
+ (#set! indent.close_delimiter ")"))
+
+((for_statement) @indent.begin
+ (#set! indent.immediate 1))
+
+((if_statement) @indent.begin
+ (#set! indent.immediate 1))
+
+((while_statement) @indent.begin
+ (#set! indent.immediate 1))
+
+((try_statement) @indent.begin
+ (#set! indent.immediate 1))
+
+(ERROR
+ "try"
+ .
+ ":"
+ (#set! indent.immediate 1)) @indent.begin
+
+(ERROR
+ "try"
+ .
+ ":"
+ (ERROR
+ (block
+ (expression_statement
+ (identifier) @_except) @indent.branch))
+ (#eq? @_except "except"))
+
+((function_definition) @indent.begin
+ (#set! indent.immediate 1))
+
+((class_definition) @indent.begin
+ (#set! indent.immediate 1))
+
+((with_statement) @indent.begin
+ (#set! indent.immediate 1))
+
+((match_statement) @indent.begin
+ (#set! indent.immediate 1))
+
+((case_clause) @indent.begin
+ (#set! indent.immediate 1))
+
+; if (cond1
+; or cond2
+; or cond3):
+; pass
+;
+(if_statement
+ condition: (parenthesized_expression) @indent.align
+ (#lua-match? @indent.align "^%([^\n]")
+ (#set! indent.open_delimiter "(")
+ (#set! indent.close_delimiter ")")
+ (#set! indent.avoid_last_matching_next 1))
+
+; while (
+; cond1
+; or cond2
+; or cond3):
+; pass
+;
+(while_statement
+ condition: (parenthesized_expression) @indent.align
+ (#lua-match? @indent.align "[^\n ]%)$")
+ (#set! indent.open_delimiter "(")
+ (#set! indent.close_delimiter ")")
+ (#set! indent.avoid_last_matching_next 1))
+
+; if (
+; cond1
+; or cond2
+; or cond3):
+; pass
+;
+(if_statement
+ condition: (parenthesized_expression) @indent.align
+ (#lua-match? @indent.align "[^\n ]%)$")
+ (#set! indent.open_delimiter "(")
+ (#set! indent.close_delimiter ")")
+ (#set! indent.avoid_last_matching_next 1))
+
+(ERROR
+ "(" @indent.align
+ (#set! indent.open_delimiter "(")
+ (#set! indent.close_delimiter ")")
+ .
+ (_))
+
+((argument_list) @indent.align
+ (#set! indent.open_delimiter "(")
+ (#set! indent.close_delimiter ")"))
+
+((parameters) @indent.align
+ (#set! indent.open_delimiter "(")
+ (#set! indent.close_delimiter ")"))
+
+((parameters) @indent.align
+ (#lua-match? @indent.align "[^\n ]%)$")
+ (#set! indent.open_delimiter "(")
+ (#set! indent.close_delimiter ")")
+ (#set! indent.avoid_last_matching_next 1))
+
+((tuple) @indent.align
+ (#set! indent.open_delimiter "(")
+ (#set! indent.close_delimiter ")"))
+
+(ERROR
+ "[" @indent.align
+ (#set! indent.open_delimiter "[")
+ (#set! indent.close_delimiter "]")
+ .
+ (_))
+
+(ERROR
+ "{" @indent.align
+ (#set! indent.open_delimiter "{")
+ (#set! indent.close_delimiter "}")
+ .
+ (_))
+
+[
+ (break_statement)
+ (continue_statement)
+] @indent.dedent
+
+(ERROR
+ (_) @indent.branch
+ ":"
+ .
+ (#lua-match? @indent.branch "^else"))
+
+(ERROR
+ (_) @indent.branch @indent.dedent
+ ":"
+ .
+ (#lua-match? @indent.branch "^elif"))
+
+(generator_expression
+ ")" @indent.end)
+
+(list_comprehension
+ "]" @indent.end)
+
+(set_comprehension
+ "}" @indent.end)
+
+(dictionary_comprehension
+ "}" @indent.end)
+
+(tuple_pattern
+ ")" @indent.end)
+
+(list_pattern
+ "]" @indent.end)
+
+(return_statement
+ [
+ (_) @indent.end
+ (_
+ [
+ (_)
+ ")"
+ "}"
+ "]"
+ ] @indent.end .)
+ (attribute
+ attribute: (_) @indent.end)
+ (call
+ arguments: (_
+ ")" @indent.end))
+ "return" @indent.end
+ ] .)
+
+[
+ ")"
+ "]"
+ "}"
+ (elif_clause)
+ (else_clause)
+ (except_clause)
+ (finally_clause)
+] @indent.branch
+
+(string) @indent.auto
diff --git a/runtime/queries/python/injections.scm b/runtime/queries/python/injections.scm
new file mode 100644
index 000000000..bbc924be4
--- /dev/null
+++ b/runtime/queries/python/injections.scm
@@ -0,0 +1,18 @@
+(call
+ function: (attribute
+ object: (identifier) @_re)
+ arguments: (argument_list
+ .
+ (string
+ (string_content) @injection.content))
+ (#eq? @_re "re")
+ (#set! injection.language "regex"))
+
+((binary_operator
+ left: (string
+ (string_content) @injection.content)
+ operator: "%")
+ (#set! injection.language "printf"))
+
+((comment) @injection.content
+ (#set! injection.language "comment"))
diff --git a/runtime/queries/python/locals.scm b/runtime/queries/python/locals.scm
new file mode 100644
index 000000000..b012e9eee
--- /dev/null
+++ b/runtime/queries/python/locals.scm
@@ -0,0 +1,124 @@
+; Program structure
+(module) @local.scope
+
+(class_definition
+ body: (block
+ (expression_statement
+ (assignment
+ left: (identifier) @local.definition.field)))) @local.scope
+
+(class_definition
+ body: (block
+ (expression_statement
+ (assignment
+ left: (_
+ (identifier) @local.definition.field))))) @local.scope
+
+; Imports
+(aliased_import
+ alias: (identifier) @local.definition.import) @local.scope
+
+(import_statement
+ name: (dotted_name
+ (identifier) @local.definition.import)) @local.scope
+
+(import_from_statement
+ name: (dotted_name
+ (identifier) @local.definition.import)) @local.scope
+
+; Function with parameters, defines parameters
+(parameters
+ (identifier) @local.definition.parameter)
+
+(default_parameter
+ (identifier) @local.definition.parameter)
+
+(typed_parameter
+ (identifier) @local.definition.parameter)
+
+(typed_default_parameter
+ (identifier) @local.definition.parameter)
+
+; *args parameter
+(parameters
+ (list_splat_pattern
+ (identifier) @local.definition.parameter))
+
+; **kwargs parameter
+(parameters
+ (dictionary_splat_pattern
+ (identifier) @local.definition.parameter))
+
+; Function defines function and scope
+((function_definition
+ name: (identifier) @local.definition.function) @local.scope
+ (#set! definition.function.scope "parent"))
+
+((class_definition
+ name: (identifier) @local.definition.type) @local.scope
+ (#set! definition.type.scope "parent"))
+
+(class_definition
+ body: (block
+ (function_definition
+ name: (identifier) @local.definition.method)))
+
+; Loops
+; not a scope!
+(for_statement
+ left: (pattern_list
+ (identifier) @local.definition.var))
+
+(for_statement
+ left: (tuple_pattern
+ (identifier) @local.definition.var))
+
+(for_statement
+ left: (identifier) @local.definition.var)
+
+; not a scope!
+;(while_statement) @local.scope
+; for in list comprehension
+(for_in_clause
+ left: (identifier) @local.definition.var)
+
+(for_in_clause
+ left: (tuple_pattern
+ (identifier) @local.definition.var))
+
+(for_in_clause
+ left: (pattern_list
+ (identifier) @local.definition.var))
+
+(dictionary_comprehension) @local.scope
+
+(list_comprehension) @local.scope
+
+(set_comprehension) @local.scope
+
+; Assignments
+(assignment
+ left: (identifier) @local.definition.var)
+
+(assignment
+ left: (pattern_list
+ (identifier) @local.definition.var))
+
+(assignment
+ left: (tuple_pattern
+ (identifier) @local.definition.var))
+
+(assignment
+ left: (attribute
+ (identifier)
+ (identifier) @local.definition.field))
+
+; Walrus operator x := 1
+(named_expression
+ (identifier) @local.definition.var)
+
+(as_pattern
+ alias: (as_pattern_target) @local.definition.var)
+
+; REFERENCES
+(identifier) @local.reference