Skip to content

Commit 369f477

Browse files
committed
fix: Move cursor before generated generic
1 parent 762eace commit 369f477

File tree

2 files changed

+20
-12
lines changed

2 files changed

+20
-12
lines changed

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

Lines changed: 19 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
use syntax::{
2-
ast::{self, edit_in_place::GenericParamsOwnerEdit, make, AstNode},
2+
ast::{self, edit_in_place::GenericParamsOwnerEdit, make, AstNode, HasGenericParams},
33
ted,
44
};
55

@@ -14,7 +14,7 @@ use crate::{utils::suggest_name, AssistContext, AssistId, AssistKind, Assists};
1414
// ```
1515
// ->
1616
// ```
17-
// fn foo<B: Bar>(bar: B) {}
17+
// fn foo<$0B: Bar>(bar: B) {}
1818
// ```
1919
pub(crate) fn introduce_named_generic(acc: &mut Assists, ctx: &AssistContext<'_>) -> Option<()> {
2020
let impl_trait_type = ctx.find_node_at_offset::<ast::ImplTraitType>()?;
@@ -39,7 +39,15 @@ pub(crate) fn introduce_named_generic(acc: &mut Assists, ctx: &AssistContext<'_>
3939
let new_ty = make::ty(&type_param_name).clone_for_update();
4040

4141
ted::replace(impl_trait_type.syntax(), new_ty.syntax());
42-
fn_.get_or_create_generic_param_list().add_generic_param(type_param.into())
42+
fn_.get_or_create_generic_param_list().add_generic_param(type_param.into());
43+
44+
if let Some(cap) = ctx.config.snippet_cap {
45+
if let Some(generic_param) =
46+
fn_.generic_param_list().and_then(|it| it.generic_params().last())
47+
{
48+
edit.add_tabstop_before(cap, generic_param);
49+
}
50+
}
4351
},
4452
)
4553
}
@@ -55,7 +63,7 @@ mod tests {
5563
check_assist(
5664
introduce_named_generic,
5765
r#"fn foo<G>(bar: $0impl Bar) {}"#,
58-
r#"fn foo<G, B: Bar>(bar: B) {}"#,
66+
r#"fn foo<G, $0B: Bar>(bar: B) {}"#,
5967
);
6068
}
6169

@@ -64,7 +72,7 @@ mod tests {
6472
check_assist(
6573
introduce_named_generic,
6674
r#"fn foo(bar: $0impl Bar) {}"#,
67-
r#"fn foo<B: Bar>(bar: B) {}"#,
75+
r#"fn foo<$0B: Bar>(bar: B) {}"#,
6876
);
6977
}
7078

@@ -73,7 +81,7 @@ mod tests {
7381
check_assist(
7482
introduce_named_generic,
7583
r#"fn foo<G>(foo: impl Foo, bar: $0impl Bar) {}"#,
76-
r#"fn foo<G, B: Bar>(foo: impl Foo, bar: B) {}"#,
84+
r#"fn foo<G, $0B: Bar>(foo: impl Foo, bar: B) {}"#,
7785
);
7886
}
7987

@@ -82,7 +90,7 @@ mod tests {
8290
check_assist(
8391
introduce_named_generic,
8492
r#"fn foo<>(bar: $0impl Bar) {}"#,
85-
r#"fn foo<B: Bar>(bar: B) {}"#,
93+
r#"fn foo<$0B: Bar>(bar: B) {}"#,
8694
);
8795
}
8896

@@ -95,7 +103,7 @@ fn foo<
95103
>(bar: $0impl Bar) {}
96104
"#,
97105
r#"
98-
fn foo<B: Bar
106+
fn foo<$0B: Bar
99107
>(bar: B) {}
100108
"#,
101109
);
@@ -108,7 +116,7 @@ fn foo<B: Bar
108116
check_assist(
109117
introduce_named_generic,
110118
r#"fn foo<B>(bar: $0impl Bar) {}"#,
111-
r#"fn foo<B, B: Bar>(bar: B) {}"#,
119+
r#"fn foo<B, $0B: Bar>(bar: B) {}"#,
112120
);
113121
}
114122

@@ -127,7 +135,7 @@ fn foo<
127135
fn foo<
128136
G: Foo,
129137
F,
130-
H, B: Bar,
138+
H, $0B: Bar,
131139
>(bar: B) {}
132140
"#,
133141
);
@@ -138,7 +146,7 @@ fn foo<
138146
check_assist(
139147
introduce_named_generic,
140148
r#"fn foo(bar: $0impl Foo + Bar) {}"#,
141-
r#"fn foo<F: Foo + Bar>(bar: F) {}"#,
149+
r#"fn foo<$0F: Foo + Bar>(bar: F) {}"#,
142150
);
143151
}
144152
}

crates/ide-assists/src/tests/generated.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1596,7 +1596,7 @@ fn doctest_introduce_named_generic() {
15961596
fn foo(bar: $0impl Bar) {}
15971597
"#####,
15981598
r#####"
1599-
fn foo<B: Bar>(bar: B) {}
1599+
fn foo<$0B: Bar>(bar: B) {}
16001600
"#####,
16011601
)
16021602
}

0 commit comments

Comments
 (0)