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: Enhance syntax highlighting for type hints #18185

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

ClanEver
Copy link
Contributor

@ClanEver ClanEver commented Sep 21, 2024

Release Notes:

  • Python: Enhance syntax highlighting for type hints

Before

image

After

image


Why manual recursion?


Unable to highlight when simple structures appear before complex structures, example:

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:

@cla-bot cla-bot bot added the cla-signed The user has signed the Contributor License Agreement label Sep 21, 2024
@zed-industries-bot
Copy link

zed-industries-bot commented Sep 21, 2024

Messages
📖

This PR includes links to the following GitHub Issues: #tree-sitter-grammars/tree-sitter-lua#24, #14715
If this PR aims to close an issue, please include a Closes #ISSUE line at the top of the PR body.

Generated by 🚫 dangerJS against 1a2db3f

@ClanEver ClanEver changed the title Python: Add syntax highlighting for Union Types Python: Enhance syntax highlighting for type hints Sep 22, 2024
@maxdeviant maxdeviant changed the title Python: Enhance syntax highlighting for type hints python: Enhance syntax highlighting for type hints Sep 23, 2024
(generic_type (identifier) @type)

; Union type X | Y (up to 8 types)
(type
Copy link
Member

Choose a reason for hiding this comment

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

@maxbrunsfeld Do you have thoughts on this approach?

Copy link
Collaborator

Choose a reason for hiding this comment

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

I don't think we should land this code. I get that it's working around a limitation in the query system right now, but I think we just need to fix that limitation.

I worry this is going to have a negative performance impact, and it's also just hard to understand and maintain.

@maxbrunsfeld
Copy link
Collaborator

@ClanEver Can you back out the one complex pattern from the query (sorry that it's not possible to highlight union types cleanly with our current scheme) so that we can land the other changes in the query?

@ClanEver
Copy link
Contributor Author

@maxbrunsfeld Would it be acceptable to reduce the depth to 3?

; Union type X | Y (up to 3 types)
(type
  (binary_operator
    left: [
      (binary_operator
        left:  (_) @type
        right: (_) @type
      ) @type
      (_) @type
    ]
    right: (_) @type
  ) @type
)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
cla-signed The user has signed the Contributor License Agreement
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants