Skip to content

Commit 43c8254

Browse files
more, keep for now
1 parent b233cfe commit 43c8254

File tree

3 files changed

+46
-9
lines changed

3 files changed

+46
-9
lines changed

pyrefly/lib/lsp/module_helpers.rs

Lines changed: 32 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ use tracing::warn;
1717
use crate::module::bundled::BundledStub;
1818
use crate::module::typeshed::typeshed;
1919
use crate::module::typeshed_third_party::typeshed_third_party;
20+
use crate::types::stdlib::Stdlib;
2021

2122
/// Convert to a path we can show to the user. The contents may not match the disk, but it has
2223
/// to be basically right.
@@ -50,9 +51,38 @@ pub fn to_real_path(path: &ModulePath) -> Option<PathBuf> {
5051
}
5152
}
5253

54+
#[allow(dead_code)]
5355
pub fn collect_symbol_def_paths(t: &Type) -> Vec<(QName, PathBuf)> {
56+
collect_symbol_def_paths_with_tuple_qname(t, None)
57+
}
58+
59+
pub fn collect_symbol_def_paths_with_stdlib(t: &Type, stdlib: &Stdlib) -> Vec<(QName, PathBuf)> {
60+
collect_symbol_def_paths_with_tuple_qname(t, Some(stdlib.tuple_object().qname().clone()))
61+
}
62+
63+
fn collect_symbol_def_paths_with_tuple_qname(
64+
t: &Type,
65+
tuple_qname: Option<QName>,
66+
) -> Vec<(QName, PathBuf)> {
5467
let mut tracked_def_locs = SmallSet::new();
55-
t.universe(&mut |t| tracked_def_locs.extend(t.qname()));
68+
t.universe(&mut |t| {
69+
if let Some(qname) = t.qname() {
70+
tracked_def_locs.insert(qname.clone());
71+
}
72+
});
73+
74+
if let Some(tuple_qname) = tuple_qname {
75+
let mut has_tuple = false;
76+
t.universe(&mut |ty| {
77+
if matches!(ty, Type::Tuple(_)) {
78+
has_tuple = true;
79+
}
80+
});
81+
if has_tuple {
82+
tracked_def_locs.insert(tuple_qname);
83+
}
84+
}
85+
5686
tracked_def_locs
5787
.into_iter()
5888
.map(|qname| {
@@ -64,7 +94,7 @@ pub fn collect_symbol_def_paths(t: &Type) -> Vec<(QName, PathBuf)> {
6494
}
6595
_ => module_path.as_path().to_path_buf(),
6696
};
67-
(qname.clone(), file_path)
97+
(qname, file_path)
6898
})
6999
.collect()
70100
}

pyrefly/lib/lsp/wasm/hover.rs

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
*/
77

88
use std::collections::HashMap;
9+
use std::path::PathBuf;
910

1011
use lsp_types::Hover;
1112
use lsp_types::HoverContents;
@@ -18,6 +19,7 @@ use pyrefly_python::docstring::parse_parameter_documentation;
1819
use pyrefly_python::ignore::Ignore;
1920
use pyrefly_python::ignore::Tool;
2021
use pyrefly_python::ignore::find_comment_start_in_line;
22+
use pyrefly_python::qname::QName;
2123
use pyrefly_python::symbol_kind::SymbolKind;
2224
use pyrefly_types::callable::Callable;
2325
use pyrefly_types::callable::Param;
@@ -33,7 +35,7 @@ use ruff_text_size::TextSize;
3335

3436
use crate::alt::answers_solver::AnswersSolver;
3537
use crate::error::error::Error;
36-
use crate::lsp::module_helpers::collect_symbol_def_paths;
38+
use crate::lsp::module_helpers::collect_symbol_def_paths_with_stdlib;
3739
use crate::state::lsp::DefinitionMetadata;
3840
use crate::state::lsp::FindDefinitionItemWithDocstring;
3941
use crate::state::lsp::FindPreference;
@@ -148,12 +150,12 @@ pub struct HoverValue {
148150
pub parameter_doc: Option<(String, String)>,
149151
pub display: Option<String>,
150152
pub show_go_to_links: bool,
153+
pub symbol_def_paths: Vec<(QName, PathBuf)>,
151154
}
152155

153156
impl HoverValue {
154157
#[cfg(not(target_arch = "wasm32"))]
155-
fn format_symbol_def_locations(t: &Type) -> Option<String> {
156-
let symbol_paths = collect_symbol_def_paths(t);
158+
fn format_symbol_def_locations(symbol_paths: &[(QName, PathBuf)]) -> Option<String> {
157159
let linked_names = symbol_paths
158160
.into_iter()
159161
.filter_map(|(qname, file_path)| {
@@ -189,7 +191,7 @@ impl HoverValue {
189191
}
190192

191193
#[cfg(target_arch = "wasm32")]
192-
fn format_symbol_def_locations(t: &Type) -> Option<String> {
194+
fn format_symbol_def_locations(_symbol_paths: &[(QName, PathBuf)]) -> Option<String> {
193195
None
194196
}
195197

@@ -222,7 +224,7 @@ impl HoverValue {
222224
.as_ref()
223225
.map_or("".to_owned(), |s| format!("{s}: "));
224226
let symbol_def_formatted = if self.show_go_to_links {
225-
HoverValue::format_symbol_def_locations(&self.type_).unwrap_or("".to_owned())
227+
HoverValue::format_symbol_def_locations(&self.symbol_def_paths).unwrap_or("".to_owned())
226228
} else {
227229
String::new()
228230
};
@@ -404,6 +406,9 @@ pub fn get_hover(
404406
}
405407
}
406408

409+
let stdlib = transaction.get_stdlib(handle);
410+
let symbol_def_paths = collect_symbol_def_paths_with_stdlib(&type_, stdlib.as_ref());
411+
407412
Some(
408413
HoverValue {
409414
kind,
@@ -413,6 +418,7 @@ pub fn get_hover(
413418
parameter_doc,
414419
display: type_display,
415420
show_go_to_links,
421+
symbol_def_paths,
416422
}
417423
.format(),
418424
)

pyrefly/lib/state/lsp.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ use crate::binding::binding::Key;
6969
use crate::config::error_kind::ErrorKind;
7070
use crate::export::exports::Export;
7171
use crate::export::exports::ExportLocation;
72-
use crate::lsp::module_helpers::collect_symbol_def_paths;
72+
use crate::lsp::module_helpers::collect_symbol_def_paths_with_stdlib;
7373
use crate::state::ide::IntermediateDefinition;
7474
use crate::state::ide::import_regular_import_edit;
7575
use crate::state::ide::insert_import_edit;
@@ -1567,7 +1567,8 @@ impl<'a> Transaction<'a> {
15671567
let type_ = self.get_type_at(handle, position);
15681568

15691569
if let Some(t) = type_ {
1570-
let symbol_def_paths = collect_symbol_def_paths(&t);
1570+
let stdlib = self.get_stdlib(handle);
1571+
let symbol_def_paths = collect_symbol_def_paths_with_stdlib(&t, stdlib.as_ref());
15711572

15721573
if !symbol_def_paths.is_empty() {
15731574
return symbol_def_paths.map(|(qname, _)| {

0 commit comments

Comments
 (0)