From 638dcac092c130aa817d3ca94d3ed743a6d42938 Mon Sep 17 00:00:00 2001 From: Lukas Wirth Date: Tue, 23 Mar 2021 16:50:36 +0100 Subject: Make more use of the HIR in rename::rename_to_self --- crates/hir/src/lib.rs | 21 +++++++++++++++++---- 1 file changed, 17 insertions(+), 4 deletions(-) (limited to 'crates/hir/src') diff --git a/crates/hir/src/lib.rs b/crates/hir/src/lib.rs index 5da6a0340..bdc1ad852 100644 --- a/crates/hir/src/lib.rs +++ b/crates/hir/src/lib.rs @@ -852,6 +852,7 @@ impl Function { }) .collect() } + pub fn method_params(self, db: &dyn HirDatabase) -> Option> { if self.self_param(db).is_none() { return None; @@ -909,7 +910,7 @@ impl From for Access { } } -#[derive(Debug)] +#[derive(Clone, Debug)] pub struct Param { func: Function, /// The index in parameter list, including self parameter. @@ -922,13 +923,25 @@ impl Param { &self.ty } + pub fn as_local(&self, db: &dyn HirDatabase) -> Local { + let parent = DefWithBodyId::FunctionId(self.func.into()); + let body = db.body(parent); + Local { parent, pat_id: body.params[self.idx] } + } + pub fn pattern_source(&self, db: &dyn HirDatabase) -> Option { - let params = self.func.source(db)?.value.param_list()?; + self.source(db).and_then(|p| p.value.pat()) + } + + pub fn source(&self, db: &dyn HirDatabase) -> Option> { + let InFile { file_id, value } = self.func.source(db)?; + let params = value.param_list()?; if params.self_param().is_some() { - params.params().nth(self.idx.checked_sub(1)?)?.pat() + params.params().nth(self.idx.checked_sub(1)?) } else { - params.params().nth(self.idx)?.pat() + params.params().nth(self.idx) } + .map(|value| InFile { file_id, value }) } } -- cgit v1.2.3