aboutsummaryrefslogtreecommitdiff
path: root/crates/hir
diff options
context:
space:
mode:
Diffstat (limited to 'crates/hir')
-rw-r--r--crates/hir/src/lib.rs36
1 files changed, 32 insertions, 4 deletions
diff --git a/crates/hir/src/lib.rs b/crates/hir/src/lib.rs
index 5da6a0340..eb7865c84 100644
--- a/crates/hir/src/lib.rs
+++ b/crates/hir/src/lib.rs
@@ -852,6 +852,7 @@ impl Function {
852 }) 852 })
853 .collect() 853 .collect()
854 } 854 }
855
855 pub fn method_params(self, db: &dyn HirDatabase) -> Option<Vec<Param>> { 856 pub fn method_params(self, db: &dyn HirDatabase) -> Option<Vec<Param>> {
856 if self.self_param(db).is_none() { 857 if self.self_param(db).is_none() {
857 return None; 858 return None;
@@ -909,7 +910,7 @@ impl From<hir_ty::Mutability> for Access {
909 } 910 }
910} 911}
911 912
912#[derive(Debug)] 913#[derive(Clone, Debug)]
913pub struct Param { 914pub struct Param {
914 func: Function, 915 func: Function,
915 /// The index in parameter list, including self parameter. 916 /// The index in parameter list, including self parameter.
@@ -922,13 +923,25 @@ impl Param {
922 &self.ty 923 &self.ty
923 } 924 }
924 925
926 pub fn as_local(&self, db: &dyn HirDatabase) -> Local {
927 let parent = DefWithBodyId::FunctionId(self.func.into());
928 let body = db.body(parent);
929 Local { parent, pat_id: body.params[self.idx] }
930 }
931
925 pub fn pattern_source(&self, db: &dyn HirDatabase) -> Option<ast::Pat> { 932 pub fn pattern_source(&self, db: &dyn HirDatabase) -> Option<ast::Pat> {
926 let params = self.func.source(db)?.value.param_list()?; 933 self.source(db).and_then(|p| p.value.pat())
934 }
935
936 pub fn source(&self, db: &dyn HirDatabase) -> Option<InFile<ast::Param>> {
937 let InFile { file_id, value } = self.func.source(db)?;
938 let params = value.param_list()?;
927 if params.self_param().is_some() { 939 if params.self_param().is_some() {
928 params.params().nth(self.idx.checked_sub(1)?)?.pat() 940 params.params().nth(self.idx.checked_sub(1)?)
929 } else { 941 } else {
930 params.params().nth(self.idx)?.pat() 942 params.params().nth(self.idx)
931 } 943 }
944 .map(|value| InFile { file_id, value })
932 } 945 }
933} 946}
934 947
@@ -960,6 +973,14 @@ impl SelfParam {
960 Access::Owned => "self", 973 Access::Owned => "self",
961 } 974 }
962 } 975 }
976
977 pub fn source(&self, db: &dyn HirDatabase) -> Option<InFile<ast::SelfParam>> {
978 let InFile { file_id, value } = Function::from(self.func).source(db)?;
979 value
980 .param_list()
981 .and_then(|params| params.self_param())
982 .map(|value| InFile { file_id, value })
983 }
963} 984}
964 985
965impl HasVisibility for Function { 986impl HasVisibility for Function {
@@ -1335,6 +1356,13 @@ impl Local {
1335 } 1356 }
1336 } 1357 }
1337 1358
1359 pub fn as_self_param(self, db: &dyn HirDatabase) -> Option<SelfParam> {
1360 match self.parent {
1361 DefWithBodyId::FunctionId(func) if self.is_self(db) => Some(SelfParam { func }),
1362 _ => None,
1363 }
1364 }
1365
1338 // FIXME: why is this an option? It shouldn't be? 1366 // FIXME: why is this an option? It shouldn't be?
1339 pub fn name(self, db: &dyn HirDatabase) -> Option<Name> { 1367 pub fn name(self, db: &dyn HirDatabase) -> Option<Name> {
1340 let body = db.body(self.parent); 1368 let body = db.body(self.parent);