Skip to content

Commit 643560f

Browse files
fix
1 parent 2662ff6 commit 643560f

4 files changed

Lines changed: 25 additions & 18 deletions

File tree

pyrefly/lib/lsp/wasm/hover.rs

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -549,13 +549,7 @@ pub fn get_hover(
549549
}
550550

551551
let fallback_name_from_type = fallback_hover_name_from_type(&type_);
552-
let (kind, name, docstring_range, module) = if let Some(FindDefinitionItemWithDocstring {
553-
metadata,
554-
definition_range: definition_location,
555-
module,
556-
docstring_range,
557-
display_name,
558-
}) = transaction
552+
let hover_definitions = transaction
559553
.find_definition(
560554
handle,
561555
position,
@@ -565,7 +559,14 @@ pub fn get_hover(
565559
},
566560
)
567561
.map(Vec1::into_vec)
568-
.unwrap_or_default()
562+
.unwrap_or_default();
563+
let (kind, name, docstring_range, module) = if let Some(FindDefinitionItemWithDocstring {
564+
metadata,
565+
definition_range: definition_location,
566+
module,
567+
docstring_range,
568+
display_name,
569+
}) = hover_definitions
569570
// TODO: handle more than 1 definition
570571
.into_iter()
571572
.next()

pyrefly/lib/state/lsp.rs

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1596,6 +1596,9 @@ impl<'a> Transaction<'a> {
15961596
position: TextSize,
15971597
covering_nodes: &[AnyNodeRef],
15981598
) -> Result<Option<(Type, Name)>, EmptyResponseReason> {
1599+
let module = self
1600+
.get_module_info(handle)
1601+
.ok_or(EmptyResponseReason::ModuleInfoNotFound)?;
15991602
// Look up the type of an expression, distinguishing "no answers"
16001603
// from "answers available but no type trace for this range."
16011604
let type_at = |range: TextRange| -> Result<Type, EmptyResponseReason> {
@@ -1680,11 +1683,18 @@ impl<'a> Transaction<'a> {
16801683
}))
16811684
}
16821685
AnyNodeRef::ExprSubscript(subscript) => {
1683-
if !Self::position_is_between(
1684-
position,
1686+
let between = TextRange::new(
16851687
subscript.value.range().end(),
16861688
subscript.slice.range().start(),
1687-
) {
1689+
);
1690+
let suffix = module.code_at(TextRange::new(
1691+
subscript.value.range().end(),
1692+
subscript.range().end(),
1693+
));
1694+
if position >= subscript.slice.range().start()
1695+
|| !between.contains(position)
1696+
|| !suffix.chars().next().is_some_and(char::is_whitespace)
1697+
{
16881698
return None;
16891699
}
16901700
let dunder_name = match subscript.ctx {

pyrefly/lib/test/lsp/definition.rs

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1203,15 +1203,11 @@ Definition Result:
12031203
12041204
25 | dict["foo"]
12051205
^
1206-
Definition Result:
1207-
3632 | def __getitem__(self, key: _KT, /) -> _VT:
1208-
^^^^^^^^^^^
1206+
Definition Result: None
12091207
12101208
27 | dict["bar"]
12111209
^
1212-
Definition Result:
1213-
3632 | def __getitem__(self, key: _KT, /) -> _VT:
1214-
^^^^^^^^^^^
1210+
Definition Result: None
12151211
"#
12161212
.trim(),
12171213
report.trim(),

pyrefly/lib/test/lsp/hover.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -697,7 +697,7 @@ c[0]
697697
6 | c[0]
698698
^
699699
```python
700-
(attribute) __getitem__: Literal[0]
700+
Literal[0]
701701
```
702702
"#
703703
.trim(),

0 commit comments

Comments
 (0)