fix go-to definition / hover includes the lhs in operators #1517#3022
fix go-to definition / hover includes the lhs in operators #1517#3022asukaminato0721 wants to merge 2 commits intofacebook:mainfrom
Conversation
There was a problem hiding this comment.
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.
| 6 | c[0] | ||
| ^ | ||
| ```python | ||
| (attribute) __getitem__: Literal[0] |
There was a problem hiding this comment.
can't fix this... i guess it's another issue
|
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. |
|
According to mypy_primer, this change doesn't affect type check results on a corpus of open source code. ✅ |
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