From 11693dad88fec48de802b936b792a4776064fc1d Mon Sep 17 00:00:00 2001 From: yue4u Date: Mon, 6 Jun 2022 01:34:01 +0900 Subject: [PATCH] fix: avoid adding enum parens in use --- crates/ide-completion/src/render/literal.rs | 23 ++++++++++++++----- crates/ide-completion/src/tests/expression.rs | 4 ++-- crates/ide-completion/src/tests/pattern.rs | 4 ++-- crates/ide-completion/src/tests/use_tree.rs | 22 ++++++++++++++++++ 4 files changed, 43 insertions(+), 10 deletions(-) diff --git a/crates/ide-completion/src/render/literal.rs b/crates/ide-completion/src/render/literal.rs index e6540e6ac53c8..fd76dedad0654 100644 --- a/crates/ide-completion/src/render/literal.rs +++ b/crates/ide-completion/src/render/literal.rs @@ -4,7 +4,7 @@ use hir::{db::HirDatabase, Documentation, HasAttrs, StructKind}; use ide_db::SymbolKind; use crate::{ - context::{CompletionContext, PathCompletionCtx}, + context::{CompletionContext, PathCompletionCtx, PathKind}, item::{Builder, CompletionItem}, render::{ compute_ref_match, compute_type_match, @@ -50,9 +50,15 @@ fn render( path: Option, ) -> Option { let db = completion.db; - let kind = thing.kind(db); - let has_call_parens = - matches!(completion.path_context(), Some(PathCompletionCtx { has_call_parens: true, .. })); + let mut kind = thing.kind(db); + let should_add_parens = match completion.path_context() { + Some(PathCompletionCtx { has_call_parens: true, .. }) => false, + Some(PathCompletionCtx { kind: PathKind::Use | PathKind::Type { .. }, .. }) => { + cov_mark::hit!(no_parens_in_use_item); + false + } + _ => true, + }; let fields = thing.fields(completion)?; let (qualified_name, short_qualified_name, qualified) = match path { @@ -69,10 +75,10 @@ fn render( let snippet_cap = ctx.snippet_cap(); let mut rendered = match kind { - StructKind::Tuple if !has_call_parens => { + StructKind::Tuple if should_add_parens => { render_tuple_lit(db, snippet_cap, &fields, &qualified_name) } - StructKind::Record if !has_call_parens => { + StructKind::Record if should_add_parens => { render_record_lit(db, snippet_cap, &fields, &qualified_name) } _ => RenderedLiteral { literal: qualified_name.clone(), detail: qualified_name.clone() }, @@ -82,6 +88,11 @@ fn render( rendered.literal.push_str("$0"); } + // only show name in label if not adding parens + if !should_add_parens { + kind = StructKind::Unit; + } + let mut item = CompletionItem::new( CompletionItemKind::SymbolKind(thing.symbol_kind()), ctx.source_range(), diff --git a/crates/ide-completion/src/tests/expression.rs b/crates/ide-completion/src/tests/expression.rs index 61246b963e15f..c05a7ad9fa5d0 100644 --- a/crates/ide-completion/src/tests/expression.rs +++ b/crates/ide-completion/src/tests/expression.rs @@ -600,8 +600,8 @@ fn func() { } "#, expect![[r#" - fn variant fn() -> Enum - ev Variant(…) Variant + fn variant fn() -> Enum + ev Variant Variant "#]], ); } diff --git a/crates/ide-completion/src/tests/pattern.rs b/crates/ide-completion/src/tests/pattern.rs index bf88070769eb8..d21f62f4adbc2 100644 --- a/crates/ide-completion/src/tests/pattern.rs +++ b/crates/ide-completion/src/tests/pattern.rs @@ -435,7 +435,7 @@ fn foo() { } "#, expect![[r#" - ev TupleVariant(…) TupleVariant + ev TupleVariant TupleVariant "#]], ); check_empty( @@ -450,7 +450,7 @@ fn foo() { } "#, expect![[r#" - ev RecordVariant {…} RecordVariant + ev RecordVariant RecordVariant "#]], ); } diff --git a/crates/ide-completion/src/tests/use_tree.rs b/crates/ide-completion/src/tests/use_tree.rs index 70aa5e5468a5b..2d2ec864d373f 100644 --- a/crates/ide-completion/src/tests/use_tree.rs +++ b/crates/ide-completion/src/tests/use_tree.rs @@ -172,6 +172,28 @@ impl Foo { ); } +#[test] +fn enum_no_parens_in_qualified_use_tree() { + cov_mark::check!(no_parens_in_use_item); + cov_mark::check!(enum_plain_qualified_use_tree); + check( + r#" +use Foo::$0 + +enum Foo { + UnitVariant, + TupleVariant(), + RecordVariant {}, +} +"#, + expect![[r#" + ev RecordVariant RecordVariant + ev TupleVariant TupleVariant + ev UnitVariant UnitVariant + "#]], + ); +} + #[test] fn self_qualified_use_tree() { check(