@@ -100,7 +100,7 @@ private void InsertLocalVariableDeclarationAndAssignment()
100
100
{
101
101
string [ ] lines = { BuildLocalCopyDeclaration ( ) , BuildLocalCopyAssignment ( ) } ;
102
102
var module = Selection . QualifiedName . Component . CodeModule ;
103
- module . InsertLines ( ( ( VBAParser . ArgListContext ) _target . Context . Parent ) . Stop . Line + 1 , lines ) ;
103
+ module . InsertLines ( ( ( VBAParser . ArgListContext ) _target . Context . Parent ) . Stop . Line + 1 , lines ) ;
104
104
}
105
105
106
106
private string BuildLocalCopyDeclaration ( )
@@ -116,50 +116,54 @@ private string BuildLocalCopyAssignment()
116
116
117
117
private IEnumerable < string > GetIdentifierNamesAccessibleToProcedureContext ( )
118
118
{
119
- var allSameProcedureDeclarations = _parserState . AllUserDeclarations
120
- . Where ( item => item . ParentScope == _target . ParentScope )
121
- . ToList ( ) ;
122
-
123
- var sameModuleDeclarations = _parserState . AllUserDeclarations
124
- . Where ( item => item . ComponentName == _target . ComponentName
125
- && ! IsDeclaredInMethodOrProperty ( item . ParentDeclaration . Context ) )
126
- . ToList ( ) ;
127
-
128
- var allGloballyAccessibleDeclarations = _parserState . AllUserDeclarations
129
- . Where ( item => item . ProjectName == _target . ProjectName
130
- && ! ( item . ParentScopeDeclaration is ClassModuleDeclaration )
131
- && ( item . Accessibility == Accessibility . Public
132
- || ( ( item . Accessibility == Accessibility . Implicit )
133
- && ( item . ParentScopeDeclaration is ProceduralModuleDeclaration ) ) ) )
134
- . ToList ( ) ;
135
-
136
- var accessibleIdentifierNames = new List < string > ( ) ;
137
- accessibleIdentifierNames . AddRange ( allSameProcedureDeclarations . Select ( d => d . IdentifierName ) ) ;
138
- accessibleIdentifierNames . AddRange ( sameModuleDeclarations . Select ( d => d . IdentifierName ) ) ;
139
- accessibleIdentifierNames . AddRange ( allGloballyAccessibleDeclarations . Select ( d => d . IdentifierName ) ) ;
140
-
141
- return accessibleIdentifierNames . Distinct ( ) ;
119
+ return _parserState . AllUserDeclarations
120
+ . Where ( candidateDeclaration =>
121
+ (
122
+ IsDeclarationInTheSameProcedure ( candidateDeclaration , _target )
123
+ || IsDeclarationInTheSameModule ( candidateDeclaration , _target )
124
+ || IsProjectGlobalDeclaration ( candidateDeclaration , _target ) )
125
+ ) . Select ( declaration => declaration . IdentifierName ) . Distinct ( ) ;
142
126
}
143
127
144
- private bool IsDeclaredInMethodOrProperty ( RuleContext context )
128
+ private bool IsDeclarationInTheSameProcedure ( Declaration candidateDeclaration , Declaration scopingDeclaration )
145
129
{
146
- if ( context is VBAParser . SubStmtContext )
130
+ return candidateDeclaration . ParentScope == scopingDeclaration . ParentScope ;
131
+ }
132
+
133
+ private bool IsDeclarationInTheSameModule ( Declaration candidateDeclaration , Declaration scopingDeclaration )
134
+ {
135
+ return candidateDeclaration . ComponentName == scopingDeclaration . ComponentName
136
+ && ! IsDeclaredInMethodOrProperty ( candidateDeclaration . ParentDeclaration . Context ) ;
137
+ }
138
+
139
+ private bool IsProjectGlobalDeclaration ( Declaration candidateDeclaration , Declaration scopingDeclaration )
140
+ {
141
+ return candidateDeclaration . ProjectName == scopingDeclaration . ProjectName
142
+ && ! ( candidateDeclaration . ParentScopeDeclaration is ClassModuleDeclaration )
143
+ && ( candidateDeclaration . Accessibility == Accessibility . Public
144
+ || ( ( candidateDeclaration . Accessibility == Accessibility . Implicit )
145
+ && ( candidateDeclaration . ParentScopeDeclaration is ProceduralModuleDeclaration ) ) ) ;
146
+ }
147
+
148
+ private bool IsDeclaredInMethodOrProperty ( RuleContext procedureContext )
149
+ {
150
+ if ( procedureContext is VBAParser . SubStmtContext )
147
151
{
148
152
return true ;
149
153
}
150
- else if ( context is VBAParser . FunctionStmtContext )
154
+ else if ( procedureContext is VBAParser . FunctionStmtContext )
151
155
{
152
156
return true ;
153
157
}
154
- else if ( context is VBAParser . PropertyLetStmtContext )
158
+ else if ( procedureContext is VBAParser . PropertyLetStmtContext )
155
159
{
156
160
return true ;
157
161
}
158
- else if ( context is VBAParser . PropertyGetStmtContext )
162
+ else if ( procedureContext is VBAParser . PropertyGetStmtContext )
159
163
{
160
164
return true ;
161
165
}
162
- else if ( context is VBAParser . PropertySetStmtContext )
166
+ else if ( procedureContext is VBAParser . PropertySetStmtContext )
163
167
{
164
168
return true ;
165
169
}
0 commit comments