Skip to content

Commit 7ef37f3

Browse files
committed
builder methods
1 parent f4db9f1 commit 7ef37f3

File tree

3 files changed

+32
-20
lines changed

3 files changed

+32
-20
lines changed

crates/ide-completion/src/completions.rs

Lines changed: 16 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ use crate::{
4545
union_literal::render_union_literal,
4646
RenderContext,
4747
},
48-
CompletionContext, CompletionItem, CompletionItemKind, CompletionRelevance,
48+
CompletionContext, CompletionItem, CompletionItemKind,
4949
};
5050

5151
/// Represents an in-progress set of completions being built.
@@ -600,31 +600,30 @@ impl Completions {
600600
/// fn with param that returns itself
601601
pub(crate) fn sort_new_first(&mut self) {
602602
// ToDo: Ensure these fn returns Self
603-
fn creates_self(item: &CompletionItem) -> bool {
603+
fn maybe_new(item: &CompletionItem) -> bool {
604604
item.detail.as_ref().map(|d| d.starts_with("fn() -> ")).unwrap_or_default()
605605
}
606-
fn creates_self_given_args(item: &CompletionItem) -> bool {
606+
fn maybe_new_with_args(item: &CompletionItem) -> bool {
607607
item.detail
608608
.as_ref()
609609
.map(|d| d.starts_with("fn(") && d.contains("->") && !d.contains("&self"))
610610
.unwrap_or_default()
611611
}
612612

613+
fn maybe_builder(item: &CompletionItem) -> bool {
614+
item.detail
615+
.as_ref()
616+
.map(|d| d.starts_with("fn() -> ") && d.contains("Builder"))
617+
.unwrap_or_default()
618+
}
619+
613620
for item in self.buf.iter_mut() {
614-
if creates_self(&item) {
615-
//item.sort_text = Some(format!("{0:08x}", 0));
616-
item.relevance = CompletionRelevance {
617-
exact_name_match: true,
618-
is_definite: true,
619-
..Default::default()
620-
};
621-
} else if creates_self_given_args(&item) {
622-
//item.sort_text = Some(format!("{0:08x}", 1));
623-
item.relevance = CompletionRelevance {
624-
exact_name_match: true,
625-
is_local: true,
626-
..Default::default()
627-
};
621+
if maybe_new(&item) {
622+
item.bump_relevance_by(30);
623+
} else if maybe_builder(&item) {
624+
item.bump_relevance_by(20);
625+
} else if maybe_new_with_args(&item) {
626+
item.bump_relevance_by(10);
628627
}
629628
}
630629
}

crates/ide-completion/src/item.rs

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -164,6 +164,9 @@ pub struct CompletionRelevance {
164164
pub postfix_match: Option<CompletionRelevancePostfixMatch>,
165165
/// This is set for type inference results
166166
pub is_definite: bool,
167+
/// Any other bonuses we want to add,
168+
/// eg. bonus for good behavior!
169+
pub bonus_score: u32,
167170
}
168171

169172
#[derive(Debug, Clone, Copy, Eq, PartialEq)]
@@ -228,6 +231,7 @@ impl CompletionRelevance {
228231
is_private_editable,
229232
postfix_match,
230233
is_definite,
234+
bonus_score,
231235
} = self;
232236

233237
// lower rank private things
@@ -269,7 +273,8 @@ impl CompletionRelevance {
269273
if is_definite {
270274
score += 10;
271275
}
272-
score
276+
277+
score + bonus_score
273278
}
274279

275280
/// Returns true when the score for this threshold is above
@@ -386,6 +391,10 @@ impl CompletionItem {
386391
)
387392
})
388393
}
394+
395+
pub fn bump_relevance_by(&mut self, bonus: u32) {
396+
self.relevance.bonus_score += bonus;
397+
}
389398
}
390399

391400
/// A helper to make `CompletionItem`s.

crates/ide-completion/src/render.rs

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1637,10 +1637,12 @@ fn main() {
16371637
check_relevance(
16381638
r#"
16391639
struct A;
1640+
struct ABuilder;
16401641
impl A {
16411642
fn foo(&self) {}
16421643
fn new_1(input: u32) -> A { A }
16431644
fn new_2() -> Self { A }
1645+
fn aaaabuilder() -> ABuilder { A }
16441646
}
16451647
16461648
fn test() {
@@ -1649,10 +1651,12 @@ fn test() {
16491651
"#,
16501652
// preference:
16511653
// fn with no param that returns itself
1654+
// builder like fn
16521655
// fn with param that returns itself
16531656
expect![[r#"
1654-
fn new_2() [name]
1655-
fn new_1(…) [name+local]
1657+
fn new_2() [type_could_unify]
1658+
fn aaaabuilder() [type_could_unify]
1659+
fn new_1(…) [type_could_unify]
16561660
me foo(…) [type_could_unify]
16571661
"#]],
16581662
);

0 commit comments

Comments
 (0)