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_assists/src/utils.rs | 77 ++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 75 insertions(+), 2 deletions(-) (limited to 'crates/ra_assists/src/utils.rs') diff --git a/crates/ra_assists/src/utils.rs b/crates/ra_assists/src/utils.rs index 0d5722295..7bc21c6e4 100644 --- a/crates/ra_assists/src/utils.rs +++ b/crates/ra_assists/src/utils.rs @@ -1,10 +1,83 @@ //! Assorted functions shared by several assists. use ra_syntax::{ - ast::{self, make}, - T, + ast::{self, make, NameOwner}, + AstNode, T, }; +use hir::db::HirDatabase; + +use rustc_hash::FxHashSet; + +pub fn get_missing_impl_items( + db: &impl HirDatabase, + analyzer: &hir::SourceAnalyzer, + impl_block: &ast::ImplBlock, +) -> Vec { + // since the names are unique only to each associated type (fn/type/const), + // create buckets of each already implemented type that we'll use in the + // lookup later. + let mut impl_fns = FxHashSet::default(); + let mut impl_type = FxHashSet::default(); + let mut impl_const = FxHashSet::default(); + + if let Some(item_list) = impl_block.item_list() { + for item in item_list.impl_items() { + match item { + ast::ImplItem::FnDef(f) => { + if let Some(n) = f.name() { + impl_fns.insert(n.syntax().to_string()); + } + } + + ast::ImplItem::TypeAliasDef(t) => { + if let Some(n) = t.name() { + impl_type.insert(n.syntax().to_string()); + } + } + + ast::ImplItem::ConstDef(c) => { + if let Some(n) = c.name() { + impl_const.insert(n.syntax().to_string()); + } + } + } + } + } + + resolve_target_trait(db, analyzer, impl_block).map_or(vec![], |target_trait| { + target_trait + .items(db) + .iter() + .filter(|i| match i { + hir::AssocItem::Function(f) => !impl_fns.contains(&f.name(db).to_string()), + hir::AssocItem::TypeAlias(t) => !impl_type.contains(&t.name(db).to_string()), + hir::AssocItem::Const(c) => { + c.name(db).map(|n| !impl_const.contains(&n.to_string())).unwrap_or_default() + } + }) + .map(|i| i.clone()) + .collect() + }) +} + +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), + _ => None, + } +} + pub(crate) fn invert_boolean_expression(expr: ast::Expr) -> ast::Expr { if let Some(expr) = invert_special_case(&expr) { return expr; -- cgit v1.2.3 From f0f242cb4f9ee49427b03af21c31239e8132ac96 Mon Sep 17 00:00:00 2001 From: Kevin DeLorey <2295721+kdelorey@users.noreply.github.com> Date: Mon, 10 Feb 2020 21:09:04 -0600 Subject: Adjusted the hashset buckets to lump functions/consts together as their names must be unique. --- crates/ra_assists/src/utils.rs | 19 ++++++++++--------- 1 file changed, 10 insertions(+), 9 deletions(-) (limited to 'crates/ra_assists/src/utils.rs') diff --git a/crates/ra_assists/src/utils.rs b/crates/ra_assists/src/utils.rs index 7bc21c6e4..660da3645 100644 --- a/crates/ra_assists/src/utils.rs +++ b/crates/ra_assists/src/utils.rs @@ -9,24 +9,25 @@ use hir::db::HirDatabase; use rustc_hash::FxHashSet; +/// Generate a collection of associated items that are missing from a +/// `impl Trait for` block. pub fn get_missing_impl_items( db: &impl HirDatabase, analyzer: &hir::SourceAnalyzer, impl_block: &ast::ImplBlock, ) -> Vec { - // since the names are unique only to each associated type (fn/type/const), - // create buckets of each already implemented type that we'll use in the - // lookup later. - let mut impl_fns = FxHashSet::default(); + + // Names must be unique between constants and functions. However, type aliases + // may share the same name as a function or constant. + let mut impl_fns_consts = FxHashSet::default(); let mut impl_type = FxHashSet::default(); - let mut impl_const = FxHashSet::default(); if let Some(item_list) = impl_block.item_list() { for item in item_list.impl_items() { match item { ast::ImplItem::FnDef(f) => { if let Some(n) = f.name() { - impl_fns.insert(n.syntax().to_string()); + impl_fns_consts.insert(n.syntax().to_string()); } } @@ -38,7 +39,7 @@ pub fn get_missing_impl_items( ast::ImplItem::ConstDef(c) => { if let Some(n) = c.name() { - impl_const.insert(n.syntax().to_string()); + impl_fns_consts.insert(n.syntax().to_string()); } } } @@ -50,10 +51,10 @@ pub fn get_missing_impl_items( .items(db) .iter() .filter(|i| match i { - hir::AssocItem::Function(f) => !impl_fns.contains(&f.name(db).to_string()), + hir::AssocItem::Function(f) => !impl_fns_consts.contains(&f.name(db).to_string()), hir::AssocItem::TypeAlias(t) => !impl_type.contains(&t.name(db).to_string()), hir::AssocItem::Const(c) => { - c.name(db).map(|n| !impl_const.contains(&n.to_string())).unwrap_or_default() + c.name(db).map(|n| !impl_fns_consts.contains(&n.to_string())).unwrap_or_default() } }) .map(|i| i.clone()) -- cgit v1.2.3 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. --- crates/ra_assists/src/utils.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'crates/ra_assists/src/utils.rs') diff --git a/crates/ra_assists/src/utils.rs b/crates/ra_assists/src/utils.rs index 660da3645..7628933fb 100644 --- a/crates/ra_assists/src/utils.rs +++ b/crates/ra_assists/src/utils.rs @@ -62,7 +62,7 @@ pub fn get_missing_impl_items( }) } -fn resolve_target_trait( +pub(crate) fn resolve_target_trait( db: &impl HirDatabase, analyzer: &hir::SourceAnalyzer, impl_block: &ast::ImplBlock, -- 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/utils.rs | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) (limited to 'crates/ra_assists/src/utils.rs') diff --git a/crates/ra_assists/src/utils.rs b/crates/ra_assists/src/utils.rs index 7628933fb..461f01536 100644 --- a/crates/ra_assists/src/utils.rs +++ b/crates/ra_assists/src/utils.rs @@ -9,14 +9,13 @@ use hir::db::HirDatabase; use rustc_hash::FxHashSet; -/// Generate a collection of associated items that are missing from a +/// Generate a collection of associated items that are missing from a /// `impl Trait for` block. pub fn get_missing_impl_items( db: &impl HirDatabase, analyzer: &hir::SourceAnalyzer, impl_block: &ast::ImplBlock, ) -> Vec { - // Names must be unique between constants and functions. However, type aliases // may share the same name as a function or constant. let mut impl_fns_consts = FxHashSet::default(); @@ -53,9 +52,10 @@ pub fn get_missing_impl_items( .filter(|i| match i { hir::AssocItem::Function(f) => !impl_fns_consts.contains(&f.name(db).to_string()), hir::AssocItem::TypeAlias(t) => !impl_type.contains(&t.name(db).to_string()), - hir::AssocItem::Const(c) => { - c.name(db).map(|n| !impl_fns_consts.contains(&n.to_string())).unwrap_or_default() - } + hir::AssocItem::Const(c) => c + .name(db) + .map(|n| !impl_fns_consts.contains(&n.to_string())) + .unwrap_or_default(), }) .map(|i| i.clone()) .collect() -- cgit v1.2.3 From e664cd73e3f91086dc765fb5ec74ebec2daa8ffa Mon Sep 17 00:00:00 2001 From: Kevin DeLorey <2295721+kdelorey@users.noreply.github.com> Date: Tue, 11 Feb 2020 09:48:26 -0600 Subject: Removed doc comments entirely from the changes. --- crates/ra_assists/src/utils.rs | 2 -- 1 file changed, 2 deletions(-) (limited to 'crates/ra_assists/src/utils.rs') diff --git a/crates/ra_assists/src/utils.rs b/crates/ra_assists/src/utils.rs index 461f01536..1280a4fdc 100644 --- a/crates/ra_assists/src/utils.rs +++ b/crates/ra_assists/src/utils.rs @@ -9,8 +9,6 @@ use hir::db::HirDatabase; use rustc_hash::FxHashSet; -/// Generate a collection of associated items that are missing from a -/// `impl Trait for` block. pub fn get_missing_impl_items( db: &impl HirDatabase, analyzer: &hir::SourceAnalyzer, -- cgit v1.2.3 From 47d314e85681c075ff859e13343927e7406e1b46 Mon Sep 17 00:00:00 2001 From: Kevin DeLorey <2295721+kdelorey@users.noreply.github.com> Date: Tue, 11 Feb 2020 10:04:30 -0600 Subject: Fixing minor suggestions and added module level documentation. --- crates/ra_assists/src/utils.rs | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) (limited to 'crates/ra_assists/src/utils.rs') diff --git a/crates/ra_assists/src/utils.rs b/crates/ra_assists/src/utils.rs index 1280a4fdc..6ff44c95c 100644 --- a/crates/ra_assists/src/utils.rs +++ b/crates/ra_assists/src/utils.rs @@ -6,7 +6,6 @@ use ra_syntax::{ }; use hir::db::HirDatabase; - use rustc_hash::FxHashSet; pub fn get_missing_impl_items( @@ -55,7 +54,7 @@ pub fn get_missing_impl_items( .map(|n| !impl_fns_consts.contains(&n.to_string())) .unwrap_or_default(), }) - .map(|i| i.clone()) + .cloned() .collect() }) } -- cgit v1.2.3