Skip to content

Fix semantic token highlighting for match captures#4257

Open
magic-akari wants to merge 1 commit into
facebook:mainfrom
magic-akari:fix/match-capture-semantic-token
Open

Fix semantic token highlighting for match captures#4257
magic-akari wants to merge 1 commit into
facebook:mainfrom
magic-akari:fix/match-capture-semantic-token

Conversation

@magic-akari

Copy link
Copy Markdown
Contributor

Summary

Match capture patterns bind regular variables, but Pyrefly’s semantic-token walker only handled expression names. This caused capture definitions and some references to be missing or inconsistently highlighted.

This change:

  • Emits plain variable tokens for all capture patterns.
  • Introduces a type-transparent PatternCapture binding that preserves the capture’s declaration identity.
  • Keeps go-to-definition anchored at the capture site.
  • Excludes wildcards and class-pattern keyword labels.
  • Adds coverage for nested, starred, mapping, as, and OR patterns.

Test Plan

  • Semantic-token tests
  • Pattern-matching tests
  • Formatting and linting checks

@github-actions

Copy link
Copy Markdown

According to mypy_primer, this change doesn't affect type check results on a corpus of open source code. ✅

@magic-akari
magic-akari marked this pull request as ready for review July 23, 2026 11:49

@kinto0 kinto0 left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

thanks for the PR! would you mind updating the summary with more details about the bug it fixed and maybe a screenshot of before / after? thanks!

}

#[test]
fn pattern_capture_test() {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

do we need the test to be this big?

Forward(Idx<Key>),
/// A definition boundary for a capture pattern. It forwards type information during solving,
/// but origin and definition lookups must not follow it through to the matched subject.
PatternCapture(Idx<Key>),

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

can you help me understand why we need a new binding for this just for semantic tokens? why can't we handle this with the AST?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants