diff options
Diffstat (limited to 'crates/ra_hir_ty/src/utils.rs')
-rw-r--r-- | crates/ra_hir_ty/src/utils.rs | 83 |
1 files changed, 82 insertions, 1 deletions
diff --git a/crates/ra_hir_ty/src/utils.rs b/crates/ra_hir_ty/src/utils.rs index e4ba890ef..936cfe25e 100644 --- a/crates/ra_hir_ty/src/utils.rs +++ b/crates/ra_hir_ty/src/utils.rs | |||
@@ -5,9 +5,10 @@ use std::sync::Arc; | |||
5 | use hir_def::{ | 5 | use hir_def::{ |
6 | adt::VariantData, | 6 | adt::VariantData, |
7 | db::DefDatabase, | 7 | db::DefDatabase, |
8 | generics::{GenericParamData, GenericParams}, | ||
8 | resolver::{HasResolver, TypeNs}, | 9 | resolver::{HasResolver, TypeNs}, |
9 | type_ref::TypeRef, | 10 | type_ref::TypeRef, |
10 | TraitId, TypeAliasId, VariantId, | 11 | ContainerId, GenericDefId, GenericParamId, Lookup, TraitId, TypeAliasId, VariantId, |
11 | }; | 12 | }; |
12 | use hir_expand::name::{self, Name}; | 13 | use hir_expand::name::{self, Name}; |
13 | 14 | ||
@@ -82,3 +83,83 @@ pub(crate) fn make_mut_slice<T: Clone>(a: &mut Arc<[T]>) -> &mut [T] { | |||
82 | } | 83 | } |
83 | Arc::get_mut(a).unwrap() | 84 | Arc::get_mut(a).unwrap() |
84 | } | 85 | } |
86 | |||
87 | pub(crate) fn generics(db: &impl DefDatabase, def: GenericDefId) -> Generics { | ||
88 | let parent_generics = parent_generic_def(db, def).map(|def| Box::new(generics(db, def))); | ||
89 | Generics { def, params: db.generic_params(def), parent_generics } | ||
90 | } | ||
91 | |||
92 | pub(crate) struct Generics { | ||
93 | def: GenericDefId, | ||
94 | pub(crate) params: Arc<GenericParams>, | ||
95 | parent_generics: Option<Box<Generics>>, | ||
96 | } | ||
97 | |||
98 | impl Generics { | ||
99 | pub(crate) fn iter<'a>(&'a self) -> impl Iterator<Item = (u32, &'a GenericParamData)> + 'a { | ||
100 | self.parent_generics | ||
101 | .as_ref() | ||
102 | .into_iter() | ||
103 | .flat_map(|it| it.params.params.iter()) | ||
104 | .chain(self.params.params.iter()) | ||
105 | .enumerate() | ||
106 | .map(|(i, (_local_id, p))| (i as u32, p)) | ||
107 | } | ||
108 | |||
109 | pub(crate) fn iter_parent<'a>( | ||
110 | &'a self, | ||
111 | ) -> impl Iterator<Item = (u32, &'a GenericParamData)> + 'a { | ||
112 | self.parent_generics | ||
113 | .as_ref() | ||
114 | .into_iter() | ||
115 | .flat_map(|it| it.params.params.iter()) | ||
116 | .enumerate() | ||
117 | .map(|(i, (_local_id, p))| (i as u32, p)) | ||
118 | } | ||
119 | |||
120 | pub(crate) fn len(&self) -> usize { | ||
121 | self.len_split().0 | ||
122 | } | ||
123 | /// (total, parents, child) | ||
124 | pub(crate) fn len_split(&self) -> (usize, usize, usize) { | ||
125 | let parent = self.parent_generics.as_ref().map_or(0, |p| p.len()); | ||
126 | let child = self.params.params.len(); | ||
127 | (parent + child, parent, child) | ||
128 | } | ||
129 | pub(crate) fn param_idx(&self, param: GenericParamId) -> u32 { | ||
130 | self.find_param(param).0 | ||
131 | } | ||
132 | pub(crate) fn param_name(&self, param: GenericParamId) -> Name { | ||
133 | self.find_param(param).1.name.clone() | ||
134 | } | ||
135 | fn find_param(&self, param: GenericParamId) -> (u32, &GenericParamData) { | ||
136 | if param.parent == self.def { | ||
137 | let (idx, (_local_id, data)) = self | ||
138 | .params | ||
139 | .params | ||
140 | .iter() | ||
141 | .enumerate() | ||
142 | .find(|(_, (idx, _))| *idx == param.local_id) | ||
143 | .unwrap(); | ||
144 | let (_total, parent_len, _child) = self.len_split(); | ||
145 | return ((parent_len + idx) as u32, data); | ||
146 | } | ||
147 | self.parent_generics.as_ref().unwrap().find_param(param) | ||
148 | } | ||
149 | } | ||
150 | |||
151 | fn parent_generic_def(db: &impl DefDatabase, def: GenericDefId) -> Option<GenericDefId> { | ||
152 | let container = match def { | ||
153 | GenericDefId::FunctionId(it) => it.lookup(db).container, | ||
154 | GenericDefId::TypeAliasId(it) => it.lookup(db).container, | ||
155 | GenericDefId::ConstId(it) => it.lookup(db).container, | ||
156 | GenericDefId::EnumVariantId(it) => return Some(it.parent.into()), | ||
157 | GenericDefId::AdtId(_) | GenericDefId::TraitId(_) | GenericDefId::ImplId(_) => return None, | ||
158 | }; | ||
159 | |||
160 | match container { | ||
161 | ContainerId::ImplId(it) => Some(it.into()), | ||
162 | ContainerId::TraitId(it) => Some(it.into()), | ||
163 | ContainerId::ModuleId(_) => None, | ||
164 | } | ||
165 | } | ||