2020-02-07 08:57:38 -06:00
|
|
|
//! Assorted functions shared by several assists.
|
2020-02-28 14:53:20 -06:00
|
|
|
pub(crate) mod insert_use;
|
2020-02-07 08:57:38 -06:00
|
|
|
|
2020-02-18 11:35:10 -06:00
|
|
|
use hir::Semantics;
|
|
|
|
use ra_ide_db::RootDatabase;
|
2020-02-07 08:57:38 -06:00
|
|
|
use ra_syntax::{
|
2020-02-09 12:24:34 -06:00
|
|
|
ast::{self, make, NameOwner},
|
|
|
|
AstNode, T,
|
2020-02-07 08:57:38 -06:00
|
|
|
};
|
2020-02-09 12:24:34 -06:00
|
|
|
use rustc_hash::FxHashSet;
|
|
|
|
|
2020-02-28 14:53:20 -06:00
|
|
|
pub use insert_use::insert_use_statement;
|
|
|
|
|
2020-02-09 12:24:34 -06:00
|
|
|
pub fn get_missing_impl_items(
|
2020-02-18 11:35:10 -06:00
|
|
|
sema: &Semantics<RootDatabase>,
|
2020-02-29 14:24:40 -06:00
|
|
|
impl_def: &ast::ImplDef,
|
2020-02-09 12:24:34 -06:00
|
|
|
) -> Vec<hir::AssocItem> {
|
2020-02-10 21:09:04 -06:00
|
|
|
// 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();
|
2020-02-09 12:24:34 -06:00
|
|
|
let mut impl_type = FxHashSet::default();
|
|
|
|
|
2020-02-29 14:24:40 -06:00
|
|
|
if let Some(item_list) = impl_def.item_list() {
|
2020-02-09 12:24:34 -06:00
|
|
|
for item in item_list.impl_items() {
|
|
|
|
match item {
|
|
|
|
ast::ImplItem::FnDef(f) => {
|
|
|
|
if let Some(n) = f.name() {
|
2020-02-10 21:09:04 -06:00
|
|
|
impl_fns_consts.insert(n.syntax().to_string());
|
2020-02-09 12:24:34 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
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() {
|
2020-02-10 21:09:04 -06:00
|
|
|
impl_fns_consts.insert(n.syntax().to_string());
|
2020-02-09 12:24:34 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-02-29 14:24:40 -06:00
|
|
|
resolve_target_trait(sema, impl_def).map_or(vec![], |target_trait| {
|
2020-02-09 12:24:34 -06:00
|
|
|
target_trait
|
2020-02-18 11:35:10 -06:00
|
|
|
.items(sema.db)
|
2020-02-09 12:24:34 -06:00
|
|
|
.iter()
|
|
|
|
.filter(|i| match i {
|
2020-02-18 11:35:10 -06:00
|
|
|
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()),
|
2020-02-11 09:40:08 -06:00
|
|
|
hir::AssocItem::Const(c) => c
|
2020-02-18 11:35:10 -06:00
|
|
|
.name(sema.db)
|
2020-02-11 09:40:08 -06:00
|
|
|
.map(|n| !impl_fns_consts.contains(&n.to_string()))
|
|
|
|
.unwrap_or_default(),
|
2020-02-09 12:24:34 -06:00
|
|
|
})
|
2020-02-11 10:04:30 -06:00
|
|
|
.cloned()
|
2020-02-09 12:24:34 -06:00
|
|
|
.collect()
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2020-02-11 06:57:26 -06:00
|
|
|
pub(crate) fn resolve_target_trait(
|
2020-02-18 11:35:10 -06:00
|
|
|
sema: &Semantics<RootDatabase>,
|
2020-02-29 14:24:40 -06:00
|
|
|
impl_def: &ast::ImplDef,
|
2020-02-09 12:24:34 -06:00
|
|
|
) -> Option<hir::Trait> {
|
2020-02-29 14:24:40 -06:00
|
|
|
let ast_path = impl_def
|
2020-02-09 12:24:34 -06:00
|
|
|
.target_trait()
|
|
|
|
.map(|it| it.syntax().clone())
|
|
|
|
.and_then(ast::PathType::cast)?
|
|
|
|
.path()?;
|
|
|
|
|
2020-02-18 11:35:10 -06:00
|
|
|
match sema.resolve_path(&ast_path) {
|
2020-02-09 12:24:34 -06:00
|
|
|
Some(hir::PathResolution::Def(hir::ModuleDef::Trait(def))) => Some(def),
|
|
|
|
_ => None,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-02-07 08:57:38 -06:00
|
|
|
pub(crate) fn invert_boolean_expression(expr: ast::Expr) -> ast::Expr {
|
|
|
|
if let Some(expr) = invert_special_case(&expr) {
|
|
|
|
return expr;
|
|
|
|
}
|
|
|
|
make::expr_prefix(T![!], expr)
|
|
|
|
}
|
|
|
|
|
|
|
|
fn invert_special_case(expr: &ast::Expr) -> Option<ast::Expr> {
|
|
|
|
match expr {
|
|
|
|
ast::Expr::BinExpr(bin) => match bin.op_kind()? {
|
|
|
|
ast::BinOp::NegatedEqualityTest => bin.replace_op(T![==]).map(|it| it.into()),
|
|
|
|
ast::BinOp::EqualityTest => bin.replace_op(T![!=]).map(|it| it.into()),
|
|
|
|
_ => None,
|
|
|
|
},
|
|
|
|
ast::Expr::PrefixExpr(pe) if pe.op_kind()? == ast::PrefixOp::Not => pe.expr(),
|
|
|
|
// FIXME:
|
|
|
|
// ast::Expr::Literal(true | false )
|
|
|
|
_ => None,
|
|
|
|
}
|
|
|
|
}
|