Skip to content

Commit 81212b4

Browse files
more, keep for now
1 parent a12d7a0 commit 81212b4

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;
@@ -32,7 +34,7 @@ use ruff_text_size::TextSize;
3234

3335
use crate::alt::answers_solver::AnswersSolver;
3436
use crate::error::error::Error;
35-
use crate::lsp::module_helpers::collect_symbol_def_paths;
37+
use crate::lsp::module_helpers::collect_symbol_def_paths_with_stdlib;
3638
use crate::state::lsp::DefinitionMetadata;
3739
use crate::state::lsp::FindDefinitionItemWithDocstring;
3840
use crate::state::lsp::FindPreference;
@@ -113,12 +115,12 @@ pub struct HoverValue {
113115
pub parameter_doc: Option<(String, String)>,
114116
pub display: Option<String>,
115117
pub show_go_to_links: bool,
118+
pub symbol_def_paths: Vec<(QName, PathBuf)>,
116119
}
117120

118121
impl HoverValue {
119122
#[cfg(not(target_arch = "wasm32"))]
120-
fn format_symbol_def_locations(t: &Type) -> Option<String> {
121-
let symbol_paths = collect_symbol_def_paths(t);
123+
fn format_symbol_def_locations(symbol_paths: &[(QName, PathBuf)]) -> Option<String> {
122124
let linked_names = symbol_paths
123125
.into_iter()
124126
.filter_map(|(qname, file_path)| {
@@ -154,7 +156,7 @@ impl HoverValue {
154156
}
155157

156158
#[cfg(target_arch = "wasm32")]
157-
fn format_symbol_def_locations(t: &Type) -> Option<String> {
159+
fn format_symbol_def_locations(_symbol_paths: &[(QName, PathBuf)]) -> Option<String> {
158160
None
159161
}
160162

@@ -187,7 +189,7 @@ impl HoverValue {
187189
.as_ref()
188190
.map_or("".to_owned(), |s| format!("{s}: "));
189191
let symbol_def_formatted = if self.show_go_to_links {
190-
HoverValue::format_symbol_def_locations(&self.type_).unwrap_or("".to_owned())
192+
HoverValue::format_symbol_def_locations(&self.symbol_def_paths).unwrap_or("".to_owned())
191193
} else {
192194
String::new()
193195
};
@@ -365,6 +367,9 @@ pub fn get_hover(
365367
}
366368
}
367369

370+
let stdlib = transaction.get_stdlib(handle);
371+
let symbol_def_paths = collect_symbol_def_paths_with_stdlib(&type_, stdlib.as_ref());
372+
368373
Some(
369374
HoverValue {
370375
kind,
@@ -374,6 +379,7 @@ pub fn get_hover(
374379
parameter_doc,
375380
display: type_display,
376381
show_go_to_links,
382+
symbol_def_paths,
377383
}
378384
.format(),
379385
)

pyrefly/lib/state/lsp.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ use crate::binding::binding::Key;
6767
use crate::config::error_kind::ErrorKind;
6868
use crate::export::exports::Export;
6969
use crate::export::exports::ExportLocation;
70-
use crate::lsp::module_helpers::collect_symbol_def_paths;
70+
use crate::lsp::module_helpers::collect_symbol_def_paths_with_stdlib;
7171
use crate::state::ide::IntermediateDefinition;
7272
use crate::state::ide::import_regular_import_edit;
7373
use crate::state::ide::insert_import_edit;
@@ -1565,7 +1565,8 @@ impl<'a> Transaction<'a> {
15651565
let type_ = self.get_type_at(handle, position);
15661566

15671567
if let Some(t) = type_ {
1568-
let symbol_def_paths = collect_symbol_def_paths(&t);
1568+
let stdlib = self.get_stdlib(handle);
1569+
let symbol_def_paths = collect_symbol_def_paths_with_stdlib(&t, stdlib.as_ref());
15691570

15701571
if !symbol_def_paths.is_empty() {
15711572
return symbol_def_paths.map(|(qname, _)| {

0 commit comments

Comments
 (0)