2 helpers::{mod_path_to_ast, FamousDefs},
3 traits::resolve_target_trait,
5 use syntax::ast::{self, AstNode, NameOwner};
7 use crate::{AssistContext, AssistId, AssistKind, Assists};
9 // FIXME: this should be a diagnostic
11 // Assist: convert_into_to_from
13 // Converts an Into impl to an equivalent From impl.
16 // # //- minicore: from
17 // impl $0Into<Thing> for usize {
18 // fn into(self) -> Thing {
20 // b: self.to_string(),
28 // impl From<usize> for Thing {
29 // fn from(val: usize) -> Self {
31 // b: val.to_string(),
37 pub(crate) fn convert_into_to_from(acc: &mut Assists, ctx: &AssistContext) -> Option<()> {
38 let impl_ = ctx.find_node_at_offset::<ast::Impl>()?;
39 let src_type = impl_.self_ty()?;
40 let ast_trait = impl_.trait_()?;
42 let module = ctx.sema.scope(impl_.syntax()).module()?;
44 let trait_ = resolve_target_trait(&ctx.sema, &impl_)?;
45 if trait_ != FamousDefs(&ctx.sema, Some(module.krate())).core_convert_Into()? {
50 let src_type_path = src_type.syntax().descendants().find_map(ast::Path::cast)?;
51 let src_type_def = match ctx.sema.resolve_path(&src_type_path) {
52 Some(hir::PathResolution::Def(module_def)) => module_def,
56 mod_path_to_ast(&module.find_use_path(ctx.db(), src_type_def)?)
59 let dest_type = match &ast_trait {
60 ast::Type::PathType(path) => {
61 path.path()?.segment()?.generic_arg_list()?.generic_args().next()?
66 let into_fn = impl_.assoc_item_list()?.assoc_items().find_map(|item| {
67 if let ast::AssocItem::Fn(f) = item {
68 if f.name()?.text() == "into" {
75 let into_fn_name = into_fn.name()?;
76 let into_fn_params = into_fn.param_list()?;
77 let into_fn_return = into_fn.ret_type()?;
83 .filter_map(ast::NameRef::cast)
84 .filter(|name| name.text() == "self" || name.text() == "Self");
87 AssistId("convert_into_to_from", AssistKind::RefactorRewrite),
88 "Convert Into to From",
89 impl_.syntax().text_range(),
91 builder.replace(src_type.syntax().text_range(), dest_type.to_string());
92 builder.replace(ast_trait.syntax().text_range(), format!("From<{}>", src_type));
93 builder.replace(into_fn_return.syntax().text_range(), "-> Self");
95 into_fn_params.syntax().text_range(),
96 format!("(val: {})", src_type.to_string()),
98 builder.replace(into_fn_name.syntax().text_range(), "from");
101 match s.text().as_ref() {
102 "self" => builder.replace(s.syntax().text_range(), "val"),
103 "Self" => builder.replace(s.syntax().text_range(), src_type_path.to_string()),
115 use crate::tests::{check_assist, check_assist_not_applicable};
118 fn convert_into_to_from_converts_a_struct() {
120 convert_into_to_from,
128 impl $0core::convert::Into<Thing> for usize {
129 fn into(self) -> Thing {
143 impl From<usize> for Thing {
144 fn from(val: usize) -> Self {
156 fn convert_into_to_from_converts_enums() {
158 convert_into_to_from,
166 impl $0core::convert::Into<String> for Thing {
167 fn into(self) -> String {
181 impl From<Thing> for String {
182 fn from(val: Thing) -> Self {
194 fn convert_into_to_from_on_enum_with_lifetimes() {
196 convert_into_to_from,
204 impl<'a> $0core::convert::Into<&'a str> for Thing<'a> {
205 fn into(self) -> &'a str {
219 impl<'a> From<Thing<'a>> for &'a str {
220 fn from(val: Thing<'a>) -> Self {
232 fn convert_into_to_from_works_on_references() {
234 convert_into_to_from,
237 struct Thing(String);
239 impl $0core::convert::Into<String> for &Thing {
240 fn into(self) -> Thing {
246 struct Thing(String);
248 impl From<&Thing> for String {
249 fn from(val: &Thing) -> Self {
258 fn convert_into_to_from_works_on_qualified_structs() {
260 convert_into_to_from,
264 pub struct Thing(String);
265 pub struct BetterThing(String);
268 impl $0core::convert::Into<things::BetterThing> for &things::Thing {
269 fn into(self) -> Thing {
270 things::BetterThing(self.0.clone())
276 pub struct Thing(String);
277 pub struct BetterThing(String);
280 impl From<&things::Thing> for things::BetterThing {
281 fn from(val: &things::Thing) -> Self {
282 things::BetterThing(val.0.clone())
290 fn convert_into_to_from_works_on_qualified_enums() {
292 convert_into_to_from,
299 pub struct BetterThing {
304 impl $0core::convert::Into<things::BetterThing> for &things::Thing {
305 fn into(self) -> Thing {
307 Self::A(s) => things::BetterThing::B(s)
317 pub struct BetterThing {
322 impl From<&things::Thing> for things::BetterThing {
323 fn from(val: &things::Thing) -> Self {
325 things::Thing::A(s) => things::BetterThing::B(s)
334 fn convert_into_to_from_not_applicable_on_any_trait_named_into() {
335 check_assist_not_applicable(
336 convert_into_to_from,
340 pub fn into(self) -> T;
347 impl $0Into<Thing> for String {
348 fn into(self) -> Thing {