@@ -11,7 +11,7 @@ use rustc_hash::FxHashMap;
1111use span:: Edition ;
1212use syntax:: {
1313 NodeOrToken , SyntaxNode ,
14- ast:: { self , AstNode , HasGenericArgs , make} ,
14+ ast:: { self , AstNode , HasGenericArgs , HasName , make} ,
1515 syntax_editor:: { self , SyntaxEditor } ,
1616} ;
1717
@@ -315,32 +315,49 @@ impl Ctx<'_> {
315315 }
316316
317317 fn transform_path ( & self , path : & SyntaxNode ) -> SyntaxNode {
318- fn find_child_paths ( root_path : & SyntaxNode ) -> Vec < ast:: Path > {
319- let mut result = Vec :: new ( ) ;
318+ fn find_child_paths_and_ident_pats (
319+ root_path : & SyntaxNode ,
320+ ) -> Vec < Either < ast:: Path , ast:: IdentPat > > {
321+ let mut result: Vec < Either < ast:: Path , ast:: IdentPat > > = Vec :: new ( ) ;
320322 for child in root_path. children ( ) {
321323 if let Some ( child_path) = ast:: Path :: cast ( child. clone ( ) ) {
322- result. push ( child_path) ;
324+ result. push ( either:: Left ( child_path) ) ;
325+ } else if let Some ( child_ident_pat) = ast:: IdentPat :: cast ( child. clone ( ) ) {
326+ result. push ( either:: Right ( child_ident_pat) ) ;
323327 } else {
324- result. extend ( find_child_paths ( & child) ) ;
328+ result. extend ( find_child_paths_and_ident_pats ( & child) ) ;
325329 }
326330 }
327331 result
328332 }
333+
329334 let root_path = path. clone_subtree ( ) ;
330- let result = find_child_paths ( & root_path) ;
335+
336+ let result = find_child_paths_and_ident_pats ( & root_path) ;
331337 let mut editor = SyntaxEditor :: new ( root_path. clone ( ) ) ;
332338 for sub_path in result {
333339 let new = self . transform_path ( sub_path. syntax ( ) ) ;
334340 editor. replace ( sub_path. syntax ( ) , new) ;
335341 }
342+
336343 let update_sub_item = editor. finish ( ) . new_root ( ) . clone ( ) . clone_subtree ( ) ;
337- let item = find_child_paths ( & update_sub_item) ;
344+ let item = find_child_paths_and_ident_pats ( & update_sub_item) ;
338345 let mut editor = SyntaxEditor :: new ( update_sub_item) ;
339346 for sub_path in item {
340- self . transform_path_ ( & mut editor, & sub_path) ;
347+ self . transform_path_or_ident_pat ( & mut editor, & sub_path) ;
341348 }
342349 editor. finish ( ) . new_root ( ) . clone ( )
343350 }
351+ fn transform_path_or_ident_pat (
352+ & self ,
353+ editor : & mut SyntaxEditor ,
354+ item : & Either < ast:: Path , ast:: IdentPat > ,
355+ ) -> Option < ( ) > {
356+ match item {
357+ Either :: Left ( path) => self . transform_path_ ( editor, path) ,
358+ Either :: Right ( ident_pat) => self . transform_ident_pat ( editor, ident_pat) ,
359+ }
360+ }
344361
345362 fn transform_path_ ( & self , editor : & mut SyntaxEditor , path : & ast:: Path ) -> Option < ( ) > {
346363 if path. qualifier ( ) . is_some ( ) {
@@ -515,6 +532,34 @@ impl Ctx<'_> {
515532 }
516533 Some ( ( ) )
517534 }
535+
536+ fn transform_ident_pat (
537+ & self ,
538+ editor : & mut SyntaxEditor ,
539+ ident_pat : & ast:: IdentPat ,
540+ ) -> Option < ( ) > {
541+ let name = ident_pat. name ( ) ?;
542+
543+ let temp_path = make:: path_from_text ( & name. text ( ) ) ;
544+
545+ let resolution = self . source_scope . speculative_resolve ( & temp_path) ?;
546+
547+ match resolution {
548+ hir:: PathResolution :: Def ( def) if def. as_assoc_item ( self . source_scope . db ) . is_none ( ) => {
549+ let cfg = ImportPathConfig {
550+ prefer_no_std : false ,
551+ prefer_prelude : true ,
552+ prefer_absolute : false ,
553+ allow_unstable : true ,
554+ } ;
555+ let found_path = self . target_module . find_path ( self . source_scope . db , def, cfg) ?;
556+ let res = mod_path_to_ast ( & found_path, self . target_edition ) . clone_for_update ( ) ;
557+ editor. replace ( ident_pat. syntax ( ) , res. syntax ( ) ) ;
558+ Some ( ( ) )
559+ }
560+ _ => None ,
561+ }
562+ }
518563}
519564
520565// FIXME: It would probably be nicer if we could get this via HIR (i.e. get the
0 commit comments