47 lines
1.7 KiB
Rust
Raw Normal View History

//! Complete fields in record literals and patterns.
use ide_db::{helpers::FamousDefs, SymbolKind};
use syntax::ast::Expr;
use crate::{
item::CompletionKind, patterns::ImmediateLocation, CompletionContext, CompletionItem,
Completions,
};
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<()> {
let missing_fields = match &ctx.completion_location {
Some(ImmediateLocation::RecordExpr(record_expr)) => {
let ty = ctx.sema.type_of_expr(&Expr::RecordExpr(record_expr.clone()));
let default_trait = FamousDefs(&ctx.sema, ctx.krate).core_default_Default();
let impl_default_trait = default_trait
2021-05-27 03:47:20 +02:00
.zip(ty)
.map_or(false, |(default_trait, ty)| ty.impls_trait(ctx.db, default_trait, &[]));
let missing_fields = ctx.sema.record_literal_missing_fields(record_expr);
if impl_default_trait && !missing_fields.is_empty() {
let completion_text = "..Default::default()";
2021-03-12 12:12:32 +03:00
let mut item = CompletionItem::new(
CompletionKind::Snippet,
ctx.source_range(),
2021-05-27 03:47:20 +02:00
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-03-12 12:12:32 +03:00
item.insert_text(completion_text).kind(SymbolKind::Field);
item.add_to(acc);
}
missing_fields
}
Some(ImmediateLocation::RecordPat(record_pat)) => {
ctx.sema.record_pattern_missing_fields(record_pat)
}
_ => return None,
};
for (field, ty) in missing_fields {
acc.add_field(ctx, None, field, &ty);
}
Some(())
}