@@ -337,67 +337,65 @@ fn insert_use_(
337
337
Some ( ( path, has_tl, node) )
338
338
} ) ;
339
339
340
- if !group_imports {
340
+ if group_imports {
341
+ // Iterator that discards anything thats not in the required grouping
342
+ // This implementation allows the user to rearrange their import groups as this only takes the first group that fits
343
+ let group_iter = path_node_iter
344
+ . clone ( )
345
+ . skip_while ( |( path, ..) | ImportGroup :: new ( path) != group)
346
+ . take_while ( |( path, ..) | ImportGroup :: new ( path) == group) ;
347
+
348
+ // track the last element we iterated over, if this is still None after the iteration then that means we never iterated in the first place
349
+ let mut last = None ;
350
+ // find the element that would come directly after our new import
351
+ let post_insert: Option < ( _ , _ , SyntaxNode ) > = group_iter
352
+ . inspect ( |( .., node) | last = Some ( node. clone ( ) ) )
353
+ . find ( |& ( ref path, has_tl, _) | {
354
+ use_tree_path_cmp ( insert_path, false , path, has_tl) != Ordering :: Greater
355
+ } ) ;
356
+
357
+ if let Some ( ( .., node) ) = post_insert {
358
+ cov_mark:: hit!( insert_group) ;
359
+ // insert our import before that element
360
+ return ted:: insert ( ted:: Position :: before ( node) , use_item. syntax ( ) ) ;
361
+ }
362
+ if let Some ( node) = last {
363
+ cov_mark:: hit!( insert_group_last) ;
364
+ // there is no element after our new import, so append it to the end of the group
365
+ return ted:: insert ( ted:: Position :: after ( node) , use_item. syntax ( ) ) ;
366
+ }
367
+
368
+ // the group we were looking for actually doesn't exist, so insert
369
+
370
+ let mut last = None ;
371
+ // find the group that comes after where we want to insert
372
+ let post_group = path_node_iter
373
+ . inspect ( |( .., node) | last = Some ( node. clone ( ) ) )
374
+ . find ( |( p, ..) | ImportGroup :: new ( p) > group) ;
375
+ if let Some ( ( .., node) ) = post_group {
376
+ cov_mark:: hit!( insert_group_new_group) ;
377
+ ted:: insert ( ted:: Position :: before ( & node) , use_item. syntax ( ) ) ;
378
+ if let Some ( node) = algo:: non_trivia_sibling ( node. into ( ) , Direction :: Prev ) {
379
+ ted:: insert ( ted:: Position :: after ( node) , make:: tokens:: single_newline ( ) ) ;
380
+ }
381
+ return ;
382
+ }
383
+ // there is no such group, so append after the last one
384
+ if let Some ( node) = last {
385
+ cov_mark:: hit!( insert_group_no_group) ;
386
+ ted:: insert ( ted:: Position :: after ( & node) , use_item. syntax ( ) ) ;
387
+ ted:: insert ( ted:: Position :: after ( node) , make:: tokens:: single_newline ( ) ) ;
388
+ return ;
389
+ }
390
+ } else {
391
+ // There exists a group, so append to the end of it
341
392
if let Some ( ( _, _, node) ) = path_node_iter. last ( ) {
342
393
cov_mark:: hit!( insert_no_grouping_last) ;
343
394
ted:: insert ( ted:: Position :: after ( node) , use_item. syntax ( ) ) ;
344
- } else {
345
- cov_mark:: hit!( insert_no_grouping_last2) ;
346
- ted:: insert ( ted:: Position :: first_child_of ( scope_syntax) , make:: tokens:: blank_line ( ) ) ;
347
- ted:: insert ( ted:: Position :: first_child_of ( scope_syntax) , use_item. syntax ( ) ) ;
395
+ return ;
348
396
}
349
- return ;
350
- }
351
-
352
- // Iterator that discards anything thats not in the required grouping
353
- // This implementation allows the user to rearrange their import groups as this only takes the first group that fits
354
- let group_iter = path_node_iter
355
- . clone ( )
356
- . skip_while ( |( path, ..) | ImportGroup :: new ( path) != group)
357
- . take_while ( |( path, ..) | ImportGroup :: new ( path) == group) ;
358
-
359
- // track the last element we iterated over, if this is still None after the iteration then that means we never iterated in the first place
360
- let mut last = None ;
361
- // find the element that would come directly after our new import
362
- let post_insert: Option < ( _ , _ , SyntaxNode ) > = group_iter
363
- . inspect ( |( .., node) | last = Some ( node. clone ( ) ) )
364
- . find ( |& ( ref path, has_tl, _) | {
365
- use_tree_path_cmp ( insert_path, false , path, has_tl) != Ordering :: Greater
366
- } ) ;
367
-
368
- if let Some ( ( .., node) ) = post_insert {
369
- cov_mark:: hit!( insert_group) ;
370
- // insert our import before that element
371
- return ted:: insert ( ted:: Position :: before ( node) , use_item. syntax ( ) ) ;
372
- }
373
- if let Some ( node) = last {
374
- cov_mark:: hit!( insert_group_last) ;
375
- // there is no element after our new import, so append it to the end of the group
376
- return ted:: insert ( ted:: Position :: after ( node) , use_item. syntax ( ) ) ;
377
397
}
378
398
379
- // the group we were looking for actually doesn't exist, so insert
380
-
381
- let mut last = None ;
382
- // find the group that comes after where we want to insert
383
- let post_group = path_node_iter
384
- . inspect ( |( .., node) | last = Some ( node. clone ( ) ) )
385
- . find ( |( p, ..) | ImportGroup :: new ( p) > group) ;
386
- if let Some ( ( .., node) ) = post_group {
387
- cov_mark:: hit!( insert_group_new_group) ;
388
- ted:: insert ( ted:: Position :: before ( & node) , use_item. syntax ( ) ) ;
389
- if let Some ( node) = algo:: non_trivia_sibling ( node. into ( ) , Direction :: Prev ) {
390
- ted:: insert ( ted:: Position :: after ( node) , make:: tokens:: single_newline ( ) ) ;
391
- }
392
- return ;
393
- }
394
- // there is no such group, so append after the last one
395
- if let Some ( node) = last {
396
- cov_mark:: hit!( insert_group_no_group) ;
397
- ted:: insert ( ted:: Position :: after ( & node) , use_item. syntax ( ) ) ;
398
- ted:: insert ( ted:: Position :: after ( node) , make:: tokens:: single_newline ( ) ) ;
399
- return ;
400
- }
401
399
// there are no imports in this file at all
402
400
if let Some ( last_inner_element) = scope_syntax
403
401
. children_with_tokens ( )
@@ -407,14 +405,14 @@ fn insert_use_(
407
405
} )
408
406
. last ( )
409
407
{
410
- cov_mark:: hit!( insert_group_empty_inner_attr ) ;
408
+ cov_mark:: hit!( insert_empty_inner_attr ) ;
411
409
ted:: insert ( ted:: Position :: after ( & last_inner_element) , use_item. syntax ( ) ) ;
412
410
ted:: insert ( ted:: Position :: after ( last_inner_element) , make:: tokens:: single_newline ( ) ) ;
413
411
return ;
414
412
}
415
413
let l_curly = match scope {
416
414
ImportScope :: File ( _) => {
417
- cov_mark:: hit!( insert_group_empty_file ) ;
415
+ cov_mark:: hit!( insert_empty_file ) ;
418
416
ted:: insert ( ted:: Position :: first_child_of ( scope_syntax) , make:: tokens:: blank_line ( ) ) ;
419
417
ted:: insert ( ted:: Position :: first_child_of ( scope_syntax) , use_item. syntax ( ) ) ;
420
418
return ;
@@ -426,7 +424,7 @@ fn insert_use_(
426
424
} ;
427
425
match l_curly {
428
426
Some ( b) => {
429
- cov_mark:: hit!( insert_group_empty_module ) ;
427
+ cov_mark:: hit!( insert_empty_module ) ;
430
428
ted:: insert ( ted:: Position :: after ( & b) , make:: tokens:: single_newline ( ) ) ;
431
429
ted:: insert ( ted:: Position :: after ( & b) , use_item. syntax ( ) ) ;
432
430
}
0 commit comments