feature: Make replace_or_with_or_else assists more generally applicable

This commit is contained in:
Lukas Wirth 2023-03-06 22:17:16 +01:00
parent e6ba791dce
commit 199bc82ce8
8 changed files with 397 additions and 411 deletions

View file

@ -1676,6 +1676,10 @@ impl Function {
.collect()
}
pub fn num_params(self, db: &dyn HirDatabase) -> usize {
db.function_data(self.id).params.len()
}
pub fn method_params(self, db: &dyn HirDatabase) -> Option<Vec<Param>> {
if self.self_param(db).is_none() {
return None;
@ -3846,11 +3850,13 @@ impl Type {
}
}
// FIXME: Document this
#[derive(Debug)]
pub struct Callable {
ty: Type,
sig: CallableSig,
callee: Callee,
/// Whether this is a method that was called with method call syntax.
pub(crate) is_bound_method: bool,
}
@ -3884,14 +3890,14 @@ impl Callable {
Other => CallableKind::Other,
}
}
pub fn receiver_param(&self, db: &dyn HirDatabase) -> Option<ast::SelfParam> {
pub fn receiver_param(&self, db: &dyn HirDatabase) -> Option<(ast::SelfParam, Type)> {
let func = match self.callee {
Callee::Def(CallableDefId::FunctionId(it)) if self.is_bound_method => it,
_ => return None,
};
let src = func.lookup(db.upcast()).source(db.upcast());
let param_list = src.value.param_list()?;
param_list.self_param()
Some((param_list.self_param()?, self.ty.derived(self.sig.params()[0].clone())))
}
pub fn n_params(&self) -> usize {
self.sig.params().len() - if self.is_bound_method { 1 } else { 0 }