Skip to content

Commit f5ccde6

Browse files
committed
Simplify CompletionRelevance
1 parent fb38e4b commit f5ccde6

File tree

4 files changed

+85
-90
lines changed

4 files changed

+85
-90
lines changed

src/tools/rust-analyzer/crates/ide-completion/src/completions/item_list/trait_impl.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -221,7 +221,7 @@ fn add_function_impl_(
221221
let mut item = CompletionItem::new(completion_kind, replacement_range, label, ctx.edition);
222222
item.lookup_by(format!("{}fn {}", async_, fn_name.display(ctx.db, ctx.edition)))
223223
.set_documentation(func.docs(ctx.db))
224-
.set_relevance(CompletionRelevance { is_item_from_trait: true, ..Default::default() });
224+
.set_relevance(CompletionRelevance { exact_name_match: true, ..Default::default() });
225225

226226
if let Some(source) = ctx.sema.source(func) {
227227
if let Some(transformed_fn) =
@@ -366,7 +366,7 @@ fn add_type_alias_impl(
366366
CompletionItem::new(SymbolKind::TypeAlias, replacement_range, label, ctx.edition);
367367
item.lookup_by(format!("type {alias_name}"))
368368
.set_documentation(type_alias.docs(ctx.db))
369-
.set_relevance(CompletionRelevance { is_item_from_trait: true, ..Default::default() });
369+
.set_relevance(CompletionRelevance { exact_name_match: true, ..Default::default() });
370370

371371
if let Some(source) = ctx.sema.source(type_alias) {
372372
let assoc_item = ast::AssocItem::TypeAlias(source.value);
@@ -440,7 +440,7 @@ fn add_const_impl(
440440
item.lookup_by(format_smolstr!("const {const_name}"))
441441
.set_documentation(const_.docs(ctx.db))
442442
.set_relevance(CompletionRelevance {
443-
is_item_from_trait: true,
443+
exact_name_match: true,
444444
..Default::default()
445445
});
446446
match ctx.config.snippet_cap {

src/tools/rust-analyzer/crates/ide-completion/src/item.rs

Lines changed: 46 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,10 @@ use crate::{
1919
};
2020

2121
/// `CompletionItem` describes a single completion entity which expands to 1 or more entries in the
22-
/// editor pop-up. It is basically a POD with various properties. To construct a
23-
/// [`CompletionItem`], use [`Builder::new`] method and the [`Builder`] struct.
22+
/// editor pop-up.
23+
///
24+
/// It is basically a POD with various properties. To construct a [`CompletionItem`],
25+
/// use [`Builder::new`] method and the [`Builder`] struct.
2426
#[derive(Clone)]
2527
#[non_exhaustive]
2628
pub struct CompletionItem {
@@ -129,7 +131,8 @@ impl fmt::Debug for CompletionItem {
129131

130132
#[derive(Debug, Clone, Copy, Eq, PartialEq, Default)]
131133
pub struct CompletionRelevance {
132-
/// This is set in cases like these:
134+
/// This is set when the identifier being completed matches up with the name that is expected,
135+
/// like in a function argument.
133136
///
134137
/// ```
135138
/// fn f(spam: String) {}
@@ -139,9 +142,9 @@ pub struct CompletionRelevance {
139142
/// }
140143
/// ```
141144
pub exact_name_match: bool,
142-
/// See CompletionRelevanceTypeMatch doc comments for cases where this is set.
145+
/// See [`CompletionRelevanceTypeMatch`].
143146
pub type_match: Option<CompletionRelevanceTypeMatch>,
144-
/// This is set in cases like these:
147+
/// Set for local variables.
145148
///
146149
/// ```
147150
/// fn foo(a: u32) {
@@ -150,25 +153,26 @@ pub struct CompletionRelevance {
150153
/// }
151154
/// ```
152155
pub is_local: bool,
153-
/// This is set when trait items are completed in an impl of that trait.
154-
pub is_item_from_trait: bool,
155-
/// This is set for when trait items are from traits with `#[doc(notable_trait)]`
156-
pub is_item_from_notable_trait: bool,
157-
/// This is set when an import is suggested whose name is already imported.
156+
/// Populated when the completion item comes from a trait (impl).
157+
pub trait_: Option<CompletionRelevanceTraitInfo>,
158+
/// This is set when an import is suggested in a use item whose name is already imported.
158159
pub is_name_already_imported: bool,
159160
/// This is set for completions that will insert a `use` item.
160161
pub requires_import: bool,
161-
/// Set for method completions of the `core::ops` and `core::cmp` family.
162-
pub is_op_method: bool,
163162
/// Set for item completions that are private but in the workspace.
164163
pub is_private_editable: bool,
165164
/// Set for postfix snippet item completions
166165
pub postfix_match: Option<CompletionRelevancePostfixMatch>,
167-
/// This is set for type inference results
168-
pub is_definite: bool,
169166
/// This is set for items that are function (associated or method)
170167
pub function: Option<CompletionRelevanceFn>,
171168
}
169+
#[derive(Debug, Clone, Copy, Eq, PartialEq)]
170+
pub struct CompletionRelevanceTraitInfo {
171+
/// The trait this item is from is a `#[doc(notable_trait)]`
172+
pub notable_trait: bool,
173+
/// Set for method completions of the `core::ops` and `core::cmp` family.
174+
pub is_op_method: bool,
175+
}
172176

173177
#[derive(Debug, Clone, Copy, Eq, PartialEq)]
174178
pub enum CompletionRelevanceTypeMatch {
@@ -182,7 +186,7 @@ pub enum CompletionRelevanceTypeMatch {
182186
/// }
183187
/// ```
184188
CouldUnify,
185-
/// This is set in cases like these:
189+
/// This is set in cases where the type matches the expected type, like:
186190
///
187191
/// ```
188192
/// fn f(spam: String) {}
@@ -243,23 +247,29 @@ impl CompletionRelevance {
243247
exact_name_match,
244248
type_match,
245249
is_local,
246-
is_item_from_trait,
247250
is_name_already_imported,
248251
requires_import,
249-
is_op_method,
250252
is_private_editable,
251253
postfix_match,
252-
is_definite,
253-
is_item_from_notable_trait,
254+
trait_,
254255
function,
255256
} = self;
256257

257258
// lower rank private things
258259
if !is_private_editable {
259260
score += 1;
260261
}
261-
// lower rank trait op methods
262-
if !is_op_method {
262+
263+
if let Some(trait_) = trait_ {
264+
if trait_.notable_trait {
265+
score += 1;
266+
}
267+
// lower rank trait op methods
268+
if !trait_.is_op_method {
269+
score += 10;
270+
}
271+
} else {
272+
// lower rank trait op methods
263273
score += 10;
264274
}
265275
// lower rank for conflicting import names
@@ -287,16 +297,6 @@ impl CompletionRelevance {
287297
if is_local {
288298
score += 1;
289299
}
290-
if is_item_from_trait {
291-
score += 1;
292-
}
293-
if is_item_from_notable_trait {
294-
score += 1;
295-
}
296-
if is_definite {
297-
score += 10;
298-
}
299-
300300
score += function
301301
.map(|asf| {
302302
let mut fn_score = match asf.return_type {
@@ -701,8 +701,21 @@ mod tests {
701701
// that any items in the same vec have the same score.
702702
let expected_relevance_order = vec![
703703
vec![],
704-
vec![Cr { is_op_method: true, is_private_editable: true, ..default }],
705-
vec![Cr { is_op_method: true, ..default }],
704+
vec![Cr {
705+
trait_: Some(crate::item::CompletionRelevanceTraitInfo {
706+
notable_trait: false,
707+
is_op_method: true,
708+
}),
709+
is_private_editable: true,
710+
..default
711+
}],
712+
vec![Cr {
713+
trait_: Some(crate::item::CompletionRelevanceTraitInfo {
714+
notable_trait: false,
715+
is_op_method: true,
716+
}),
717+
..default
718+
}],
706719
vec![Cr { postfix_match: Some(CompletionRelevancePostfixMatch::NonExact), ..default }],
707720
vec![Cr { is_private_editable: true, ..default }],
708721
vec![default],

src/tools/rust-analyzer/crates/ide-completion/src/render.rs

Lines changed: 27 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -249,7 +249,11 @@ pub(crate) fn render_type_inference(
249249
ty_string,
250250
ctx.edition,
251251
);
252-
builder.set_relevance(CompletionRelevance { is_definite: true, ..Default::default() });
252+
builder.set_relevance(CompletionRelevance {
253+
type_match: Some(CompletionRelevanceTypeMatch::Exact),
254+
exact_name_match: true,
255+
..Default::default()
256+
});
253257
builder.build(ctx.db)
254258
}
255259

@@ -756,7 +760,7 @@ mod tests {
756760
relevance.postfix_match == Some(CompletionRelevancePostfixMatch::Exact),
757761
"snippet",
758762
),
759-
(relevance.is_op_method, "op_method"),
763+
(relevance.trait_.map_or(false, |it| it.is_op_method), "op_method"),
760764
(relevance.requires_import, "requires_import"),
761765
]
762766
.into_iter()
@@ -1272,14 +1276,11 @@ fn main() { let _: m::Spam = S$0 }
12721276
Exact,
12731277
),
12741278
is_local: false,
1275-
is_item_from_trait: false,
1276-
is_item_from_notable_trait: false,
1279+
trait_: None,
12771280
is_name_already_imported: false,
12781281
requires_import: false,
1279-
is_op_method: false,
12801282
is_private_editable: false,
12811283
postfix_match: None,
1282-
is_definite: false,
12831284
function: None,
12841285
},
12851286
trigger_call_info: true,
@@ -1300,14 +1301,11 @@ fn main() { let _: m::Spam = S$0 }
13001301
Exact,
13011302
),
13021303
is_local: false,
1303-
is_item_from_trait: false,
1304-
is_item_from_notable_trait: false,
1304+
trait_: None,
13051305
is_name_already_imported: false,
13061306
requires_import: false,
1307-
is_op_method: false,
13081307
is_private_editable: false,
13091308
postfix_match: None,
1310-
is_definite: false,
13111309
function: None,
13121310
},
13131311
trigger_call_info: true,
@@ -1380,14 +1378,11 @@ fn foo() { A { the$0 } }
13801378
CouldUnify,
13811379
),
13821380
is_local: false,
1383-
is_item_from_trait: false,
1384-
is_item_from_notable_trait: false,
1381+
trait_: None,
13851382
is_name_already_imported: false,
13861383
requires_import: false,
1387-
is_op_method: false,
13881384
is_private_editable: false,
13891385
postfix_match: None,
1390-
is_definite: false,
13911386
function: None,
13921387
},
13931388
},
@@ -1431,14 +1426,11 @@ impl S {
14311426
exact_name_match: false,
14321427
type_match: None,
14331428
is_local: false,
1434-
is_item_from_trait: false,
1435-
is_item_from_notable_trait: false,
1429+
trait_: None,
14361430
is_name_already_imported: false,
14371431
requires_import: false,
1438-
is_op_method: false,
14391432
is_private_editable: false,
14401433
postfix_match: None,
1441-
is_definite: false,
14421434
function: Some(
14431435
CompletionRelevanceFn {
14441436
has_params: true,
@@ -1558,14 +1550,11 @@ fn foo(s: S) { s.$0 }
15581550
exact_name_match: false,
15591551
type_match: None,
15601552
is_local: false,
1561-
is_item_from_trait: false,
1562-
is_item_from_notable_trait: false,
1553+
trait_: None,
15631554
is_name_already_imported: false,
15641555
requires_import: false,
1565-
is_op_method: false,
15661556
is_private_editable: false,
15671557
postfix_match: None,
1568-
is_definite: false,
15691558
function: Some(
15701559
CompletionRelevanceFn {
15711560
has_params: true,
@@ -1774,14 +1763,11 @@ fn f() -> i32 {
17741763
Exact,
17751764
),
17761765
is_local: false,
1777-
is_item_from_trait: false,
1778-
is_item_from_notable_trait: false,
1766+
trait_: None,
17791767
is_name_already_imported: false,
17801768
requires_import: false,
1781-
is_op_method: false,
17821769
is_private_editable: false,
17831770
postfix_match: None,
1784-
is_definite: false,
17851771
function: None,
17861772
},
17871773
},
@@ -2492,14 +2478,11 @@ fn foo(f: Foo) { let _: &u32 = f.b$0 }
24922478
exact_name_match: false,
24932479
type_match: None,
24942480
is_local: false,
2495-
is_item_from_trait: false,
2496-
is_item_from_notable_trait: false,
2481+
trait_: None,
24972482
is_name_already_imported: false,
24982483
requires_import: false,
2499-
is_op_method: false,
25002484
is_private_editable: false,
25012485
postfix_match: None,
2502-
is_definite: false,
25032486
function: Some(
25042487
CompletionRelevanceFn {
25052488
has_params: true,
@@ -2574,14 +2557,11 @@ fn foo() {
25742557
Exact,
25752558
),
25762559
is_local: false,
2577-
is_item_from_trait: false,
2578-
is_item_from_notable_trait: false,
2560+
trait_: None,
25792561
is_name_already_imported: false,
25802562
requires_import: false,
2581-
is_op_method: false,
25822563
is_private_editable: false,
25832564
postfix_match: None,
2584-
is_definite: false,
25852565
function: None,
25862566
},
25872567
},
@@ -2624,14 +2604,11 @@ fn main() {
26242604
exact_name_match: false,
26252605
type_match: None,
26262606
is_local: false,
2627-
is_item_from_trait: false,
2628-
is_item_from_notable_trait: false,
2607+
trait_: None,
26292608
is_name_already_imported: false,
26302609
requires_import: false,
2631-
is_op_method: false,
26322610
is_private_editable: false,
26332611
postfix_match: None,
2634-
is_definite: false,
26352612
function: Some(
26362613
CompletionRelevanceFn {
26372614
has_params: false,
@@ -2996,14 +2973,16 @@ fn main() {
29962973
exact_name_match: false,
29972974
type_match: None,
29982975
is_local: false,
2999-
is_item_from_trait: false,
3000-
is_item_from_notable_trait: true,
2976+
trait_: Some(
2977+
CompletionRelevanceTraitInfo {
2978+
notable_trait: true,
2979+
is_op_method: false,
2980+
},
2981+
),
30012982
is_name_already_imported: false,
30022983
requires_import: false,
3003-
is_op_method: false,
30042984
is_private_editable: false,
30052985
postfix_match: None,
3006-
is_definite: false,
30072986
function: None,
30082987
},
30092988
},
@@ -3021,14 +3000,16 @@ fn main() {
30213000
exact_name_match: false,
30223001
type_match: None,
30233002
is_local: false,
3024-
is_item_from_trait: false,
3025-
is_item_from_notable_trait: true,
3003+
trait_: Some(
3004+
CompletionRelevanceTraitInfo {
3005+
notable_trait: true,
3006+
is_op_method: false,
3007+
},
3008+
),
30263009
is_name_already_imported: false,
30273010
requires_import: false,
3028-
is_op_method: false,
30293011
is_private_editable: false,
30303012
postfix_match: None,
3031-
is_definite: false,
30323013
function: None,
30333014
},
30343015
},

0 commit comments

Comments
 (0)