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>, completion: &'a CompletionContext<'a>,
is_private_editable: bool, is_private_editable: bool,
import_to_add: Option<LocatedImport>, 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> { impl<'a> RenderContext<'a> {
pub(crate) fn new(completion: &'a CompletionContext<'a>) -> RenderContext<'a> { pub(crate) fn new(completion: &'a CompletionContext<'a>) -> RenderContext<'a> {
RenderContext { RenderContext { completion, is_private_editable: false, import_to_add: None }
completion,
is_private_editable: false,
import_to_add: None,
is_variant_missing: None,
}
} }
pub(crate) fn private_editable(mut self, private_editable: bool) -> Self { 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(); 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( pub(crate) fn render_variant_pat(
mut ctx: RenderContext<'_>, ctx: RenderContext<'_>,
pattern_ctx: &PatternContext, pattern_ctx: &PatternContext,
path_ctx: Option<&PathCompletionCtx>, path_ctx: Option<&PathCompletionCtx>,
variant: hir::Variant, 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 (visible_fields, fields_omitted) = visible_fields(ctx.completion, &fields, variant)?;
let enum_ty = variant.parent_enum(ctx.db()).ty(ctx.db()); 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 { let (name, escaped_name) = match path {
Some(path) => (path.unescaped().to_string().into(), path.to_string().into()), Some(path) => (path.unescaped().to_string().into(), path.to_string().into()),
None => { 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( fn build_completion(
@ -99,10 +102,12 @@ fn build_completion(
pat: String, pat: String,
def: impl HasAttrs + Copy, def: impl HasAttrs + Copy,
adt_ty: hir::Type, adt_ty: hir::Type,
// Missing in context of match statement completions
is_variant_missing: bool,
) -> CompletionItem { ) -> CompletionItem {
let mut relevance = ctx.completion_relevance(); 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); relevance.type_match = super::compute_type_match(ctx.completion, &adt_ty);
} }