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: Improve function syntax highlighting #20487

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
34 changes: 23 additions & 11 deletions crates/languages/src/python/highlights.scm
Original file line number Diff line number Diff line change
Expand Up @@ -5,32 +5,43 @@
; 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_]*$"))
osiewicz marked this conversation as resolved.
Show resolved Hide resolved

; TypeVar with constraints in type parameters
(type
(tuple (identifier) @type)
)

; Function calls

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

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

; Function definitions
; Function and class definitions

(function_definition
name: (identifier) @function)
name: (identifier) @function.definition)

; Identifier naming conventions
; Class definitions and calling: needs to come after the regex matching above

((identifier) @type
(#match? @type "^[A-Z]"))
(class_definition
name: (identifier) @type.class.definition)

((identifier) @constant
(#match? @constant "^_*[A-Z][A-Z\\d_]*$"))
(call
function: (identifier) @type.class.call
(#match? @type.class.call "^[A-Z][A-Z0-9_]*[a-z]"))

; Builtin functions

Expand All @@ -46,6 +57,7 @@
(none)
(true)
(false)
(ellipsis)
] @constant.builtin

[
Expand All @@ -58,7 +70,7 @@
[
(parameters (identifier) @variable.special)
(attribute (identifier) @variable.special)
(#match? @variable.special "^self$")
(#match? @variable.special "^self|cls$")
]

(comment) @comment
Expand Down
Loading