Skip to content

fix go-to definition / hover includes the lhs in operators #1517#3022

Open
asukaminato0721 wants to merge 2 commits intofacebook:mainfrom
asukaminato0721:1517
Open

fix go-to definition / hover includes the lhs in operators #1517#3022
asukaminato0721 wants to merge 2 commits intofacebook:mainfrom
asukaminato0721:1517

Conversation

@asukaminato0721
Copy link
Copy Markdown
Contributor

@asukaminato0721 asukaminato0721 commented Apr 5, 2026

Summary

Fixes #1517

The operator-definition path now only activates when the cursor is actually in the operator gap, instead of anywhere inside the enclosing expression, so hovering or go-to-def on the left 1 in 1 + 1 no longer resolves int.__add__.

Test Plan

update test

@meta-cla meta-cla bot added the cla signed label Apr 5, 2026
@github-actions github-actions bot added the size/m label Apr 5, 2026
@asukaminato0721 asukaminato0721 marked this pull request as ready for review April 5, 2026 14:59
Copilot AI review requested due to automatic review settings April 5, 2026 14:59
Copy link
Copy Markdown

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull request overview

This PR fixes an LSP bug where go-to-definition and hover could incorrectly treat the left-hand operand of an operator expression as if the cursor were on the operator itself (e.g., hovering the 1 in 1 + 1 showing the operator dunder).

Changes:

  • Tighten operator “cursor hit-testing” by requiring the cursor to be positioned between operands (or in the operator region) before resolving operator dunder definitions.
  • Extend operator handling for chained comparisons to select the correct left/right operands for the specific operator under the cursor.
  • Add regression tests for hover and definition on a binop LHS literal.

Reviewed changes

Copilot reviewed 3 out of 3 changed files in this pull request and generated no comments.

File Description
pyrefly/lib/state/lsp.rs Adds cursor-position gating for operator dunder/definition resolution to avoid matching operands as operators.
pyrefly/lib/test/lsp/hover.rs Adds hover regression test ensuring LHS literal hover shows Literal[1], not an operator dunder.
pyrefly/lib/test/lsp/definition.rs Adds go-to-definition regression test ensuring LHS literal doesn’t resolve to an operator definition.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

@github-actions github-actions bot added size/m and removed size/m labels Apr 5, 2026
6 | c[0]
^
```python
(attribute) __getitem__: Literal[0]
Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

can't fix this... i guess it's another issue

@Arths17
Copy link
Copy Markdown
Contributor

Arths17 commented Apr 5, 2026

Hey @asukaminato072, I was just looking over this. Resolving int.add when hovering an LHS literal was definitely an annoying bug for users, so thanks for that

and for the subscript range logic for c[0] is definitely a separate beast compared to binary operators. It's better to keep this PR focused on fixing the #1517 binop regression rather than expanding the scope.

@github-actions
Copy link
Copy Markdown

github-actions bot commented Apr 7, 2026

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

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.

go-to definition / hover includes the lhs in operators

3 participants