From ca43bb3ff787c17173a2a5d18f55eabb1bc5136b Mon Sep 17 00:00:00 2001 From: Kevin DeLorey <2295721+kdelorey@users.noreply.github.com> Date: Tue, 11 Feb 2020 06:57:26 -0600 Subject: Updated the `add_missing_impl_members` to use the shared utility. --- .../src/handlers/add_missing_impl_members.rs | 47 +++++++--------------- 1 file changed, 14 insertions(+), 33 deletions(-) (limited to 'crates/ra_assists/src/handlers') diff --git a/crates/ra_assists/src/handlers/add_missing_impl_members.rs b/crates/ra_assists/src/handlers/add_missing_impl_members.rs index 448697d31..495493a52 100644 --- a/crates/ra_assists/src/handlers/add_missing_impl_members.rs +++ b/crates/ra_assists/src/handlers/add_missing_impl_members.rs @@ -7,6 +7,7 @@ use ra_syntax::{ use crate::{ ast_transform::{self, AstTransform, QualifyPaths, SubstituteTypeParams}, Assist, AssistCtx, AssistId, + utils::{get_missing_impl_items, resolve_target_trait}, }; #[derive(PartialEq)] @@ -103,11 +104,9 @@ fn add_missing_impl_members_inner( let impl_node = ctx.find_node_at_offset::()?; let impl_item_list = impl_node.item_list()?; - let (trait_, trait_def) = { - let analyzer = ctx.source_analyzer(impl_node.syntax(), None); + let analyzer = ctx.source_analyzer(impl_node.syntax(), None); - resolve_target_trait_def(ctx.db, &analyzer, &impl_node)? - }; + let trait_ = resolve_target_trait(ctx.db, &analyzer, &impl_node)?; let def_name = |item: &ast::ImplItem| -> Option { match item { @@ -118,20 +117,23 @@ fn add_missing_impl_members_inner( .map(|it| it.text().clone()) }; - let trait_items = trait_def.item_list()?.impl_items(); - let impl_items = impl_item_list.impl_items().collect::>(); - - let missing_items: Vec<_> = trait_items - .filter(|t| def_name(t).is_some()) + let missing_items = get_missing_impl_items(ctx.db, &analyzer, &impl_node) + .iter() + .map(|i| match i { + hir::AssocItem::Function(i) => ast::ImplItem::FnDef(i.source(ctx.db).value), + hir::AssocItem::TypeAlias(i) => ast::ImplItem::TypeAliasDef(i.source(ctx.db).value), + hir::AssocItem::Const(i) => ast::ImplItem::ConstDef(i.source(ctx.db).value), + }) + .filter(|t| def_name(&t).is_some()) .filter(|t| match t { ast::ImplItem::FnDef(def) => match mode { AddMissingImplMembersMode::DefaultMethodsOnly => def.body().is_some(), AddMissingImplMembersMode::NoDefaultMethods => def.body().is_none(), - }, + } _ => mode == AddMissingImplMembersMode::NoDefaultMethods, }) - .filter(|t| impl_items.iter().all(|i| def_name(i) != def_name(t))) - .collect(); + .collect::>(); + if missing_items.is_empty() { return None; } @@ -177,27 +179,6 @@ fn add_body(fn_def: ast::FnDef) -> ast::FnDef { } } -/// Given an `ast::ImplBlock`, resolves the target trait (the one being -/// implemented) to a `ast::TraitDef`. -fn resolve_target_trait_def( - db: &impl HirDatabase, - analyzer: &hir::SourceAnalyzer, - impl_block: &ast::ImplBlock, -) -> Option<(hir::Trait, ast::TraitDef)> { - 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, def.source(db).value)) - } - _ => None, - } -} - #[cfg(test)] mod tests { use super::*; -- cgit v1.2.3 From b4429a9d64759f4aee3cb48cb938da021a24b7b5 Mon Sep 17 00:00:00 2001 From: Kevin DeLorey <2295721+kdelorey@users.noreply.github.com> Date: Tue, 11 Feb 2020 07:10:06 -0600 Subject: Fixed warning generated after refactoring. --- crates/ra_assists/src/handlers/add_missing_impl_members.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'crates/ra_assists/src/handlers') diff --git a/crates/ra_assists/src/handlers/add_missing_impl_members.rs b/crates/ra_assists/src/handlers/add_missing_impl_members.rs index 495493a52..4c937b154 100644 --- a/crates/ra_assists/src/handlers/add_missing_impl_members.rs +++ b/crates/ra_assists/src/handlers/add_missing_impl_members.rs @@ -1,4 +1,4 @@ -use hir::{db::HirDatabase, HasSource, InFile}; +use hir::{HasSource, InFile}; use ra_syntax::{ ast::{self, edit, make, AstNode, NameOwner}, SmolStr, -- cgit v1.2.3 From 3aaf46afa13b6fcbfdc36d8eb0cce48196d824a7 Mon Sep 17 00:00:00 2001 From: Kevin DeLorey <2295721+kdelorey@users.noreply.github.com> Date: Tue, 11 Feb 2020 09:40:08 -0600 Subject: Formatted changes. --- crates/ra_assists/src/handlers/add_missing_impl_members.rs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) (limited to 'crates/ra_assists/src/handlers') diff --git a/crates/ra_assists/src/handlers/add_missing_impl_members.rs b/crates/ra_assists/src/handlers/add_missing_impl_members.rs index 4c937b154..ab21388c8 100644 --- a/crates/ra_assists/src/handlers/add_missing_impl_members.rs +++ b/crates/ra_assists/src/handlers/add_missing_impl_members.rs @@ -6,8 +6,8 @@ use ra_syntax::{ use crate::{ ast_transform::{self, AstTransform, QualifyPaths, SubstituteTypeParams}, - Assist, AssistCtx, AssistId, utils::{get_missing_impl_items, resolve_target_trait}, + Assist, AssistCtx, AssistId, }; #[derive(PartialEq)] @@ -129,7 +129,7 @@ fn add_missing_impl_members_inner( ast::ImplItem::FnDef(def) => match mode { AddMissingImplMembersMode::DefaultMethodsOnly => def.body().is_some(), AddMissingImplMembersMode::NoDefaultMethods => def.body().is_none(), - } + }, _ => mode == AddMissingImplMembersMode::NoDefaultMethods, }) .collect::>(); -- cgit v1.2.3