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

Merged
merged 3 commits into from
Oct 28, 2024

Conversation

ClanEver
Copy link
Contributor

@ClanEver ClanEver commented Sep 21, 2024

Release Notes:

  • Python: Improved 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
@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
)

Copy link
Member

@maxdeviant maxdeviant left a comment

Choose a reason for hiding this comment

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

Thank you!

I removed the union type highlighting until we can come up with a better solution.

@maxdeviant maxdeviant merged commit 188a893 into zed-industries:main Oct 28, 2024
11 checks passed
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.

4 participants