From d85abd77b98ff5925621c18f2ffe121640d17c80 Mon Sep 17 00:00:00 2001 From: Kevin DeLorey <2295721+kdelorey@users.noreply.github.com> Date: Sun, 9 Feb 2020 12:24:34 -0600 Subject: Added a utility function that can be used to determine the missing impl items. --- crates/ra_ide/src/completion.rs | 2 +- .../ra_ide/src/completion/complete_trait_impl.rs | 132 +++------------------ 2 files changed, 19 insertions(+), 115 deletions(-) (limited to 'crates/ra_ide') diff --git a/crates/ra_ide/src/completion.rs b/crates/ra_ide/src/completion.rs index 4f24cd1f9..4bdc6ba23 100644 --- a/crates/ra_ide/src/completion.rs +++ b/crates/ra_ide/src/completion.rs @@ -76,6 +76,6 @@ pub(crate) fn completions(db: &RootDatabase, position: FilePosition) -> Option { - let f_name = f.name(ctx.db).to_string(); - - item_list - .impl_items() - .find(|impl_item| { - match impl_item { - ast::ImplItem::FnDef(impl_f) => { - if let Some(n) = impl_f.name() { - f_name == n.syntax().to_string() - } else { - false - } - }, - _ => false - } - }).is_none() - }, - hir::AssocItem::Const(c) => { - let c_name = c.name(ctx.db) - .map(|f| f.to_string()); - - if c_name.is_none() { - return false; - } - - let c_name = c_name.unwrap(); - - item_list - .impl_items() - .find(|impl_item| { - match impl_item { - ast::ImplItem::ConstDef(c) => { - if let Some(n) = c.name() { - c_name == n.syntax().to_string() - } else { - false - } - }, - _ => false - } - }).is_none() - }, - hir::AssocItem::TypeAlias(t) => { - let t_name = t.name(ctx.db).to_string(); - - item_list - .impl_items() - .find(|impl_item| { - match impl_item { - ast::ImplItem::TypeAliasDef(t) => { - if let Some(n) = t.name() { - t_name == n.syntax().to_string() - } else { - false - } - }, - _ => false - } - }).is_none() - } - } - }); - - for item in missing_items { + for item in get_missing_impl_items(ctx.db, &ctx.analyzer, impl_block) { match item { - hir::AssocItem::Function(f) => add_function_impl(acc, ctx, f), - hir::AssocItem::TypeAlias(t) => add_type_alias_impl(acc, ctx, t), - _ => {}, - } - } -} - -fn resolve_target_trait( - db: &impl HirDatabase, - analyzer: &hir::SourceAnalyzer, - impl_block: &ast::ImplBlock -) -> Option { - let ast_path = impl_block - .target_trait() - .map(|it| it.syntax().clone()) - .and_then(ast::PathType::cast)? - .path()?; - - match analyzer.resolve_path(db, &ast_path) { - Some(hir::PathResolution::Def(hir::ModuleDef::Trait(def))) => { - Some(def) + hir::AssocItem::Function(f) => add_function_impl(acc, ctx, &f), + hir::AssocItem::TypeAlias(t) => add_type_alias_impl(acc, ctx, &t), + _ => {} } - _ => None, } } @@ -144,20 +47,21 @@ fn add_function_impl(acc: &mut Completions, ctx: &CompletionContext, func: &hir: } else { CompletionItemKind::Function }; - + let snippet = { let mut s = format!("{}", display); s.push_str(" {}"); s }; - builder - .insert_text(snippet) - .kind(completion_kind) - .add_to(acc); + builder.insert_text(snippet).kind(completion_kind).add_to(acc); } -fn add_type_alias_impl(acc: &mut Completions, ctx: &CompletionContext, type_alias: &hir::TypeAlias) { +fn add_type_alias_impl( + acc: &mut Completions, + ctx: &CompletionContext, + type_alias: &hir::TypeAlias, +) { let snippet = format!("type {} = ", type_alias.name(ctx.db).to_string()); CompletionItem::new(CompletionKind::Magic, ctx.source_range(), snippet.clone()) @@ -290,4 +194,4 @@ mod tests { ] "###); } -} \ No newline at end of file +} -- cgit v1.2.3