Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

python: Refine highlighting #21389

Merged
merged 7 commits into from
Dec 13, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
56 changes: 36 additions & 20 deletions crates/languages/src/python/highlights.scm
Original file line number Diff line number Diff line change
@@ -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)
(type (identifier) @type)
(generic_type (identifier) @type)
(comment) @comment
(string) @string
(escape_sequence) @string.escape

; Type alias
(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
(type
(tuple (identifier) @type)
Expand All @@ -26,15 +31,20 @@

; Function calls

(decorator
"@" @punctuation.special
(identifier) @function.decorator)

(call
function: (attribute attribute: (identifier) @function.method.call))
(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_definition
Expand All @@ -47,16 +57,19 @@

(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
function: (identifier) @function.builtin)
(#match?
@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__)$"))

((identifier) @type.builtin
(#any-of? @type.builtin "int" "float" "complex" "bool" "list" "tuple" "range" "str" "bytes" "bytearray" "memoryview" "set" "frozenset" "dict"))

; Literals

[
Expand All @@ -79,10 +92,6 @@
(#match? @variable.special "^self|cls$")
]

(comment) @comment
(string) @string
(escape_sequence) @string.escape

[
"("
")"
Expand Down Expand Up @@ -114,7 +123,10 @@
. (expression_statement (string) @string.doc))

(module
(expression_statement (assignment))
[
(expression_statement (assignment))
(type_alias_statement)
]
. (expression_statement (string) @string.doc))

(class_definition
Expand Down Expand Up @@ -163,14 +175,17 @@
">>"
"|"
"~"
] @operator

[
"and"
"in"
"is"
"not"
"or"
"is not"
"not in"
] @operator
] @keyword.operator

[
"as"
Expand All @@ -185,6 +200,7 @@
"elif"
"else"
"except"
"except*"
"exec"
"finally"
"for"
Expand Down
Loading