Skip to content

Commit

Permalink
Merge pull request #1 from paul-scott/types
Browse files Browse the repository at this point in the history
Python highlight improvements: type, parameter etc
  • Loading branch information
Zeddicus414 authored May 13, 2022
2 parents 25815df + d5b6bc4 commit fc84edd
Showing 1 changed file with 54 additions and 20 deletions.
74 changes: 54 additions & 20 deletions runtime/queries/python/highlights.scm
Original file line number Diff line number Diff line change
@@ -1,44 +1,78 @@
; Identifier naming conventions

((identifier) @constant
(#match? @constant "^[A-Z_]*$"))

((identifier) @constructor
(#match? @constructor "^[A-Z]"))

; Types

((identifier) @type
(#match?
@type
"^(bool|bytes|dict|float|frozenset|int|list|set|str|tuple)$"))

(type (identifier)) @type

; Builtin functions

((call
function: (identifier) @function.builtin)
(#match?
@function.builtin
"^(abs|all|any|ascii|bin|breakpoint|bytearray|callable|chr|classmethod|compile|complex|delattr|dir|divmod|enumerate|eval|exec|filter|format|getattr|globals|hasattr|hash|help|hex|id|input|isinstance|issubclass|iter|len|locals|map|max|memoryview|min|next|object|oct|open|ord|pow|print|property|range|repr|reversed|round|setattr|slice|sorted|staticmethod|sum|super|type|vars|zip|__import__)$"))
"^(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 calls

(decorator) @function
(call
function: (attribute attribute: (identifier) @constructor)
(#match? @constructor "^[A-Z]"))
(call
function: (identifier) @constructor
(#match? @constructor "^[A-Z]"))

(call
function: (attribute attribute: (identifier) @function.method))

(call
function: (identifier) @function)

; Function definitions

(function_definition
name: (identifier) @constructor
(#match? @constructor "^(__new__|__init__)$"))

(function_definition
name: (identifier) @function)

(identifier) @variable
; Decorators

(decorator) @function
(decorator (identifier) @function)

; Parameters

((identifier) @variable.builtin
(#match? @variable.builtin "^(self|cls)$"))

(parameters (identifier) @variable.parameter)
(parameters (typed_parameter (identifier) @variable.parameter))

; Types

((identifier) @type.builtin
(#match?
@type.builtin
"^(bool|bytes|dict|float|frozenset|int|list|set|str|tuple)$"))

; In type hints make everything types to catch non-conforming identifiers
; (e.g., datetime.datetime) and None
(type [(identifier) (none)] @type)
; Handle [] . and | nesting 4 levels deep
(type
(_ [(identifier) (none)]? @type
(_ [(identifier) (none)]? @type
(_ [(identifier) (none)]? @type
(_ [(identifier) (none)]? @type)))))

(class_definition name: (identifier) @type)
(class_definition superclasses: (argument_list (identifier) @type))

; Variables

((identifier) @constant
(#match? @constant "^[A-Z_]{2,}$"))

((identifier) @type
(#match? @type "^[A-Z]"))

(attribute attribute: (identifier) @variable.other.member)
(identifier) @variable

; Literals

Expand Down

0 comments on commit fc84edd

Please sign in to comment.