aboutsummaryrefslogtreecommitdiff
path: root/crates/completion/src/render/pattern.rs
diff options
context:
space:
mode:
Diffstat (limited to 'crates/completion/src/render/pattern.rs')
-rw-r--r--crates/completion/src/render/pattern.rs41
1 files changed, 31 insertions, 10 deletions
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 @@
3use hir::{db::HirDatabase, HasVisibility, Name, StructKind}; 3use hir::{db::HirDatabase, HasVisibility, Name, StructKind};
4use itertools::Itertools; 4use itertools::Itertools;
5 5
6use crate::{item::CompletionKind, render::RenderContext, CompletionItem, CompletionItemKind}; 6use crate::{
7 config::SnippetCap, item::CompletionKind, render::RenderContext, CompletionItem,
8 CompletionItemKind,
9};
7 10
8pub(crate) fn render_struct_pat<'a>( 11pub(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
107fn render_record_as_pat( 114fn 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
121fn render_tuple_as_pat(fields: &[hir::Field], name: &str, fields_omitted: bool) -> String { 142fn 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 )