@@ -16,7 +16,7 @@ use syntax::{
16
16
edit_in_place:: { AttrsOwnerEdit , Indent } ,
17
17
make, HasName ,
18
18
} ,
19
- match_ast , ted, AstNode , NodeOrToken , SyntaxNode , T ,
19
+ ted, AstNode , NodeOrToken , SyntaxKind , SyntaxNode , T ,
20
20
} ;
21
21
use text_edit:: TextRange ;
22
22
@@ -472,33 +472,16 @@ fn add_enum_def(
472
472
) ;
473
473
}
474
474
475
- /// Finds where to put the new enum definition, at the nearest module or at top-level.
476
- fn node_to_insert_before ( mut target_node : SyntaxNode ) -> SyntaxNode {
477
- let mut ancestors = target_node. ancestors ( ) ;
478
-
479
- while let Some ( ancestor) = ancestors. next ( ) {
480
- match_ast ! {
481
- match ancestor {
482
- ast:: Item ( item) => {
483
- if item
484
- . syntax( )
485
- . parent( )
486
- . and_then( |item_list| item_list. parent( ) )
487
- . and_then( ast:: Module :: cast)
488
- . is_some( )
489
- {
490
- return ancestor;
491
- }
492
- } ,
493
- ast:: SourceFile ( _) => break ,
494
- _ => ( ) ,
495
- }
496
- }
497
-
498
- target_node = ancestor;
499
- }
500
-
475
+ /// Finds where to put the new enum definition.
476
+ /// Tries to find the ast node at the nearest module or at top-level, otherwise just
477
+ /// returns the input node.
478
+ fn node_to_insert_before ( target_node : SyntaxNode ) -> SyntaxNode {
501
479
target_node
480
+ . ancestors ( )
481
+ . take_while ( |it| !matches ! ( it. kind( ) , SyntaxKind :: MODULE | SyntaxKind :: SOURCE_FILE ) )
482
+ . filter ( |it| ast:: Item :: can_cast ( it. kind ( ) ) )
483
+ . last ( )
484
+ . unwrap_or ( target_node)
502
485
}
503
486
504
487
fn make_bool_enum ( make_pub : bool ) -> ast:: Enum {
0 commit comments