2019-04-22 10:01:33 +00:00
|
|
|
use crate::{Assist, AssistId, AssistCtx, ast_editor::{AstEditor, AstBuilder}};
|
2019-03-06 22:45:11 +00:00
|
|
|
|
2019-03-06 13:41:22 +00:00
|
|
|
use hir::db::HirDatabase;
|
2019-04-22 10:01:33 +00:00
|
|
|
use ra_syntax::{SmolStr, TreeArc};
|
|
|
|
use ra_syntax::ast::{self, AstNode, FnDef, ImplItem, ImplItemKind, NameOwner};
|
2019-03-06 22:45:11 +00:00
|
|
|
use ra_db::FilePosition;
|
|
|
|
|
2019-03-23 15:06:25 +00: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> {
|
2019-03-16 22:19:14 +00:00
|
|
|
let impl_node = ctx.node_at_offset::<ast::ImplBlock>()?;
|
2019-03-07 00:48:31 +00:00
|
|
|
let impl_item_list = impl_node.item_list()?;
|
2019-03-06 22:45:11 +00:00
|
|
|
|
|
|
|
let trait_def = {
|
2019-03-16 22:19:14 +00:00
|
|
|
let file_id = ctx.frange.file_id;
|
|
|
|
let position = FilePosition { file_id, offset: impl_node.syntax().range().start() };
|
2019-04-12 21:44:47 +00:00
|
|
|
let analyzer = hir::SourceAnalyzer::new(ctx.db, position.file_id, impl_node.syntax(), None);
|
2019-03-06 22:45:11 +00:00
|
|
|
|
2019-04-11 14:15:20 +00:00
|
|
|
resolve_target_trait_def(ctx.db, &analyzer, impl_node)?
|
2019-03-06 22:45:11 +00:00
|
|
|
};
|
|
|
|
|
2019-03-07 15:20:37 +00:00
|
|
|
let missing_fns: Vec<_> = {
|
|
|
|
let fn_def_opt = |kind| if let ImplItemKind::FnDef(def) = kind { Some(def) } else { None };
|
|
|
|
let def_name = |def| -> Option<&SmolStr> { FnDef::name(def).map(ast::Name::text) };
|
2019-03-06 22:45:11 +00:00
|
|
|
|
2019-03-07 15:20:37 +00:00
|
|
|
let trait_items =
|
|
|
|
trait_def.syntax().descendants().find_map(ast::ItemList::cast)?.impl_items();
|
|
|
|
let impl_items = impl_item_list.impl_items();
|
2019-03-06 22:45:11 +00:00
|
|
|
|
2019-05-05 17:28:22 +00:00
|
|
|
let trait_fns = trait_items.map(ImplItem::kind).filter_map(fn_def_opt);
|
2019-03-07 15:20:37 +00:00
|
|
|
let impl_fns = impl_items.map(ImplItem::kind).filter_map(fn_def_opt).collect::<Vec<_>>();
|
2019-03-06 22:45:11 +00:00
|
|
|
|
2019-03-07 15:20:37 +00:00
|
|
|
trait_fns
|
|
|
|
.filter(|t| def_name(t).is_some())
|
2019-03-23 15:06:25 +00:00
|
|
|
.filter(|t| match mode {
|
|
|
|
AddMissingImplMembersMode::DefaultMethodsOnly => t.body().is_some(),
|
|
|
|
AddMissingImplMembersMode::NoDefaultMethods => t.body().is_none(),
|
|
|
|
})
|
2019-03-07 15:20:37 +00:00
|
|
|
.filter(|t| impl_fns.iter().all(|i| def_name(i) != def_name(t)))
|
|
|
|
.collect()
|
|
|
|
};
|
2019-03-07 00:48:31 +00:00
|
|
|
if missing_fns.is_empty() {
|
|
|
|
return None;
|
|
|
|
}
|
|
|
|
|
2019-03-23 15:06:25 +00:00
|
|
|
ctx.add_action(AssistId(assist_id), label, |edit| {
|
2019-04-22 10:01:33 +00:00
|
|
|
let n_existing_items = impl_item_list.impl_items().count();
|
|
|
|
let fns = missing_fns.into_iter().map(add_body_and_strip_docstring).collect::<Vec<_>>();
|
|
|
|
|
|
|
|
let mut ast_editor = AstEditor::new(impl_item_list);
|
|
|
|
if n_existing_items == 0 {
|
|
|
|
ast_editor.make_multiline();
|
|
|
|
}
|
|
|
|
ast_editor.append_functions(fns.iter().map(|it| &**it));
|
|
|
|
let first_new_item = ast_editor.ast().impl_items().nth(n_existing_items).unwrap();
|
|
|
|
let cursor_poisition = first_new_item.syntax().range().start();
|
|
|
|
ast_editor.into_text_edit(edit.text_edit_builder());
|
|
|
|
|
|
|
|
edit.set_cursor(cursor_poisition);
|
2019-03-07 00:48:31 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
ctx.build()
|
2019-03-06 13:41:22 +00:00
|
|
|
}
|
|
|
|
|
2019-04-22 10:01:33 +00:00
|
|
|
fn add_body_and_strip_docstring(fn_def: &ast::FnDef) -> TreeArc<ast::FnDef> {
|
|
|
|
let mut ast_editor = AstEditor::new(fn_def);
|
|
|
|
if fn_def.body().is_none() {
|
|
|
|
ast_editor.set_body(&AstBuilder::<ast::Block>::single_expr(
|
|
|
|
&AstBuilder::<ast::Expr>::unimplemented(),
|
|
|
|
));
|
|
|
|
}
|
|
|
|
ast_editor.strip_attrs_and_docs();
|
|
|
|
ast_editor.ast().to_owned()
|
|
|
|
}
|
|
|
|
|
2019-03-16 22:24:17 +00:00
|
|
|
/// 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 12:51:02 +00:00
|
|
|
analyzer: &hir::SourceAnalyzer,
|
2019-03-16 22:24:17 +00:00
|
|
|
impl_block: &ast::ImplBlock,
|
|
|
|
) -> Option<TreeArc<ast::TraitDef>> {
|
2019-04-10 08:15:55 +00:00
|
|
|
let ast_path =
|
|
|
|
impl_block.target_trait().map(AstNode::syntax).and_then(ast::PathType::cast)?.path()?;
|
2019-03-16 22:24:17 +00:00
|
|
|
|
2019-04-11 12:51:02 +00:00
|
|
|
match analyzer.resolve_path(db, &ast_path) {
|
2019-04-10 08:15:55 +00:00
|
|
|
Some(hir::PathResolution::Def(hir::ModuleDef::Trait(def))) => Some(def.source(db).1),
|
2019-03-16 22:24:17 +00:00
|
|
|
_ => None,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-03-06 13:41:22 +00:00
|
|
|
#[cfg(test)]
|
|
|
|
mod tests {
|
|
|
|
use super::*;
|
2019-03-07 12:21:56 +00:00
|
|
|
use crate::helpers::{check_assist, check_assist_not_applicable};
|
2019-03-06 13:41:22 +00:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_add_missing_impl_members() {
|
|
|
|
check_assist(
|
|
|
|
add_missing_impl_members,
|
|
|
|
"
|
|
|
|
trait Foo {
|
|
|
|
fn foo(&self);
|
2019-03-07 00:48:31 +00:00
|
|
|
fn bar(&self);
|
2019-03-07 11:02:53 +00:00
|
|
|
fn baz(&self);
|
2019-03-06 13:41:22 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
struct S;
|
|
|
|
|
|
|
|
impl Foo for S {
|
2019-03-07 00:48:31 +00:00
|
|
|
fn bar(&self) {}
|
2019-04-22 10:01:33 +00:00
|
|
|
<|>
|
2019-03-06 13:41:22 +00:00
|
|
|
}",
|
|
|
|
"
|
|
|
|
trait Foo {
|
|
|
|
fn foo(&self);
|
2019-03-07 00:48:31 +00:00
|
|
|
fn bar(&self);
|
2019-03-07 11:02:53 +00:00
|
|
|
fn baz(&self);
|
2019-03-06 13:41:22 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
struct S;
|
|
|
|
|
|
|
|
impl Foo for S {
|
2019-03-07 00:48:31 +00:00
|
|
|
fn bar(&self) {}
|
2019-04-22 10:01:33 +00:00
|
|
|
<|>fn foo(&self) { unimplemented!() }
|
|
|
|
fn baz(&self) { unimplemented!() }
|
|
|
|
|
2019-03-06 13:41:22 +00:00
|
|
|
}",
|
|
|
|
);
|
|
|
|
}
|
2019-03-07 12:21:56 +00:00
|
|
|
|
2019-03-07 12:44:01 +00:00
|
|
|
#[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) {}
|
2019-04-22 10:01:33 +00:00
|
|
|
<|>
|
2019-03-07 12:44:01 +00:00
|
|
|
}",
|
|
|
|
"
|
|
|
|
trait Foo {
|
|
|
|
fn foo(&self);
|
|
|
|
fn bar(&self) -> bool { true }
|
|
|
|
fn baz(&self) -> u32 { 42 }
|
|
|
|
}
|
|
|
|
|
|
|
|
struct S;
|
|
|
|
|
|
|
|
impl Foo for S {
|
|
|
|
fn bar(&self) {}
|
2019-04-22 10:01:33 +00:00
|
|
|
<|>fn foo(&self) { unimplemented!() }
|
|
|
|
|
2019-03-07 12:44:01 +00:00
|
|
|
}",
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2019-03-07 12:21:56 +00:00
|
|
|
#[test]
|
|
|
|
fn test_empty_impl_block() {
|
|
|
|
check_assist(
|
|
|
|
add_missing_impl_members,
|
|
|
|
"
|
|
|
|
trait Foo { fn foo(&self); }
|
|
|
|
struct S;
|
2019-03-23 14:49:20 +00:00
|
|
|
impl Foo for S { <|> }",
|
2019-03-07 12:21:56 +00:00
|
|
|
"
|
|
|
|
trait Foo { fn foo(&self); }
|
|
|
|
struct S;
|
|
|
|
impl Foo for S {
|
2019-04-22 10:01:33 +00:00
|
|
|
<|>fn foo(&self) { unimplemented!() }
|
2019-03-07 12:21:56 +00:00
|
|
|
}",
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_cursor_after_empty_impl_block() {
|
2019-03-16 22:19:14 +00:00
|
|
|
check_assist(
|
2019-03-07 12:21:56 +00:00
|
|
|
add_missing_impl_members,
|
|
|
|
"
|
|
|
|
trait Foo { fn foo(&self); }
|
|
|
|
struct S;
|
|
|
|
impl Foo for S {}<|>",
|
2019-03-16 22:19:14 +00:00
|
|
|
"
|
|
|
|
trait Foo { fn foo(&self); }
|
|
|
|
struct S;
|
|
|
|
impl Foo for S {
|
2019-04-22 10:01:33 +00:00
|
|
|
<|>fn foo(&self) { unimplemented!() }
|
2019-03-16 22:19:14 +00:00
|
|
|
}",
|
2019-03-07 12:21:56 +00:00
|
|
|
)
|
|
|
|
}
|
2019-03-07 12:44:01 +00:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_empty_trait() {
|
|
|
|
check_assist_not_applicable(
|
|
|
|
add_missing_impl_members,
|
|
|
|
"
|
|
|
|
trait Foo;
|
|
|
|
struct S;
|
|
|
|
impl Foo for S { <|> }",
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
2019-03-23 14:49:20 +00:00
|
|
|
fn test_ignore_unnamed_trait_members_and_default_methods() {
|
|
|
|
check_assist_not_applicable(
|
2019-03-07 12:44:01 +00:00
|
|
|
add_missing_impl_members,
|
|
|
|
"
|
|
|
|
trait Foo {
|
|
|
|
fn (arg: u32);
|
|
|
|
fn valid(some: u32) -> bool { false }
|
|
|
|
}
|
|
|
|
struct S;
|
|
|
|
impl Foo for S { <|> }",
|
2019-03-07 15:10:21 +00:00
|
|
|
)
|
|
|
|
}
|
|
|
|
|
2019-03-23 14:49:20 +00:00
|
|
|
#[test]
|
|
|
|
fn test_with_docstring_and_attrs() {
|
|
|
|
check_assist(
|
|
|
|
add_missing_impl_members,
|
|
|
|
r#"
|
|
|
|
#[doc(alias = "test alias")]
|
|
|
|
trait Foo {
|
|
|
|
/// doc string
|
|
|
|
#[must_use]
|
|
|
|
fn foo(&self);
|
|
|
|
}
|
|
|
|
struct S;
|
|
|
|
impl Foo for S {}<|>"#,
|
|
|
|
r#"
|
|
|
|
#[doc(alias = "test alias")]
|
|
|
|
trait Foo {
|
|
|
|
/// doc string
|
|
|
|
#[must_use]
|
|
|
|
fn foo(&self);
|
|
|
|
}
|
|
|
|
struct S;
|
|
|
|
impl Foo for S {
|
2019-04-22 10:01:33 +00:00
|
|
|
<|>fn foo(&self) { unimplemented!() }
|
2019-03-23 14:49:20 +00:00
|
|
|
}"#,
|
|
|
|
)
|
|
|
|
}
|
2019-03-23 15:06:25 +00:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_default_methods() {
|
|
|
|
check_assist(
|
|
|
|
add_missing_default_members,
|
|
|
|
"
|
|
|
|
trait Foo {
|
|
|
|
fn valid(some: u32) -> bool { false }
|
|
|
|
fn foo(some: u32) -> bool;
|
|
|
|
}
|
|
|
|
struct S;
|
|
|
|
impl Foo for S { <|> }",
|
|
|
|
"
|
|
|
|
trait Foo {
|
|
|
|
fn valid(some: u32) -> bool { false }
|
|
|
|
fn foo(some: u32) -> bool;
|
|
|
|
}
|
|
|
|
struct S;
|
|
|
|
impl Foo for S {
|
2019-04-22 10:01:33 +00:00
|
|
|
<|>fn valid(some: u32) -> bool { false }
|
2019-03-23 15:06:25 +00:00
|
|
|
}",
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
2019-03-06 13:41:22 +00:00
|
|
|
}
|