Skip to content

Commit f3dcc67

Browse files
committed
Migrate add_type_ascription
1 parent 5fc8cc5 commit f3dcc67

File tree

1 file changed

+31
-8
lines changed

1 file changed

+31
-8
lines changed

crates/ide-assists/src/handlers/add_turbo_fish.rs

Lines changed: 31 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -85,20 +85,23 @@ pub(crate) fn add_turbo_fish(acc: &mut Assists, ctx: &AssistContext<'_>) -> Opti
8585

8686
if let Some(let_stmt) = ctx.find_node_at_offset::<ast::LetStmt>() {
8787
if let_stmt.colon_token().is_none() {
88-
let type_pos = let_stmt.pat()?.syntax().last_token()?.text_range().end();
89-
let semi_pos = let_stmt.syntax().last_token()?.text_range().end();
90-
9188
acc.add(
9289
AssistId("add_type_ascription", AssistKind::RefactorRewrite),
9390
"Add `: _` before assignment operator",
9491
ident.text_range(),
95-
|builder| {
92+
|edit| {
93+
let let_stmt = edit.make_mut(let_stmt);
94+
9695
if let_stmt.semicolon_token().is_none() {
97-
builder.insert(semi_pos, ";");
96+
ted::append_child(let_stmt.syntax(), make::tokens::semicolon());
9897
}
99-
match ctx.config.snippet_cap {
100-
Some(cap) => builder.insert_snippet(cap, type_pos, ": ${0:_}"),
101-
None => builder.insert(type_pos, ": _"),
98+
99+
let placeholder_ty = make::ty_placeholder().clone_for_update();
100+
101+
let_stmt.set_ty(Some(placeholder_ty.clone()));
102+
103+
if let Some(cap) = ctx.config.snippet_cap {
104+
edit.add_placeholder_snippet(cap, placeholder_ty);
102105
}
103106
},
104107
)?
@@ -395,6 +398,26 @@ fn main() {
395398
);
396399
}
397400

401+
#[test]
402+
fn add_type_ascription_missing_pattern() {
403+
check_assist_by_label(
404+
add_turbo_fish,
405+
r#"
406+
fn make<T>() -> T {}
407+
fn main() {
408+
let = make$0()
409+
}
410+
"#,
411+
r#"
412+
fn make<T>() -> T {}
413+
fn main() {
414+
let : ${0:_} = make();
415+
}
416+
"#,
417+
"Add `: _` before assignment operator",
418+
);
419+
}
420+
398421
#[test]
399422
fn add_turbo_fish_function_lifetime_parameter() {
400423
check_assist(

0 commit comments

Comments
 (0)