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

Update python highlighting to take advantage of themes. #2451

Merged
merged 13 commits into from
May 20, 2022
Merged
Show file tree
Hide file tree
Changes from 12 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
105 changes: 80 additions & 25 deletions runtime/queries/python/highlights.scm
Original file line number Diff line number Diff line change
@@ -1,11 +1,3 @@
; Identifier naming conventions

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

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

; Builtin functions

((call
Expand All @@ -16,21 +8,77 @@

; 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)
(decorator (attribute attribute: (identifier) @function))
(decorator (call
function: (attribute attribute: (identifier) @function)))

; Parameters

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

(parameters (identifier) @variable.parameter)
(parameters (typed_parameter (identifier) @variable.parameter))
(parameters (default_parameter name: (identifier) @variable.parameter))
(parameters (typed_default_parameter name: (identifier) @variable.parameter))
(keyword_argument name: (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)
(type (identifier) @type)
(identifier) @variable

; Literals

Expand Down Expand Up @@ -81,41 +129,48 @@
">>"
"|"
"~"
"and"
"in"
"is"
"not"
"or"
] @operator

[
"as"
"assert"
"async"
"await"
"break"
"class"
"continue"
"def"
"del"
"elif"
"else"
"except"
"exec"
"finally"
"for"
"from"
"global"
"if"
"import"
"lambda"
"nonlocal"
"pass"
"print"
"raise"
"return"
"try"
"while"
"with"
"yield"
] @keyword.control

(for_statement "in" @keyword.control)
(for_in_clause "in" @keyword.control)

[
"and"
"async"
"class"
"def"
"del"
"exec"
"global"
"in"
"is"
"lambda"
"nonlocal"
"not"
"or"
"print"
] @keyword

2 changes: 1 addition & 1 deletion runtime/themes/dark_plus.toml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
"type" = { fg = "type" }
"type.builtin" = { fg = "type" }
"type.enum.variant" = { fg = "constant" }
"constructor" = { fg = "constant" }
"constructor" = { fg = "type" }
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

\cc @kirawi to confirm this

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hmm, I think that the usage of @constructor has become somewhat conflated with @type as in many highlight queries it applies to all identifiers with ^[A-Z]. I'm fine with this change.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The recent commits we made to this PR fixed up the usage of @constructor so it is no longer conflated with @type in python at least. I suspect originally that was the main motivation for @Zeddicus414 wanting to change this. For style reasons, maybe the change here is still a good idea. I just thought I'd point this out.

"variable.other.member" = { fg = "variable" }

"keyword" = { fg = "blue2" }
Expand Down