refactor: pass is_variant_missing as args to build_completion

This commit is contained in:
feniljain 2023-03-18 11:43:57 +05:30
parent f7113685b5
commit a79a76a942
2 changed files with 15 additions and 21 deletions

View File

@ -32,22 +32,11 @@ pub(crate) struct RenderContext<'a> {
completion: &'a CompletionContext<'a>,
is_private_editable: bool,
import_to_add: Option<LocatedImport>,
// For variants which are missing
// in match completion context
//
// Option -> only applicable for enums
// bool -> is enum variant missing or not?
is_variant_missing: Option<bool>,
}
impl<'a> RenderContext<'a> {
pub(crate) fn new(completion: &'a CompletionContext<'a>) -> RenderContext<'a> {
RenderContext {
completion,
is_private_editable: false,
import_to_add: None,
is_variant_missing: None,
}
RenderContext { completion, is_private_editable: false, import_to_add: None }
}
pub(crate) fn private_editable(mut self, private_editable: bool) -> Self {

View File

@ -39,11 +39,11 @@ pub(crate) fn render_struct_pat(
let db = ctx.db();
Some(build_completion(ctx, label, lookup, pat, strukt, strukt.ty(db)))
Some(build_completion(ctx, label, lookup, pat, strukt, strukt.ty(db), false))
}
pub(crate) fn render_variant_pat(
mut ctx: RenderContext<'_>,
ctx: RenderContext<'_>,
pattern_ctx: &PatternContext,
path_ctx: Option<&PathCompletionCtx>,
variant: hir::Variant,
@ -56,11 +56,6 @@ pub(crate) fn render_variant_pat(
let (visible_fields, fields_omitted) = visible_fields(ctx.completion, &fields, variant)?;
let enum_ty = variant.parent_enum(ctx.db()).ty(ctx.db());
// Missing in context of match statement completions
if pattern_ctx.missing_variants.contains(&variant) {
ctx.is_variant_missing = Some(true);
}
let (name, escaped_name) = match path {
Some(path) => (path.unescaped().to_string().into(), path.to_string().into()),
None => {
@ -89,7 +84,15 @@ pub(crate) fn render_variant_pat(
}
};
Some(build_completion(ctx, label, lookup, pat, variant, enum_ty))
Some(build_completion(
ctx,
label,
lookup,
pat,
variant,
enum_ty,
pattern_ctx.missing_variants.contains(&variant),
))
}
fn build_completion(
@ -99,10 +102,12 @@ fn build_completion(
pat: String,
def: impl HasAttrs + Copy,
adt_ty: hir::Type,
// Missing in context of match statement completions
is_variant_missing: bool,
) -> CompletionItem {
let mut relevance = ctx.completion_relevance();
if let Some(true) = ctx.is_variant_missing {
if is_variant_missing {
relevance.type_match = super::compute_type_match(ctx.completion, &adt_ty);
}