Skip to content

Commit d4fad2b

Browse files
committed
refactor: re-use add_turbo_fish function
1 parent 4a36129 commit d4fad2b

File tree

3 files changed

+137
-201
lines changed

3 files changed

+137
-201
lines changed

crates/ide_assists/src/handlers/add_turbo_fish.rs

Lines changed: 137 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,11 +31,13 @@ pub(crate) fn add_turbo_fish(acc: &mut Assists, ctx: &AssistContext) -> Option<(
3131
return None;
3232
}
3333
mark::hit!(add_turbo_fish_after_call);
34+
mark::hit!(add_type_ascription_after_call);
3435
arg_list.l_paren_token()?.prev_token().filter(|it| it.kind() == SyntaxKind::IDENT)
3536
})?;
3637
let next_token = ident.next_token()?;
3738
if next_token.kind() == T![::] {
3839
mark::hit!(add_turbo_fish_one_fish_is_enough);
40+
mark::hit!(add_type_ascription_turbofished);
3941
return None;
4042
}
4143
let name_ref = ast::NameRef::cast(ident.parent())?;
@@ -50,8 +52,27 @@ pub(crate) fn add_turbo_fish(acc: &mut Assists, ctx: &AssistContext) -> Option<(
5052
let generics = hir::GenericDef::Function(fun).params(ctx.sema.db);
5153
if generics.is_empty() {
5254
mark::hit!(add_turbo_fish_non_generic);
55+
mark::hit!(add_type_ascription_non_generic);
5356
return None;
5457
}
58+
59+
if let Some(let_stmt) = ctx.find_node_at_offset::<ast::LetStmt>() {
60+
if let_stmt.colon_token().is_none() {
61+
let type_pos = let_stmt.pat()?.syntax().last_token()?.text_range().end();
62+
acc.add(
63+
AssistId("add_type_ascription", AssistKind::RefactorRewrite),
64+
"Add `: _` before assignment operator",
65+
ident.text_range(),
66+
|builder| match ctx.config.snippet_cap {
67+
Some(cap) => builder.insert_snippet(cap, type_pos, ": ${0:_}"),
68+
None => builder.insert(type_pos, ": _"),
69+
},
70+
)?
71+
} else {
72+
mark::hit!(add_type_ascription_already_typed);
73+
}
74+
}
75+
5576
acc.add(
5677
AssistId("add_turbo_fish", AssistKind::RefactorRewrite),
5778
"Add `::<>`",
@@ -65,7 +86,7 @@ pub(crate) fn add_turbo_fish(acc: &mut Assists, ctx: &AssistContext) -> Option<(
6586

6687
#[cfg(test)]
6788
mod tests {
68-
use crate::tests::{check_assist, check_assist_not_applicable};
89+
use crate::tests::{check_assist, check_assist_by_label, check_assist_not_applicable};
6990

7091
use super::*;
7192
use test_utils::mark;
@@ -158,6 +179,121 @@ fn make() -> () {}
158179
fn main() {
159180
make$0();
160181
}
182+
"#,
183+
);
184+
}
185+
186+
#[test]
187+
fn add_type_ascription_function() {
188+
check_assist_by_label(
189+
add_turbo_fish,
190+
r#"
191+
fn make<T>() -> T {}
192+
fn main() {
193+
let x = make$0();
194+
}
195+
"#,
196+
r#"
197+
fn make<T>() -> T {}
198+
fn main() {
199+
let x: ${0:_} = make();
200+
}
201+
"#,
202+
"Add `: _` before assignment operator",
203+
);
204+
}
205+
206+
#[test]
207+
fn add_type_ascription_after_call() {
208+
mark::check!(add_type_ascription_after_call);
209+
check_assist_by_label(
210+
add_turbo_fish,
211+
r#"
212+
fn make<T>() -> T {}
213+
fn main() {
214+
let x = make()$0;
215+
}
216+
"#,
217+
r#"
218+
fn make<T>() -> T {}
219+
fn main() {
220+
let x: ${0:_} = make();
221+
}
222+
"#,
223+
"Add `: _` before assignment operator",
224+
);
225+
}
226+
227+
#[test]
228+
fn add_type_ascription_method() {
229+
check_assist_by_label(
230+
add_turbo_fish,
231+
r#"
232+
struct S;
233+
impl S {
234+
fn make<T>(&self) -> T {}
235+
}
236+
fn main() {
237+
let x = S.make$0();
238+
}
239+
"#,
240+
r#"
241+
struct S;
242+
impl S {
243+
fn make<T>(&self) -> T {}
244+
}
245+
fn main() {
246+
let x: ${0:_} = S.make();
247+
}
248+
"#,
249+
"Add `: _` before assignment operator",
250+
);
251+
}
252+
253+
#[test]
254+
fn add_type_ascription_turbofished() {
255+
mark::check!(add_type_ascription_turbofished);
256+
check_assist_not_applicable(
257+
add_turbo_fish,
258+
r#"
259+
fn make<T>() -> T {}
260+
fn main() {
261+
let x = make$0::<()>();
262+
}
263+
"#,
264+
);
265+
}
266+
267+
#[test]
268+
fn add_type_ascription_already_typed() {
269+
mark::check!(add_type_ascription_already_typed);
270+
check_assist(
271+
add_turbo_fish,
272+
r#"
273+
fn make<T>() -> T {}
274+
fn main() {
275+
let x: () = make$0();
276+
}
277+
"#,
278+
r#"
279+
fn make<T>() -> T {}
280+
fn main() {
281+
let x: () = make::<${0:_}>();
282+
}
283+
"#,
284+
);
285+
}
286+
287+
#[test]
288+
fn add_type_ascription_non_generic() {
289+
mark::check!(add_type_ascription_non_generic);
290+
check_assist_not_applicable(
291+
add_turbo_fish,
292+
r#"
293+
fn make() -> () {}
294+
fn main() {
295+
let x = make$0();
296+
}
161297
"#,
162298
);
163299
}

crates/ide_assists/src/handlers/add_type_ascription.rs

Lines changed: 0 additions & 198 deletions
This file was deleted.

crates/ide_assists/src/lib.rs

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -111,7 +111,6 @@ mod handlers {
111111
mod add_lifetime_to_type;
112112
mod add_missing_impl_members;
113113
mod add_turbo_fish;
114-
mod add_type_ascription;
115114
mod apply_demorgan;
116115
mod auto_import;
117116
mod change_visibility;
@@ -176,7 +175,6 @@ mod handlers {
176175
add_explicit_type::add_explicit_type,
177176
add_lifetime_to_type::add_lifetime_to_type,
178177
add_turbo_fish::add_turbo_fish,
179-
add_type_ascription::add_type_ascription,
180178
apply_demorgan::apply_demorgan,
181179
auto_import::auto_import,
182180
change_visibility::change_visibility,

0 commit comments

Comments
 (0)