From 2f6a24950a555bdfecbda7a50735d643f9d0e7f9 Mon Sep 17 00:00:00 2001 From: Lukas Wirth Date: Sun, 20 Dec 2020 19:16:28 +0100 Subject: [PATCH] Emit snippets for struct pattern completion if enabled --- crates/completion/src/completions/pattern.rs | 10 ++--- 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 496f0b04000..23a00803c0a 100644 --- a/crates/completion/src/completions/pattern.rs +++ b/crates/completion/src/completions/pattern.rs @@ -156,7 +156,7 @@ fn foo() { } "#, expect![[r#" - bn Bar Bar { f }$0 + bn Bar Bar { ${1:f} }$0 "#]], ); } @@ -171,7 +171,7 @@ struct Foo { bar: String, baz: String } fn outer(<|>) {} "#, 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 "#]], ) @@ -189,7 +189,7 @@ fn outer() { } "#, expect![[r#" - bn Foo Foo { bar, baz }$0 + bn Foo Foo { ${1:bar}, ${2:baz} }$0 bn Bar Bar($1, $2)$0 "#]], ) @@ -209,7 +209,7 @@ fn outer() { } "#, expect![[r#" - bn Foo Foo { bar, baz }$0 + bn Foo Foo { ${1:bar}, ${2:baz} }$0 bn Bar Bar($1, $2)$0 "#]], ) @@ -233,7 +233,7 @@ fn outer() { } "#, expect![[r#" - bn Foo Foo { bar, .. }$0 + bn Foo Foo { ${1:bar}, .. }$0 bn Bar Bar($1, ..)$0 "#]], ) diff --git a/crates/completion/src/render/pattern.rs b/crates/completion/src/render/pattern.rs index e20b0027bd9..2327bf36309 100644 --- a/crates/completion/src/render/pattern.rs +++ b/crates/completion/src/render/pattern.rs @@ -3,7 +3,10 @@ use hir::{db::HirDatabase, HasVisibility, Name, StructKind}; 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>( ctx: RenderContext<'a>, @@ -31,7 +34,9 @@ pub(crate) fn render_struct_pat<'a>( StructKind::Tuple if ctx.snippet_cap().is_some() => { 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, }; @@ -79,7 +84,9 @@ pub(crate) fn render_variant_pat<'a>( StructKind::Tuple if ctx.snippet_cap().is_some() => { 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, }; @@ -106,22 +113,36 @@ pub(crate) fn render_variant_pat<'a>( fn render_record_as_pat( db: &dyn HirDatabase, + snippet_cap: Option, fields: &[hir::Field], name: &str, fields_omitted: bool, ) -> String { - format!( - "{name} {{ {}{} }}", - fields.into_iter().map(|field| field.name(db)).format(", "), - if fields_omitted { ", .." } else { "" }, - name = name - ) + let fields = fields.iter(); + if snippet_cap.is_some() { + format!( + "{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 { format!( "{name}({}{})", - fields.into_iter().enumerate().map(|(idx, _)| format!("${}", idx + 1)).format(", "), + fields.iter().enumerate().map(|(idx, _)| format!("${}", idx + 1)).format(", "), if fields_omitted { ", .." } else { "" }, name = name )