Skip to content

Commit 6a3cde3

Browse files
committed
Highlight binary expressions on operator hover
Return the enclosing binary expression range with operator hover responses so editors highlight the complete operation instead of only showing hover content.
1 parent 12064f9 commit 6a3cde3

2 files changed

Lines changed: 29 additions & 2 deletions

File tree

‎pyrefly/lib/lsp/wasm/hover.rs‎

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1488,8 +1488,8 @@ pub fn get_hover_with_verbosity(
14881488
return keyword_hover;
14891489
};
14901490

1491-
// `a and b and c` is a single flat BoolOp, so hovering any operator in the
1492-
// chain highlights the whole expression. `not` highlights its unary expression.
1491+
// Operators highlight their enclosing expression. `a and b and c` is a single flat
1492+
// BoolOp, so hovering any operator in the chain highlights the whole expression.
14931493
let range = ast
14941494
.as_deref()
14951495
.zip(module_info.as_ref())
@@ -1501,6 +1501,7 @@ pub fn get_hover_with_verbosity(
15011501
AnyNodeRef::ExprBoolOp(bool_op) => {
15021502
Some(module_info.to_lsp_range(bool_op.range()))
15031503
}
1504+
AnyNodeRef::ExprBinOp(bin_op) => Some(module_info.to_lsp_range(bin_op.range())),
15041505
AnyNodeRef::ExprUnaryOp(unary_op) if unary_op.op == UnaryOp::Not => {
15051506
Some(module_info.to_lsp_range(unary_op.range()))
15061507
}

‎pyrefly/lib/test/lsp/hover.rs‎

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2606,6 +2606,32 @@ y = 1 or 2
26062606
);
26072607
}
26082608

2609+
#[test]
2610+
fn hover_over_binary_operator_highlights_binary_expression() {
2611+
let code = r#"
2612+
x = 1 | 2
2613+
# ^
2614+
"#;
2615+
let mut test_env = TestEnv::new();
2616+
test_env.add("main", code);
2617+
let (state, handle) = test_env
2618+
.with_default_require_level(Require::Exports)
2619+
.to_state();
2620+
let handle = handle("main");
2621+
let position = extract_cursors_for_test(code)[0];
2622+
let range = match get_hover(&state.transaction(), &handle, position, false) {
2623+
Some(hover) => hover.range,
2624+
None => panic!("Expected hover result for binary operator"),
2625+
};
2626+
assert_eq!(
2627+
range,
2628+
Some(Range {
2629+
start: Position::new(1, 4),
2630+
end: Position::new(1, 9),
2631+
})
2632+
);
2633+
}
2634+
26092635
#[test]
26102636
fn hover_over_not_operator_highlights_unary_expression() {
26112637
let code = r#"

0 commit comments

Comments
 (0)