@@ -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) ;
0 commit comments