Skip to content

Commit 1df4dd3

Browse files
committed
feat: suggest new like fn first
1 parent 6091d22 commit 1df4dd3

File tree

2 files changed

+24
-1
lines changed

2 files changed

+24
-1
lines changed

crates/ide-completion/src/completions.rs

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -593,6 +593,28 @@ 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+
fn creates_self(item: &CompletionItem) -> Option<bool> {
603+
item.detail.as_ref().filter(|d| d.starts_with("fn() -> ")).map(|_| false)
604+
}
605+
fn creates_self_given_args(item: &CompletionItem) -> Option<bool> {
606+
item.detail
607+
.as_ref()
608+
.filter(|d| d.starts_with("fn(") && d.contains("->") && !d.contains("&self"))
609+
.map(|_| false)
610+
}
611+
612+
self.buf.sort_by(|a, b| {
613+
creates_self(b)
614+
.cmp(&creates_self(a))
615+
.then(creates_self_given_args(b).cmp(&creates_self_given_args(a)))
616+
});
617+
}
596618
}
597619

598620
/// Calls the callback for each variant of the provided enum with the path to the variant.
@@ -694,6 +716,7 @@ pub(super) fn complete_name_ref(
694716
dot::complete_undotted_self(acc, ctx, path_ctx, expr_ctx);
695717
item_list::complete_item_list_in_expr(acc, ctx, path_ctx, expr_ctx);
696718
snippet::complete_expr_snippet(acc, ctx, path_ctx, expr_ctx);
719+
acc.sort_new_first();
697720
}
698721
PathKind::Type { location } => {
699722
r#type::complete_type_path(acc, ctx, path_ctx, location);

crates/ide-completion/src/completions/dot.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -272,7 +272,7 @@ fn foo(a: A) { a.$0() }
272272
}
273273

274274
#[test]
275-
fn test_usable_types_first() {
275+
fn test_suggest_new_first() {
276276
check_exact_order(
277277
r#"
278278
struct A;

0 commit comments

Comments
 (0)