From 8d3d509af77756758cea14cc4939d099b4f95993 Mon Sep 17 00:00:00 2001 From: Igor Aleksanov Date: Sat, 24 Oct 2020 10:47:23 +0300 Subject: Remove dependency on 'assists' from 'completion' crate --- crates/ide_db/src/lib.rs | 2 ++ crates/ide_db/src/traits.rs | 78 ++++++++++++++++++++++++++++++++++++++++++ crates/ide_db/src/ty_filter.rs | 58 +++++++++++++++++++++++++++++++ 3 files changed, 138 insertions(+) create mode 100644 crates/ide_db/src/traits.rs create mode 100644 crates/ide_db/src/ty_filter.rs (limited to 'crates/ide_db') diff --git a/crates/ide_db/src/lib.rs b/crates/ide_db/src/lib.rs index 7eff247c7..88f74265c 100644 --- a/crates/ide_db/src/lib.rs +++ b/crates/ide_db/src/lib.rs @@ -10,6 +10,8 @@ pub mod defs; pub mod search; pub mod imports_locator; pub mod source_change; +pub mod ty_filter; +pub mod traits; use std::{fmt, sync::Arc}; diff --git a/crates/ide_db/src/traits.rs b/crates/ide_db/src/traits.rs new file mode 100644 index 000000000..dcd61a595 --- /dev/null +++ b/crates/ide_db/src/traits.rs @@ -0,0 +1,78 @@ +//! Functionality for obtaining data related to traits from the DB. + +use crate::RootDatabase; +use hir::Semantics; +use rustc_hash::FxHashSet; +use syntax::{ + ast::{self, NameOwner}, + AstNode, +}; + +/// Given the `impl` block, attempts to find the trait this `impl` corresponds to. +pub fn resolve_target_trait( + sema: &Semantics, + impl_def: &ast::Impl, +) -> Option { + let ast_path = + impl_def.trait_().map(|it| it.syntax().clone()).and_then(ast::PathType::cast)?.path()?; + + match sema.resolve_path(&ast_path) { + Some(hir::PathResolution::Def(hir::ModuleDef::Trait(def))) => Some(def), + _ => None, + } +} + +/// Given the `impl` block, returns the list of associated items (e.g. functions or types) that are +/// missing in this `impl` block. +pub fn get_missing_assoc_items( + sema: &Semantics, + impl_def: &ast::Impl, +) -> 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(); + let mut impl_type = FxHashSet::default(); + + if let Some(item_list) = impl_def.assoc_item_list() { + for item in item_list.assoc_items() { + match item { + ast::AssocItem::Fn(f) => { + if let Some(n) = f.name() { + impl_fns_consts.insert(n.syntax().to_string()); + } + } + + ast::AssocItem::TypeAlias(t) => { + if let Some(n) = t.name() { + impl_type.insert(n.syntax().to_string()); + } + } + + ast::AssocItem::Const(c) => { + if let Some(n) = c.name() { + impl_fns_consts.insert(n.syntax().to_string()); + } + } + ast::AssocItem::MacroCall(_) => (), + } + } + } + + resolve_target_trait(sema, impl_def).map_or(vec![], |target_trait| { + target_trait + .items(sema.db) + .iter() + .filter(|i| match i { + hir::AssocItem::Function(f) => { + !impl_fns_consts.contains(&f.name(sema.db).to_string()) + } + hir::AssocItem::TypeAlias(t) => !impl_type.contains(&t.name(sema.db).to_string()), + hir::AssocItem::Const(c) => c + .name(sema.db) + .map(|n| !impl_fns_consts.contains(&n.to_string())) + .unwrap_or_default(), + }) + .cloned() + .collect() + }) +} diff --git a/crates/ide_db/src/ty_filter.rs b/crates/ide_db/src/ty_filter.rs new file mode 100644 index 000000000..63a945282 --- /dev/null +++ b/crates/ide_db/src/ty_filter.rs @@ -0,0 +1,58 @@ +//! This module contains structures for filtering the expected types. +//! Use case for structures in this module is, for example, situation when you need to process +//! only certain `Enum`s. + +use crate::RootDatabase; +use hir::{Adt, Semantics, Type}; +use std::iter; +use syntax::ast::{self, make}; + +/// Enum types that implement `std::ops::Try` trait. +#[derive(Clone, Copy)] +pub enum TryEnum { + Result, + Option, +} + +impl TryEnum { + const ALL: [TryEnum; 2] = [TryEnum::Option, TryEnum::Result]; + + /// Returns `Some(..)` if the provided type is an enum that implements `std::ops::Try`. + pub fn from_ty(sema: &Semantics, ty: &Type) -> Option { + let enum_ = match ty.as_adt() { + Some(Adt::Enum(it)) => it, + _ => return None, + }; + TryEnum::ALL.iter().find_map(|&var| { + if &enum_.name(sema.db).to_string() == var.type_name() { + return Some(var); + } + None + }) + } + + pub fn happy_case(self) -> &'static str { + match self { + TryEnum::Result => "Ok", + TryEnum::Option => "Some", + } + } + + pub fn sad_pattern(self) -> ast::Pat { + match self { + TryEnum::Result => make::tuple_struct_pat( + make::path_unqualified(make::path_segment(make::name_ref("Err"))), + iter::once(make::wildcard_pat().into()), + ) + .into(), + TryEnum::Option => make::ident_pat(make::name("None")).into(), + } + } + + fn type_name(self) -> &'static str { + match self { + TryEnum::Result => "Result", + TryEnum::Option => "Option", + } + } +} -- cgit v1.2.3