diff options
author | Lukas Wirth <[email protected]> | 2020-12-20 18:16:28 +0000 |
---|---|---|
committer | Lukas Wirth <[email protected]> | 2020-12-20 18:16:28 +0000 |
commit | 2f6a24950a555bdfecbda7a50735d643f9d0e7f9 (patch) | |
tree | ea2374257c5f3886961f84db8f13113737ac9e5d /crates/completion/src | |
parent | b184bfad7a2dc6a9bf6654a7eec6c68a27c49f70 (diff) |
Emit snippets for struct pattern completion if enabled
Diffstat (limited to 'crates/completion/src')
-rw-r--r-- | crates/completion/src/completions/pattern.rs | 10 | ||||
-rw-r--r-- | crates/completion/src/render/pattern.rs | 41 |
2 files changed, 36 insertions, 15 deletions
diff --git a/crates/completion/src/completions/pattern.rs b/crates/completion/src/completions/pattern.rs index 496f0b040..23a00803c 100644 --- a/crates/completion/src/completions/pattern.rs +++ b/crates/completion/src/completions/pattern.rs | |||
@@ -156,7 +156,7 @@ fn foo() { | |||
156 | } | 156 | } |
157 | "#, | 157 | "#, |
158 | expect![[r#" | 158 | expect![[r#" |
159 | bn Bar Bar { f }$0 | 159 | bn Bar Bar { ${1:f} }$0 |
160 | "#]], | 160 | "#]], |
161 | ); | 161 | ); |
162 | } | 162 | } |
@@ -171,7 +171,7 @@ struct Baz; | |||
171 | fn outer(<|>) {} | 171 | fn outer(<|>) {} |
172 | "#, | 172 | "#, |
173 | expect![[r#" | 173 | expect![[r#" |
174 | bn Foo Foo { bar, baz }: Foo$0 | 174 | bn Foo Foo { ${1:bar}, ${2:baz} }: Foo$0 |
175 | bn Bar Bar($1, $2): Bar$0 | 175 | bn Bar Bar($1, $2): Bar$0 |
176 | "#]], | 176 | "#]], |
177 | ) | 177 | ) |
@@ -189,7 +189,7 @@ fn outer() { | |||
189 | } | 189 | } |
190 | "#, | 190 | "#, |
191 | expect![[r#" | 191 | expect![[r#" |
192 | bn Foo Foo { bar, baz }$0 | 192 | bn Foo Foo { ${1:bar}, ${2:baz} }$0 |
193 | bn Bar Bar($1, $2)$0 | 193 | bn Bar Bar($1, $2)$0 |
194 | "#]], | 194 | "#]], |
195 | ) | 195 | ) |
@@ -209,7 +209,7 @@ fn outer() { | |||
209 | } | 209 | } |
210 | "#, | 210 | "#, |
211 | expect![[r#" | 211 | expect![[r#" |
212 | bn Foo Foo { bar, baz }$0 | 212 | bn Foo Foo { ${1:bar}, ${2:baz} }$0 |
213 | bn Bar Bar($1, $2)$0 | 213 | bn Bar Bar($1, $2)$0 |
214 | "#]], | 214 | "#]], |
215 | ) | 215 | ) |
@@ -233,7 +233,7 @@ fn outer() { | |||
233 | } | 233 | } |
234 | "#, | 234 | "#, |
235 | expect![[r#" | 235 | expect![[r#" |
236 | bn Foo Foo { bar, .. }$0 | 236 | bn Foo Foo { ${1:bar}, .. }$0 |
237 | bn Bar Bar($1, ..)$0 | 237 | bn Bar Bar($1, ..)$0 |
238 | "#]], | 238 | "#]], |
239 | ) | 239 | ) |
diff --git a/crates/completion/src/render/pattern.rs b/crates/completion/src/render/pattern.rs index e20b0027b..2327bf363 100644 --- a/crates/completion/src/render/pattern.rs +++ b/crates/completion/src/render/pattern.rs | |||
@@ -3,7 +3,10 @@ | |||
3 | use hir::{db::HirDatabase, HasVisibility, Name, StructKind}; | 3 | use hir::{db::HirDatabase, HasVisibility, Name, StructKind}; |
4 | use itertools::Itertools; | 4 | use itertools::Itertools; |
5 | 5 | ||
6 | use crate::{item::CompletionKind, render::RenderContext, CompletionItem, CompletionItemKind}; | 6 | use crate::{ |
7 | config::SnippetCap, item::CompletionKind, render::RenderContext, CompletionItem, | ||
8 | CompletionItemKind, | ||
9 | }; | ||
7 | 10 | ||
8 | pub(crate) fn render_struct_pat<'a>( | 11 | pub(crate) fn render_struct_pat<'a>( |
9 | ctx: RenderContext<'a>, | 12 | ctx: RenderContext<'a>, |
@@ -31,7 +34,9 @@ pub(crate) fn render_struct_pat<'a>( | |||
31 | StructKind::Tuple if ctx.snippet_cap().is_some() => { | 34 | StructKind::Tuple if ctx.snippet_cap().is_some() => { |
32 | render_tuple_as_pat(&fields, &name, fields_omitted) | 35 | render_tuple_as_pat(&fields, &name, fields_omitted) |
33 | } | 36 | } |
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 | } | ||
35 | _ => return None, | 40 | _ => return None, |
36 | }; | 41 | }; |
37 | 42 | ||
@@ -79,7 +84,9 @@ pub(crate) fn render_variant_pat<'a>( | |||
79 | StructKind::Tuple if ctx.snippet_cap().is_some() => { | 84 | StructKind::Tuple if ctx.snippet_cap().is_some() => { |
80 | render_tuple_as_pat(&fields, &name, fields_omitted) | 85 | render_tuple_as_pat(&fields, &name, fields_omitted) |
81 | } | 86 | } |
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 | } | ||
83 | _ => return None, | 90 | _ => return None, |
84 | }; | 91 | }; |
85 | 92 | ||
@@ -106,22 +113,36 @@ pub(crate) fn render_variant_pat<'a>( | |||
106 | 113 | ||
107 | fn render_record_as_pat( | 114 | fn render_record_as_pat( |
108 | db: &dyn HirDatabase, | 115 | db: &dyn HirDatabase, |
116 | snippet_cap: Option<SnippetCap>, | ||
109 | fields: &[hir::Field], | 117 | fields: &[hir::Field], |
110 | name: &str, | 118 | name: &str, |
111 | fields_omitted: bool, | 119 | fields_omitted: bool, |
112 | ) -> String { | 120 | ) -> String { |
113 | format!( | 121 | let fields = fields.iter(); |
114 | "{name} {{ {}{} }}", | 122 | if snippet_cap.is_some() { |
115 | fields.into_iter().map(|field| field.name(db)).format(", "), | 123 | format!( |
116 | if fields_omitted { ", .." } else { "" }, | 124 | "{name} {{ {}{} }}", |
117 | name = name | 125 | fields |
118 | ) | 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 | } | ||
119 | } | 140 | } |
120 | 141 | ||
121 | fn render_tuple_as_pat(fields: &[hir::Field], name: &str, fields_omitted: bool) -> String { | 142 | fn render_tuple_as_pat(fields: &[hir::Field], name: &str, fields_omitted: bool) -> String { |
122 | format!( | 143 | format!( |
123 | "{name}({}{})", | 144 | "{name}({}{})", |
124 | fields.into_iter().enumerate().map(|(idx, _)| format!("${}", idx + 1)).format(", "), | 145 | fields.iter().enumerate().map(|(idx, _)| format!("${}", idx + 1)).format(", "), |
125 | if fields_omitted { ", .." } else { "" }, | 146 | if fields_omitted { ", .." } else { "" }, |
126 | name = name | 147 | name = name |
127 | ) | 148 | ) |