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/lib.rs | 2 +- crates/ra_assists/src/utils.rs | 77 ++++++++++++++++++++++++++++++++++++++++-- 2 files changed, 76 insertions(+), 3 deletions(-) (limited to 'crates/ra_assists') diff --git a/crates/ra_assists/src/lib.rs b/crates/ra_assists/src/lib.rs index 828a8e9e8..cb124eaf0 100644 --- a/crates/ra_assists/src/lib.rs +++ b/crates/ra_assists/src/lib.rs @@ -9,7 +9,7 @@ mod assist_ctx; mod marks; #[cfg(test)] mod doc_tests; -mod utils; +pub mod utils; pub mod ast_transform; use ra_db::FileRange; 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