@@ -25,28 +25,32 @@ public static class IModuleRewriterExtensions
2525 /// </remarks>
2626 public static void RemoveVariables ( this IModuleRewriter rewriter , IEnumerable < VariableDeclaration > toRemove , bool removeEndOfStmtContext = true )
2727 {
28- if ( ! toRemove . Any ( ) ) { return ; }
28+ if ( ! toRemove . Any ( ) )
29+ {
30+ return ;
31+ }
2932
30- var fieldsByListContext = toRemove . Distinct ( )
31- . GroupBy ( f => f . Context . GetAncestor < VBAParser . VariableListStmtContext > ( ) ) ;
33+ var fieldsToDeleteByListContext = toRemove . Distinct ( )
34+ . ToLookup ( f => f . Context . GetAncestor < VBAParser . VariableListStmtContext > ( ) ) ;
3235
33- foreach ( var fieldsGroup in fieldsByListContext )
36+ foreach ( var fieldsToDelete in fieldsToDeleteByListContext )
3437 {
35- var variables = fieldsGroup . Key . children . Where ( ch => ch is VBAParser . VariableSubStmtContext ) ;
36- if ( variables . Count ( ) == fieldsGroup . Count ( ) )
38+ var variableList = fieldsToDelete . Key . children . OfType < VBAParser . VariableSubStmtContext > ( ) ;
39+
40+ if ( variableList . Count ( ) == fieldsToDelete . Count ( ) )
3741 {
38- if ( fieldsGroup . First ( ) . ParentDeclaration . DeclarationType . HasFlag ( DeclarationType . Module ) )
42+ if ( fieldsToDelete . First ( ) . ParentDeclaration . DeclarationType . HasFlag ( DeclarationType . Module ) )
3943 {
40- rewriter . RemoveDeclaration < VBAParser . ModuleDeclarationsElementContext > ( fieldsGroup . First ( ) , removeEndOfStmtContext ) ;
44+ rewriter . RemoveDeclarationContext < VBAParser . ModuleDeclarationsElementContext > ( fieldsToDelete . First ( ) , removeEndOfStmtContext ) ;
4145 }
4246 else
4347 {
44- rewriter . RemoveDeclaration < VBAParser . BlockStmtContext > ( fieldsGroup . First ( ) , removeEndOfStmtContext ) ;
48+ rewriter . RemoveDeclarationContext < VBAParser . BlockStmtContext > ( fieldsToDelete . First ( ) , removeEndOfStmtContext ) ;
4549 }
4650 continue ;
4751 }
4852
49- foreach ( var target in fieldsGroup )
53+ foreach ( var target in fieldsToDelete )
5054 {
5155 rewriter . Remove ( target ) ;
5256 }
@@ -76,15 +80,18 @@ public static void RemoveMember(this IModuleRewriter rewriter, ModuleBodyElement
7680 /// </remarks>
7781 public static void RemoveMembers ( this IModuleRewriter rewriter , IEnumerable < ModuleBodyElementDeclaration > toRemove , bool removeEndOfStmtContext = true )
7882 {
79- if ( ! toRemove . Any ( ) ) { return ; }
83+ if ( ! toRemove . Any ( ) )
84+ {
85+ return ;
86+ }
8087
8188 foreach ( var member in toRemove )
8289 {
83- rewriter . RemoveDeclaration < VBAParser . ModuleBodyElementContext > ( member , removeEndOfStmtContext ) ;
90+ rewriter . RemoveDeclarationContext < VBAParser . ModuleBodyElementContext > ( member , removeEndOfStmtContext ) ;
8491 }
8592 }
8693
87- private static void RemoveDeclaration < T > ( this IModuleRewriter rewriter , Declaration declaration , bool removeEndOfStmtContext = true ) where T : ParserRuleContext
94+ private static void RemoveDeclarationContext < T > ( this IModuleRewriter rewriter , Declaration declaration , bool removeEndOfStmtContext = true ) where T : ParserRuleContext
8895 {
8996 if ( ! declaration . Context . TryGetAncestor < T > ( out var elementContext ) )
9097 {
0 commit comments