@@ -16,7 +16,7 @@ use syntax::{
1616 edit_in_place:: { AttrsOwnerEdit , Indent } ,
1717 make, HasName ,
1818 } ,
19- match_ast , ted, AstNode , NodeOrToken , SyntaxNode , T ,
19+ ted, AstNode , NodeOrToken , SyntaxKind , SyntaxNode , T ,
2020} ;
2121use text_edit:: TextRange ;
2222
@@ -472,33 +472,16 @@ fn add_enum_def(
472472 ) ;
473473}
474474
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 {
501479 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)
502485}
503486
504487fn make_bool_enum ( make_pub : bool ) -> ast:: Enum {
0 commit comments