Skip to content

Commit b6fae56

Browse files
committed
Auto merge of rust-lang#13028 - yue4u:fix/literal-lookup, r=jonas-schievink
fix: record completion filtering close rust-lang#12975
2 parents 3903243 + 91358bd commit b6fae56

File tree

7 files changed

+46
-22
lines changed

7 files changed

+46
-22
lines changed

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

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -129,7 +129,7 @@ mod tests {
129129
#[test]
130130
fn literal_struct_completion_edit() {
131131
check_edit(
132-
"FooDesc {…}",
132+
"FooDesc{}",
133133
r#"
134134
struct FooDesc { pub bar: bool }
135135
@@ -154,7 +154,7 @@ fn baz() {
154154
#[test]
155155
fn literal_struct_impl_self_completion() {
156156
check_edit(
157-
"Self {…}",
157+
"Self{}",
158158
r#"
159159
struct Foo {
160160
bar: u64,
@@ -180,7 +180,7 @@ impl Foo {
180180
);
181181

182182
check_edit(
183-
"Self()",
183+
"Self()",
184184
r#"
185185
mod submod {
186186
pub struct Foo(pub u64);
@@ -209,7 +209,7 @@ impl submod::Foo {
209209
#[test]
210210
fn literal_struct_completion_from_sub_modules() {
211211
check_edit(
212-
"submod::Struct {…}",
212+
"submod::Struct{}",
213213
r#"
214214
mod submod {
215215
pub struct Struct {
@@ -238,7 +238,7 @@ fn f() -> submod::Struct {
238238
#[test]
239239
fn literal_struct_complexion_module() {
240240
check_edit(
241-
"FooDesc {…}",
241+
"FooDesc{}",
242242
r#"
243243
mod _69latrick {
244244
pub struct FooDesc { pub six: bool, pub neuf: Vec<String>, pub bar: bool }

crates/ide-completion/src/render.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -565,6 +565,7 @@ fn main() { Foo::Fo$0 }
565565
kind: SymbolKind(
566566
Variant,
567567
),
568+
lookup: "Foo{}",
568569
detail: "Foo { x: i32, y: i32 }",
569570
},
570571
]
@@ -591,6 +592,7 @@ fn main() { Foo::Fo$0 }
591592
kind: SymbolKind(
592593
Variant,
593594
),
595+
lookup: "Foo()",
594596
detail: "Foo(i32, i32)",
595597
},
596598
]
@@ -707,7 +709,7 @@ fn main() { let _: m::Spam = S$0 }
707709
kind: SymbolKind(
708710
Variant,
709711
),
710-
lookup: "Spam::Bar()",
712+
lookup: "Spam::Bar()",
711713
detail: "m::Spam::Bar(i32)",
712714
relevance: CompletionRelevance {
713715
exact_name_match: false,

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

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,8 @@ use crate::{
1010
render::{
1111
compute_ref_match, compute_type_match,
1212
variant::{
13-
format_literal_label, render_record_lit, render_tuple_lit, visible_fields,
14-
RenderedLiteral,
13+
format_literal_label, format_literal_lookup, render_record_lit, render_tuple_lit,
14+
visible_fields, RenderedLiteral,
1515
},
1616
RenderContext,
1717
},
@@ -97,23 +97,27 @@ fn render(
9797
if !should_add_parens {
9898
kind = StructKind::Unit;
9999
}
100+
let label = format_literal_label(&qualified_name, kind);
101+
let lookup = if qualified {
102+
format_literal_lookup(&short_qualified_name.to_string(), kind)
103+
} else {
104+
format_literal_lookup(&qualified_name, kind)
105+
};
100106

101107
let mut item = CompletionItem::new(
102108
CompletionItemKind::SymbolKind(thing.symbol_kind()),
103109
ctx.source_range(),
104-
format_literal_label(&qualified_name, kind),
110+
label,
105111
);
106112

113+
item.lookup_by(lookup);
107114
item.detail(rendered.detail);
108115

109116
match snippet_cap {
110117
Some(snippet_cap) => item.insert_snippet(snippet_cap, rendered.literal),
111118
None => item.insert_text(rendered.literal),
112119
};
113120

114-
if qualified {
115-
item.lookup_by(format_literal_label(&short_qualified_name.to_string(), kind));
116-
}
117121
item.set_documentation(thing.docs(db)).set_deprecated(thing.is_deprecated(&ctx));
118122

119123
let ty = thing.ty(db);

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

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ use syntax::SmolStr;
88
use crate::{
99
context::{ParamContext, ParamKind, PathCompletionCtx, PatternContext},
1010
render::{
11-
variant::{format_literal_label, visible_fields},
11+
variant::{format_literal_label, format_literal_lookup, visible_fields},
1212
RenderContext,
1313
},
1414
CompletionItem, CompletionItemKind,
@@ -34,9 +34,10 @@ pub(crate) fn render_struct_pat(
3434
let (name, escaped_name) = (name.unescaped().to_smol_str(), name.to_smol_str());
3535
let kind = strukt.kind(ctx.db());
3636
let label = format_literal_label(name.as_str(), kind);
37+
let lookup = format_literal_lookup(name.as_str(), kind);
3738
let pat = render_pat(&ctx, pattern_ctx, &escaped_name, kind, &visible_fields, fields_omitted)?;
3839

39-
Some(build_completion(ctx, label, pat, strukt))
40+
Some(build_completion(ctx, label, lookup, pat, strukt))
4041
}
4142

4243
pub(crate) fn render_variant_pat(
@@ -60,11 +61,14 @@ pub(crate) fn render_variant_pat(
6061
}
6162
};
6263

63-
let (label, pat) = match path_ctx {
64-
Some(PathCompletionCtx { has_call_parens: true, .. }) => (name, escaped_name.to_string()),
64+
let (label, lookup, pat) = match path_ctx {
65+
Some(PathCompletionCtx { has_call_parens: true, .. }) => {
66+
(name.clone(), name, escaped_name.to_string())
67+
}
6568
_ => {
6669
let kind = variant.kind(ctx.db());
6770
let label = format_literal_label(name.as_str(), kind);
71+
let lookup = format_literal_lookup(name.as_str(), kind);
6872
let pat = render_pat(
6973
&ctx,
7074
pattern_ctx,
@@ -73,23 +77,25 @@ pub(crate) fn render_variant_pat(
7377
&visible_fields,
7478
fields_omitted,
7579
)?;
76-
(label, pat)
80+
(label, lookup, pat)
7781
}
7882
};
7983

80-
Some(build_completion(ctx, label, pat, variant))
84+
Some(build_completion(ctx, label, lookup, pat, variant))
8185
}
8286

8387
fn build_completion(
8488
ctx: RenderContext<'_>,
8589
label: SmolStr,
90+
lookup: SmolStr,
8691
pat: String,
8792
def: impl HasAttrs + Copy,
8893
) -> CompletionItem {
8994
let mut item = CompletionItem::new(CompletionItemKind::Binding, ctx.source_range(), label);
9095
item.set_documentation(ctx.docs(def))
9196
.set_deprecated(ctx.is_deprecated(def))
9297
.detail(&pat)
98+
.lookup_by(lookup)
9399
.set_relevance(ctx.completion_relevance());
94100
match ctx.snippet_cap() {
95101
Some(snippet_cap) => item.insert_snippet(snippet_cap, pat),

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

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ use itertools::Itertools;
66

77
use crate::{
88
render::{
9-
variant::{format_literal_label, visible_fields},
9+
variant::{format_literal_label, format_literal_lookup, visible_fields},
1010
RenderContext,
1111
},
1212
CompletionItem, CompletionItemKind,
@@ -24,13 +24,16 @@ pub(crate) fn render_union_literal(
2424
Some(p) => (p.unescaped().to_string(), p.to_string()),
2525
None => (name.unescaped().to_string(), name.to_string()),
2626
};
27-
27+
let label = format_literal_label(&name.to_smol_str(), StructKind::Record);
28+
let lookup = format_literal_lookup(&name.to_smol_str(), StructKind::Record);
2829
let mut item = CompletionItem::new(
2930
CompletionItemKind::SymbolKind(SymbolKind::Union),
3031
ctx.source_range(),
31-
format_literal_label(&name.to_smol_str(), StructKind::Record),
32+
label,
3233
);
3334

35+
item.lookup_by(lookup);
36+
3437
let fields = un.fields(ctx.db());
3538
let (fields, fields_omitted) = visible_fields(ctx.completion, &fields, un)?;
3639

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

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -94,3 +94,12 @@ pub(crate) fn format_literal_label(name: &str, kind: StructKind) -> SmolStr {
9494
StructKind::Unit => name.into(),
9595
}
9696
}
97+
98+
/// Format a struct, etc. literal option for lookup used in completions filtering.
99+
pub(crate) fn format_literal_lookup(name: &str, kind: StructKind) -> SmolStr {
100+
match kind {
101+
StructKind::Tuple => SmolStr::from_iter([name, "()"]),
102+
StructKind::Record => SmolStr::from_iter([name, "{}"]),
103+
StructKind::Unit => name.into(),
104+
}
105+
}

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -467,7 +467,7 @@ fn foo() {
467467
fn completes_enum_variant_pat() {
468468
cov_mark::check!(enum_variant_pattern_path);
469469
check_edit(
470-
"RecordVariant {…}",
470+
"RecordVariant{}",
471471
r#"
472472
enum Enum {
473473
RecordVariant { field: u32 }

0 commit comments

Comments
 (0)