Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 16 additions & 6 deletions crates/pyrefly_types/src/display.rs
Original file line number Diff line number Diff line change
Expand Up @@ -753,7 +753,9 @@ impl<'a> TypeDisplayContext<'a> {
output.write_builtin("LiteralString", qname)
}
Type::Callable(c) => {
if self.lsp_display_mode == LspDisplayMode::Hover && is_toplevel {
// Hover output should be readable even when callables appear inside unions
// (e.g. constructor display that unions __new__ and __init__).
if self.lsp_display_mode == LspDisplayMode::Hover {
c.fmt_with_type_with_newlines(output, &|t, o| {
self.fmt_helper_generic(t, false, o)
})
Expand Down Expand Up @@ -824,8 +826,16 @@ impl<'a> TypeDisplayContext<'a> {
output.write_str(": ...")
}
}
_ => signature
.fmt_with_type(output, &|t, o| self.fmt_helper_generic(t, false, o)),
_ => {
if self.lsp_display_mode == LspDisplayMode::Hover {
signature.fmt_with_type_with_newlines(output, &|t, o| {
self.fmt_helper_generic(t, false, o)
})
} else {
signature
.fmt_with_type(output, &|t, o| self.fmt_helper_generic(t, false, o))
}
}
}
}
Type::Overload(overload) => {
Expand All @@ -838,8 +848,8 @@ impl<'a> TypeDisplayContext<'a> {
}
Ok(())
} else {
let multiline =
is_toplevel && self.lsp_display_mode != LspDisplayMode::ProvideType;
let multiline = self.lsp_display_mode == LspDisplayMode::Hover
|| (is_toplevel && self.lsp_display_mode != LspDisplayMode::ProvideType);
if multiline {
output.write_str("Overload[\n ")?;
} else {
Expand Down Expand Up @@ -2068,7 +2078,7 @@ pub mod tests {
ctx.set_lsp_display_mode(LspDisplayMode::Hover);
assert_eq!(
ctx.display(&tuple).to_string(),
"tuple[(hello: None, *, world: None) -> None]"
"tuple[(\n hello: None,\n *,\n world: None\n) -> None]"
);
}

Expand Down
21 changes: 21 additions & 0 deletions pyrefly/lib/test/lsp/hover.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1102,6 +1102,27 @@ Widget docstring"#
);
}

#[test]
fn hover_on_dict_constructor_is_multiline() {
let code = r#"
x: dict[str, int]
# ^
"#;
let report = get_batched_lsp_operations_report(&[("main", code)], get_test_report);
assert!(
report.contains("(class) dict:"),
"Expected dict hover to show class constructor, got: {report}"
);
assert!(
report.contains("Overload[\n "),
"Expected overload constructor signatures to be multi-line in hover, got: {report}"
);
assert!(
report.contains("(\n "),
"Expected callable constructor signatures to include newlines in hover, got: {report}"
);
}

#[test]
fn hover_on_first_component_of_multi_part_import() {
let mymod_init = r#"# mymod/__init__.py
Expand Down
Loading