@@ -413,7 +413,10 @@ impl Module {
413413 ctx,
414414 )
415415 {
416- import_paths_to_be_removed. push ( import_path) ;
416+ check_intersection_and_push (
417+ & mut import_paths_to_be_removed,
418+ import_path,
419+ ) ;
417420 }
418421 }
419422 }
@@ -439,28 +442,10 @@ impl Module {
439442 ctx,
440443 )
441444 {
442- if import_paths_to_be_removed. len ( ) > 0 {
443- // Text ranges recieved here for imports are extended to the
444- // next/previous comma which can cause intersections among them
445- // and later deletion of these can cause panics similar
446- // to reported in #11766. So to mitigate it, we
447- // check for intersection between all current members
448- // and if it exists we combine both text ranges into
449- // one
450- for i in 0 ..import_paths_to_be_removed. len ( ) {
451- if let Some ( _) =
452- import_paths_to_be_removed[ i] . intersect ( import_path)
453- {
454- import_paths_to_be_removed[ i] =
455- import_paths_to_be_removed[ i]
456- . cover ( import_path) ;
457- } else {
458- import_paths_to_be_removed. push ( import_path) ;
459- }
460- }
461- } else {
462- import_paths_to_be_removed. push ( import_path) ;
463- }
445+ check_intersection_and_push (
446+ & mut import_paths_to_be_removed,
447+ import_path,
448+ ) ;
464449 }
465450 }
466451 }
@@ -572,12 +557,14 @@ impl Module {
572557 }
573558
574559 if source_exists_outside_sel_in_same_mod {
575- let first_path_in_use_tree = use_tree_str[ use_tree_str. len ( ) - 1 ] . to_string ( ) ;
576- if !first_path_in_use_tree. contains ( "super" )
577- && !first_path_in_use_tree. contains ( "crate" )
578- {
579- let super_path = make:: ext:: ident_path ( "super" ) ;
580- use_tree_str. push ( super_path) ;
560+ if let Some ( first_path_in_use_tree) = use_tree_str. last ( ) {
561+ let first_path_in_use_tree_str = first_path_in_use_tree. to_string ( ) ;
562+ if !first_path_in_use_tree_str. contains ( "super" )
563+ && !first_path_in_use_tree_str. contains ( "crate" )
564+ {
565+ let super_path = make:: ext:: ident_path ( "super" ) ;
566+ use_tree_str. push ( super_path) ;
567+ }
581568 }
582569 }
583570
@@ -593,10 +580,12 @@ impl Module {
593580
594581 if !( !exists_outside_sel && exists_inside_sel && source_exists_outside_sel_in_same_mod)
595582 {
596- let first_path_in_use_tree = use_tree_str[ 0 ] . to_string ( ) ;
597- if first_path_in_use_tree. contains ( "super" ) {
598- let super_path = make:: ext:: ident_path ( "super" ) ;
599- use_tree_str. insert ( 0 , super_path)
583+ if let Some ( first_path_in_use_tree) = use_tree_str. first ( ) {
584+ let first_path_in_use_tree_str = first_path_in_use_tree. to_string ( ) ;
585+ if first_path_in_use_tree_str. contains ( "super" ) {
586+ let super_path = make:: ext:: ident_path ( "super" ) ;
587+ use_tree_str. insert ( 0 , super_path)
588+ }
600589 }
601590 }
602591
@@ -658,6 +647,32 @@ impl Module {
658647 }
659648}
660649
650+ fn check_intersection_and_push (
651+ import_paths_to_be_removed : & mut Vec < TextRange > ,
652+ import_path : TextRange ,
653+ ) {
654+ if import_paths_to_be_removed. len ( ) > 0 {
655+ // Text ranges recieved here for imports are extended to the
656+ // next/previous comma which can cause intersections among them
657+ // and later deletion of these can cause panics similar
658+ // to reported in #11766. So to mitigate it, we
659+ // check for intersection between all current members
660+ // and if it exists we combine both text ranges into
661+ // one
662+ let r = import_paths_to_be_removed
663+ . into_iter ( )
664+ . position ( |it| it. intersect ( import_path) . is_some ( ) ) ;
665+ match r {
666+ Some ( it) => {
667+ import_paths_to_be_removed[ it] = import_paths_to_be_removed[ it] . cover ( import_path)
668+ }
669+ None => import_paths_to_be_removed. push ( import_path) ,
670+ }
671+ } else {
672+ import_paths_to_be_removed. push ( import_path) ;
673+ }
674+ }
675+
661676fn does_source_exists_outside_sel_in_same_mod (
662677 def : Definition ,
663678 ctx : & AssistContext ,
0 commit comments