Skip to content

Commit 3f2edc7

Browse files
bors[bot]Vannevelj
andauthored
Merge #11136
11136: Turbo fish assist supports multiple type arguments r=matklad a=Vannevelj This fixes #11135 (changelog: bug). I've only started using Rust a few days ago but saw this issue on the top of the list when I looked at this repo. I based myself on [this blog post](https://techblog.tonsser.com/posts/what-is-rusts-turbofish) to understand what a "turbo fish" is so let me know if I missed anything. Co-authored-by: Jeroen Vannevel <[email protected]>
2 parents 9c210a4 + b04c4e7 commit 3f2edc7

File tree

1 file changed

+50
-2
lines changed

1 file changed

+50
-2
lines changed

crates/ide_assists/src/handlers/add_turbo_fish.rs

Lines changed: 50 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
use ide_db::defs::{Definition, NameRefClass};
2+
use itertools::Itertools;
23
use syntax::{ast, AstNode, SyntaxKind, T};
34

45
use crate::{
@@ -77,13 +78,22 @@ pub(crate) fn add_turbo_fish(acc: &mut Assists, ctx: &AssistContext) -> Option<(
7778
}
7879
}
7980

81+
let number_of_arguments = generics.len();
82+
let fish_head = std::iter::repeat("_").take(number_of_arguments).collect_vec().join(",");
83+
8084
acc.add(
8185
AssistId("add_turbo_fish", AssistKind::RefactorRewrite),
8286
"Add `::<>`",
8387
ident.text_range(),
8488
|builder| match ctx.config.snippet_cap {
85-
Some(cap) => builder.insert_snippet(cap, ident.text_range().end(), "::<${0:_}>"),
86-
None => builder.insert(ident.text_range().end(), "::<_>"),
89+
Some(cap) => {
90+
let snip = format!("::<${{0:{}}}>", fish_head);
91+
builder.insert_snippet(cap, ident.text_range().end(), snip)
92+
}
93+
None => {
94+
let snip = format!("::<{}>", fish_head);
95+
builder.insert(ident.text_range().end(), snip);
96+
}
8797
},
8898
)
8999
}
@@ -113,6 +123,44 @@ fn main() {
113123
);
114124
}
115125

126+
#[test]
127+
fn add_turbo_fish_function_multiple_generic_types() {
128+
check_assist(
129+
add_turbo_fish,
130+
r#"
131+
fn make<T, A>() -> T {}
132+
fn main() {
133+
make$0();
134+
}
135+
"#,
136+
r#"
137+
fn make<T, A>() -> T {}
138+
fn main() {
139+
make::<${0:_,_}>();
140+
}
141+
"#,
142+
);
143+
}
144+
145+
#[test]
146+
fn add_turbo_fish_function_many_generic_types() {
147+
check_assist(
148+
add_turbo_fish,
149+
r#"
150+
fn make<T, A, B, C, D, E, F>() -> T {}
151+
fn main() {
152+
make$0();
153+
}
154+
"#,
155+
r#"
156+
fn make<T, A, B, C, D, E, F>() -> T {}
157+
fn main() {
158+
make::<${0:_,_,_,_,_,_,_}>();
159+
}
160+
"#,
161+
);
162+
}
163+
116164
#[test]
117165
fn add_turbo_fish_after_call() {
118166
cov_mark::check!(add_turbo_fish_after_call);

0 commit comments

Comments
 (0)