@@ -4,7 +4,7 @@ mod analysis;
44#[ cfg( test) ]
55mod tests;
66
7- use std:: { iter, ops :: ControlFlow } ;
7+ use std:: iter;
88
99use base_db:: RootQueryDb as _;
1010use hir:: {
@@ -21,7 +21,6 @@ use syntax::{
2121 SyntaxKind :: { self , * } ,
2222 SyntaxToken , T , TextRange , TextSize ,
2323 ast:: { self , AttrKind , NameOrNameRef } ,
24- match_ast,
2524} ;
2625
2726use crate :: {
@@ -818,48 +817,20 @@ impl<'db> CompletionContext<'db> {
818817 . extend ( exclude_traits. iter ( ) . map ( |& t| ( t. into ( ) , AutoImportExclusionType :: Always ) ) ) ;
819818
820819 // FIXME: This should be part of `CompletionAnalysis` / `expand_and_analyze`
821- let complete_semicolon = if config. add_semicolon_to_unit {
822- let inside_closure_ret = token. parent_ancestors ( ) . try_for_each ( |ancestor| {
823- match_ast ! {
824- match ancestor {
825- ast:: BlockExpr ( _) => ControlFlow :: Break ( false ) ,
826- ast:: ClosureExpr ( _) => ControlFlow :: Break ( true ) ,
827- _ => ControlFlow :: Continue ( ( ) )
828- }
829- }
830- } ) ;
831-
832- if inside_closure_ret == ControlFlow :: Break ( true ) {
833- CompleteSemicolon :: DoNotComplete
834- } else {
835- let next_non_trivia_token =
836- std:: iter:: successors ( token. next_token ( ) , |it| it. next_token ( ) )
837- . find ( |it| !it. kind ( ) . is_trivia ( ) ) ;
838- let in_match_arm = token. parent_ancestors ( ) . try_for_each ( |ancestor| {
839- if ast:: MatchArm :: can_cast ( ancestor. kind ( ) ) {
840- ControlFlow :: Break ( true )
841- } else if matches ! (
842- ancestor. kind( ) ,
843- SyntaxKind :: EXPR_STMT | SyntaxKind :: BLOCK_EXPR
844- ) {
845- ControlFlow :: Break ( false )
846- } else {
847- ControlFlow :: Continue ( ( ) )
848- }
849- } ) ;
850- // FIXME: This will assume expr macros are not inside match, we need to somehow go to the "parent" of the root node.
851- let in_match_arm = match in_match_arm {
852- ControlFlow :: Continue ( ( ) ) => false ,
853- ControlFlow :: Break ( it) => it,
854- } ;
855- let complete_token = if in_match_arm { T ! [ , ] } else { T ! [ ; ] } ;
856- if next_non_trivia_token. map ( |it| it. kind ( ) ) == Some ( complete_token) {
857- CompleteSemicolon :: DoNotComplete
858- } else if in_match_arm {
859- CompleteSemicolon :: CompleteComma
860- } else {
861- CompleteSemicolon :: CompleteSemi
862- }
820+ let complete_semicolon = if !config. add_semicolon_to_unit {
821+ CompleteSemicolon :: DoNotComplete
822+ } else if let Some ( term_node) =
823+ sema. token_ancestors_with_macros ( token. clone ( ) ) . find ( |node| {
824+ matches ! ( node. kind( ) , BLOCK_EXPR | MATCH_ARM | CLOSURE_EXPR | ARG_LIST | PAREN_EXPR )
825+ } )
826+ {
827+ let next_token = iter:: successors ( token. next_token ( ) , |it| it. next_token ( ) )
828+ . map ( |it| it. kind ( ) )
829+ . find ( |kind| !kind. is_trivia ( ) ) ;
830+ match term_node. kind ( ) {
831+ MATCH_ARM if next_token != Some ( T ! [ , ] ) => CompleteSemicolon :: CompleteComma ,
832+ BLOCK_EXPR if next_token != Some ( T ! [ ; ] ) => CompleteSemicolon :: CompleteSemi ,
833+ _ => CompleteSemicolon :: DoNotComplete ,
863834 }
864835 } else {
865836 CompleteSemicolon :: DoNotComplete
0 commit comments