python: Refine highlighting (#21389)
Fixes: * Types in binary unions as per [PEP 604](https://peps.python.org/pep-0604/) not highlighted; * `except*` keyword not highlighted; * Classes beginning with `_` not recognized as such, however `_` is a valid first character for private classes; additionally the regex for parsing constant/class names appeared inconsistent and incomplete so was adjusted; * Builtin types such as `float`, `dict`, etc not recognized as types; * **Update:** decorators with arguments not recognized as decorators; * **Update:** docstrings after type alias assignments not recognized as docstrings; * **Update:** `and/in/is/not/or/is not/not in` not capturable as keywords; * **Update:** decorators with "nesting" (@x.y.z) not recognized as decorators; Before:  After:  Release Notes: - N/A --------- Co-authored-by: Piotr Osiewicz <24362066+osiewicz@users.noreply.github.com>
This commit is contained in:
parent
5318f529de
commit
6838b6203a
1 changed files with 36 additions and 20 deletions
|
@ -1,18 +1,23 @@
|
||||||
|
; Identifier naming conventions; these "soft conventions" should stay at the top of the file as they're often overridden
|
||||||
|
|
||||||
|
; CamelCase for classes
|
||||||
|
((identifier) @type.class
|
||||||
|
(#match? @type.class "^_*[A-Z][A-Za-z0-9_]*$"))
|
||||||
|
|
||||||
|
; ALL_CAPS for constants:
|
||||||
|
((identifier) @constant
|
||||||
|
(#match? @constant "^_*[A-Z][A-Z0-9_]*$"))
|
||||||
|
|
||||||
(attribute attribute: (identifier) @property)
|
(attribute attribute: (identifier) @property)
|
||||||
(type (identifier) @type)
|
(type (identifier) @type)
|
||||||
(generic_type (identifier) @type)
|
(generic_type (identifier) @type)
|
||||||
|
(comment) @comment
|
||||||
|
(string) @string
|
||||||
|
(escape_sequence) @string.escape
|
||||||
|
|
||||||
; Type alias
|
; Type alias
|
||||||
(type_alias_statement "type" @keyword)
|
(type_alias_statement "type" @keyword)
|
||||||
|
|
||||||
; Identifier naming conventions
|
|
||||||
|
|
||||||
((identifier) @type.class
|
|
||||||
(#match? @type.class "^[A-Z]"))
|
|
||||||
|
|
||||||
((identifier) @constant
|
|
||||||
(#match? @constant "^_*[A-Z][A-Z\\d_]*$"))
|
|
||||||
|
|
||||||
; TypeVar with constraints in type parameters
|
; TypeVar with constraints in type parameters
|
||||||
(type
|
(type
|
||||||
(tuple (identifier) @type)
|
(tuple (identifier) @type)
|
||||||
|
@ -26,15 +31,20 @@
|
||||||
|
|
||||||
; Function calls
|
; Function calls
|
||||||
|
|
||||||
(decorator
|
|
||||||
"@" @punctuation.special
|
|
||||||
(identifier) @function.decorator)
|
|
||||||
|
|
||||||
(call
|
(call
|
||||||
function: (attribute attribute: (identifier) @function.method.call))
|
function: (attribute attribute: (identifier) @function.method.call))
|
||||||
(call
|
(call
|
||||||
function: (identifier) @function.call)
|
function: (identifier) @function.call)
|
||||||
|
|
||||||
|
(decorator
|
||||||
|
"@" @punctuation.special
|
||||||
|
[
|
||||||
|
(identifier) @function.decorator
|
||||||
|
(attribute attribute: (identifier) @function.decorator)
|
||||||
|
(call function: (identifier) @function.decorator.call)
|
||||||
|
(call (attribute attribute: (identifier) @function.decorator.call))
|
||||||
|
])
|
||||||
|
|
||||||
; Function and class definitions
|
; Function and class definitions
|
||||||
|
|
||||||
(function_definition
|
(function_definition
|
||||||
|
@ -47,9 +57,9 @@
|
||||||
|
|
||||||
(call
|
(call
|
||||||
function: (identifier) @type.class.call
|
function: (identifier) @type.class.call
|
||||||
(#match? @type.class.call "^[A-Z][A-Z0-9_]*[a-z]"))
|
(#match? @type.class.call "^_*[A-Z][A-Za-z0-9_]*$"))
|
||||||
|
|
||||||
; Builtin functions
|
; Builtins
|
||||||
|
|
||||||
((call
|
((call
|
||||||
function: (identifier) @function.builtin)
|
function: (identifier) @function.builtin)
|
||||||
|
@ -57,6 +67,9 @@
|
||||||
@function.builtin
|
@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__)$"))
|
"^(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__)$"))
|
||||||
|
|
||||||
|
((identifier) @type.builtin
|
||||||
|
(#any-of? @type.builtin "int" "float" "complex" "bool" "list" "tuple" "range" "str" "bytes" "bytearray" "memoryview" "set" "frozenset" "dict"))
|
||||||
|
|
||||||
; Literals
|
; Literals
|
||||||
|
|
||||||
[
|
[
|
||||||
|
@ -79,10 +92,6 @@
|
||||||
(#match? @variable.special "^self|cls$")
|
(#match? @variable.special "^self|cls$")
|
||||||
]
|
]
|
||||||
|
|
||||||
(comment) @comment
|
|
||||||
(string) @string
|
|
||||||
(escape_sequence) @string.escape
|
|
||||||
|
|
||||||
[
|
[
|
||||||
"("
|
"("
|
||||||
")"
|
")"
|
||||||
|
@ -114,7 +123,10 @@
|
||||||
. (expression_statement (string) @string.doc))
|
. (expression_statement (string) @string.doc))
|
||||||
|
|
||||||
(module
|
(module
|
||||||
(expression_statement (assignment))
|
[
|
||||||
|
(expression_statement (assignment))
|
||||||
|
(type_alias_statement)
|
||||||
|
]
|
||||||
. (expression_statement (string) @string.doc))
|
. (expression_statement (string) @string.doc))
|
||||||
|
|
||||||
(class_definition
|
(class_definition
|
||||||
|
@ -163,6 +175,9 @@
|
||||||
">>"
|
">>"
|
||||||
"|"
|
"|"
|
||||||
"~"
|
"~"
|
||||||
|
] @operator
|
||||||
|
|
||||||
|
[
|
||||||
"and"
|
"and"
|
||||||
"in"
|
"in"
|
||||||
"is"
|
"is"
|
||||||
|
@ -170,7 +185,7 @@
|
||||||
"or"
|
"or"
|
||||||
"is not"
|
"is not"
|
||||||
"not in"
|
"not in"
|
||||||
] @operator
|
] @keyword.operator
|
||||||
|
|
||||||
[
|
[
|
||||||
"as"
|
"as"
|
||||||
|
@ -185,6 +200,7 @@
|
||||||
"elif"
|
"elif"
|
||||||
"else"
|
"else"
|
||||||
"except"
|
"except"
|
||||||
|
"except*"
|
||||||
"exec"
|
"exec"
|
||||||
"finally"
|
"finally"
|
||||||
"for"
|
"for"
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue