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
31 changes: 28 additions & 3 deletions pyrefly/lib/alt/answers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,7 @@ use crate::types::class::ClassFields;
use crate::types::env::TypeEnv;
use crate::types::equality::TypeEq;
use crate::types::equality::TypeEqCtx;
use crate::types::function::FuncDefId;
use crate::types::heap::TypeHeap;
use crate::types::stdlib::Stdlib;
use crate::types::types::Forall;
Expand Down Expand Up @@ -173,13 +174,15 @@ impl VisitMut<Type> for Traces {
let OverloadTrace {
callable,
tparams: _,
..
} = trace;
callable.visit_mut(f);
}
OverloadedCallee::Candidates { all, closest, .. } => {
for OverloadTrace {
callable,
tparams: _,
..
} in all.iter_mut().chain(std::iter::once(closest))
{
callable.visit_mut(f);
Expand All @@ -194,11 +197,20 @@ impl VisitMut<Type> for Traces {
pub struct OverloadTrace {
callable: Callable,
tparams: Option<Arc<TParams>>,
func_def_id: Option<Arc<FuncDefId>>,
}

impl OverloadTrace {
pub(crate) fn new(callable: Callable, tparams: Option<Arc<TParams>>) -> Self {
Self { callable, tparams }
pub(crate) fn new(
callable: Callable,
tparams: Option<Arc<TParams>>,
func_def_id: Option<Arc<FuncDefId>>,
) -> Self {
Self {
callable,
tparams,
func_def_id,
}
}

fn as_type(&self) -> Type {
Expand Down Expand Up @@ -1888,6 +1900,19 @@ impl Answers {
}
}

/// The source definition chosen for a successfully resolved overload call.
pub fn get_chosen_overload_func_def_id(&self, range: TextRange) -> Option<Arc<FuncDefId>> {
let lock = self.trace.as_ref()?.lock();
match lock.overloaded_callees.get(&range)? {
OverloadedCallee::Candidates {
closest,
is_closest_chosen: true,
..
} => closest.func_def_id.clone(),
_ => None,
}
}

/// Returns all the overload, and the index of a chosen one
pub fn get_all_overload_trace(
&self,
Expand Down Expand Up @@ -1937,7 +1962,7 @@ impl<'ctx, 'answer, Ans: LookupAnswer> AnswersSolver<'ctx, 'answer, Ans> {
self.trace_state().record_resolved_trace(
loc,
OverloadedCallee::Resolved {
callable: OverloadTrace::new(callable, None),
callable: OverloadTrace::new(callable, None, None),
},
);
}
Expand Down
12 changes: 11 additions & 1 deletion pyrefly/lib/alt/overload.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
*/

use std::cmp::max;
use std::sync::Arc;

use itertools::Either;
use itertools::Itertools;
Expand Down Expand Up @@ -520,7 +521,16 @@ impl<'ctx, 'answer, Ans: LookupAnswer> AnswersSolver<'ctx, 'answer, Ans> {
.as_ref()
.filter(|tparams| !tparams.is_empty())
.cloned();
OverloadTrace::new(target.1.signature.clone(), tparams)
OverloadTrace::new(
target.1.signature.clone(),
tparams,
target
.1
.metadata
.kind
.as_func_def_id()
.map(|func_id| Arc::new(func_id.clone())),
)
};
let all_overload_traces = overloads.iter().map(&mut overload_trace).collect();
let closest_overload_trace = overload_trace(closest_overload.func);
Expand Down
91 changes: 87 additions & 4 deletions pyrefly/lib/lsp/wasm/hover.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
// @lint-ignore-every SPELL

use std::cell::LazyCell;
use std::sync::Arc;
use std::sync::LazyLock;

use dupe::Dupe;
Expand Down Expand Up @@ -37,6 +38,7 @@ use pyrefly_types::callable::Required;
use pyrefly_types::class::Class;
use pyrefly_types::class::ClassType;
use pyrefly_types::display::LspDisplayMode;
use pyrefly_types::function::FuncDefId;
use pyrefly_types::type_var::Variance;
use pyrefly_types::types::Type;
use pyrefly_util::absolutize::Absolutize as _;
Expand Down Expand Up @@ -1487,6 +1489,72 @@ fn nonempty_docstring(docstring: Docstring) -> Option<Docstring> {
docstring.has_content().then_some(docstring)
}

fn docstring_for_func_def(
transaction: &Transaction<'_>,
handle: &Handle,
func_id: &FuncDefId,
) -> Option<Docstring> {
let definition_handle = transaction.imported_handle(
handle,
func_id.qname.module_name(),
func_id.qname.module_path().dupe(),
);
let ast = transaction.get_ast(&definition_handle)?;
let function = Ast::locate_node(ast.as_ref(), func_id.qname.range().start())
.into_iter()
.find_map(|node| match node {
AnyNodeRef::StmtFunctionDef(function)
if function.name.range == func_id.qname.range() =>
{
Some(function)
}
_ => None,
})?;
let range = function_doc_ranges(function).docstring()?;
Some(Docstring(
range,
transaction.get_module_info(&definition_handle)?,
))
}

/// Find the source definition selected by the overload call under the cursor.
fn selected_overload_func_def(
transaction: &Transaction<'_>,
handle: &Handle,
ast: &ModModule,
position: TextSize,
) -> Option<Arc<FuncDefId>> {
let (_, arguments_range, on_name) = callee_at_position(transaction, handle, ast, position)?;
if !on_name {
return None;
}
transaction
.get_answers(handle)?
.get_chosen_overload_func_def_id(arguments_range)
}

fn overload_implementation_docstring(
transaction: &Transaction<'_>,
handle: &Handle,
ast: &ModModule,
position: TextSize,
) -> Option<Docstring> {
let (callee_range, _, on_name) = callee_at_position(transaction, handle, ast, position)?;
if !on_name {
return None;
}
let func_id = transaction
.get_answers(handle)?
.get_type_trace(callee_range)
.and_then(|type_| {
type_
.toplevel_func_metadata()
.and_then(|metadata| metadata.kind.as_func_def_id())
.cloned()
})?;
docstring_for_func_def(transaction, handle, &func_id)
}

/// Class docstring for a constructor call whose `__init__` or `__new__` has none.
///
/// Eligibility comes from the callee's class type. `find_definition` with dunder
Expand Down Expand Up @@ -1797,10 +1865,25 @@ pub fn get_hover_with_verbosity(
} else {
(None, fallback_name_from_type, None, Vec::new())
};
let docstring = interface_docstring
.and_then(nonempty_docstring)
.or_else(|| fallback_docstring.and_then(nonempty_docstring))
.or_else(|| docstring_for_class_object_type(transaction, handle, &type_));
let selected_overload = ast
.as_deref()
.and_then(|ast| selected_overload_func_def(transaction, handle, ast, position));
let docstring = if let Some(func_id) = selected_overload {
docstring_for_func_def(transaction, handle, &func_id)
.and_then(nonempty_docstring)
.or_else(|| {
ast.as_deref()
.and_then(|ast| {
overload_implementation_docstring(transaction, handle, ast, position)
})
.and_then(nonempty_docstring)
})
} else {
interface_docstring
.and_then(nonempty_docstring)
.or_else(|| fallback_docstring.and_then(nonempty_docstring))
.or_else(|| docstring_for_class_object_type(transaction, handle, &type_))
};

let docstring = if hover_constructor_dunder {
docstring.or_else(|| class_docstring_for_constructor(transaction, handle, position))
Expand Down
Loading
Loading