Skip to content

Commit 2f6a249

Browse files
committed
Emit snippets for struct pattern completion if enabled
1 parent b184bfa commit 2f6a249

File tree

2 files changed

+36
-15
lines changed

2 files changed

+36
-15
lines changed

crates/completion/src/completions/pattern.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -156,7 +156,7 @@ fn foo() {
156156
}
157157
"#,
158158
expect![[r#"
159-
bn Bar Bar { f }$0
159+
bn Bar Bar { ${1:f} }$0
160160
"#]],
161161
);
162162
}
@@ -171,7 +171,7 @@ struct Baz;
171171
fn outer(<|>) {}
172172
"#,
173173
expect![[r#"
174-
bn Foo Foo { bar, baz }: Foo$0
174+
bn Foo Foo { ${1:bar}, ${2:baz} }: Foo$0
175175
bn Bar Bar($1, $2): Bar$0
176176
"#]],
177177
)
@@ -189,7 +189,7 @@ fn outer() {
189189
}
190190
"#,
191191
expect![[r#"
192-
bn Foo Foo { bar, baz }$0
192+
bn Foo Foo { ${1:bar}, ${2:baz} }$0
193193
bn Bar Bar($1, $2)$0
194194
"#]],
195195
)
@@ -209,7 +209,7 @@ fn outer() {
209209
}
210210
"#,
211211
expect![[r#"
212-
bn Foo Foo { bar, baz }$0
212+
bn Foo Foo { ${1:bar}, ${2:baz} }$0
213213
bn Bar Bar($1, $2)$0
214214
"#]],
215215
)
@@ -233,7 +233,7 @@ fn outer() {
233233
}
234234
"#,
235235
expect![[r#"
236-
bn Foo Foo { bar, .. }$0
236+
bn Foo Foo { ${1:bar}, .. }$0
237237
bn Bar Bar($1, ..)$0
238238
"#]],
239239
)

crates/completion/src/render/pattern.rs

Lines changed: 31 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,10 @@
33
use hir::{db::HirDatabase, HasVisibility, Name, StructKind};
44
use itertools::Itertools;
55

6-
use crate::{item::CompletionKind, render::RenderContext, CompletionItem, CompletionItemKind};
6+
use crate::{
7+
config::SnippetCap, item::CompletionKind, render::RenderContext, CompletionItem,
8+
CompletionItemKind,
9+
};
710

811
pub(crate) fn render_struct_pat<'a>(
912
ctx: RenderContext<'a>,
@@ -31,7 +34,9 @@ pub(crate) fn render_struct_pat<'a>(
3134
StructKind::Tuple if ctx.snippet_cap().is_some() => {
3235
render_tuple_as_pat(&fields, &name, fields_omitted)
3336
}
34-
StructKind::Record => render_record_as_pat(ctx.db(), &fields, &name, fields_omitted),
37+
StructKind::Record => {
38+
render_record_as_pat(ctx.db(), ctx.snippet_cap(), &fields, &name, fields_omitted)
39+
}
3540
_ => return None,
3641
};
3742

@@ -79,7 +84,9 @@ pub(crate) fn render_variant_pat<'a>(
7984
StructKind::Tuple if ctx.snippet_cap().is_some() => {
8085
render_tuple_as_pat(&fields, &name, fields_omitted)
8186
}
82-
StructKind::Record => render_record_as_pat(ctx.db(), &fields, &name, fields_omitted),
87+
StructKind::Record => {
88+
render_record_as_pat(ctx.db(), ctx.snippet_cap(), &fields, &name, fields_omitted)
89+
}
8390
_ => return None,
8491
};
8592

@@ -106,22 +113,36 @@ pub(crate) fn render_variant_pat<'a>(
106113

107114
fn render_record_as_pat(
108115
db: &dyn HirDatabase,
116+
snippet_cap: Option<SnippetCap>,
109117
fields: &[hir::Field],
110118
name: &str,
111119
fields_omitted: bool,
112120
) -> String {
113-
format!(
114-
"{name} {{ {}{} }}",
115-
fields.into_iter().map(|field| field.name(db)).format(", "),
116-
if fields_omitted { ", .." } else { "" },
117-
name = name
118-
)
121+
let fields = fields.iter();
122+
if snippet_cap.is_some() {
123+
format!(
124+
"{name} {{ {}{} }}",
125+
fields
126+
.enumerate()
127+
.map(|(idx, field)| format!("${{{}:{}}}", idx + 1, field.name(db)))
128+
.format(", "),
129+
if fields_omitted { ", .." } else { "" },
130+
name = name
131+
)
132+
} else {
133+
format!(
134+
"{name} {{ {}{} }}",
135+
fields.map(|field| field.name(db)).format(", "),
136+
if fields_omitted { ", .." } else { "" },
137+
name = name
138+
)
139+
}
119140
}
120141

121142
fn render_tuple_as_pat(fields: &[hir::Field], name: &str, fields_omitted: bool) -> String {
122143
format!(
123144
"{name}({}{})",
124-
fields.into_iter().enumerate().map(|(idx, _)| format!("${}", idx + 1)).format(", "),
145+
fields.iter().enumerate().map(|(idx, _)| format!("${}", idx + 1)).format(", "),
125146
if fields_omitted { ", .." } else { "" },
126147
name = name
127148
)

0 commit comments

Comments
 (0)