2020-04-01 00:50:45 +03:00
|
|
|
//! Complete fields in record literals and patterns.
|
2021-12-22 02:25:38 +01:00
|
|
|
use ide_db::SymbolKind;
|
2021-08-10 17:03:38 +02:00
|
|
|
use syntax::{ast::Expr, T};
|
2020-11-13 17:17:16 +01:00
|
|
|
|
2021-05-30 21:23:42 +02:00
|
|
|
use crate::{
|
2021-10-27 17:18:42 +02:00
|
|
|
patterns::ImmediateLocation, CompletionContext, CompletionItem, CompletionItemKind, Completions,
|
2021-05-30 21:23:42 +02:00
|
|
|
};
|
2020-04-07 15:07:18 +02:00
|
|
|
|
2020-10-25 10:59:15 +03:00
|
|
|
pub(crate) fn complete_record(acc: &mut Completions, ctx: &CompletionContext) -> Option<()> {
|
2021-05-30 21:23:42 +02:00
|
|
|
let missing_fields = match &ctx.completion_location {
|
2021-08-10 17:03:38 +02:00
|
|
|
Some(
|
|
|
|
ImmediateLocation::RecordExpr(record_expr)
|
|
|
|
| ImmediateLocation::RecordExprUpdate(record_expr),
|
|
|
|
) => {
|
2021-05-30 21:23:42 +02:00
|
|
|
let ty = ctx.sema.type_of_expr(&Expr::RecordExpr(record_expr.clone()));
|
2021-12-22 02:25:38 +01:00
|
|
|
let default_trait = ctx.famous_defs().core_default_Default();
|
2021-08-03 17:28:51 +02:00
|
|
|
let impl_default_trait = default_trait.zip(ty).map_or(false, |(default_trait, ty)| {
|
|
|
|
ty.original.impls_trait(ctx.db, default_trait, &[])
|
|
|
|
});
|
2020-11-13 17:17:16 +01:00
|
|
|
|
2021-05-30 21:23:42 +02:00
|
|
|
let missing_fields = ctx.sema.record_literal_missing_fields(record_expr);
|
2021-08-21 20:45:15 +02:00
|
|
|
if impl_default_trait && !missing_fields.is_empty() && ctx.path_qual().is_none() {
|
2021-01-01 11:17:15 +11:00
|
|
|
let completion_text = "..Default::default()";
|
2021-10-27 17:18:42 +02:00
|
|
|
let mut item =
|
|
|
|
CompletionItem::new(SymbolKind::Field, ctx.source_range(), completion_text);
|
2021-05-27 03:47:20 +02:00
|
|
|
let completion_text =
|
|
|
|
completion_text.strip_prefix(ctx.token.text()).unwrap_or(completion_text);
|
2021-10-27 17:18:42 +02:00
|
|
|
item.insert_text(completion_text);
|
2021-03-12 12:12:32 +03:00
|
|
|
item.add_to(acc);
|
2020-11-13 17:17:16 +01:00
|
|
|
}
|
2021-08-10 17:03:38 +02:00
|
|
|
if ctx.previous_token_is(T![.]) {
|
|
|
|
let mut item =
|
2021-10-27 17:18:42 +02:00
|
|
|
CompletionItem::new(CompletionItemKind::Snippet, ctx.source_range(), "..");
|
|
|
|
item.insert_text(".");
|
2021-08-10 17:03:38 +02:00
|
|
|
item.add_to(acc);
|
|
|
|
return None;
|
|
|
|
}
|
2020-11-13 17:17:16 +01:00
|
|
|
missing_fields
|
|
|
|
}
|
2021-05-30 21:23:42 +02:00
|
|
|
Some(ImmediateLocation::RecordPat(record_pat)) => {
|
|
|
|
ctx.sema.record_pattern_missing_fields(record_pat)
|
|
|
|
}
|
|
|
|
_ => return None,
|
2020-04-07 17:09:02 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
for (field, ty) in missing_fields {
|
2021-05-28 14:02:53 +02:00
|
|
|
acc.add_field(ctx, None, field, &ty);
|
2020-04-01 00:50:45 +03:00
|
|
|
}
|
|
|
|
|
2020-04-07 17:09:02 +02:00
|
|
|
Some(())
|
2020-04-01 00:50:45 +03:00
|
|
|
}
|
2021-07-22 19:59:01 +02:00
|
|
|
|
2021-08-04 00:46:50 +02:00
|
|
|
pub(crate) fn complete_record_literal(
|
|
|
|
acc: &mut Completions,
|
|
|
|
ctx: &CompletionContext,
|
|
|
|
) -> Option<()> {
|
|
|
|
if !ctx.expects_expression() {
|
|
|
|
return None;
|
|
|
|
}
|
|
|
|
|
|
|
|
if let hir::Adt::Struct(strukt) = ctx.expected_type.as_ref()?.as_adt()? {
|
2022-02-01 23:29:40 +01:00
|
|
|
let module = if let Some(module) = ctx.module { module } else { strukt.module(ctx.db) };
|
2021-11-23 10:57:29 -06:00
|
|
|
|
|
|
|
let path = module.find_use_path(ctx.db, hir::ModuleDef::from(strukt));
|
|
|
|
|
|
|
|
acc.add_struct_literal(ctx, strukt, path, None);
|
2021-08-04 00:46:50 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
Some(())
|
|
|
|
}
|
|
|
|
|
2021-07-22 19:59:01 +02:00
|
|
|
#[cfg(test)]
|
|
|
|
mod tests {
|
|
|
|
use crate::tests::check_edit;
|
|
|
|
|
|
|
|
#[test]
|
2021-08-04 00:46:50 +02:00
|
|
|
fn literal_struct_completion_edit() {
|
|
|
|
check_edit(
|
|
|
|
"FooDesc {…}",
|
|
|
|
r#"
|
|
|
|
struct FooDesc { pub bar: bool }
|
|
|
|
|
|
|
|
fn create_foo(foo_desc: &FooDesc) -> () { () }
|
|
|
|
|
|
|
|
fn baz() {
|
|
|
|
let foo = create_foo(&$0);
|
|
|
|
}
|
|
|
|
"#,
|
|
|
|
r#"
|
|
|
|
struct FooDesc { pub bar: bool }
|
|
|
|
|
|
|
|
fn create_foo(foo_desc: &FooDesc) -> () { () }
|
|
|
|
|
|
|
|
fn baz() {
|
|
|
|
let foo = create_foo(&FooDesc { bar: ${1:()} }$0);
|
|
|
|
}
|
|
|
|
"#,
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
2021-11-23 10:57:29 -06:00
|
|
|
#[test]
|
|
|
|
fn literal_struct_completion_from_sub_modules() {
|
|
|
|
check_edit(
|
|
|
|
"Struct {…}",
|
|
|
|
r#"
|
|
|
|
mod submod {
|
|
|
|
pub struct Struct {
|
|
|
|
pub a: u64,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn f() -> submod::Struct {
|
|
|
|
Stru$0
|
|
|
|
}
|
|
|
|
"#,
|
|
|
|
r#"
|
|
|
|
mod submod {
|
|
|
|
pub struct Struct {
|
|
|
|
pub a: u64,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn f() -> submod::Struct {
|
|
|
|
submod::Struct { a: ${1:()} }$0
|
|
|
|
}
|
|
|
|
"#,
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
2021-08-04 00:46:50 +02:00
|
|
|
#[test]
|
|
|
|
fn literal_struct_complexion_module() {
|
|
|
|
check_edit(
|
|
|
|
"FooDesc {…}",
|
|
|
|
r#"
|
|
|
|
mod _69latrick {
|
|
|
|
pub struct FooDesc { pub six: bool, pub neuf: Vec<String>, pub bar: bool }
|
|
|
|
pub fn create_foo(foo_desc: &FooDesc) -> () { () }
|
|
|
|
}
|
|
|
|
|
|
|
|
fn baz() {
|
|
|
|
use _69latrick::*;
|
|
|
|
|
|
|
|
let foo = create_foo(&$0);
|
|
|
|
}
|
|
|
|
"#,
|
|
|
|
r#"
|
|
|
|
mod _69latrick {
|
|
|
|
pub struct FooDesc { pub six: bool, pub neuf: Vec<String>, pub bar: bool }
|
|
|
|
pub fn create_foo(foo_desc: &FooDesc) -> () { () }
|
|
|
|
}
|
|
|
|
|
|
|
|
fn baz() {
|
|
|
|
use _69latrick::*;
|
|
|
|
|
|
|
|
let foo = create_foo(&FooDesc { six: ${1:()}, neuf: ${2:()}, bar: ${3:()} }$0);
|
|
|
|
}
|
|
|
|
"#,
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
2021-07-22 19:59:01 +02:00
|
|
|
fn default_completion_edit() {
|
|
|
|
check_edit(
|
|
|
|
"..Default::default()",
|
|
|
|
r#"
|
|
|
|
//- minicore: default
|
|
|
|
struct Struct { foo: u32, bar: usize }
|
|
|
|
|
|
|
|
impl Default for Struct {
|
|
|
|
fn default() -> Self {}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn foo() {
|
|
|
|
let other = Struct {
|
|
|
|
foo: 5,
|
|
|
|
.$0
|
|
|
|
};
|
|
|
|
}
|
|
|
|
"#,
|
|
|
|
r#"
|
|
|
|
struct Struct { foo: u32, bar: usize }
|
|
|
|
|
|
|
|
impl Default for Struct {
|
|
|
|
fn default() -> Self {}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn foo() {
|
|
|
|
let other = Struct {
|
|
|
|
foo: 5,
|
|
|
|
..Default::default()
|
|
|
|
};
|
|
|
|
}
|
|
|
|
"#,
|
|
|
|
);
|
|
|
|
check_edit(
|
|
|
|
"..Default::default()",
|
|
|
|
r#"
|
|
|
|
//- minicore: default
|
|
|
|
struct Struct { foo: u32, bar: usize }
|
|
|
|
|
|
|
|
impl Default for Struct {
|
|
|
|
fn default() -> Self {}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn foo() {
|
|
|
|
let other = Struct {
|
|
|
|
foo: 5,
|
|
|
|
$0
|
|
|
|
};
|
|
|
|
}
|
|
|
|
"#,
|
|
|
|
r#"
|
|
|
|
struct Struct { foo: u32, bar: usize }
|
|
|
|
|
|
|
|
impl Default for Struct {
|
|
|
|
fn default() -> Self {}
|
|
|
|
}
|
|
|
|
|
2021-08-10 14:58:14 +02:00
|
|
|
fn foo() {
|
|
|
|
let other = Struct {
|
|
|
|
foo: 5,
|
|
|
|
..Default::default()
|
|
|
|
};
|
|
|
|
}
|
|
|
|
"#,
|
|
|
|
);
|
|
|
|
check_edit(
|
|
|
|
"..Default::default()",
|
|
|
|
r#"
|
|
|
|
//- minicore: default
|
|
|
|
struct Struct { foo: u32, bar: usize }
|
|
|
|
|
|
|
|
impl Default for Struct {
|
|
|
|
fn default() -> Self {}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn foo() {
|
|
|
|
let other = Struct {
|
|
|
|
foo: 5,
|
|
|
|
..$0
|
|
|
|
};
|
|
|
|
}
|
|
|
|
"#,
|
|
|
|
r#"
|
|
|
|
struct Struct { foo: u32, bar: usize }
|
|
|
|
|
|
|
|
impl Default for Struct {
|
|
|
|
fn default() -> Self {}
|
|
|
|
}
|
|
|
|
|
2021-07-22 19:59:01 +02:00
|
|
|
fn foo() {
|
|
|
|
let other = Struct {
|
|
|
|
foo: 5,
|
|
|
|
..Default::default()
|
|
|
|
};
|
|
|
|
}
|
|
|
|
"#,
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|