Skip to content

Commit b494e47

Browse files
Snippet support in extract_type_alias
1 parent 201fbac commit b494e47

File tree

2 files changed

+26
-15
lines changed

2 files changed

+26
-15
lines changed

crates/ide_assists/src/handlers/extract_type_alias.rs

Lines changed: 24 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -16,10 +16,10 @@ use crate::{AssistContext, AssistId, AssistKind, Assists};
1616
// ```
1717
// ->
1818
// ```
19-
// type Type = (u8, u8, u8);
19+
// type ${0:Type} = (u8, u8, u8);
2020
//
2121
// struct S {
22-
// field: Type,
22+
// field: ${0:Type},
2323
// }
2424
// ```
2525
pub(crate) fn extract_type_alias(acc: &mut Assists, ctx: &AssistContext) -> Option<()> {
@@ -56,9 +56,20 @@ pub(crate) fn extract_type_alias(acc: &mut Assists, ctx: &AssistContext) -> Opti
5656
target,
5757
|builder| {
5858
builder.edit_file(ctx.frange.file_id);
59-
// FIXME: add snippet support
60-
builder.replace(target, "Type");
61-
builder.insert(insert, format!("type Type = {};\n\n", node));
59+
match ctx.config.snippet_cap {
60+
Some(cap) => {
61+
builder.replace_snippet(cap, target, "${0:Type}");
62+
builder.insert_snippet(
63+
cap,
64+
insert,
65+
format!("type ${{0:Type}} = {};\n\n", node),
66+
);
67+
}
68+
None => {
69+
builder.replace(target, "Type");
70+
builder.insert(insert, format!("type Type = {};\n\n", node));
71+
}
72+
}
6273
},
6374
)
6475
}
@@ -91,10 +102,10 @@ struct S {
91102
}
92103
",
93104
r#"
94-
type Type = u8;
105+
type ${0:Type} = u8;
95106
96107
struct S {
97-
field: Type,
108+
field: ${0:Type},
98109
}
99110
"#,
100111
);
@@ -114,10 +125,10 @@ fn f() {
114125
r#"
115126
fn generic<T>() {}
116127
117-
type Type = ();
128+
type ${0:Type} = ();
118129
119130
fn f() {
120-
generic::<Type>();
131+
generic::<${0:Type}>();
121132
}
122133
"#,
123134
);
@@ -135,10 +146,10 @@ struct S {
135146
",
136147
r#"
137148
struct Vec<T> {}
138-
type Type = Vec<u8>;
149+
type ${0:Type} = Vec<u8>;
139150
140151
struct S {
141-
v: Vec<Vec<Type>>,
152+
v: Vec<Vec<${0:Type}>>,
142153
}
143154
"#,
144155
);
@@ -154,10 +165,10 @@ struct S {
154165
}
155166
",
156167
r#"
157-
type Type = u8;
168+
type ${0:Type} = u8;
158169
159170
struct S {
160-
field: (Type,),
171+
field: (${0:Type},),
161172
}
162173
"#,
163174
);

crates/ide_assists/src/tests/generated.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -338,10 +338,10 @@ struct S {
338338
}
339339
"#####,
340340
r#####"
341-
type Type = (u8, u8, u8);
341+
type ${0:Type} = (u8, u8, u8);
342342
343343
struct S {
344-
field: Type,
344+
field: ${0:Type},
345345
}
346346
"#####,
347347
)

0 commit comments

Comments
 (0)