6 use crate::{AssistContext, AssistId, AssistKind, Assists};
8 // Assist: extract_type_alias
10 // Extracts the selected type as a type alias.
14 // field: $0(u8, u8, u8)$0,
19 // type $0Type = (u8, u8, u8);
25 pub(crate) fn extract_type_alias(acc: &mut Assists, ctx: &AssistContext) -> Option<()> {
26 if ctx.frange.range.is_empty() {
30 let node = ctx.find_node_at_range::<ast::Type>()?;
31 let item = ctx.find_node_at_offset::<ast::Item>()?;
32 let insert = match_ast! {
33 match (item.syntax().parent()?) {
34 ast::AssocItemList(it) => it.syntax().parent()?.text_range().start(),
35 _ => item.syntax().text_range().start(),
38 let target = node.syntax().text_range();
41 AssistId("extract_type_alias", AssistKind::RefactorExtract),
42 "Extract type as type alias",
45 builder.edit_file(ctx.frange.file_id);
46 builder.replace(target, "Type");
47 match ctx.config.snippet_cap {
49 builder.insert_snippet(cap, insert, format!("type $0Type = {};\n\n", node));
52 builder.insert(insert, format!("type Type = {};\n\n", node));
61 use crate::tests::{check_assist, check_assist_not_applicable};
66 fn test_not_applicable_without_selection() {
67 check_assist_not_applicable(
71 field: $0(u8, u8, u8),
78 fn test_simple_types() {
97 fn test_generic_type_arg() {
120 fn test_inner_type_arg() {
126 v: Vec<Vec<$0Vec<u8>$0>>,
131 type $0Type = Vec<u8>;
141 fn test_extract_inner_type() {
160 fn extract_from_impl_or_trait() {
161 // When invoked in an impl/trait, extracted type alias should be placed next to the
162 // impl/trait, not inside.
167 fn f() -> $0(u8, u8)$0 {}
171 type $0Type = (u8, u8);
182 fn f() -> $0(u8, u8)$0 {}
186 type $0Type = (u8, u8);