Skip to content

Commit 257870e

Browse files
committed
add tests to verify relevance of notable traits and some refactors
1 parent df5c647 commit 257870e

File tree

2 files changed

+84
-9
lines changed

2 files changed

+84
-9
lines changed

crates/ide-completion/src/render.rs

+77
Original file line numberDiff line numberDiff line change
@@ -2443,4 +2443,81 @@ impl S {
24432443
"#,
24442444
)
24452445
}
2446+
2447+
#[test]
2448+
fn notable_traits_method_relevance() {
2449+
check_kinds(
2450+
r#"
2451+
#[doc(notable_trait)]
2452+
trait Write {
2453+
fn write(&self);
2454+
fn flush(&self);
2455+
}
2456+
2457+
struct Writer;
2458+
2459+
impl Write for Writer {
2460+
fn write(&self) {}
2461+
fn flush(&self) {}
2462+
}
2463+
2464+
fn main() {
2465+
Writer.$0
2466+
}
2467+
"#,
2468+
&[
2469+
CompletionItemKind::Method,
2470+
CompletionItemKind::SymbolKind(SymbolKind::Field),
2471+
CompletionItemKind::SymbolKind(SymbolKind::Function),
2472+
],
2473+
expect![[r#"
2474+
[
2475+
CompletionItem {
2476+
label: "flush()",
2477+
source_range: 193..193,
2478+
delete: 193..193,
2479+
insert: "flush()$0",
2480+
kind: Method,
2481+
lookup: "flush",
2482+
detail: "fn(&self)",
2483+
relevance: CompletionRelevance {
2484+
exact_name_match: false,
2485+
type_match: None,
2486+
is_local: false,
2487+
is_item_from_trait: false,
2488+
is_item_from_notable_trait: true,
2489+
is_name_already_imported: false,
2490+
requires_import: false,
2491+
is_op_method: false,
2492+
is_private_editable: false,
2493+
postfix_match: None,
2494+
is_definite: false,
2495+
},
2496+
},
2497+
CompletionItem {
2498+
label: "write()",
2499+
source_range: 193..193,
2500+
delete: 193..193,
2501+
insert: "write()$0",
2502+
kind: Method,
2503+
lookup: "write",
2504+
detail: "fn(&self)",
2505+
relevance: CompletionRelevance {
2506+
exact_name_match: false,
2507+
type_match: None,
2508+
is_local: false,
2509+
is_item_from_trait: false,
2510+
is_item_from_notable_trait: true,
2511+
is_name_already_imported: false,
2512+
requires_import: false,
2513+
is_op_method: false,
2514+
is_private_editable: false,
2515+
postfix_match: None,
2516+
is_definite: false,
2517+
},
2518+
},
2519+
]
2520+
"#]],
2521+
);
2522+
}
24462523
}

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

+7-9
Original file line numberDiff line numberDiff line change
@@ -74,15 +74,13 @@ fn render(
7474
);
7575

7676
let ret_type = func.ret_type(db);
77-
let is_op_method = func
78-
.as_assoc_item(ctx.db())
79-
.and_then(|trait_| trait_.containing_trait_or_trait_impl(ctx.db()))
80-
.map_or(false, |trait_| completion.is_ops_trait(trait_));
77+
let assoc_item = func.as_assoc_item(db);
8178

82-
let is_item_from_notable_trait = func
83-
.as_assoc_item(ctx.db())
84-
.and_then(|trait_| trait_.containing_trait(ctx.db()))
85-
.map_or(false, |trait_| completion.is_doc_notable_trait(trait_));
79+
let trait_ = assoc_item.and_then(|trait_| trait_.containing_trait_or_trait_impl(db));
80+
let is_op_method = trait_.map_or(false, |trait_| completion.is_ops_trait(trait_));
81+
82+
let is_item_from_notable_trait =
83+
trait_.map_or(false, |trait_| completion.is_doc_notable_trait(trait_));
8684

8785
let (has_dot_receiver, has_call_parens, cap) = match func_kind {
8886
FuncKind::Function(&PathCompletionCtx {
@@ -147,7 +145,7 @@ fn render(
147145
item.add_import(import_to_add);
148146
}
149147
None => {
150-
if let Some(actm) = func.as_assoc_item(db) {
148+
if let Some(actm) = assoc_item {
151149
if let Some(trt) = actm.containing_trait_or_trait_impl(db) {
152150
item.trait_name(trt.name(db).to_smol_str());
153151
}

0 commit comments

Comments
 (0)