Skip to content

Commit 0bbead9

Browse files
committed
Auto merge of rust-lang#12473 - yue4u:fix/no-enum-parens-in-use, r=Veykril
fix: avoid adding enum parens in use path close rust-lang#12420
2 parents e9d3fe0 + 11693da commit 0bbead9

File tree

4 files changed

+43
-10
lines changed

4 files changed

+43
-10
lines changed

crates/ide-completion/src/render/literal.rs

+17-6
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ use hir::{db::HirDatabase, Documentation, HasAttrs, StructKind};
44
use ide_db::SymbolKind;
55

66
use crate::{
7-
context::{CompletionContext, PathCompletionCtx},
7+
context::{CompletionContext, PathCompletionCtx, PathKind},
88
item::{Builder, CompletionItem},
99
render::{
1010
compute_ref_match, compute_type_match,
@@ -50,9 +50,15 @@ fn render(
5050
path: Option<hir::ModPath>,
5151
) -> Option<Builder> {
5252
let db = completion.db;
53-
let kind = thing.kind(db);
54-
let has_call_parens =
55-
matches!(completion.path_context(), Some(PathCompletionCtx { has_call_parens: true, .. }));
53+
let mut kind = thing.kind(db);
54+
let should_add_parens = match completion.path_context() {
55+
Some(PathCompletionCtx { has_call_parens: true, .. }) => false,
56+
Some(PathCompletionCtx { kind: PathKind::Use | PathKind::Type { .. }, .. }) => {
57+
cov_mark::hit!(no_parens_in_use_item);
58+
false
59+
}
60+
_ => true,
61+
};
5662

5763
let fields = thing.fields(completion)?;
5864
let (qualified_name, short_qualified_name, qualified) = match path {
@@ -69,10 +75,10 @@ fn render(
6975
let snippet_cap = ctx.snippet_cap();
7076

7177
let mut rendered = match kind {
72-
StructKind::Tuple if !has_call_parens => {
78+
StructKind::Tuple if should_add_parens => {
7379
render_tuple_lit(db, snippet_cap, &fields, &qualified_name)
7480
}
75-
StructKind::Record if !has_call_parens => {
81+
StructKind::Record if should_add_parens => {
7682
render_record_lit(db, snippet_cap, &fields, &qualified_name)
7783
}
7884
_ => RenderedLiteral { literal: qualified_name.clone(), detail: qualified_name.clone() },
@@ -82,6 +88,11 @@ fn render(
8288
rendered.literal.push_str("$0");
8389
}
8490

91+
// only show name in label if not adding parens
92+
if !should_add_parens {
93+
kind = StructKind::Unit;
94+
}
95+
8596
let mut item = CompletionItem::new(
8697
CompletionItemKind::SymbolKind(thing.symbol_kind()),
8798
ctx.source_range(),

crates/ide-completion/src/tests/expression.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -600,8 +600,8 @@ fn func() {
600600
}
601601
"#,
602602
expect![[r#"
603-
fn variant fn() -> Enum
604-
ev Variant(…) Variant
603+
fn variant fn() -> Enum
604+
ev Variant Variant
605605
"#]],
606606
);
607607
}

crates/ide-completion/src/tests/pattern.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -435,7 +435,7 @@ fn foo() {
435435
}
436436
"#,
437437
expect![[r#"
438-
ev TupleVariant(…) TupleVariant
438+
ev TupleVariant TupleVariant
439439
"#]],
440440
);
441441
check_empty(
@@ -450,7 +450,7 @@ fn foo() {
450450
}
451451
"#,
452452
expect![[r#"
453-
ev RecordVariant {…} RecordVariant
453+
ev RecordVariant RecordVariant
454454
"#]],
455455
);
456456
}

crates/ide-completion/src/tests/use_tree.rs

+22
Original file line numberDiff line numberDiff line change
@@ -172,6 +172,28 @@ impl Foo {
172172
);
173173
}
174174

175+
#[test]
176+
fn enum_no_parens_in_qualified_use_tree() {
177+
cov_mark::check!(no_parens_in_use_item);
178+
cov_mark::check!(enum_plain_qualified_use_tree);
179+
check(
180+
r#"
181+
use Foo::$0
182+
183+
enum Foo {
184+
UnitVariant,
185+
TupleVariant(),
186+
RecordVariant {},
187+
}
188+
"#,
189+
expect![[r#"
190+
ev RecordVariant RecordVariant
191+
ev TupleVariant TupleVariant
192+
ev UnitVariant UnitVariant
193+
"#]],
194+
);
195+
}
196+
175197
#[test]
176198
fn self_qualified_use_tree() {
177199
check(

0 commit comments

Comments
 (0)