Emit snippets for struct pattern completion if enabled
This commit is contained in:
parent
b184bfad7a
commit
2f6a24950a
@ -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
|
||||
"#]],
|
||||
)
|
||||
|
@ -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<SnippetCap>,
|
||||
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
|
||||
)
|
||||
|
Loading…
Reference in New Issue
Block a user