@@ -11,7 +11,7 @@ use rustc_hash::FxHashMap;
11
11
use span:: Edition ;
12
12
use syntax:: {
13
13
NodeOrToken , SyntaxNode ,
14
- ast:: { self , AstNode , HasGenericArgs , make} ,
14
+ ast:: { self , AstNode , HasGenericArgs , HasName , make} ,
15
15
syntax_editor:: { self , SyntaxEditor } ,
16
16
} ;
17
17
@@ -315,32 +315,49 @@ impl Ctx<'_> {
315
315
}
316
316
317
317
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 ( ) ;
320
322
for child in root_path. children ( ) {
321
323
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) ) ;
323
327
} else {
324
- result. extend ( find_child_paths ( & child) ) ;
328
+ result. extend ( find_child_paths_and_ident_pats ( & child) ) ;
325
329
}
326
330
}
327
331
result
328
332
}
333
+
329
334
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) ;
331
337
let mut editor = SyntaxEditor :: new ( root_path. clone ( ) ) ;
332
338
for sub_path in result {
333
339
let new = self . transform_path ( sub_path. syntax ( ) ) ;
334
340
editor. replace ( sub_path. syntax ( ) , new) ;
335
341
}
342
+
336
343
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) ;
338
345
let mut editor = SyntaxEditor :: new ( update_sub_item) ;
339
346
for sub_path in item {
340
- self . transform_path_ ( & mut editor, & sub_path) ;
347
+ self . transform_path_or_ident_pat ( & mut editor, & sub_path) ;
341
348
}
342
349
editor. finish ( ) . new_root ( ) . clone ( )
343
350
}
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
+ }
344
361
345
362
fn transform_path_ ( & self , editor : & mut SyntaxEditor , path : & ast:: Path ) -> Option < ( ) > {
346
363
if path. qualifier ( ) . is_some ( ) {
@@ -515,6 +532,34 @@ impl Ctx<'_> {
515
532
}
516
533
Some ( ( ) )
517
534
}
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
+ }
518
563
}
519
564
520
565
// FIXME: It would probably be nicer if we could get this via HIR (i.e. get the
0 commit comments