fix: variants rendering in pattern path

This commit is contained in:
yue4u 2022-06-26 03:33:19 +09:00
parent ec78b6b08a
commit 622b516c74
9 changed files with 83 additions and 73 deletions

View File

@ -357,6 +357,12 @@ impl Completions {
variant: hir::Variant,
local_name: Option<hir::Name>,
) {
if let PathCompletionCtx { kind: PathKind::Pat { pat_ctx }, .. } = path_ctx {
cov_mark::hit!(enum_variant_pattern_path);
self.add_variant_pat(ctx, pat_ctx, variant, local_name);
return;
}
if let Some(builder) =
render_variant_lit(RenderContext::new(ctx), path_ctx, local_name, variant, None)
{

View File

@ -115,6 +115,7 @@ pub(crate) fn complete_expr_path(
};
if let Some(hir::Adt::Enum(e)) = ty.as_adt() {
cov_mark::hit!(completes_variant_through_self);
acc.add_enum_variants(ctx, path_ctx, e);
}

View File

@ -151,7 +151,6 @@ pub(crate) fn complete_pattern_path(
};
if let Some(hir::Adt::Enum(e)) = ty.as_adt() {
cov_mark::hit!(enum_plain_qualified_use_tree);
acc.add_enum_variants(ctx, path_ctx, e);
}

View File

@ -79,9 +79,7 @@ pub(crate) fn complete_use_path(
}
hir::PathResolution::Def(hir::ModuleDef::Adt(hir::Adt::Enum(e))) => {
cov_mark::hit!(enum_plain_qualified_use_tree);
e.variants(ctx.db)
.into_iter()
.for_each(|variant| acc.add_enum_variant(ctx, path_ctx, variant, None));
acc.add_enum_variants(ctx, path_ctx, *e);
}
_ => {}
}

View File

@ -7,7 +7,10 @@ use syntax::SmolStr;
use crate::{
context::{ParamKind, PatternContext},
render::{variant::visible_fields, RenderContext},
render::{
variant::{format_literal_label, visible_fields},
RenderContext,
},
CompletionItem, CompletionItemKind,
};
@ -29,16 +32,11 @@ pub(crate) fn render_struct_pat(
let name = local_name.unwrap_or_else(|| strukt.name(ctx.db()));
let (name, escaped_name) = (name.to_smol_str(), name.escaped().to_smol_str());
let pat = render_pat(
&ctx,
pattern_ctx,
&escaped_name,
strukt.kind(ctx.db()),
&visible_fields,
fields_omitted,
)?;
let kind = strukt.kind(ctx.db());
let label = format_literal_label(name.as_str(), kind);
let pat = render_pat(&ctx, pattern_ctx, &escaped_name, kind, &visible_fields, fields_omitted)?;
Some(build_completion(ctx, name, pat, strukt))
Some(build_completion(ctx, label, pat, strukt))
}
pub(crate) fn render_variant_pat(
@ -60,25 +58,20 @@ pub(crate) fn render_variant_pat(
(name.to_smol_str(), name.escaped().to_smol_str())
}
};
let pat = render_pat(
&ctx,
pattern_ctx,
&escaped_name,
variant.kind(ctx.db()),
&visible_fields,
fields_omitted,
)?;
let kind = variant.kind(ctx.db());
let label = format_literal_label(name.as_str(), kind);
let pat = render_pat(&ctx, pattern_ctx, &escaped_name, kind, &visible_fields, fields_omitted)?;
Some(build_completion(ctx, name, pat, variant))
Some(build_completion(ctx, label, pat, variant))
}
fn build_completion(
ctx: RenderContext<'_>,
name: SmolStr,
label: SmolStr,
pat: String,
def: impl HasAttrs + Copy,
) -> CompletionItem {
let mut item = CompletionItem::new(CompletionItemKind::Binding, ctx.source_range(), name);
let mut item = CompletionItem::new(CompletionItemKind::Binding, ctx.source_range(), label);
item.set_documentation(ctx.docs(def))
.set_deprecated(ctx.is_deprecated(def))
.detail(&pat)

View File

@ -139,8 +139,8 @@ fn foo2($0) {}
"#,
expect![[r#"
st Bar
bn Bar Bar { bar$1 }: Bar$0
bn Bar { bar }: Bar
bn Bar {} Bar { bar$1 }: Bar$0
kw mut
kw ref
"#]],

View File

@ -1,7 +1,7 @@
//! Completion tests for pattern position.
use expect_test::{expect, Expect};
use crate::tests::{completion_list, BASE_ITEMS_FIXTURE};
use crate::tests::{check_edit, completion_list, BASE_ITEMS_FIXTURE};
fn check_empty(ra_fixture: &str, expect: Expect) {
let actual = completion_list(ra_fixture);
@ -127,15 +127,15 @@ fn foo() {
expect![[r#"
ct CONST
en Enum
ma makro!() macro_rules! makro
ma makro!() macro_rules! makro
md module
st Record
st Tuple
st Unit
ev TupleV
bn Record Record { field$1 }$0
bn Tuple Tuple($1)$0
bn TupleV TupleV($1)$0
bn Record {} Record { field$1 }$0
bn Tuple() Tuple($1)$0
bn TupleV() TupleV($1)$0
kw mut
kw ref
"#]],
@ -162,8 +162,8 @@ fn foo() {
st Tuple
st Unit
ev Variant
bn Record Record { field$1 }$0
bn Tuple Tuple($1)$0
bn Record {} Record { field$1 }$0
bn Tuple() Tuple($1)$0
kw mut
kw ref
"#]],
@ -178,13 +178,13 @@ fn foo(a$0) {
}
"#,
expect![[r#"
ma makro!() macro_rules! makro
ma makro!() macro_rules! makro
md module
st Record
st Tuple
st Unit
bn Record Record { field$1 }: Record$0
bn Tuple Tuple($1): Tuple$0
bn Record {} Record { field$1 }: Record$0
bn Tuple() Tuple($1): Tuple$0
kw mut
kw ref
"#]],
@ -195,13 +195,13 @@ fn foo(a$0: Tuple) {
}
"#,
expect![[r#"
ma makro!() macro_rules! makro
ma makro!() macro_rules! makro
md module
st Record
st Tuple
st Unit
bn Record Record { field$1 }$0
bn Tuple Tuple($1)$0
bn Record {} Record { field$1 }$0
bn Tuple() Tuple($1)$0
kw mut
kw ref
"#]],
@ -269,8 +269,8 @@ fn outer() {
st Invisible
st Record
st Tuple
bn Record Record { field$1, .. }$0
bn Tuple Tuple($1, ..)$0
bn Record {} Record { field$1, .. }$0
bn Tuple() Tuple($1, ..)$0
kw mut
kw ref
"#]],
@ -293,8 +293,8 @@ impl Foo {
expect![[r#"
sp Self
st Foo
bn Foo Foo($1)$0
bn Self Self($1)$0
bn Foo() Foo($1)$0
bn Self() Self($1)$0
kw mut
kw ref
"#]],
@ -316,9 +316,8 @@ fn func() {
"#,
expect![[r#"
ct ASSOC_CONST const ASSOC_CONST: ()
ev RecordV {} RecordV { field: u32 }
ev TupleV() TupleV(u32)
ev UnitV UnitV
bn RecordV {} RecordV { field$1 }$0
bn TupleV() TupleV($1)$0
"#]],
);
}
@ -334,8 +333,8 @@ fn outer(Foo { bar: $0 }: Foo) {}
expect![[r#"
st Bar
st Foo
bn Bar Bar($1)$0
bn Foo Foo { bar$1 }$0
bn Bar() Bar($1)$0
bn Foo {} Foo { bar$1 }$0
kw mut
kw ref
"#]],
@ -368,8 +367,8 @@ fn foo($0) {}
expect![[r#"
st Bar
st Foo
bn Bar Bar($1): Bar$0
bn Foo Foo { bar$1 }: Foo$0
bn Bar() Bar($1): Bar$0
bn Foo {} Foo { bar$1 }: Foo$0
kw mut
kw ref
"#]],
@ -389,8 +388,8 @@ fn foo() {
expect![[r#"
st Bar
st Foo
bn Bar Bar($1)$0
bn Foo Foo { bar$1 }$0
bn Bar() Bar($1)$0
bn Foo {} Foo { bar$1 }$0
kw mut
kw ref
"#]],
@ -443,7 +442,7 @@ fn foo() {
}
"#,
expect![[r#"
ev TupleVariant TupleVariant
bn TupleVariant() TupleVariant($1)$0
"#]],
);
check_empty(
@ -458,11 +457,39 @@ fn foo() {
}
"#,
expect![[r#"
ev RecordVariant RecordVariant
bn RecordVariant {} RecordVariant { field$1 }$0
"#]],
);
}
#[test]
fn completes_enum_variant_pat() {
cov_mark::check!(enum_variant_pattern_path);
check_edit(
"RecordVariant {…}",
r#"
enum Enum {
RecordVariant { field: u32 }
}
fn foo() {
match (Enum::RecordVariant { field: 0 }) {
Enum::RecordV$0
}
}
"#,
r#"
enum Enum {
RecordVariant { field: u32 }
}
fn foo() {
match (Enum::RecordVariant { field: 0 }) {
Enum::RecordVariant { field$1 }$0
}
}
"#,
);
}
#[test]
fn completes_associated_const() {
check_empty(

View File

@ -519,6 +519,7 @@ fn foo() {
#[test]
fn completes_variant_through_self() {
cov_mark::check!(completes_variant_through_self);
check(
r#"
enum Foo {

View File

@ -165,30 +165,15 @@ fn enum_plain_qualified_use_tree() {
r#"
use Foo::$0
enum Foo { Variant }
impl Foo {
const CONST: () = ()
fn func() {}
}
"#,
expect![[r#"
ev Variant Variant
"#]],
);
}
#[test]
fn enum_no_parens_in_qualified_use_tree() {
cov_mark::check!(enum_plain_qualified_use_tree);
check(
r#"
use Foo::$0
enum Foo {
UnitVariant,
TupleVariant(),
RecordVariant {},
}
impl Foo {
const CONST: () = ()
fn func() {}
}
"#,
expect![[r#"
ev RecordVariant RecordVariant