@@ -593,6 +593,28 @@ impl Completions {
593
593
}
594
594
self . add_opt ( render_struct_pat ( RenderContext :: new ( ctx) , pattern_ctx, strukt, local_name) ) ;
595
595
}
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
+ }
596
618
}
597
619
598
620
/// 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(
694
716
dot:: complete_undotted_self ( acc, ctx, path_ctx, expr_ctx) ;
695
717
item_list:: complete_item_list_in_expr ( acc, ctx, path_ctx, expr_ctx) ;
696
718
snippet:: complete_expr_snippet ( acc, ctx, path_ctx, expr_ctx) ;
719
+ acc. sort_new_first ( ) ;
697
720
}
698
721
PathKind :: Type { location } => {
699
722
r#type:: complete_type_path ( acc, ctx, path_ctx, location) ;
0 commit comments