Skip to content

Commit 754d868

Browse files
bors[bot]Veykril
andauthored
Merge #11785
11785: fix: Fix tuple- and record struct completions not working with existing braces r=Veykril a=Veykril bors r+ Co-authored-by: Lukas Wirth <[email protected]>
2 parents d61e02d + 000e681 commit 754d868

File tree

4 files changed

+49
-16
lines changed

4 files changed

+49
-16
lines changed

crates/ide_completion/src/render/literal.rs

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -49,12 +49,12 @@ fn render(
4949
name: hir::Name,
5050
path: Option<hir::ModPath>,
5151
) -> Option<CompletionItem> {
52-
if let Some(PathCompletionCtx { has_call_parens: true, .. }) = completion.path_context {
53-
return None;
54-
}
5552
let db = completion.db;
56-
let fields = thing.fields(completion)?;
53+
let kind = thing.kind(db);
54+
let has_call_parens =
55+
matches!(completion.path_context, Some(PathCompletionCtx { has_call_parens: true, .. }));
5756

57+
let fields = thing.fields(completion)?;
5858
let (qualified_name, short_qualified_name, qualified) = match path {
5959
Some(path) => {
6060
let short = hir::ModPath::from_segments(
@@ -68,13 +68,14 @@ fn render(
6868
let qualified_name = qualified_name.to_string();
6969
let snippet_cap = ctx.snippet_cap();
7070

71-
let kind = thing.kind(db);
7271
let mut rendered = match kind {
73-
StructKind::Tuple => render_tuple_lit(db, snippet_cap, &fields, &qualified_name),
74-
StructKind::Record => render_record_lit(db, snippet_cap, &fields, &qualified_name),
75-
StructKind::Unit => {
76-
RenderedLiteral { literal: qualified_name.clone(), detail: qualified_name.clone() }
72+
StructKind::Tuple if !has_call_parens => {
73+
render_tuple_lit(db, snippet_cap, &fields, &qualified_name)
74+
}
75+
StructKind::Record if !has_call_parens => {
76+
render_record_lit(db, snippet_cap, &fields, &qualified_name)
7777
}
78+
_ => RenderedLiteral { literal: qualified_name.clone(), detail: qualified_name.clone() },
7879
};
7980

8081
if snippet_cap.is_some() {

crates/ide_completion/src/render/pattern.rs

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ use itertools::Itertools;
66
use syntax::SmolStr;
77

88
use crate::{
9-
context::{ParamKind, PatternContext},
9+
context::{ParamKind, PathCompletionCtx, PatternContext},
1010
render::{variant::visible_fields, RenderContext},
1111
CompletionItem, CompletionItemKind,
1212
};
@@ -77,12 +77,19 @@ fn render_pat(
7777
fields: &[hir::Field],
7878
fields_omitted: bool,
7979
) -> Option<String> {
80+
let has_call_parens = matches!(
81+
ctx.completion.path_context,
82+
Some(PathCompletionCtx { has_call_parens: true, .. })
83+
);
8084
let mut pat = match kind {
81-
StructKind::Tuple => render_tuple_as_pat(ctx.snippet_cap(), fields, name, fields_omitted),
82-
StructKind::Record => {
85+
StructKind::Tuple if !has_call_parens => {
86+
render_tuple_as_pat(ctx.snippet_cap(), fields, name, fields_omitted)
87+
}
88+
StructKind::Record if !has_call_parens => {
8389
render_record_as_pat(ctx.db(), ctx.snippet_cap(), fields, name, fields_omitted)
8490
}
85-
_ => return None,
91+
StructKind::Unit => return None,
92+
_ => name.to_owned(),
8693
};
8794

8895
if matches!(
@@ -91,7 +98,7 @@ fn render_pat(
9198
param_ctx: Some((.., ParamKind::Function(_))),
9299
has_type_ascription: false,
93100
..
94-
})
101+
}) if !has_call_parens
95102
) {
96103
pat.push(':');
97104
pat.push(' ');

crates/ide_completion/src/tests/expression.rs

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -552,3 +552,24 @@ fn func() {
552552
"#]],
553553
);
554554
}
555+
556+
#[test]
557+
fn with_parens() {
558+
check_empty(
559+
r#"
560+
enum Enum {
561+
Variant()
562+
}
563+
impl Enum {
564+
fn variant() -> Self { Enum::Variant() }
565+
}
566+
fn func() {
567+
Enum::$0()
568+
}
569+
"#,
570+
expect![[r#"
571+
ev Variant(…) Variant
572+
fn variant fn() -> Enum
573+
"#]],
574+
);
575+
}

crates/ide_completion/src/tests/pattern.rs

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -429,7 +429,9 @@ fn foo() {
429429
}
430430
}
431431
"#,
432-
expect![[r#""#]],
432+
expect![[r#"
433+
ev TupleVariant(…) TupleVariant
434+
"#]],
433435
);
434436
check_empty(
435437
r#"
@@ -442,7 +444,9 @@ fn foo() {
442444
}
443445
}
444446
"#,
445-
expect![[r#""#]],
447+
expect![[r#"
448+
ev RecordVariant {…} RecordVariant
449+
"#]],
446450
);
447451
}
448452

0 commit comments

Comments
 (0)