@@ -25,28 +25,32 @@ public static class IModuleRewriterExtensions
25
25
/// </remarks>
26
26
public static void RemoveVariables ( this IModuleRewriter rewriter , IEnumerable < VariableDeclaration > toRemove , bool removeEndOfStmtContext = true )
27
27
{
28
- if ( ! toRemove . Any ( ) ) { return ; }
28
+ if ( ! toRemove . Any ( ) )
29
+ {
30
+ return ;
31
+ }
29
32
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 > ( ) ) ;
32
35
33
- foreach ( var fieldsGroup in fieldsByListContext )
36
+ foreach ( var fieldsToDelete in fieldsToDeleteByListContext )
34
37
{
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 ( ) )
37
41
{
38
- if ( fieldsGroup . First ( ) . ParentDeclaration . DeclarationType . HasFlag ( DeclarationType . Module ) )
42
+ if ( fieldsToDelete . First ( ) . ParentDeclaration . DeclarationType . HasFlag ( DeclarationType . Module ) )
39
43
{
40
- rewriter . RemoveDeclaration < VBAParser . ModuleDeclarationsElementContext > ( fieldsGroup . First ( ) , removeEndOfStmtContext ) ;
44
+ rewriter . RemoveDeclarationContext < VBAParser . ModuleDeclarationsElementContext > ( fieldsToDelete . First ( ) , removeEndOfStmtContext ) ;
41
45
}
42
46
else
43
47
{
44
- rewriter . RemoveDeclaration < VBAParser . BlockStmtContext > ( fieldsGroup . First ( ) , removeEndOfStmtContext ) ;
48
+ rewriter . RemoveDeclarationContext < VBAParser . BlockStmtContext > ( fieldsToDelete . First ( ) , removeEndOfStmtContext ) ;
45
49
}
46
50
continue ;
47
51
}
48
52
49
- foreach ( var target in fieldsGroup )
53
+ foreach ( var target in fieldsToDelete )
50
54
{
51
55
rewriter . Remove ( target ) ;
52
56
}
@@ -76,15 +80,18 @@ public static void RemoveMember(this IModuleRewriter rewriter, ModuleBodyElement
76
80
/// </remarks>
77
81
public static void RemoveMembers ( this IModuleRewriter rewriter , IEnumerable < ModuleBodyElementDeclaration > toRemove , bool removeEndOfStmtContext = true )
78
82
{
79
- if ( ! toRemove . Any ( ) ) { return ; }
83
+ if ( ! toRemove . Any ( ) )
84
+ {
85
+ return ;
86
+ }
80
87
81
88
foreach ( var member in toRemove )
82
89
{
83
- rewriter . RemoveDeclaration < VBAParser . ModuleBodyElementContext > ( member , removeEndOfStmtContext ) ;
90
+ rewriter . RemoveDeclarationContext < VBAParser . ModuleBodyElementContext > ( member , removeEndOfStmtContext ) ;
84
91
}
85
92
}
86
93
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
88
95
{
89
96
if ( ! declaration . Context . TryGetAncestor < T > ( out var elementContext ) )
90
97
{
0 commit comments