2020-02-07 14:57:38 +00:00
|
|
|
//! Assorted functions shared by several assists.
|
2020-02-28 20:53:20 +00:00
|
|
|
pub(crate) mod insert_use;
|
2020-02-07 14:57:38 +00:00
|
|
|
|
2020-05-19 20:25:07 +00:00
|
|
|
use std::{iter, ops};
|
2020-04-29 09:57:06 +00:00
|
|
|
|
2020-04-29 12:49:54 +00:00
|
|
|
use hir::{Adt, Crate, Semantics, Trait, Type};
|
2020-02-18 17:35:10 +00:00
|
|
|
use ra_ide_db::RootDatabase;
|
2020-02-07 14:57:38 +00:00
|
|
|
use ra_syntax::{
|
2020-02-09 18:24:34 +00:00
|
|
|
ast::{self, make, NameOwner},
|
2020-05-19 20:25:07 +00:00
|
|
|
AstNode, SyntaxNode, T,
|
2020-02-07 14:57:38 +00:00
|
|
|
};
|
2020-02-09 18:24:34 +00:00
|
|
|
use rustc_hash::FxHashSet;
|
|
|
|
|
2020-05-02 19:24:55 +00:00
|
|
|
pub(crate) use insert_use::insert_use_statement;
|
2020-02-28 20:53:20 +00:00
|
|
|
|
2020-05-19 20:25:07 +00:00
|
|
|
pub(crate) fn render_snippet(node: &SyntaxNode, placeholder: &SyntaxNode) -> String {
|
|
|
|
assert!(placeholder.ancestors().any(|it| it == *node));
|
|
|
|
let range = placeholder.text_range() - node.text_range().start();
|
|
|
|
let range: ops::Range<usize> = range.into();
|
|
|
|
|
|
|
|
let mut placeholder = placeholder.to_string();
|
|
|
|
escape(&mut placeholder);
|
|
|
|
let tab_stop = format!("${{0:{}}}", placeholder);
|
|
|
|
|
|
|
|
let mut buf = node.to_string();
|
|
|
|
buf.replace_range(range, &tab_stop);
|
|
|
|
return buf;
|
|
|
|
|
|
|
|
fn escape(buf: &mut String) {
|
|
|
|
stdx::replace(buf, '{', r"\{");
|
|
|
|
stdx::replace(buf, '}', r"\}");
|
|
|
|
stdx::replace(buf, '$', r"\$");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-05-05 15:56:10 +00:00
|
|
|
pub fn get_missing_assoc_items(
|
2020-02-18 17:35:10 +00:00
|
|
|
sema: &Semantics<RootDatabase>,
|
2020-02-29 20:24:40 +00:00
|
|
|
impl_def: &ast::ImplDef,
|
2020-02-09 18:24:34 +00:00
|
|
|
) -> Vec<hir::AssocItem> {
|
2020-02-11 03:09:04 +00: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 18:24:34 +00:00
|
|
|
let mut impl_type = FxHashSet::default();
|
|
|
|
|
2020-02-29 20:24:40 +00:00
|
|
|
if let Some(item_list) = impl_def.item_list() {
|
2020-05-05 15:56:10 +00:00
|
|
|
for item in item_list.assoc_items() {
|
2020-02-09 18:24:34 +00:00
|
|
|
match item {
|
2020-05-05 15:56:10 +00:00
|
|
|
ast::AssocItem::FnDef(f) => {
|
2020-02-09 18:24:34 +00:00
|
|
|
if let Some(n) = f.name() {
|
2020-02-11 03:09:04 +00:00
|
|
|
impl_fns_consts.insert(n.syntax().to_string());
|
2020-02-09 18:24:34 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-05-05 15:56:10 +00:00
|
|
|
ast::AssocItem::TypeAliasDef(t) => {
|
2020-02-09 18:24:34 +00:00
|
|
|
if let Some(n) = t.name() {
|
|
|
|
impl_type.insert(n.syntax().to_string());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-05-05 15:56:10 +00:00
|
|
|
ast::AssocItem::ConstDef(c) => {
|
2020-02-09 18:24:34 +00:00
|
|
|
if let Some(n) = c.name() {
|
2020-02-11 03:09:04 +00:00
|
|
|
impl_fns_consts.insert(n.syntax().to_string());
|
2020-02-09 18:24:34 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-02-29 20:24:40 +00:00
|
|
|
resolve_target_trait(sema, impl_def).map_or(vec![], |target_trait| {
|
2020-02-09 18:24:34 +00:00
|
|
|
target_trait
|
2020-02-18 17:35:10 +00:00
|
|
|
.items(sema.db)
|
2020-02-09 18:24:34 +00:00
|
|
|
.iter()
|
|
|
|
.filter(|i| match i {
|
2020-02-18 17:35:10 +00: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 15:40:08 +00:00
|
|
|
hir::AssocItem::Const(c) => c
|
2020-02-18 17:35:10 +00:00
|
|
|
.name(sema.db)
|
2020-02-11 15:40:08 +00:00
|
|
|
.map(|n| !impl_fns_consts.contains(&n.to_string()))
|
|
|
|
.unwrap_or_default(),
|
2020-02-09 18:24:34 +00:00
|
|
|
})
|
2020-02-11 16:04:30 +00:00
|
|
|
.cloned()
|
2020-02-09 18:24:34 +00:00
|
|
|
.collect()
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2020-02-11 12:57:26 +00:00
|
|
|
pub(crate) fn resolve_target_trait(
|
2020-02-18 17:35:10 +00:00
|
|
|
sema: &Semantics<RootDatabase>,
|
2020-02-29 20:24:40 +00:00
|
|
|
impl_def: &ast::ImplDef,
|
2020-02-09 18:24:34 +00:00
|
|
|
) -> Option<hir::Trait> {
|
2020-02-29 20:24:40 +00:00
|
|
|
let ast_path = impl_def
|
2020-02-09 18:24:34 +00:00
|
|
|
.target_trait()
|
|
|
|
.map(|it| it.syntax().clone())
|
|
|
|
.and_then(ast::PathType::cast)?
|
|
|
|
.path()?;
|
|
|
|
|
2020-02-18 17:35:10 +00:00
|
|
|
match sema.resolve_path(&ast_path) {
|
2020-02-09 18:24:34 +00:00
|
|
|
Some(hir::PathResolution::Def(hir::ModuleDef::Trait(def))) => Some(def),
|
|
|
|
_ => None,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-02-07 14:57:38 +00: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,
|
|
|
|
}
|
|
|
|
}
|
2020-04-29 08:38:18 +00:00
|
|
|
|
2020-04-29 09:57:06 +00:00
|
|
|
#[derive(Clone, Copy)]
|
2020-05-10 10:45:35 +00:00
|
|
|
pub enum TryEnum {
|
2020-04-29 09:57:06 +00:00
|
|
|
Result,
|
|
|
|
Option,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl TryEnum {
|
|
|
|
const ALL: [TryEnum; 2] = [TryEnum::Option, TryEnum::Result];
|
|
|
|
|
2020-05-10 10:45:35 +00:00
|
|
|
pub fn from_ty(sema: &Semantics<RootDatabase>, ty: &Type) -> Option<TryEnum> {
|
2020-04-29 09:57:06 +00:00
|
|
|
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(crate) fn happy_case(self) -> &'static str {
|
|
|
|
match self {
|
|
|
|
TryEnum::Result => "Ok",
|
|
|
|
TryEnum::Option => "Some",
|
2020-04-29 08:38:18 +00:00
|
|
|
}
|
2020-04-29 09:57:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
pub(crate) 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::placeholder_pat().into()),
|
|
|
|
)
|
|
|
|
.into(),
|
|
|
|
TryEnum::Option => make::bind_pat(make::name("None")).into(),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn type_name(self) -> &'static str {
|
|
|
|
match self {
|
|
|
|
TryEnum::Result => "Result",
|
|
|
|
TryEnum::Option => "Option",
|
|
|
|
}
|
|
|
|
}
|
2020-04-29 08:38:18 +00:00
|
|
|
}
|
2020-04-29 12:49:54 +00:00
|
|
|
|
|
|
|
/// Helps with finding well-know things inside the standard library. This is
|
|
|
|
/// somewhat similar to the known paths infra inside hir, but it different; We
|
|
|
|
/// want to make sure that IDE specific paths don't become interesting inside
|
|
|
|
/// the compiler itself as well.
|
|
|
|
pub(crate) struct FamousDefs<'a, 'b>(pub(crate) &'a Semantics<'b, RootDatabase>, pub(crate) Crate);
|
|
|
|
|
|
|
|
#[allow(non_snake_case)]
|
|
|
|
impl FamousDefs<'_, '_> {
|
|
|
|
#[cfg(test)]
|
|
|
|
pub(crate) const FIXTURE: &'static str = r#"
|
|
|
|
//- /libcore.rs crate:core
|
|
|
|
pub mod convert{
|
|
|
|
pub trait From<T> {
|
|
|
|
fn from(T) -> Self;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub mod prelude { pub use crate::convert::From }
|
|
|
|
#[prelude_import]
|
|
|
|
pub use prelude::*;
|
|
|
|
"#;
|
|
|
|
|
|
|
|
pub(crate) fn core_convert_From(&self) -> Option<Trait> {
|
|
|
|
self.find_trait("core:convert:From")
|
|
|
|
}
|
|
|
|
|
|
|
|
fn find_trait(&self, path: &str) -> Option<Trait> {
|
|
|
|
let db = self.0.db;
|
|
|
|
let mut path = path.split(':');
|
|
|
|
let trait_ = path.next_back()?;
|
|
|
|
let std_crate = path.next()?;
|
|
|
|
let std_crate = self
|
|
|
|
.1
|
|
|
|
.dependencies(db)
|
|
|
|
.into_iter()
|
|
|
|
.find(|dep| &dep.name.to_string() == std_crate)?
|
|
|
|
.krate;
|
|
|
|
|
|
|
|
let mut module = std_crate.root_module(db)?;
|
|
|
|
for segment in path {
|
|
|
|
module = module.children(db).find_map(|child| {
|
|
|
|
let name = child.name(db)?;
|
|
|
|
if &name.to_string() == segment {
|
|
|
|
Some(child)
|
|
|
|
} else {
|
|
|
|
None
|
|
|
|
}
|
|
|
|
})?;
|
|
|
|
}
|
|
|
|
let def =
|
|
|
|
module.scope(db, None).into_iter().find(|(name, _def)| &name.to_string() == trait_)?.1;
|
|
|
|
match def {
|
|
|
|
hir::ScopeDef::ModuleDef(hir::ModuleDef::Trait(it)) => Some(it),
|
|
|
|
_ => None,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|