From 188a893fd0c6f5b62f578c74612d7b71b60c8a5c Mon Sep 17 00:00:00 2001 From: ClanEver <562211524@qq.com> Date: Tue, 29 Oct 2024 04:56:59 +0800 Subject: [PATCH] python: Enhance syntax highlighting for type hints (#18185) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Release Notes: - Python: Improved syntax highlighting for type hints. # Before ![image](https://github.com/user-attachments/assets/876a69ab-a572-4d1b-af99-e6f85f249ea6) # After ![image](https://github.com/user-attachments/assets/4fb98a9b-bc5d-4799-b535-057047884383) --- Why manual recursion? - Due to tree-sitter grammar not supporting recursion in query (https://github.com/tree-sitter-grammars/tree-sitter-lua/issues/24), currently only manual recursion is possible (refer to https://github.com/projekt0n/github-nvim-theme/pull/250/files).
Unable to highlight when simple structures appear before complex structures, example: ```python def t() -> str | dict[int, dict[int, dict[int, str]]]: pass ``` Because complex structures are parsed as `subscript` rather than `generic_type` by tree-sitter in this case ☹
Related: - https://github.com/zed-industries/zed/issues/14715 - [Union Type (Python Doc)](https://docs.python.org/3/library/stdtypes.html#union-type) - [Type parameter lists (Python Doc)](https://docs.python.org/3/reference/compound_stmts.html#type-parameter-lists) --------- Co-authored-by: Marshall Bowers --- crates/languages/src/python/highlights.scm | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/crates/languages/src/python/highlights.scm b/crates/languages/src/python/highlights.scm index 5b64642771..e5f1b4d423 100644 --- a/crates/languages/src/python/highlights.scm +++ b/crates/languages/src/python/highlights.scm @@ -1,5 +1,14 @@ (attribute attribute: (identifier) @property) (type (identifier) @type) +(generic_type (identifier) @type) + +; Type alias +(type_alias_statement "type" @keyword) + +; TypeVar with constraints in type parameters +(type + (tuple (identifier) @type) +) ; Function calls