Skip to content
This repository was archived by the owner on May 28, 2025. It is now read-only.

Commit 8b07898

Browse files
committed
Reimplement auto-ref completions for fields
1 parent 46d2271 commit 8b07898

File tree

4 files changed

+35
-12
lines changed

4 files changed

+35
-12
lines changed

crates/ide-completion/src/completions.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -379,6 +379,7 @@ impl Completions {
379379
pub(crate) fn add_field(
380380
&mut self,
381381
ctx: &CompletionContext,
382+
dot_access: &DotAccess,
382383
receiver: Option<hir::Name>,
383384
field: hir::Field,
384385
ty: &hir::Type,
@@ -390,6 +391,7 @@ impl Completions {
390391
};
391392
let item = render_field(
392393
RenderContext::new(ctx).private_editable(is_private_editable),
394+
dot_access,
393395
receiver,
394396
field,
395397
ty,

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

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ pub(crate) fn complete_dot(acc: &mut Completions, ctx: &CompletionContext, dot_a
2929
acc,
3030
ctx,
3131
&receiver_ty,
32-
|acc, field, ty| acc.add_field(ctx, None, field, &ty),
32+
|acc, field, ty| acc.add_field(ctx, dot_access, None, field, &ty),
3333
|acc, field, ty| acc.add_tuple_field(ctx, None, field, &ty),
3434
);
3535
}
@@ -64,7 +64,19 @@ pub(crate) fn complete_undotted_self(
6464
acc,
6565
ctx,
6666
&ty,
67-
|acc, field, ty| acc.add_field(ctx, Some(hir::known::SELF_PARAM), field, &ty),
67+
|acc, field, ty| {
68+
acc.add_field(
69+
ctx,
70+
&DotAccess {
71+
receiver: None,
72+
receiver_ty: None,
73+
kind: DotAccessKind::Field { receiver_is_ambiguous_float_literal: false },
74+
},
75+
Some(hir::known::SELF_PARAM),
76+
field,
77+
&ty,
78+
)
79+
},
6880
|acc, field, ty| acc.add_tuple_field(ctx, Some(hir::known::SELF_PARAM), field, &ty),
6981
);
7082
complete_methods(ctx, &ty, |func| {

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

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ use ide_db::SymbolKind;
33
use syntax::ast::{self, Expr};
44

55
use crate::{
6-
context::{ExprCtx, PathCompletionCtx, PatternContext, Qualified},
6+
context::{DotAccess, DotAccessKind, ExprCtx, PathCompletionCtx, PatternContext, Qualified},
77
CompletionContext, CompletionItem, CompletionItemKind, CompletionRelevance,
88
CompletionRelevancePostfixMatch, Completions,
99
};
@@ -107,7 +107,17 @@ fn complete_fields(
107107
missing_fields: Vec<(hir::Field, hir::Type)>,
108108
) {
109109
for (field, ty) in missing_fields {
110-
acc.add_field(ctx, None, field, &ty);
110+
acc.add_field(
111+
ctx,
112+
&DotAccess {
113+
receiver: None,
114+
receiver_ty: None,
115+
kind: DotAccessKind::Field { receiver_is_ambiguous_float_literal: false },
116+
},
117+
None,
118+
field,
119+
&ty,
120+
);
111121
}
112122
}
113123

crates/ide-completion/src/render.rs

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ use ide_db::{
1717
use syntax::{AstNode, SmolStr, SyntaxKind, TextRange};
1818

1919
use crate::{
20-
context::{PathCompletionCtx, PathKind, PatternContext},
20+
context::{DotAccess, PathCompletionCtx, PathKind, PatternContext},
2121
item::{Builder, CompletionRelevanceTypeMatch},
2222
render::{
2323
function::render_fn,
@@ -110,6 +110,7 @@ impl<'a> RenderContext<'a> {
110110

111111
pub(crate) fn render_field(
112112
ctx: RenderContext<'_>,
113+
dot_access: &DotAccess,
113114
receiver: Option<hir::Name>,
114115
field: hir::Field,
115116
ty: &hir::Type,
@@ -134,10 +135,10 @@ pub(crate) fn render_field(
134135
if is_keyword && !matches!(name.as_str(), "self" | "crate" | "super" | "Self") {
135136
item.insert_text(format!("r#{}", name));
136137
}
137-
if let Some(_ref_match) = compute_ref_match(ctx.completion, ty) {
138-
// FIXME
139-
// For now we don't properly calculate the edits for ref match
140-
// completions on struct fields, so we've disabled them. See #8058.
138+
if let Some(receiver) = &dot_access.receiver {
139+
if let Some(ref_match) = compute_ref_match(ctx.completion, ty) {
140+
item.ref_match(ref_match, receiver.syntax().text_range().start());
141+
}
141142
}
142143
item.build()
143144
}
@@ -1535,9 +1536,6 @@ impl Foo { fn baz(&self) -> u32 { 0 } }
15351536
fn foo(f: Foo) { let _: &u32 = f.b$0 }
15361537
"#,
15371538
&[CompletionItemKind::Method, CompletionItemKind::SymbolKind(SymbolKind::Field)],
1538-
// FIXME
1539-
// Ideally we'd also suggest &f.bar as exact
1540-
// type matches. See #8058.
15411539
expect![[r#"
15421540
[
15431541
CompletionItem {
@@ -1559,6 +1557,7 @@ fn foo(f: Foo) { let _: &u32 = f.b$0 }
15591557
Field,
15601558
),
15611559
detail: "u32",
1560+
ref_match: "&@96",
15621561
},
15631562
]
15641563
"#]],

0 commit comments

Comments
 (0)