From 1fee60181fea56ebe6b5e4aeb11cf9df25a1d087 Mon Sep 17 00:00:00 2001 From: Matthew Hall Date: Wed, 1 Apr 2020 22:26:41 +0100 Subject: Add impl From for enum variant assist Basically adds a From impl for tuple enum variants with one field. Added to cover the fairly common case of implementing your own Error that can be created from another one, although other use cases exist. --- .../src/handlers/add_from_impl_for_enum.rs | 218 +++++++++++++++++++++ crates/ra_assists/src/lib.rs | 2 + 2 files changed, 220 insertions(+) create mode 100644 crates/ra_assists/src/handlers/add_from_impl_for_enum.rs (limited to 'crates/ra_assists') diff --git a/crates/ra_assists/src/handlers/add_from_impl_for_enum.rs b/crates/ra_assists/src/handlers/add_from_impl_for_enum.rs new file mode 100644 index 000000000..cf94a214a --- /dev/null +++ b/crates/ra_assists/src/handlers/add_from_impl_for_enum.rs @@ -0,0 +1,218 @@ +use hir::ImplDef; +use ra_syntax::{ + ast::{self, AstNode, NameOwner}, + TextUnit, +}; +use stdx::format_to; + +use crate::{Assist, AssistCtx, AssistId}; +use ra_ide_db::RootDatabase; + +// Assist add_from_impl_for_enum +// +// Adds a From impl for an enum variant with one tuple field +// +// ``` +// enum A { <|>One(u32) } +// ``` +// -> +// ``` +// enum A { One(u32) } +// +// impl From for A { +// fn from(v: u32) -> Self { +// A::One(v) +// } +// } +// ``` +pub(crate) fn add_from_impl_for_enum(ctx: AssistCtx) -> Option { + let variant = ctx.find_node_at_offset::()?; + let variant_name = variant.name()?; + let enum_name = variant.parent_enum().name()?; + let field_list = match variant.kind() { + ast::StructKind::Tuple(field_list) => field_list, + _ => return None, + }; + if field_list.fields().count() != 1 { + return None; + } + let field_type = field_list.fields().next()?.type_ref()?; + let path = match field_type { + ast::TypeRef::PathType(p) => p, + _ => return None, + }; + + if already_has_from_impl(ctx.sema, &variant) { + return None; + } + + ctx.add_assist( + AssistId("add_from_impl_for_enum"), + "Add From impl for this enum variant", + |edit| { + let start_offset = variant.parent_enum().syntax().text_range().end(); + let mut buf = String::new(); + format_to!( + buf, + r#" + +impl From<{0}> for {1} {{ + fn from(v: {0}) -> Self {{ + {1}::{2}(v) + }} +}}"#, + path.syntax(), + enum_name, + variant_name + ); + edit.insert(start_offset, buf); + edit.set_cursor(start_offset + TextUnit::of_str("\n\n")); + }, + ) +} + +fn already_has_from_impl( + sema: &'_ hir::Semantics<'_, RootDatabase>, + variant: &ast::EnumVariant, +) -> bool { + let scope = sema.scope(&variant.syntax()); + + let from_path = ast::make::path_from_text("From"); + let from_hir_path = match hir::Path::from_ast(from_path) { + Some(p) => p, + None => return false, + }; + let from_trait = match scope.resolve_hir_path(&from_hir_path) { + Some(hir::PathResolution::Def(hir::ModuleDef::Trait(t))) => t, + _ => return false, + }; + + let e: hir::Enum = match sema.to_def(&variant.parent_enum()) { + Some(e) => e, + None => return false, + }; + let e_ty = e.ty(sema.db); + + let hir_enum_var: hir::EnumVariant = match sema.to_def(variant) { + Some(ev) => ev, + None => return false, + }; + let var_ty = hir_enum_var.fields(sema.db)[0].signature_ty(sema.db); + + let krate = match scope.module() { + Some(s) => s.krate(), + _ => return false, + }; + let impls = ImplDef::for_trait(sema.db, krate, from_trait); + let imp = impls.iter().find(|imp| { + let targets_enum = imp.target_ty(sema.db) == e_ty; + let param_matches = imp.target_trait_substs_matches(sema.db, &[var_ty.clone()]); + targets_enum && param_matches + }); + + imp.is_some() +} + +#[cfg(test)] +mod tests { + use super::*; + use crate::helpers::{check_assist, check_assist_not_applicable}; + + #[test] + fn test_add_from_impl_for_enum() { + check_assist( + add_from_impl_for_enum, + "enum A { <|>One(u32) }", + r#"enum A { One(u32) } + +<|>impl From for A { + fn from(v: u32) -> Self { + A::One(v) + } +}"#, + ); + } + + #[test] + fn test_add_from_impl_for_enum_complicated_path() { + check_assist( + add_from_impl_for_enum, + "enum A { <|>One(foo::bar::baz::Boo) }", + r#"enum A { One(foo::bar::baz::Boo) } + +<|>impl From for A { + fn from(v: foo::bar::baz::Boo) -> Self { + A::One(v) + } +}"#, + ); + } + + #[test] + fn test_add_from_impl_no_element() { + check_assist_not_applicable(add_from_impl_for_enum, "enum A { <|>One }"); + } + + #[test] + fn test_add_from_impl_more_than_one_element_in_tuple() { + check_assist_not_applicable(add_from_impl_for_enum, "enum A { <|>One(u32, String) }"); + } + + #[test] + fn test_add_from_impl_struct_variant() { + check_assist_not_applicable(add_from_impl_for_enum, "enum A { <|>One { x: u32 } }"); + } + + #[test] + fn test_add_from_impl_already_exists() { + check_assist_not_applicable( + add_from_impl_for_enum, + r#"enum A { <|>One(u32), } + +impl From for A { + fn from(v: u32) -> Self { + A::One(v) + } +} + +pub trait From { + fn from(T) -> Self; +}"#, + ); + } + + #[test] + fn test_add_from_impl_different_variant_impl_exists() { + check_assist( + add_from_impl_for_enum, + r#"enum A { <|>One(u32), Two(String), } + +impl From for A { + fn from(v: String) -> Self { + A::Two(v) + } +} + +pub trait From { + fn from(T) -> Self; +}"#, + r#"enum A { One(u32), Two(String), } + +<|>impl From for A { + fn from(v: u32) -> Self { + A::One(v) + } +} + +impl From for A { + fn from(v: String) -> Self { + A::Two(v) + } +} + +pub trait From { + fn from(T) -> Self; +}"#, + ); + } +} diff --git a/crates/ra_assists/src/lib.rs b/crates/ra_assists/src/lib.rs index fa1f3dd26..6b4c56dcd 100644 --- a/crates/ra_assists/src/lib.rs +++ b/crates/ra_assists/src/lib.rs @@ -122,6 +122,7 @@ mod handlers { mod replace_qualified_name_with_use; mod replace_unwrap_with_match; mod split_import; + mod add_from_impl_for_enum; pub(crate) fn all() -> &'static [AssistHandler] { &[ @@ -159,6 +160,7 @@ mod handlers { replace_qualified_name_with_use::replace_qualified_name_with_use, replace_unwrap_with_match::replace_unwrap_with_match, split_import::split_import, + add_from_impl_for_enum::add_from_impl_for_enum, ] } } -- cgit v1.2.3 From 6a2127be28a837215801f4ac3cd7d46ef7c4485b Mon Sep 17 00:00:00 2001 From: Matthew Hall Date: Thu, 2 Apr 2020 18:42:30 +0100 Subject: Cleanup checking for existing impls in impl From assist Use the trait solver to check if there's an existing implementation of From for the enum. --- .../ra_assists/src/handlers/add_from_impl_for_enum.rs | 18 +++--------------- 1 file changed, 3 insertions(+), 15 deletions(-) (limited to 'crates/ra_assists') diff --git a/crates/ra_assists/src/handlers/add_from_impl_for_enum.rs b/crates/ra_assists/src/handlers/add_from_impl_for_enum.rs index cf94a214a..864373aa5 100644 --- a/crates/ra_assists/src/handlers/add_from_impl_for_enum.rs +++ b/crates/ra_assists/src/handlers/add_from_impl_for_enum.rs @@ -1,4 +1,3 @@ -use hir::ImplDef; use ra_syntax::{ ast::{self, AstNode, NameOwner}, TextUnit, @@ -99,18 +98,7 @@ fn already_has_from_impl( }; let var_ty = hir_enum_var.fields(sema.db)[0].signature_ty(sema.db); - let krate = match scope.module() { - Some(s) => s.krate(), - _ => return false, - }; - let impls = ImplDef::for_trait(sema.db, krate, from_trait); - let imp = impls.iter().find(|imp| { - let targets_enum = imp.target_ty(sema.db) == e_ty; - let param_matches = imp.target_trait_substs_matches(sema.db, &[var_ty.clone()]); - targets_enum && param_matches - }); - - imp.is_some() + e_ty.impls_trait(sema.db, from_trait, &[var_ty.clone()]) } #[cfg(test)] @@ -192,7 +180,7 @@ impl From for A { A::Two(v) } } - + pub trait From { fn from(T) -> Self; }"#, @@ -209,7 +197,7 @@ impl From for A { A::Two(v) } } - + pub trait From { fn from(T) -> Self; }"#, -- cgit v1.2.3