Emit snippets for struct pattern completion if enabled

This commit is contained in:
Lukas Wirth 2020-12-20 19:16:28 +01:00
parent b184bfad7a
commit 2f6a24950a
2 changed files with 36 additions and 15 deletions

View file

@ -156,7 +156,7 @@ fn foo() {
} }
"#, "#,
expect![[r#" expect![[r#"
bn Bar Bar { f }$0 bn Bar Bar { ${1:f} }$0
"#]], "#]],
); );
} }
@ -171,7 +171,7 @@ struct Baz;
fn outer(<|>) {} fn outer(<|>) {}
"#, "#,
expect![[r#" expect![[r#"
bn Foo Foo { bar, baz }: Foo$0 bn Foo Foo { ${1:bar}, ${2:baz} }: Foo$0
bn Bar Bar($1, $2): Bar$0 bn Bar Bar($1, $2): Bar$0
"#]], "#]],
) )
@ -189,7 +189,7 @@ fn outer() {
} }
"#, "#,
expect![[r#" expect![[r#"
bn Foo Foo { bar, baz }$0 bn Foo Foo { ${1:bar}, ${2:baz} }$0
bn Bar Bar($1, $2)$0 bn Bar Bar($1, $2)$0
"#]], "#]],
) )
@ -209,7 +209,7 @@ fn outer() {
} }
"#, "#,
expect![[r#" expect![[r#"
bn Foo Foo { bar, baz }$0 bn Foo Foo { ${1:bar}, ${2:baz} }$0
bn Bar Bar($1, $2)$0 bn Bar Bar($1, $2)$0
"#]], "#]],
) )
@ -233,7 +233,7 @@ fn outer() {
} }
"#, "#,
expect![[r#" expect![[r#"
bn Foo Foo { bar, .. }$0 bn Foo Foo { ${1:bar}, .. }$0
bn Bar Bar($1, ..)$0 bn Bar Bar($1, ..)$0
"#]], "#]],
) )

View file

@ -3,7 +3,10 @@
use hir::{db::HirDatabase, HasVisibility, Name, StructKind}; use hir::{db::HirDatabase, HasVisibility, Name, StructKind};
use itertools::Itertools; use itertools::Itertools;
use crate::{item::CompletionKind, render::RenderContext, CompletionItem, CompletionItemKind}; use crate::{
config::SnippetCap, item::CompletionKind, render::RenderContext, CompletionItem,
CompletionItemKind,
};
pub(crate) fn render_struct_pat<'a>( pub(crate) fn render_struct_pat<'a>(
ctx: RenderContext<'a>, ctx: RenderContext<'a>,
@ -31,7 +34,9 @@ pub(crate) fn render_struct_pat<'a>(
StructKind::Tuple if ctx.snippet_cap().is_some() => { StructKind::Tuple if ctx.snippet_cap().is_some() => {
render_tuple_as_pat(&fields, &name, fields_omitted) render_tuple_as_pat(&fields, &name, fields_omitted)
} }
StructKind::Record => render_record_as_pat(ctx.db(), &fields, &name, fields_omitted), StructKind::Record => {
render_record_as_pat(ctx.db(), ctx.snippet_cap(), &fields, &name, fields_omitted)
}
_ => return None, _ => return None,
}; };
@ -79,7 +84,9 @@ pub(crate) fn render_variant_pat<'a>(
StructKind::Tuple if ctx.snippet_cap().is_some() => { StructKind::Tuple if ctx.snippet_cap().is_some() => {
render_tuple_as_pat(&fields, &name, fields_omitted) render_tuple_as_pat(&fields, &name, fields_omitted)
} }
StructKind::Record => render_record_as_pat(ctx.db(), &fields, &name, fields_omitted), StructKind::Record => {
render_record_as_pat(ctx.db(), ctx.snippet_cap(), &fields, &name, fields_omitted)
}
_ => return None, _ => return None,
}; };
@ -106,22 +113,36 @@ pub(crate) fn render_variant_pat<'a>(
fn render_record_as_pat( fn render_record_as_pat(
db: &dyn HirDatabase, db: &dyn HirDatabase,
snippet_cap: Option<SnippetCap>,
fields: &[hir::Field], fields: &[hir::Field],
name: &str, name: &str,
fields_omitted: bool, fields_omitted: bool,
) -> String { ) -> String {
format!( let fields = fields.iter();
"{name} {{ {}{} }}", if snippet_cap.is_some() {
fields.into_iter().map(|field| field.name(db)).format(", "), format!(
if fields_omitted { ", .." } else { "" }, "{name} {{ {}{} }}",
name = name fields
) .enumerate()
.map(|(idx, field)| format!("${{{}:{}}}", idx + 1, field.name(db)))
.format(", "),
if fields_omitted { ", .." } else { "" },
name = name
)
} else {
format!(
"{name} {{ {}{} }}",
fields.map(|field| field.name(db)).format(", "),
if fields_omitted { ", .." } else { "" },
name = name
)
}
} }
fn render_tuple_as_pat(fields: &[hir::Field], name: &str, fields_omitted: bool) -> String { fn render_tuple_as_pat(fields: &[hir::Field], name: &str, fields_omitted: bool) -> String {
format!( format!(
"{name}({}{})", "{name}({}{})",
fields.into_iter().enumerate().map(|(idx, _)| format!("${}", idx + 1)).format(", "), fields.iter().enumerate().map(|(idx, _)| format!("${}", idx + 1)).format(", "),
if fields_omitted { ", .." } else { "" }, if fields_omitted { ", .." } else { "" },
name = name name = name
) )