Skip to content

Commit 5c0c8ce

Browse files
committed
refactor complete_fn_fields function and correct branch checks
1 parent 8296b16 commit 5c0c8ce

File tree

2 files changed

+22
-45
lines changed

2 files changed

+22
-45
lines changed

crates/ide-completion/src/completions/dot.rs

+19-44
Original file line numberDiff line numberDiff line change
@@ -26,23 +26,19 @@ pub(crate) fn complete_dot(
2626
item.add_to(acc, ctx.db);
2727
}
2828

29+
let is_field_access = matches!(dot_access.kind, DotAccessKind::Field { .. });
30+
31+
complete_fields(
32+
acc,
33+
ctx,
34+
receiver_ty,
35+
|acc, field, ty| acc.add_field(ctx, dot_access, None, field, &ty),
36+
|acc, field, ty| acc.add_tuple_field(ctx, None, field, &ty),
37+
is_field_access,
38+
);
39+
2940
if let DotAccessKind::Method { .. } = dot_access.kind {
3041
cov_mark::hit!(test_no_struct_field_completion_for_method_call);
31-
complete_fn_fields(
32-
acc,
33-
ctx,
34-
receiver_ty,
35-
|acc, field, ty| acc.add_field(ctx, dot_access, None, field, &ty),
36-
|acc, field, ty| acc.add_tuple_field(ctx, None, field, &ty),
37-
);
38-
} else {
39-
complete_fields(
40-
acc,
41-
ctx,
42-
receiver_ty,
43-
|acc, field, ty| acc.add_field(ctx, dot_access, None, field, &ty),
44-
|acc, field, ty| acc.add_tuple_field(ctx, None, field, &ty),
45-
);
4642
}
4743
complete_methods(ctx, receiver_ty, |func| acc.add_method(ctx, dot_access, func, None, None));
4844
}
@@ -89,6 +85,7 @@ pub(crate) fn complete_undotted_self(
8985
)
9086
},
9187
|acc, field, ty| acc.add_tuple_field(ctx, Some(hir::known::SELF_PARAM), field, &ty),
88+
true,
9289
);
9390
complete_methods(ctx, &ty, |func| {
9491
acc.add_method(
@@ -111,18 +108,23 @@ fn complete_fields(
111108
receiver: &hir::Type,
112109
mut named_field: impl FnMut(&mut Completions, hir::Field, hir::Type),
113110
mut tuple_index: impl FnMut(&mut Completions, usize, hir::Type),
111+
is_field_access: bool,
114112
) {
115113
let mut seen_names = FxHashSet::default();
116114
for receiver in receiver.autoderef(ctx.db) {
117115
for (field, ty) in receiver.fields(ctx.db) {
118-
if seen_names.insert(field.name(ctx.db)) {
116+
if seen_names.insert(field.name(ctx.db))
117+
&& (is_field_access || ty.is_fn() || ty.is_closure())
118+
{
119119
named_field(acc, field, ty);
120120
}
121121
}
122122
for (i, ty) in receiver.tuple_fields(ctx.db).into_iter().enumerate() {
123123
// Tuples are always the last type in a deref chain, so just check if the name is
124124
// already seen without inserting into the hashset.
125-
if !seen_names.contains(&hir::Name::new_tuple_field(i)) {
125+
if !seen_names.contains(&hir::Name::new_tuple_field(i))
126+
&& (is_field_access || ty.is_fn() || ty.is_closure())
127+
{
126128
// Tuple fields are always public (tuple struct fields are handled above).
127129
tuple_index(acc, i, ty);
128130
}
@@ -151,33 +153,6 @@ fn complete_methods(
151153
);
152154
}
153155

154-
fn complete_fn_fields(
155-
acc: &mut Completions,
156-
ctx: &CompletionContext<'_>,
157-
receiver: &hir::Type,
158-
mut named_field: impl FnMut(&mut Completions, hir::Field, hir::Type),
159-
mut tuple_index: impl FnMut(&mut Completions, usize, hir::Type),
160-
) {
161-
let mut seen_names = FxHashSet::default();
162-
for receiver in receiver.autoderef(ctx.db) {
163-
for (field, ty) in receiver.fields(ctx.db) {
164-
if seen_names.insert(field.name(ctx.db)) && (ty.is_fn() || ty.is_closure()) {
165-
named_field(acc, field, ty);
166-
}
167-
}
168-
for (i, ty) in receiver.tuple_fields(ctx.db).into_iter().enumerate() {
169-
// Tuples are always the last type in a deref chain, so just check if the name is
170-
// already seen without inserting into the hashset.
171-
if !seen_names.contains(&hir::Name::new_tuple_field(i))
172-
&& (ty.is_fn() || ty.is_closure())
173-
{
174-
// Tuple fields are always public (tuple struct fields are handled above).
175-
tuple_index(acc, i, ty);
176-
}
177-
}
178-
}
179-
}
180-
181156
#[cfg(test)]
182157
mod tests {
183158
use expect_test::{expect, Expect};

crates/ide-completion/src/render.rs

+3-1
Original file line numberDiff line numberDiff line change
@@ -148,7 +148,9 @@ pub(crate) fn render_field(
148148
.set_documentation(field.docs(db))
149149
.set_deprecated(is_deprecated)
150150
.lookup_by(name);
151-
if ty.is_fn() || ty.is_closure() {
151+
152+
let is_field_access = matches!(dot_access.kind, DotAccessKind::Field { .. });
153+
if !is_field_access || ty.is_fn() || ty.is_closure() {
152154
let mut builder = TextEdit::builder();
153155
// Using TextEdit, insert '(' before the struct name and ')' before the
154156
// dot access, then comes the field name and optionally insert function

0 commit comments

Comments
 (0)