2019-03-06 18:48:31 -06:00
|
|
|
use crate::{Assist, AssistId, AssistCtx};
|
2019-03-06 16:45:11 -06:00
|
|
|
|
|
|
|
use hir::Resolver;
|
2019-03-06 07:41:22 -06:00
|
|
|
use hir::db::HirDatabase;
|
2019-03-07 06:21:56 -06:00
|
|
|
use ra_syntax::{SmolStr, SyntaxKind, TextRange, TextUnit, TreeArc};
|
2019-03-06 16:45:11 -06:00
|
|
|
use ra_syntax::ast::{self, AstNode, FnDef, ImplItem, ImplItemKind, NameOwner};
|
|
|
|
use ra_db::FilePosition;
|
2019-03-06 18:48:31 -06:00
|
|
|
use ra_fmt::{leading_indent, reindent};
|
|
|
|
|
|
|
|
use itertools::Itertools;
|
2019-03-06 16:45:11 -06:00
|
|
|
|
|
|
|
/// Given an `ast::ImplBlock`, resolves the target trait (the one being
|
|
|
|
/// implemented) to a `ast::TraitDef`.
|
2019-03-16 17:19:14 -05:00
|
|
|
fn resolve_target_trait_def(
|
2019-03-06 16:45:11 -06:00
|
|
|
db: &impl HirDatabase,
|
|
|
|
resolver: &Resolver,
|
|
|
|
impl_block: &ast::ImplBlock,
|
|
|
|
) -> Option<TreeArc<ast::TraitDef>> {
|
|
|
|
let ast_path = impl_block.target_trait().map(AstNode::syntax).and_then(ast::PathType::cast)?;
|
|
|
|
let hir_path = ast_path.path().and_then(hir::Path::from_ast)?;
|
|
|
|
|
|
|
|
match resolver.resolve_path(db, &hir_path).take_types() {
|
|
|
|
Some(hir::Resolution::Def(hir::ModuleDef::Trait(def))) => Some(def.source(db).1),
|
|
|
|
_ => None,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-03-16 17:19:14 -05:00
|
|
|
fn build_func_body(def: &ast::FnDef) -> String {
|
2019-03-06 18:48:31 -06:00
|
|
|
let mut buf = String::new();
|
|
|
|
|
|
|
|
for child in def.syntax().children() {
|
|
|
|
if child.kind() == SyntaxKind::SEMI {
|
|
|
|
buf.push_str(" { unimplemented!() }")
|
|
|
|
} else {
|
|
|
|
child.text().push_to(&mut buf);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
buf.trim_end().to_string()
|
|
|
|
}
|
|
|
|
|
|
|
|
pub(crate) fn add_missing_impl_members(mut ctx: AssistCtx<impl HirDatabase>) -> Option<Assist> {
|
2019-03-16 17:19:14 -05:00
|
|
|
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()?;
|
2019-03-06 16:45:11 -06:00
|
|
|
|
|
|
|
let trait_def = {
|
2019-03-16 17:19:14 -05:00
|
|
|
let file_id = ctx.frange.file_id;
|
|
|
|
let position = FilePosition { file_id, offset: impl_node.syntax().range().start() };
|
2019-03-07 09:10:03 -06:00
|
|
|
let resolver = hir::source_binder::resolver_for_position(ctx.db, position);
|
2019-03-06 16:45:11 -06:00
|
|
|
|
2019-03-07 09:10:03 -06:00
|
|
|
resolve_target_trait_def(ctx.db, &resolver, impl_node)?
|
2019-03-06 16:45:11 -06:00
|
|
|
};
|
|
|
|
|
2019-03-07 09:20:37 -06: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 16:45:11 -06:00
|
|
|
|
2019-03-07 09:20:37 -06: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 16:45:11 -06:00
|
|
|
|
2019-03-07 09:20:37 -06:00
|
|
|
let trait_fns = trait_items.map(ImplItem::kind).filter_map(fn_def_opt).collect::<Vec<_>>();
|
|
|
|
let impl_fns = impl_items.map(ImplItem::kind).filter_map(fn_def_opt).collect::<Vec<_>>();
|
2019-03-06 16:45:11 -06:00
|
|
|
|
2019-03-07 09:20:37 -06:00
|
|
|
trait_fns
|
|
|
|
.into_iter()
|
|
|
|
.filter(|t| def_name(t).is_some())
|
|
|
|
.filter(|t| impl_fns.iter().all(|i| def_name(i) != def_name(t)))
|
|
|
|
.collect()
|
|
|
|
};
|
2019-03-06 18:48:31 -06:00
|
|
|
if missing_fns.is_empty() {
|
|
|
|
return None;
|
|
|
|
}
|
|
|
|
|
2019-03-07 05:02:53 -06:00
|
|
|
ctx.add_action(AssistId("add_impl_missing_members"), "add missing impl members", |edit| {
|
2019-03-07 09:10:21 -06:00
|
|
|
let (parent_indent, indent) = {
|
2019-03-07 05:02:53 -06:00
|
|
|
// FIXME: Find a way to get the indent already used in the file.
|
|
|
|
// Now, we copy the indent of first item or indent with 4 spaces relative to impl block
|
|
|
|
const DEFAULT_INDENT: &str = " ";
|
|
|
|
let first_item = impl_item_list.impl_items().next();
|
2019-03-07 09:10:21 -06:00
|
|
|
let first_item_indent =
|
|
|
|
first_item.and_then(|i| leading_indent(i.syntax())).map(ToOwned::to_owned);
|
|
|
|
let impl_block_indent = leading_indent(impl_node.syntax()).unwrap_or_default();
|
|
|
|
|
|
|
|
(
|
|
|
|
impl_block_indent.to_owned(),
|
|
|
|
first_item_indent.unwrap_or_else(|| impl_block_indent.to_owned() + DEFAULT_INDENT),
|
|
|
|
)
|
2019-03-07 05:02:53 -06:00
|
|
|
};
|
|
|
|
|
2019-03-07 06:21:56 -06:00
|
|
|
let changed_range = {
|
2019-03-16 17:19:14 -05:00
|
|
|
let children = impl_item_list.syntax().children();
|
|
|
|
let last_whitespace = children.filter_map(ast::Whitespace::cast).last();
|
2019-03-07 06:21:56 -06:00
|
|
|
|
2019-03-16 17:19:14 -05:00
|
|
|
last_whitespace.map(|w| w.syntax().range()).unwrap_or_else(|| {
|
|
|
|
let in_brackets = impl_item_list.syntax().range().end() - TextUnit::of_str("}");
|
|
|
|
TextRange::from_to(in_brackets, in_brackets)
|
|
|
|
})
|
2019-03-07 06:21:56 -06:00
|
|
|
};
|
|
|
|
|
2019-03-07 09:20:37 -06:00
|
|
|
let func_bodies = format!("\n{}", missing_fns.into_iter().map(build_func_body).join("\n"));
|
|
|
|
let trailing_whitespace = format!("\n{}", parent_indent);
|
|
|
|
let func_bodies = reindent(&func_bodies, &indent) + &trailing_whitespace;
|
|
|
|
|
2019-03-06 18:48:31 -06:00
|
|
|
let replaced_text_range = TextUnit::of_str(&func_bodies);
|
|
|
|
|
|
|
|
edit.replace(changed_range, func_bodies);
|
2019-03-07 09:10:21 -06:00
|
|
|
edit.set_cursor(
|
|
|
|
changed_range.start() + replaced_text_range - TextUnit::of_str(&trailing_whitespace),
|
|
|
|
);
|
2019-03-06 18:48:31 -06:00
|
|
|
});
|
|
|
|
|
|
|
|
ctx.build()
|
2019-03-06 07:41:22 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
#[cfg(test)]
|
|
|
|
mod tests {
|
|
|
|
use super::*;
|
2019-03-07 06:21:56 -06:00
|
|
|
use crate::helpers::{check_assist, check_assist_not_applicable};
|
2019-03-06 07:41:22 -06:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_add_missing_impl_members() {
|
|
|
|
check_assist(
|
|
|
|
add_missing_impl_members,
|
|
|
|
"
|
|
|
|
trait Foo {
|
|
|
|
fn foo(&self);
|
2019-03-06 18:48:31 -06:00
|
|
|
fn bar(&self);
|
2019-03-07 05:02:53 -06:00
|
|
|
fn baz(&self);
|
2019-03-06 07:41:22 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
struct S;
|
|
|
|
|
|
|
|
impl Foo for S {
|
2019-03-06 18:48:31 -06:00
|
|
|
fn bar(&self) {}
|
2019-03-06 07:41:22 -06:00
|
|
|
<|>
|
|
|
|
}",
|
|
|
|
"
|
|
|
|
trait Foo {
|
|
|
|
fn foo(&self);
|
2019-03-06 18:48:31 -06:00
|
|
|
fn bar(&self);
|
2019-03-07 05:02:53 -06:00
|
|
|
fn baz(&self);
|
2019-03-06 07:41:22 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
struct S;
|
|
|
|
|
|
|
|
impl Foo for S {
|
2019-03-06 18:48:31 -06:00
|
|
|
fn bar(&self) {}
|
2019-03-07 05:02:53 -06:00
|
|
|
fn foo(&self) { unimplemented!() }
|
|
|
|
fn baz(&self) { unimplemented!() }<|>
|
2019-03-06 07:41:22 -06:00
|
|
|
}",
|
|
|
|
);
|
|
|
|
}
|
2019-03-07 06:21:56 -06:00
|
|
|
|
2019-03-07 06:44:01 -06: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) {}
|
|
|
|
<|>
|
|
|
|
}",
|
|
|
|
"
|
|
|
|
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!() }
|
|
|
|
fn baz(&self) -> u32 { 42 }<|>
|
|
|
|
}",
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2019-03-07 06:21:56 -06:00
|
|
|
#[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() {
|
2019-03-16 17:19:14 -05:00
|
|
|
check_assist(
|
2019-03-07 06:21:56 -06:00
|
|
|
add_missing_impl_members,
|
|
|
|
"
|
|
|
|
trait Foo { fn foo(&self); }
|
|
|
|
struct S;
|
|
|
|
impl Foo for S {}<|>",
|
2019-03-16 17:19:14 -05:00
|
|
|
"
|
|
|
|
trait Foo { fn foo(&self); }
|
|
|
|
struct S;
|
|
|
|
impl Foo for S {
|
|
|
|
fn foo(&self) { unimplemented!() }<|>
|
|
|
|
}",
|
2019-03-07 06:21:56 -06:00
|
|
|
)
|
|
|
|
}
|
2019-03-07 06:44:01 -06:00
|
|
|
|
|
|
|
#[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() {
|
|
|
|
check_assist(
|
|
|
|
add_missing_impl_members,
|
|
|
|
"
|
|
|
|
trait Foo {
|
|
|
|
fn (arg: u32);
|
|
|
|
fn valid(some: u32) -> bool { false }
|
|
|
|
}
|
|
|
|
struct S;
|
|
|
|
impl Foo for S { <|> }",
|
|
|
|
"
|
|
|
|
trait Foo {
|
|
|
|
fn (arg: u32);
|
|
|
|
fn valid(some: u32) -> bool { false }
|
|
|
|
}
|
|
|
|
struct S;
|
|
|
|
impl Foo for S {
|
|
|
|
fn valid(some: u32) -> bool { false }<|>
|
2019-03-07 09:10:21 -06:00
|
|
|
}",
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_indented_impl_block() {
|
|
|
|
check_assist(
|
|
|
|
add_missing_impl_members,
|
|
|
|
"
|
|
|
|
trait Foo {
|
|
|
|
fn valid(some: u32) -> bool { false }
|
|
|
|
}
|
|
|
|
struct S;
|
|
|
|
|
|
|
|
mod my_mod {
|
|
|
|
impl crate::Foo for S { <|> }
|
|
|
|
}",
|
|
|
|
"
|
|
|
|
trait Foo {
|
|
|
|
fn valid(some: u32) -> bool { false }
|
|
|
|
}
|
|
|
|
struct S;
|
|
|
|
|
|
|
|
mod my_mod {
|
|
|
|
impl crate::Foo for S {
|
|
|
|
fn valid(some: u32) -> bool { false }<|>
|
|
|
|
}
|
2019-03-07 06:44:01 -06:00
|
|
|
}",
|
|
|
|
)
|
|
|
|
}
|
2019-03-06 07:41:22 -06:00
|
|
|
}
|