aboutsummaryrefslogtreecommitdiff
path: root/crates/ra_assists/src/assists
diff options
context:
space:
mode:
Diffstat (limited to 'crates/ra_assists/src/assists')
-rw-r--r--crates/ra_assists/src/assists/add_missing_impl_members.rs121
1 files changed, 8 insertions, 113 deletions
diff --git a/crates/ra_assists/src/assists/add_missing_impl_members.rs b/crates/ra_assists/src/assists/add_missing_impl_members.rs
index 942b34dc1..bf1136193 100644
--- a/crates/ra_assists/src/assists/add_missing_impl_members.rs
+++ b/crates/ra_assists/src/assists/add_missing_impl_members.rs
@@ -1,12 +1,13 @@
1use std::collections::HashMap; 1use hir::{db::HirDatabase, HasSource, InFile};
2
3use hir::{db::HirDatabase, HasSource};
4use ra_syntax::{ 2use ra_syntax::{
5 ast::{self, edit, make, AstNode, NameOwner}, 3 ast::{self, edit, make, AstNode, NameOwner},
6 SmolStr, 4 SmolStr,
7}; 5};
8 6
9use crate::{Assist, AssistCtx, AssistId}; 7use crate::{
8 ast_transform::{self, AstTransform, QualifyPaths, SubstituteTypeParams},
9 Assist, AssistCtx, AssistId,
10};
10 11
11#[derive(PartialEq)] 12#[derive(PartialEq)]
12enum AddMissingImplMembersMode { 13enum AddMissingImplMembersMode {
@@ -146,24 +147,11 @@ fn add_missing_impl_members_inner(
146 None, 147 None,
147 ) 148 )
148 .module(); 149 .module();
149 let substs = get_syntactic_substs(impl_node).unwrap_or_default(); 150 let ast_transform = QualifyPaths::new(db, module)
150 let generic_def: hir::GenericDef = trait_.into(); 151 .or(SubstituteTypeParams::for_trait_impl(db, trait_, impl_node));
151 let substs_by_param: HashMap<_, _> = generic_def
152 .params(db)
153 .into_iter()
154 // this is a trait impl, so we need to skip the first type parameter -- this is a bit hacky
155 .skip(1)
156 .zip(substs.into_iter())
157 .collect();
158 let items = missing_items 152 let items = missing_items
159 .into_iter() 153 .into_iter()
160 .map(|it| { 154 .map(|it| ast_transform::apply(&*ast_transform, InFile::new(trait_file_id, it)))
161 substitute_type_params(db, hir::InFile::new(trait_file_id, it), &substs_by_param)
162 })
163 .map(|it| match module {
164 Some(module) => qualify_paths(db, hir::InFile::new(trait_file_id, it), module),
165 None => it,
166 })
167 .map(|it| match it { 155 .map(|it| match it {
168 ast::ImplItem::FnDef(def) => ast::ImplItem::FnDef(add_body(def)), 156 ast::ImplItem::FnDef(def) => ast::ImplItem::FnDef(add_body(def)),
169 _ => it, 157 _ => it,
@@ -188,99 +176,6 @@ fn add_body(fn_def: ast::FnDef) -> ast::FnDef {
188 } 176 }
189} 177}
190 178
191// FIXME: It would probably be nicer if we could get this via HIR (i.e. get the
192// trait ref, and then go from the types in the substs back to the syntax)
193// FIXME: This should be a general utility (not even just for assists)
194fn get_syntactic_substs(impl_block: ast::ImplBlock) -> Option<Vec<ast::TypeRef>> {
195 let target_trait = impl_block.target_trait()?;
196 let path_type = match target_trait {
197 ast::TypeRef::PathType(path) => path,
198 _ => return None,
199 };
200 let type_arg_list = path_type.path()?.segment()?.type_arg_list()?;
201 let mut result = Vec::new();
202 for type_arg in type_arg_list.type_args() {
203 let type_arg: ast::TypeArg = type_arg;
204 result.push(type_arg.type_ref()?);
205 }
206 Some(result)
207}
208
209// FIXME: This should be a general utility (not even just for assists)
210fn substitute_type_params<N: AstNode + Clone>(
211 db: &impl HirDatabase,
212 node: hir::InFile<N>,
213 substs: &HashMap<hir::TypeParam, ast::TypeRef>,
214) -> N {
215 let type_param_replacements = node
216 .clone()
217 .descendants::<ast::TypeRef>()
218 .filter_map(|n| {
219 let path = match &n.value {
220 ast::TypeRef::PathType(path_type) => path_type.path()?,
221 _ => return None,
222 };
223 let analyzer = hir::SourceAnalyzer::new(db, n.syntax(), None);
224 let resolution = analyzer.resolve_path(db, &path)?;
225 match resolution {
226 hir::PathResolution::TypeParam(tp) => Some((n.value, substs.get(&tp)?.clone())),
227 _ => None,
228 }
229 })
230 .collect::<Vec<_>>();
231
232 if type_param_replacements.is_empty() {
233 node.value
234 } else {
235 edit::replace_descendants(&node.value, type_param_replacements.into_iter())
236 }
237}
238
239use hir::PathResolution;
240
241// FIXME extract this to a general utility as well
242// FIXME handle value ns?
243// FIXME this doesn't 'commute' with `substitute_type_params`, since type params in newly generated type arg lists don't resolve. Currently we can avoid this problem, but it's worth thinking about a solution
244fn qualify_paths<N: AstNode>(db: &impl HirDatabase, node: hir::InFile<N>, from: hir::Module) -> N {
245 let path_replacements = node
246 .value
247 .syntax()
248 .descendants()
249 .filter_map(ast::Path::cast)
250 .filter_map(|p| {
251 if p.segment().and_then(|s| s.param_list()).is_some() {
252 // don't try to qualify `Fn(Foo) -> Bar` paths, they are in prelude anyway
253 return None;
254 }
255 // FIXME check if some ancestor is already being replaced, if so skip this
256 let analyzer = hir::SourceAnalyzer::new(db, node.with_value(p.syntax()), None);
257 let resolution = analyzer.resolve_path(db, &p)?;
258 match resolution {
259 PathResolution::Def(def) => {
260 let found_path = from.find_path(db, def)?;
261 // TODO fix type arg replacements being qualified
262 let args = p
263 .segment()
264 .and_then(|s| s.type_arg_list())
265 .map(|arg_list| qualify_paths(db, node.with_value(arg_list), from));
266 Some((p, make::path_with_type_arg_list(found_path.to_ast(), args)))
267 }
268 PathResolution::Local(_)
269 | PathResolution::TypeParam(_)
270 | PathResolution::SelfType(_) => None,
271 PathResolution::Macro(_) => None,
272 PathResolution::AssocItem(_) => None,
273 }
274 })
275 .collect::<Vec<_>>();
276
277 if path_replacements.is_empty() {
278 node.value
279 } else {
280 edit::replace_descendants(&node.value, path_replacements.into_iter())
281 }
282}
283
284/// Given an `ast::ImplBlock`, resolves the target trait (the one being 179/// Given an `ast::ImplBlock`, resolves the target trait (the one being
285/// implemented) to a `ast::TraitDef`. 180/// implemented) to a `ast::TraitDef`.
286fn resolve_target_trait_def( 181fn resolve_target_trait_def(