Skip to content

Commit ab31d30

Browse files
fix
1 parent 8a49437 commit ab31d30

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
@@ -697,6 +697,47 @@ impl<'a> ClassDisplayContext<'a> {
697697
}
698698
}
699699

700+
#[derive(Debug, Clone)]
701+
pub struct HoverCodeSnippet {
702+
pub heading: Option<String>,
703+
pub body: String,
704+
pub default_kind_label: Option<&'static str>,
705+
}
706+
707+
pub fn format_hover_code_snippet(
708+
type_: &Type,
709+
name: Option<&str>,
710+
display: String,
711+
) -> HoverCodeSnippet {
712+
if type_.is_function_type() {
713+
if let Some(name) = name {
714+
let trimmed = display.trim_start();
715+
let body = if trimmed.starts_with('(') {
716+
format!("def {}{}: ...", name, trimmed)
717+
} else {
718+
display
719+
};
720+
HoverCodeSnippet {
721+
heading: Some(name.to_owned()),
722+
body,
723+
default_kind_label: Some("(function) "),
724+
}
725+
} else {
726+
HoverCodeSnippet {
727+
heading: None,
728+
body: display,
729+
default_kind_label: Some("(function) "),
730+
}
731+
}
732+
} else {
733+
HoverCodeSnippet {
734+
heading: None,
735+
body: display,
736+
default_kind_label: None,
737+
}
738+
}
739+
}
740+
700741
#[cfg(test)]
701742
pub mod tests {
702743
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)