@@ -19,6 +19,7 @@ use test_utils::mark;
1919pub struct InsertUseConfig {
2020 pub merge : Option < MergeBehavior > ,
2121 pub prefix_kind : hir:: PrefixKind ,
22+ pub group : bool ,
2223}
2324
2425#[ derive( Debug , Clone ) ]
@@ -99,13 +100,13 @@ fn is_inner_comment(token: SyntaxToken) -> bool {
99100pub fn insert_use < ' a > (
100101 scope : & ImportScope ,
101102 path : ast:: Path ,
102- merge : Option < MergeBehavior > ,
103+ cfg : InsertUseConfig ,
103104) -> SyntaxRewriter < ' a > {
104105 let _p = profile:: span ( "insert_use" ) ;
105106 let mut rewriter = SyntaxRewriter :: default ( ) ;
106107 let use_item = make:: use_ ( None , make:: use_tree ( path. clone ( ) , None , None , false ) ) ;
107108 // merge into existing imports if possible
108- if let Some ( mb) = merge {
109+ if let Some ( mb) = cfg . merge {
109110 for existing_use in scope. as_syntax_node ( ) . children ( ) . filter_map ( ast:: Use :: cast) {
110111 if let Some ( merged) = try_merge_imports ( & existing_use, & use_item, mb) {
111112 rewriter. replace ( existing_use. syntax ( ) , merged. syntax ( ) ) ;
@@ -116,7 +117,7 @@ pub fn insert_use<'a>(
116117
117118 // either we weren't allowed to merge or there is no import that fits the merge conditions
118119 // so look for the place we have to insert to
119- let ( insert_position, add_blank) = find_insert_position ( scope, path) ;
120+ let ( insert_position, add_blank) = find_insert_position ( scope, path, cfg . group ) ;
120121
121122 let indent = if let ident_level @ 1 ..=usize:: MAX = scope. indent_level ( ) . 0 as usize {
122123 Some ( make:: tokens:: whitespace ( & " " . repeat ( 4 * ident_level) ) . into ( ) )
@@ -538,6 +539,7 @@ impl AddBlankLine {
538539fn find_insert_position (
539540 scope : & ImportScope ,
540541 insert_path : ast:: Path ,
542+ group_imports : bool ,
541543) -> ( InsertPosition < SyntaxElement > , AddBlankLine ) {
542544 let group = ImportGroup :: new ( & insert_path) ;
543545 let path_node_iter = scope
@@ -550,6 +552,14 @@ fn find_insert_position(
550552 let has_tl = tree. use_tree_list ( ) . is_some ( ) ;
551553 Some ( ( path, has_tl, node) )
552554 } ) ;
555+
556+ if !group_imports {
557+ if let Some ( ( _, _, node) ) = path_node_iter. last ( ) {
558+ return ( InsertPosition :: After ( node. into ( ) ) , AddBlankLine :: Before ) ;
559+ }
560+ return ( InsertPosition :: First , AddBlankLine :: AfterTwice ) ;
561+ }
562+
553563 // Iterator that discards anything thats not in the required grouping
554564 // This implementation allows the user to rearrange their import groups as this only takes the first group that fits
555565 let group_iter = path_node_iter
@@ -565,6 +575,7 @@ fn find_insert_position(
565575 use_tree_path_cmp ( & insert_path, false , path, has_tl) != Ordering :: Greater
566576 } ,
567577 ) ;
578+
568579 match post_insert {
569580 // insert our import before that element
570581 Some ( ( .., node) ) => ( InsertPosition :: Before ( node. into ( ) ) , AddBlankLine :: After ) ,
0 commit comments