Skip to content

Commit a78cf75

Browse files
committed
use placeholder_snippet
Signed-off-by: Hayashi Mikihiro <[email protected]>
1 parent cd4fadb commit a78cf75

File tree

1 file changed

+46
-15
lines changed

1 file changed

+46
-15
lines changed

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

Lines changed: 46 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -149,8 +149,35 @@ pub(crate) fn generate_new(acc: &mut Assists, ctx: &AssistContext<'_>) -> Option
149149
.clone_for_update();
150150
fn_.indent(1.into());
151151

152-
// Add a tabstop before the name
153152
if let Some(cap) = ctx.config.snippet_cap {
153+
match strukt.kind() {
154+
StructKind::Tuple(_) => {
155+
let struct_args = fn_
156+
.body()
157+
.unwrap()
158+
.syntax()
159+
.descendants()
160+
.filter(|it| syntax::ast::ArgList::can_cast(it.kind()))
161+
.flat_map(|args| args.children())
162+
.filter(|it| syntax::ast::PathExpr::can_cast(it.kind()))
163+
.enumerate()
164+
.filter_map(|(i, node)| {
165+
if trivial_constructors[i].is_none() { Some(node) } else { None }
166+
});
167+
if let Some(fn_params) = fn_.param_list() {
168+
for (struct_arg, fn_param) in struct_args.zip(fn_params.params()) {
169+
if let Some(fn_pat) = fn_param.pat() {
170+
let fn_pat = fn_pat.syntax().clone();
171+
builder
172+
.add_placeholder_snippet_group(cap, vec![struct_arg, fn_pat]);
173+
}
174+
}
175+
}
176+
}
177+
_ => {}
178+
}
179+
180+
// Add a tabstop before the name
154181
if let Some(name) = fn_.name() {
155182
builder.add_tabstop_before(cap, name);
156183
}
@@ -765,8 +792,8 @@ struct Empty;
765792
struct Foo(String, Empty);
766793
767794
impl Foo {
768-
fn $0new(_0: String) -> Self {
769-
Self(_0, Empty)
795+
fn $0new(${1:_0}: String) -> Self {
796+
Self(${1:_0}, Empty)
770797
}
771798
}
772799
"#,
@@ -805,8 +832,8 @@ struct Empty {}
805832
struct Foo(Empty);
806833
807834
impl Foo {
808-
fn $0new(_0: Empty) -> Self {
809-
Self(_0)
835+
fn $0new(${1:empty}: Empty) -> Self {
836+
Self(${1:empty})
810837
}
811838
}
812839
"#,
@@ -824,8 +851,8 @@ enum Empty { Bar {} }
824851
struct Foo(Empty);
825852
826853
impl Foo {
827-
fn $0new(_0: Empty) -> Self {
828-
Self(_0)
854+
fn $0new(${1:empty}: Empty) -> Self {
855+
Self(${1:empty})
829856
}
830857
}
831858
"#,
@@ -888,23 +915,25 @@ struct Foo(String$0);
888915
struct Foo(String);
889916
890917
impl Foo {
891-
fn $0new(_0: String) -> Self {
892-
Self(_0)
918+
fn $0new(${1:_0}: String) -> Self {
919+
Self(${1:_0})
893920
}
894921
}
895922
"#,
896923
);
897924
check_assist(
898925
generate_new,
899926
r#"
927+
struct Vec<T> { };
900928
struct Foo(String, Vec<i32>$0);
901929
"#,
902930
r#"
931+
struct Vec<T> { };
903932
struct Foo(String, Vec<i32>);
904933
905934
impl Foo {
906-
fn $0new(_0: String, _1: Vec<i32>) -> Self {
907-
Self(_0, _1)
935+
fn $0new(${1:_0}: String, ${2:items}: Vec<i32>) -> Self {
936+
Self(${1:_0}, ${2:items})
908937
}
909938
}
910939
"#,
@@ -916,14 +945,16 @@ impl Foo {
916945
check_assist(
917946
generate_new,
918947
r#"
948+
struct Vec<T> { };
919949
struct Foo(pub String, pub Vec<i32>$0);
920950
"#,
921951
r#"
952+
struct Vec<T> { };
922953
struct Foo(pub String, pub Vec<i32>);
923954
924955
impl Foo {
925-
fn $0new(_0: String, _1: Vec<i32>) -> Self {
926-
Self(_0, _1)
956+
fn $0new(${1:_0}: String, ${2:items}: Vec<i32>) -> Self {
957+
Self(${1:_0}, ${2:items})
927958
}
928959
}
929960
"#,
@@ -1013,8 +1044,8 @@ impl<N: AstNode> AstId<N> {
10131044
pub struct Source<T>(pub HirFileId, pub T);
10141045
10151046
impl<T> Source<T> {
1016-
pub fn $0new(_0: HirFileId, _1: T) -> Self {
1017-
Self(_0, _1)
1047+
pub fn $0new(${1:_0}: HirFileId, ${2:_1}: T) -> Self {
1048+
Self(${1:_0}, ${2:_1})
10181049
}
10191050
10201051
pub fn map<F: FnOnce(T) -> U, U>(self, f: F) -> Source<U> {

0 commit comments

Comments
 (0)