Impl HirDisplay for function hover message

This commit is contained in:
oxalica 2021-03-14 20:03:39 +08:00
parent 2bb8956a10
commit ef416e0154
No known key found for this signature in database
GPG key ID: CED392DE0C483D00
5 changed files with 462 additions and 15 deletions

View file

@ -822,7 +822,8 @@ impl Function {
db.function_data(self.id)
.params
.iter()
.map(|type_ref| {
.enumerate()
.map(|(idx, type_ref)| {
let ty = Type {
krate,
ty: InEnvironment {
@ -830,7 +831,7 @@ impl Function {
environment: environment.clone(),
},
};
Param { ty }
Param { func: self, ty, idx }
})
.collect()
}
@ -893,6 +894,9 @@ impl From<hir_ty::Mutability> for Access {
#[derive(Debug)]
pub struct Param {
func: Function,
/// The index in parameter list, including self parameter.
idx: usize,
ty: Type,
}
@ -900,6 +904,15 @@ impl Param {
pub fn ty(&self) -> &Type {
&self.ty
}
pub fn pattern_source(&self, db: &dyn HirDatabase) -> Option<ast::Pat> {
let params = self.func.source(db)?.value.param_list()?;
if params.self_param().is_some() {
params.params().nth(self.idx.checked_sub(1)?)?.pat()
} else {
params.params().nth(self.idx)?.pat()
}
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]