@@ -12,7 +12,7 @@ use crate::{
1212 CompletionContext , DotAccess , PathCompletionCtx , PathKind , PatternContext , Qualified ,
1313 TypeLocation ,
1414 } ,
15- render:: { render_resolution_with_import, RenderContext } ,
15+ render:: { render_resolution_with_import, render_resolution_with_import_pat , RenderContext } ,
1616} ;
1717
1818use super :: Completions ;
@@ -149,32 +149,22 @@ pub(crate) fn import_on_the_fly_path(
149149pub ( crate ) fn import_on_the_fly_pat (
150150 acc : & mut Completions ,
151151 ctx : & CompletionContext ,
152- pat_ctx : & PatternContext ,
152+ pattern_ctx : & PatternContext ,
153153) -> Option < ( ) > {
154154 if !ctx. config . enable_imports_on_the_fly {
155155 return None ;
156156 }
157- if let PatternContext { record_pat : Some ( _) , .. } = pat_ctx {
157+ if let PatternContext { record_pat : Some ( _) , .. } = pattern_ctx {
158158 return None ;
159159 }
160160
161161 let potential_import_name = import_name ( ctx) ;
162162 let import_assets = import_assets_for_path ( ctx, & potential_import_name, None ) ?;
163163
164- import_on_the_fly (
164+ import_on_the_fly_pat2 (
165165 acc,
166166 ctx,
167- & PathCompletionCtx {
168- has_call_parens : false ,
169- has_macro_bang : false ,
170- qualified : Qualified :: No ,
171- parent : None ,
172- // FIXME
173- path : syntax:: ast:: make:: ext:: ident_path ( "dummy__" ) ,
174- kind : crate :: context:: PathKind :: Pat { pat_ctx : pat_ctx. clone ( ) } ,
175- has_type_args : false ,
176- use_tree_parent : false ,
177- } ,
167+ pattern_ctx,
178168 import_assets,
179169 ctx. original_token . parent ( ) ?,
180170 potential_import_name,
@@ -289,6 +279,50 @@ fn import_on_the_fly(
289279 Some ( ( ) )
290280}
291281
282+ fn import_on_the_fly_pat2 (
283+ acc : & mut Completions ,
284+ ctx : & CompletionContext ,
285+ pattern_ctx : & PatternContext ,
286+ import_assets : ImportAssets ,
287+ position : SyntaxNode ,
288+ potential_import_name : String ,
289+ ) -> Option < ( ) > {
290+ let _p = profile:: span ( "import_on_the_fly_pat" ) . detail ( || potential_import_name. clone ( ) ) ;
291+
292+ if ImportScope :: find_insert_use_container ( & position, & ctx. sema ) . is_none ( ) {
293+ return None ;
294+ }
295+
296+ let ns_filter = |import : & LocatedImport | match import. original_item {
297+ ItemInNs :: Macros ( mac) => mac. is_fn_like ( ctx. db ) ,
298+ ItemInNs :: Types ( _) => true ,
299+ ItemInNs :: Values ( def) => matches ! ( def, hir:: ModuleDef :: Const ( _) ) ,
300+ } ;
301+ let user_input_lowercased = potential_import_name. to_lowercase ( ) ;
302+
303+ acc. add_all (
304+ import_assets
305+ . search_for_imports ( & ctx. sema , ctx. config . insert_use . prefix_kind )
306+ . into_iter ( )
307+ . filter ( ns_filter)
308+ . filter ( |import| {
309+ !ctx. is_item_hidden ( & import. item_to_import )
310+ && !ctx. is_item_hidden ( & import. original_item )
311+ } )
312+ . sorted_by_key ( |located_import| {
313+ compute_fuzzy_completion_order_key (
314+ & located_import. import_path ,
315+ & user_input_lowercased,
316+ )
317+ } )
318+ . filter_map ( |import| {
319+ render_resolution_with_import_pat ( RenderContext :: new ( ctx) , pattern_ctx, import)
320+ } )
321+ . map ( |builder| builder. build ( ) ) ,
322+ ) ;
323+ Some ( ( ) )
324+ }
325+
292326fn import_on_the_fly_method (
293327 acc : & mut Completions ,
294328 ctx : & CompletionContext ,
@@ -297,7 +331,7 @@ fn import_on_the_fly_method(
297331 position : SyntaxNode ,
298332 potential_import_name : String ,
299333) -> Option < ( ) > {
300- let _p = profile:: span ( "import_on_the_fly " ) . detail ( || potential_import_name. clone ( ) ) ;
334+ let _p = profile:: span ( "import_on_the_fly_method " ) . detail ( || potential_import_name. clone ( ) ) ;
301335
302336 if ImportScope :: find_insert_use_container ( & position, & ctx. sema ) . is_none ( ) {
303337 return None ;
0 commit comments