diff options
| author | DropDemBits <r3usrlnd@gmail.com> | 2023-07-16 16:24:13 -0400 |
|---|---|---|
| committer | DropDemBits <r3usrlnd@gmail.com> | 2023-11-11 21:05:27 -0500 |
| commit | f3dcc67dfa62ac0950352c719e657687428f79f6 (patch) | |
| tree | a2d0c7e228bc5fc1c3cd3989746227282fdc0f7d | |
| parent | 5fc8cc52e2ebc30d720873236293f5469bf5cfd8 (diff) | |
| download | rust-f3dcc67dfa62ac0950352c719e657687428f79f6.tar.gz rust-f3dcc67dfa62ac0950352c719e657687428f79f6.zip | |
Migrate `add_type_ascription`
| -rw-r--r-- | crates/ide-assists/src/handlers/add_turbo_fish.rs | 39 |
1 files changed, 31 insertions, 8 deletions
diff --git a/crates/ide-assists/src/handlers/add_turbo_fish.rs b/crates/ide-assists/src/handlers/add_turbo_fish.rs index 6d973a24c44..6afc1693fab 100644 --- a/crates/ide-assists/src/handlers/add_turbo_fish.rs +++ b/crates/ide-assists/src/handlers/add_turbo_fish.rs @@ -85,20 +85,23 @@ pub(crate) fn add_turbo_fish(acc: &mut Assists, ctx: &AssistContext<'_>) -> Opti if let Some(let_stmt) = ctx.find_node_at_offset::<ast::LetStmt>() { if let_stmt.colon_token().is_none() { - let type_pos = let_stmt.pat()?.syntax().last_token()?.text_range().end(); - let semi_pos = let_stmt.syntax().last_token()?.text_range().end(); - acc.add( AssistId("add_type_ascription", AssistKind::RefactorRewrite), "Add `: _` before assignment operator", ident.text_range(), - |builder| { + |edit| { + let let_stmt = edit.make_mut(let_stmt); + if let_stmt.semicolon_token().is_none() { - builder.insert(semi_pos, ";"); + ted::append_child(let_stmt.syntax(), make::tokens::semicolon()); } - match ctx.config.snippet_cap { - Some(cap) => builder.insert_snippet(cap, type_pos, ": ${0:_}"), - None => builder.insert(type_pos, ": _"), + + let placeholder_ty = make::ty_placeholder().clone_for_update(); + + let_stmt.set_ty(Some(placeholder_ty.clone())); + + if let Some(cap) = ctx.config.snippet_cap { + edit.add_placeholder_snippet(cap, placeholder_ty); } }, )? @@ -396,6 +399,26 @@ fn main() { } #[test] + fn add_type_ascription_missing_pattern() { + check_assist_by_label( + add_turbo_fish, + r#" +fn make<T>() -> T {} +fn main() { + let = make$0() +} +"#, + r#" +fn make<T>() -> T {} +fn main() { + let : ${0:_} = make(); +} +"#, + "Add `: _` before assignment operator", + ); + } + + #[test] fn add_turbo_fish_function_lifetime_parameter() { check_assist( add_turbo_fish, |
