Skip to content

Commit b081f24

Browse files
committed
save
1 parent 2d58983 commit b081f24

File tree

2 files changed

+62
-35
lines changed

2 files changed

+62
-35
lines changed

crates/ide-completion/src/completions.rs

Lines changed: 35 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -593,40 +593,6 @@ impl Completions {
593593
}
594594
self.add_opt(render_struct_pat(RenderContext::new(ctx), pattern_ctx, strukt, local_name));
595595
}
596-
597-
/// Sort the suggestions with `new` like functions first.
598-
/// That means:
599-
/// fn with no param that returns itself
600-
/// fn with param that returns itself
601-
pub(crate) fn sort_new_first(&mut self) {
602-
// ToDo: Ensure these fn returns Self
603-
fn maybe_new(item: &CompletionItem) -> bool {
604-
item.detail.as_ref().map(|d| d.starts_with("fn() -> ")).unwrap_or_default()
605-
}
606-
fn maybe_new_with_args(item: &CompletionItem) -> bool {
607-
item.detail
608-
.as_ref()
609-
.map(|d| d.starts_with("fn(") && d.contains("->") && !d.contains("&self"))
610-
.unwrap_or_default()
611-
}
612-
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-
620-
for item in self.buf.iter_mut() {
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);
627-
}
628-
}
629-
}
630596
}
631597

632598
/// Calls the callback for each variant of the provided enum with the path to the variant.
@@ -730,7 +696,7 @@ pub(super) fn complete_name_ref(
730696
snippet::complete_expr_snippet(acc, ctx, path_ctx, expr_ctx);
731697

732698
if matches!(ctx.token.kind(), syntax::SyntaxKind::COLON2) {
733-
acc.sort_new_first();
699+
bump_relevance_for_new_like_fns(acc);
734700
}
735701
}
736702
PathKind::Type { location } => {
@@ -805,3 +771,37 @@ fn complete_patterns(
805771
pattern::complete_pattern(acc, ctx, pattern_ctx);
806772
record::complete_record_pattern_fields(acc, ctx, pattern_ctx);
807773
}
774+
775+
/// Sort the suggestions with `new` like functions first.
776+
/// That means:
777+
/// fn with no param that returns itself
778+
/// fn with param that returns itself
779+
pub(crate) fn bump_relevance_for_new_like_fns(acc: &mut Completions) {
780+
// ToDo: Ensure these fn returns Self
781+
fn maybe_new(item: &CompletionItem) -> bool {
782+
item.detail.as_ref().map(|d| d.starts_with("fn() -> ")).unwrap_or_default()
783+
}
784+
fn maybe_new_with_args(item: &CompletionItem) -> bool {
785+
item.detail
786+
.as_ref()
787+
.map(|d| d.starts_with("fn(") && d.contains("->") && !d.contains("&self"))
788+
.unwrap_or_default()
789+
}
790+
791+
fn maybe_builder(item: &CompletionItem) -> bool {
792+
item.detail
793+
.as_ref()
794+
.map(|d| d.starts_with("fn() -> ") && d.contains("Builder"))
795+
.unwrap_or_default()
796+
}
797+
798+
for item in acc.buf.iter_mut() {
799+
if maybe_new(&item) {
800+
item.bump_relevance_by(30);
801+
} else if maybe_builder(&item) {
802+
item.bump_relevance_by(20);
803+
} else if maybe_new_with_args(&item) {
804+
item.bump_relevance_by(10);
805+
}
806+
}
807+
}

crates/ide-completion/src/render.rs

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2033,6 +2033,33 @@ fn test() {
20332033
me foo(…) [type_could_unify]
20342034
"#]],
20352035
);
2036+
2037+
check_relevance(
2038+
r#"
2039+
struct A;
2040+
struct ABuilder;
2041+
impl A {
2042+
fn foo(&self) {}
2043+
fn new_1(input: u32) -> A { A }
2044+
fn new_2() -> Self { A }
2045+
fn aaaabuilder() -> ABuilder { A }
2046+
fn test() {
2047+
Self::$0;
2048+
}
2049+
}
2050+
"#,
2051+
// preference:
2052+
// fn with no param that returns itself
2053+
// builder like fn
2054+
// fn with param that returns itself
2055+
expect![[r#"
2056+
fn new_2() []
2057+
fn aaaabuilder() []
2058+
fn new_1(…) []
2059+
me foo(…) []
2060+
fn test() []
2061+
"#]],
2062+
);
20362063
}
20372064

20382065
#[test]

0 commit comments

Comments
 (0)