From 73001a72e3c53cc7932b5c858d17756a086a2d33 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Elisi=C3=A1rio=20Couto?= Date: Mon, 20 Jan 2025 18:14:19 +0000 Subject: [PATCH] python: Add capture groups for builtin types, builtin attribute decorators, class inheritance, function arguments and definition keywords (#21454) Add capture groups for builtin types, builtin attribute decorators, class inheritance, function arguments and definition keywords. Related to #14892 Release Notes: - Improved syntax highlight for Python: new capture groups for `@function.arguments`, `@function.kwargs`, `@type.class.inheritance`, `@keyword.definition`, `@attribute.builtin` and `@type.builtin`. --- crates/languages/src/python/highlights.scm | 54 +++++++++++++++++++++- 1 file changed, 53 insertions(+), 1 deletion(-) diff --git a/crates/languages/src/python/highlights.scm b/crates/languages/src/python/highlights.scm index 9d08237e3cb45352f24f7c68b11b164f478f2428..6b296574786d1b5ca8a01a9b04530cf06dd28c83 100644 --- a/crates/languages/src/python/highlights.scm +++ b/crates/languages/src/python/highlights.scm @@ -36,6 +36,7 @@ (call function: (identifier) @function.call) +(decorator "@" @punctuation.special) (decorator "@" @punctuation.special [ @@ -50,11 +51,32 @@ (function_definition name: (identifier) @function.definition) +; Function arguments +(function_definition + parameters: (parameters + [ + (identifier) @function.arguments ; Simple parameters + (typed_parameter + (identifier) @function.arguments) ; Typed parameters + (default_parameter + name: (identifier) @function.arguments) ; Default parameters + ])) + +; Keyword arguments +(call + arguments: (argument_list + (keyword_argument + name: (identifier) @function.kwargs))) + ; Class definitions and calling: needs to come after the regex matching above (class_definition name: (identifier) @type.class.definition) +(class_definition + superclasses: (argument_list + (identifier) @type.class.inheritance)) + (call function: (identifier) @type.class.call (#match? @type.class.call "^_*[A-Z][A-Za-z0-9_]*$")) @@ -69,7 +91,7 @@ ((identifier) @type.builtin (#any-of? @type.builtin "int" "float" "complex" "bool" "list" "tuple" "range" "str" "bytes" "bytearray" "memoryview" "set" "frozenset" "dict")) - + ; Literals [ @@ -221,3 +243,33 @@ "match" "case" ] @keyword + +; Definition keywords def, class, async def, lambda +[ + "async" + "def" + "class" + "lambda" +] @keyword.definition + +((identifier) @attribute.builtin + (#any-of? @attribute.builtin "classmethod" "staticmethod" "property")) + +; Builtin types as identifiers +[ + (call + function: (identifier) @type.builtin) + (call + arguments: (argument_list + (identifier) @type.builtin)) + (call + arguments: (argument_list + (keyword_argument + value: (identifier) @type.builtin))) + (type (identifier) @type.builtin) + ; also check if type binary operator left identifier for union types + (type + (binary_operator + left: (identifier) @type.builtin)) + (#any-of? @type.builtin "bool" "bytearray" "bytes" "complex" "dict" "float" "frozenset" "int" "list" "memoryview" "object" "range" "set" "slice" "str" "tuple") +] @type.builtin