Skip to content

Commit df5c647

Browse files
committed
add notable_trait predicate to CompletionRelevance
implement the predicate set on the case function from traits
1 parent af40101 commit df5c647

File tree

5 files changed

+28
-0
lines changed

5 files changed

+28
-0
lines changed

crates/hir-def/src/attr.rs

+7
Original file line numberDiff line numberDiff line change
@@ -207,6 +207,13 @@ impl Attrs {
207207
})
208208
}
209209

210+
pub fn has_doc_notable_trait(&self) -> bool {
211+
self.by_key("doc").tt_values().any(|tt| {
212+
tt.delimiter.kind == DelimiterKind::Parenthesis &&
213+
matches!(&*tt.token_trees, [tt::TokenTree::Leaf(tt::Leaf::Ident(ident))] if ident.text == "notable_trait")
214+
})
215+
}
216+
210217
pub fn doc_exprs(&self) -> impl Iterator<Item = DocExpr> + '_ {
211218
self.by_key("doc").tt_values().map(DocExpr::parse)
212219
}

crates/ide-completion/src/context.rs

+5
Original file line numberDiff line numberDiff line change
@@ -529,6 +529,11 @@ impl CompletionContext<'_> {
529529
}
530530
}
531531

532+
/// Whether the given trait has `#[doc(notable_trait)]`
533+
pub(crate) fn is_doc_notable_trait(&self, trait_: hir::Trait) -> bool {
534+
trait_.attrs(self.db).has_doc_notable_trait()
535+
}
536+
532537
/// Returns the traits in scope, with the [`Drop`] trait removed.
533538
pub(crate) fn traits_in_scope(&self) -> hir::VisibleTraits {
534539
let mut traits_in_scope = self.scope.visible_traits();

crates/ide-completion/src/item.rs

+6
Original file line numberDiff line numberDiff line change
@@ -152,6 +152,8 @@ pub struct CompletionRelevance {
152152
pub is_local: bool,
153153
/// This is set when trait items are completed in an impl of that trait.
154154
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,
155157
/// This is set when an import is suggested whose name is already imported.
156158
pub is_name_already_imported: bool,
157159
/// This is set for completions that will insert a `use` item.
@@ -228,6 +230,7 @@ impl CompletionRelevance {
228230
is_private_editable,
229231
postfix_match,
230232
is_definite,
233+
is_item_from_notable_trait,
231234
} = self;
232235

233236
// lower rank private things
@@ -266,6 +269,9 @@ impl CompletionRelevance {
266269
if is_item_from_trait {
267270
score += 1;
268271
}
272+
if is_item_from_notable_trait {
273+
score += 1;
274+
}
269275
if is_definite {
270276
score += 10;
271277
}

crates/ide-completion/src/render.rs

+4
Original file line numberDiff line numberDiff line change
@@ -1170,6 +1170,7 @@ fn main() { let _: m::Spam = S$0 }
11701170
),
11711171
is_local: false,
11721172
is_item_from_trait: false,
1173+
is_item_from_notable_trait: false,
11731174
is_name_already_imported: false,
11741175
requires_import: false,
11751176
is_op_method: false,
@@ -1196,6 +1197,7 @@ fn main() { let _: m::Spam = S$0 }
11961197
),
11971198
is_local: false,
11981199
is_item_from_trait: false,
1200+
is_item_from_notable_trait: false,
11991201
is_name_already_imported: false,
12001202
requires_import: false,
12011203
is_op_method: false,
@@ -1274,6 +1276,7 @@ fn foo() { A { the$0 } }
12741276
),
12751277
is_local: false,
12761278
is_item_from_trait: false,
1279+
is_item_from_notable_trait: false,
12771280
is_name_already_imported: false,
12781281
requires_import: false,
12791282
is_op_method: false,
@@ -2089,6 +2092,7 @@ fn foo() {
20892092
),
20902093
is_local: false,
20912094
is_item_from_trait: false,
2095+
is_item_from_notable_trait: false,
20922096
is_name_already_imported: false,
20932097
requires_import: false,
20942098
is_op_method: false,

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

+6
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,11 @@ fn render(
7979
.and_then(|trait_| trait_.containing_trait_or_trait_impl(ctx.db()))
8080
.map_or(false, |trait_| completion.is_ops_trait(trait_));
8181

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_));
86+
8287
let (has_dot_receiver, has_call_parens, cap) = match func_kind {
8388
FuncKind::Function(&PathCompletionCtx {
8489
kind: PathKind::Expr { .. },
@@ -105,6 +110,7 @@ fn render(
105110
},
106111
exact_name_match: compute_exact_name_match(completion, &call),
107112
is_op_method,
113+
is_item_from_notable_trait,
108114
..ctx.completion_relevance()
109115
});
110116

0 commit comments

Comments
 (0)