rust/crates/ra_assists/src/assists/add_missing_impl_members.rs

333 lines
7.3 KiB
Rust
Raw Normal View History

//! FIXME: write short doc here
2019-08-22 13:31:21 -05:00
use hir::{db::HirDatabase, HasSource};
use ra_syntax::{
2019-09-28 11:50:16 -05:00
ast::{self, edit, make, AstNode, NameOwner},
2019-08-22 13:31:21 -05:00
SmolStr,
};
use crate::{Assist, AssistCtx, AssistId};
#[derive(PartialEq)]
2019-03-23 10:06:25 -05:00
enum AddMissingImplMembersMode {
DefaultMethodsOnly,
NoDefaultMethods,
}
pub(crate) fn add_missing_impl_members(ctx: AssistCtx<impl HirDatabase>) -> Option<Assist> {
add_missing_impl_members_inner(
ctx,
AddMissingImplMembersMode::NoDefaultMethods,
"add_impl_missing_members",
"add missing impl members",
)
}
pub(crate) fn add_missing_default_members(ctx: AssistCtx<impl HirDatabase>) -> Option<Assist> {
add_missing_impl_members_inner(
ctx,
AddMissingImplMembersMode::DefaultMethodsOnly,
"add_impl_default_members",
"add impl default members",
)
}
fn add_missing_impl_members_inner(
mut ctx: AssistCtx<impl HirDatabase>,
mode: AddMissingImplMembersMode,
assist_id: &'static str,
label: &'static str,
) -> Option<Assist> {
let impl_node = ctx.node_at_offset::<ast::ImplBlock>()?;
2019-03-06 18:48:31 -06:00
let impl_item_list = impl_node.item_list()?;
let trait_def = {
let file_id = ctx.frange.file_id;
2019-08-22 13:31:21 -05:00
let analyzer = hir::SourceAnalyzer::new(ctx.db, file_id, impl_node.syntax(), None);
2019-07-19 03:24:41 -05:00
resolve_target_trait_def(ctx.db, &analyzer, &impl_node)?
};
2019-08-19 06:11:09 -05:00
let def_name = |item: &ast::ImplItem| -> Option<SmolStr> {
match item {
ast::ImplItem::FnDef(def) => def.name(),
ast::ImplItem::TypeAliasDef(def) => def.name(),
ast::ImplItem::ConstDef(def) => def.name(),
}
2019-07-19 03:24:41 -05:00
.map(|it| it.text().clone())
2019-03-07 09:20:37 -06:00
};
let trait_items = trait_def.item_list()?.impl_items();
let impl_items = impl_item_list.impl_items().collect::<Vec<_>>();
let missing_items: Vec<_> = trait_items
2019-08-19 06:11:09 -05:00
.filter(|t| def_name(t).is_some())
.filter(|t| match t {
ast::ImplItem::FnDef(def) => match mode {
AddMissingImplMembersMode::DefaultMethodsOnly => def.body().is_some(),
AddMissingImplMembersMode::NoDefaultMethods => def.body().is_none(),
},
_ => mode == AddMissingImplMembersMode::NoDefaultMethods,
})
2019-08-19 06:11:09 -05:00
.filter(|t| impl_items.iter().all(|i| def_name(i) != def_name(t)))
.collect();
if missing_items.is_empty() {
2019-03-06 18:48:31 -06:00
return None;
}
2019-03-23 10:06:25 -05:00
ctx.add_action(AssistId(assist_id), label, |edit| {
let n_existing_items = impl_item_list.impl_items().count();
2019-09-30 02:08:28 -05:00
let items = missing_items
.into_iter()
.map(|it| match it {
ast::ImplItem::FnDef(def) => ast::ImplItem::FnDef(add_body(def)),
_ => it,
})
.map(|it| edit::strip_attrs_and_docs(&it));
let new_impl_item_list = impl_item_list.append_items(items);
let cursor_position = {
let first_new_item = new_impl_item_list.impl_items().nth(n_existing_items).unwrap();
first_new_item.syntax().text_range().start()
};
edit.replace_ast(impl_item_list, new_impl_item_list);
edit.set_cursor(cursor_position);
2019-03-06 18:48:31 -06:00
});
ctx.build()
}
2019-07-19 03:24:41 -05:00
fn add_body(fn_def: ast::FnDef) -> ast::FnDef {
if fn_def.body().is_none() {
2019-09-26 14:08:44 -05:00
fn_def.with_body(make::block_from_expr(make::expr_unimplemented()))
} else {
fn_def
}
}
/// Given an `ast::ImplBlock`, resolves the target trait (the one being
/// implemented) to a `ast::TraitDef`.
fn resolve_target_trait_def(
db: &impl HirDatabase,
2019-04-11 07:51:02 -05:00
analyzer: &hir::SourceAnalyzer,
impl_block: &ast::ImplBlock,
2019-07-19 03:24:41 -05:00
) -> Option<ast::TraitDef> {
let ast_path = impl_block
.target_trait()
.map(|it| it.syntax().clone())
.and_then(ast::PathType::cast)?
.path()?;
2019-04-11 07:51:02 -05:00
match analyzer.resolve_path(db, &ast_path) {
2019-06-11 09:34:01 -05:00
Some(hir::PathResolution::Def(hir::ModuleDef::Trait(def))) => Some(def.source(db).ast),
_ => None,
}
}
#[cfg(test)]
mod tests {
use super::*;
use crate::helpers::{check_assist, check_assist_not_applicable};
#[test]
fn test_add_missing_impl_members() {
check_assist(
add_missing_impl_members,
"
trait Foo {
type Output;
const CONST: usize = 42;
fn foo(&self);
2019-03-06 18:48:31 -06:00
fn bar(&self);
fn baz(&self);
}
struct S;
impl Foo for S {
2019-03-06 18:48:31 -06:00
fn bar(&self) {}
<|>
}",
"
trait Foo {
type Output;
const CONST: usize = 42;
fn foo(&self);
2019-03-06 18:48:31 -06:00
fn bar(&self);
fn baz(&self);
}
struct S;
impl Foo for S {
2019-03-06 18:48:31 -06:00
fn bar(&self) {}
<|>type Output;
const CONST: usize = 42;
fn foo(&self) { unimplemented!() }
fn baz(&self) { unimplemented!() }
}",
);
}
#[test]
fn test_copied_overriden_members() {
check_assist(
add_missing_impl_members,
"
trait Foo {
fn foo(&self);
fn bar(&self) -> bool { true }
fn baz(&self) -> u32 { 42 }
}
struct S;
impl Foo for S {
fn bar(&self) {}
<|>
}",
"
trait Foo {
fn foo(&self);
fn bar(&self) -> bool { true }
fn baz(&self) -> u32 { 42 }
}
struct S;
impl Foo for S {
fn bar(&self) {}
<|>fn foo(&self) { unimplemented!() }
}",
);
}
#[test]
fn test_empty_impl_block() {
check_assist(
add_missing_impl_members,
"
trait Foo { fn foo(&self); }
struct S;
impl Foo for S { <|> }",
"
trait Foo { fn foo(&self); }
struct S;
impl Foo for S {
<|>fn foo(&self) { unimplemented!() }
}",
);
}
#[test]
fn test_cursor_after_empty_impl_block() {
check_assist(
add_missing_impl_members,
"
trait Foo { fn foo(&self); }
struct S;
impl Foo for S {}<|>",
"
trait Foo { fn foo(&self); }
struct S;
impl Foo for S {
<|>fn foo(&self) { unimplemented!() }
}",
)
}
#[test]
fn test_empty_trait() {
check_assist_not_applicable(
add_missing_impl_members,
"
trait Foo;
struct S;
impl Foo for S { <|> }",
)
}
#[test]
fn test_ignore_unnamed_trait_members_and_default_methods() {
check_assist_not_applicable(
add_missing_impl_members,
"
trait Foo {
fn (arg: u32);
fn valid(some: u32) -> bool { false }
}
struct S;
impl Foo for S { <|> }",
)
}
#[test]
fn test_with_docstring_and_attrs() {
check_assist(
add_missing_impl_members,
r#"
#[doc(alias = "test alias")]
trait Foo {
/// doc string
type Output;
#[must_use]
fn foo(&self);
}
struct S;
impl Foo for S {}<|>"#,
r#"
#[doc(alias = "test alias")]
trait Foo {
/// doc string
type Output;
#[must_use]
fn foo(&self);
}
struct S;
impl Foo for S {
<|>type Output;
fn foo(&self) { unimplemented!() }
}"#,
)
}
2019-03-23 10:06:25 -05:00
#[test]
fn test_default_methods() {
check_assist(
add_missing_default_members,
"
trait Foo {
type Output;
const CONST: usize = 42;
2019-03-23 10:06:25 -05:00
fn valid(some: u32) -> bool { false }
fn foo(some: u32) -> bool;
}
struct S;
impl Foo for S { <|> }",
"
trait Foo {
type Output;
const CONST: usize = 42;
2019-03-23 10:06:25 -05:00
fn valid(some: u32) -> bool { false }
fn foo(some: u32) -> bool;
}
struct S;
impl Foo for S {
<|>fn valid(some: u32) -> bool { false }
2019-03-23 10:06:25 -05:00
}",
)
}
}