@@ -396,10 +396,19 @@ fn insert_use_(
396396 }
397397 }
398398
399+ let l_curly = match scope {
400+ ImportScope :: File ( _) => None ,
401+ // don't insert the imports before the item list/block expr's opening curly brace
402+ ImportScope :: Module ( item_list) => item_list. l_curly_token ( ) ,
403+ // don't insert the imports before the item list's opening curly brace
404+ ImportScope :: Block ( block) => block. l_curly_token ( ) ,
405+ } ;
399406 // there are no imports in this file at all
400407 // so put the import after all inner module attributes and possible license header comments
401408 if let Some ( last_inner_element) = scope_syntax
402409 . children_with_tokens ( )
410+ // skip the curly brace
411+ . skip ( l_curly. is_some ( ) as usize )
403412 . take_while ( |child| match child {
404413 NodeOrToken :: Node ( node) => is_inner_attribute ( node. clone ( ) ) ,
405414 NodeOrToken :: Token ( token) => {
@@ -413,30 +422,21 @@ fn insert_use_(
413422 cov_mark:: hit!( insert_empty_inner_attr) ;
414423 ted:: insert ( ted:: Position :: after ( & last_inner_element) , use_item. syntax ( ) ) ;
415424 ted:: insert ( ted:: Position :: after ( last_inner_element) , make:: tokens:: single_newline ( ) ) ;
416- return ;
417- }
418- let l_curly = match scope {
419- ImportScope :: File ( _) => {
420- cov_mark:: hit!( insert_empty_file) ;
421- ted:: insert ( ted:: Position :: first_child_of ( scope_syntax) , make:: tokens:: blank_line ( ) ) ;
422- ted:: insert ( ted:: Position :: first_child_of ( scope_syntax) , use_item. syntax ( ) ) ;
423- return ;
424- }
425- // don't insert the imports before the item list/block expr's opening curly brace
426- ImportScope :: Module ( item_list) => item_list. l_curly_token ( ) ,
427- // don't insert the imports before the item list's opening curly brace
428- ImportScope :: Block ( block) => block. l_curly_token ( ) ,
429- } ;
430- match l_curly {
431- Some ( b) => {
432- cov_mark:: hit!( insert_empty_module) ;
433- ted:: insert ( ted:: Position :: after ( & b) , make:: tokens:: single_newline ( ) ) ;
434- ted:: insert ( ted:: Position :: after ( & b) , use_item. syntax ( ) ) ;
435- }
436- None => {
437- // This should never happens, broken module syntax node
438- ted:: insert ( ted:: Position :: first_child_of ( scope_syntax) , make:: tokens:: blank_line ( ) ) ;
439- ted:: insert ( ted:: Position :: first_child_of ( scope_syntax) , use_item. syntax ( ) ) ;
425+ } else {
426+ match l_curly {
427+ Some ( b) => {
428+ cov_mark:: hit!( insert_empty_module) ;
429+ ted:: insert ( ted:: Position :: after ( & b) , make:: tokens:: single_newline ( ) ) ;
430+ ted:: insert ( ted:: Position :: after ( & b) , use_item. syntax ( ) ) ;
431+ }
432+ None => {
433+ cov_mark:: hit!( insert_empty_file) ;
434+ ted:: insert (
435+ ted:: Position :: first_child_of ( scope_syntax) ,
436+ make:: tokens:: blank_line ( ) ,
437+ ) ;
438+ ted:: insert ( ted:: Position :: first_child_of ( scope_syntax) , use_item. syntax ( ) ) ;
439+ }
440440 }
441441 }
442442}
0 commit comments