Skip to content

Commit

Permalink
feat(languages/python): Add queries for builtin types, decorators, cl…
Browse files Browse the repository at this point in the history
…ass inheritance, function arguments and definition keywords.
  • Loading branch information
elisiariocouto committed Jan 8, 2025
1 parent 8cd2afe commit 6119d3a
Showing 1 changed file with 55 additions and 1 deletion.
56 changes: 55 additions & 1 deletion crates/languages/src/python/highlights.scm
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@
(call
function: (identifier) @function.call)

(decorator "@" @punctuation.special)
(decorator
"@" @punctuation.special
[
Expand All @@ -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_]*$"))
Expand All @@ -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

[
Expand Down Expand Up @@ -221,3 +243,35 @@
"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"))

(string_prefix) @string.prefix

; 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

0 comments on commit 6119d3a

Please sign in to comment.