Skip to content

Commit 1ae78da

Browse files
fix
1 parent b1b1822 commit 1ae78da

File tree

2 files changed

+60
-28
lines changed

2 files changed

+60
-28
lines changed

crates/pyrefly_types/src/display.rs

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -604,6 +604,47 @@ impl<'a> ClassDisplayContext<'a> {
604604
}
605605
}
606606

607+
#[derive(Debug, Clone)]
608+
pub struct HoverCodeSnippet {
609+
pub heading: Option<String>,
610+
pub body: String,
611+
pub default_kind_label: Option<&'static str>,
612+
}
613+
614+
pub fn format_hover_code_snippet(
615+
type_: &Type,
616+
name: Option<&str>,
617+
display: String,
618+
) -> HoverCodeSnippet {
619+
if type_.is_function_type() {
620+
if let Some(name) = name {
621+
let trimmed = display.trim_start();
622+
let body = if trimmed.starts_with('(') {
623+
format!("def {}{}: ...", name, trimmed)
624+
} else {
625+
display
626+
};
627+
HoverCodeSnippet {
628+
heading: Some(name.to_owned()),
629+
body,
630+
default_kind_label: Some("(function) "),
631+
}
632+
} else {
633+
HoverCodeSnippet {
634+
heading: None,
635+
body: display,
636+
default_kind_label: Some("(function) "),
637+
}
638+
}
639+
} else {
640+
HoverCodeSnippet {
641+
heading: None,
642+
body: display,
643+
default_kind_label: None,
644+
}
645+
}
646+
}
647+
607648
#[cfg(test)]
608649
pub mod tests {
609650
use std::path::PathBuf;

pyrefly/lib/lsp/wasm/hover.rs

Lines changed: 19 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ use pyrefly_types::callable::Param;
2121
use pyrefly_types::callable::ParamList;
2222
use pyrefly_types::callable::Params;
2323
use pyrefly_types::callable::Required;
24+
use pyrefly_types::display::format_hover_code_snippet;
2425
use pyrefly_types::types::BoundMethodType;
2526
use pyrefly_types::types::Forallable;
2627
use pyrefly_types::types::Type;
@@ -162,13 +163,6 @@ impl HoverValue {
162163
|| "".to_owned(),
163164
|content| format!("\n---\n{}", content.trim()),
164165
);
165-
let mut kind_formatted = self.kind.map_or("".to_owned(), |kind| {
166-
format!("{} ", kind.display_for_hover())
167-
});
168-
let name_formatted = self
169-
.name
170-
.as_ref()
171-
.map_or("".to_owned(), |s| format!("{s}: "));
172166
let symbol_def_formatted = if self.show_go_to_links {
173167
HoverValue::format_symbol_def_locations(&self.type_).unwrap_or("".to_owned())
174168
} else {
@@ -178,35 +172,32 @@ impl HoverValue {
178172
.display
179173
.clone()
180174
.unwrap_or_else(|| self.type_.as_hover_string());
181-
182-
let is_function = self.type_.is_function_type();
183-
if is_function && kind_formatted.trim().is_empty() {
184-
kind_formatted = "(function) ".to_owned();
185-
}
186-
let trimmed_type_display = type_display.trim_start();
187-
let needs_def_wrapper = trimmed_type_display.starts_with('(');
188-
let (heading, body) = if is_function {
189-
let function_name = self.name.clone().unwrap_or_else(|| "function".to_owned());
190-
let heading = format!("{}{}\n", kind_formatted, function_name);
191-
let body = if needs_def_wrapper {
192-
format!("def {}{}: ...", function_name, trimmed_type_display)
193-
} else {
194-
type_display
195-
};
196-
(heading, body)
175+
let snippet = format_hover_code_snippet(&self.type_, self.name.as_deref(), type_display);
176+
let kind_formatted = self.kind.map_or_else(
177+
|| {
178+
snippet
179+
.default_kind_label
180+
.map(str::to_owned)
181+
.unwrap_or_default()
182+
},
183+
|kind| format!("{} ", kind.display_for_hover()),
184+
);
185+
let name_formatted = self
186+
.name
187+
.as_ref()
188+
.map_or("".to_owned(), |s| format!("{s}: "));
189+
let heading = if let Some(callable_heading) = snippet.heading.as_ref() {
190+
format!("{}{}\n", kind_formatted, callable_heading)
197191
} else {
198-
(
199-
format!("{}{}", kind_formatted, name_formatted),
200-
type_display,
201-
)
192+
format!("{}{}", kind_formatted, name_formatted)
202193
};
203194

204195
Hover {
205196
contents: HoverContents::Markup(MarkupContent {
206197
kind: MarkupKind::Markdown,
207198
value: format!(
208199
"```python\n{}{}\n```{}{}",
209-
heading, body, docstring_formatted, symbol_def_formatted
200+
heading, snippet.body, docstring_formatted, symbol_def_formatted
210201
),
211202
}),
212203
range: None,

0 commit comments

Comments
 (0)