add tests to verify relevance of notable traits and some refactors

This commit is contained in:
dfireBird 2024-01-10 16:30:22 +05:30
parent df5c647982
commit 257870e09f
No known key found for this signature in database
GPG key ID: 26D522CA5FC2B93D
2 changed files with 84 additions and 9 deletions

View file

@ -2443,4 +2443,81 @@ impl S {
"#,
)
}
#[test]
fn notable_traits_method_relevance() {
check_kinds(
r#"
#[doc(notable_trait)]
trait Write {
fn write(&self);
fn flush(&self);
}
struct Writer;
impl Write for Writer {
fn write(&self) {}
fn flush(&self) {}
}
fn main() {
Writer.$0
}
"#,
&[
CompletionItemKind::Method,
CompletionItemKind::SymbolKind(SymbolKind::Field),
CompletionItemKind::SymbolKind(SymbolKind::Function),
],
expect![[r#"
[
CompletionItem {
label: "flush()",
source_range: 193..193,
delete: 193..193,
insert: "flush()$0",
kind: Method,
lookup: "flush",
detail: "fn(&self)",
relevance: CompletionRelevance {
exact_name_match: false,
type_match: None,
is_local: false,
is_item_from_trait: false,
is_item_from_notable_trait: true,
is_name_already_imported: false,
requires_import: false,
is_op_method: false,
is_private_editable: false,
postfix_match: None,
is_definite: false,
},
},
CompletionItem {
label: "write()",
source_range: 193..193,
delete: 193..193,
insert: "write()$0",
kind: Method,
lookup: "write",
detail: "fn(&self)",
relevance: CompletionRelevance {
exact_name_match: false,
type_match: None,
is_local: false,
is_item_from_trait: false,
is_item_from_notable_trait: true,
is_name_already_imported: false,
requires_import: false,
is_op_method: false,
is_private_editable: false,
postfix_match: None,
is_definite: false,
},
},
]
"#]],
);
}
}

View file

@ -74,15 +74,13 @@ fn render(
);
let ret_type = func.ret_type(db);
let is_op_method = func
.as_assoc_item(ctx.db())
.and_then(|trait_| trait_.containing_trait_or_trait_impl(ctx.db()))
.map_or(false, |trait_| completion.is_ops_trait(trait_));
let assoc_item = func.as_assoc_item(db);
let is_item_from_notable_trait = func
.as_assoc_item(ctx.db())
.and_then(|trait_| trait_.containing_trait(ctx.db()))
.map_or(false, |trait_| completion.is_doc_notable_trait(trait_));
let trait_ = assoc_item.and_then(|trait_| trait_.containing_trait_or_trait_impl(db));
let is_op_method = trait_.map_or(false, |trait_| completion.is_ops_trait(trait_));
let is_item_from_notable_trait =
trait_.map_or(false, |trait_| completion.is_doc_notable_trait(trait_));
let (has_dot_receiver, has_call_parens, cap) = match func_kind {
FuncKind::Function(&PathCompletionCtx {
@ -147,7 +145,7 @@ fn render(
item.add_import(import_to_add);
}
None => {
if let Some(actm) = func.as_assoc_item(db) {
if let Some(actm) = assoc_item {
if let Some(trt) = actm.containing_trait_or_trait_impl(db) {
item.trait_name(trt.name(db).to_smol_str());
}