9
9
using Rubberduck . Common ;
10
10
using Antlr4 . Runtime ;
11
11
using System . Collections . Generic ;
12
- using Antlr4 . Runtime . Tree ;
13
12
using Rubberduck . Parsing . VBA ;
14
13
15
14
namespace Rubberduck . Inspections . QuickFixes
@@ -28,7 +27,7 @@ public AssignedByValParameterMakeLocalCopyQuickFix(Declaration target, Qualified
28
27
_target = target ;
29
28
_dialogFactory = dialogFactory ;
30
29
_parserState = parserState ;
31
- _forbiddenNames = GetIdentifierNamesAccessibleToProcedureContext ( target . Context . Parent . Parent ) ;
30
+ _forbiddenNames = GetIdentifierNamesAccessibleToProcedureContext ( ) ;
32
31
_localCopyVariableName = ComputeSuggestedName ( ) ;
33
32
}
34
33
@@ -98,16 +97,10 @@ private void ReplaceAssignedByValParameterReferences()
98
97
}
99
98
100
99
private void InsertLocalVariableDeclarationAndAssignment ( )
101
- {
102
- var block = QuickFixHelper . GetBlockStmtContexts ( _target . Context . Parent . Parent ) . FirstOrDefault ( ) ;
103
- if ( block == null )
104
- {
105
- return ;
106
- }
107
-
100
+ {
108
101
string [ ] lines = { BuildLocalCopyDeclaration ( ) , BuildLocalCopyAssignment ( ) } ;
109
102
var module = Selection . QualifiedName . Component . CodeModule ;
110
- module . InsertLines ( block . Start . Line , lines ) ;
103
+ module . InsertLines ( ( ( VBAParser . ArgListContext ) _target . Context . Parent ) . Stop . Line + 1 , lines ) ;
111
104
}
112
105
113
106
private string BuildLocalCopyDeclaration ( )
@@ -121,36 +114,23 @@ private string BuildLocalCopyAssignment()
121
114
+ _localCopyVariableName + " = " + _target . IdentifierName ;
122
115
}
123
116
124
- private IEnumerable < string > GetIdentifierNamesAccessibleToProcedureContext ( RuleContext ruleContext )
117
+ private IEnumerable < string > GetIdentifierNamesAccessibleToProcedureContext ( )
125
118
{
126
119
var allIdentifiers = new HashSet < string > ( ) ;
127
120
128
- //Locally declared variable names
129
- var blocks = QuickFixHelper . GetBlockStmtContexts ( ruleContext ) ;
130
-
131
- var blockStmtIdentifierContexts = GetIdentifierContexts ( blocks ) ;
132
- var blockStmtIdentifiers = GetVariableNamesFromRuleContexts ( blockStmtIdentifierContexts . ToArray ( ) ) ;
133
-
134
- allIdentifiers . UnionWith ( blockStmtIdentifiers ) ;
135
-
136
- //The parameters of the procedure that are unreferenced in the procedure body
137
- var args = QuickFixHelper . GetArgContexts ( ruleContext ) ;
138
-
139
- var potentiallyUnreferencedIdentifierContexts = GetIdentifierContexts ( args ) ;
140
- var potentiallyUnreferencedParameters = GetVariableNamesFromRuleContexts ( potentiallyUnreferencedIdentifierContexts . ToArray ( ) ) ;
121
+ var allParametersAndLocalVariables = _parserState . AllUserDeclarations
122
+ . Where ( item => item . ParentScope == _target . ParentScope )
123
+ . ToList ( ) ;
141
124
142
- allIdentifiers . UnionWith ( potentiallyUnreferencedParameters ) ;
125
+ allIdentifiers . UnionWith ( allParametersAndLocalVariables . Select ( d => d . IdentifierName ) ) ;
143
126
144
- //All declarations within the same module, but outside of all procedures (e.g., member variables, procedure names)
145
127
var sameModuleDeclarations = _parserState . AllUserDeclarations
146
128
. Where ( item => item . ComponentName == _target . ComponentName
147
129
&& ! IsProceduralContext ( item . ParentDeclaration . Context ) )
148
130
. ToList ( ) ;
149
131
150
132
allIdentifiers . UnionWith ( sameModuleDeclarations . Select ( d => d . IdentifierName ) ) ;
151
133
152
- //Public declarations anywhere within the project other than Public members and
153
- //procedures of Class modules
154
134
var allPublicDeclarations = _parserState . AllUserDeclarations
155
135
. Where ( item => ( item . Accessibility == Accessibility . Public
156
136
|| ( ( item . Accessibility == Accessibility . Implicit )
@@ -160,71 +140,7 @@ private IEnumerable<string> GetIdentifierNamesAccessibleToProcedureContext(RuleC
160
140
161
141
allIdentifiers . UnionWith ( allPublicDeclarations . Select ( d => d . IdentifierName ) ) ;
162
142
163
- return allIdentifiers . ToArray ( ) ;
164
- }
165
-
166
- private HashSet < string > GetVariableNamesFromRuleContexts ( RuleContext [ ] ruleContexts )
167
- {
168
- var tokenValues = typeof ( Tokens ) . GetFields ( ) . Select ( item => item . GetValue ( null ) ) . Cast < string > ( ) . Select ( item => item ) ;
169
- var results = new HashSet < string > ( ) ;
170
-
171
- foreach ( var ruleContext in ruleContexts )
172
- {
173
- var name = Identifier . GetName ( ( VBAParser . IdentifierContext ) ruleContext ) ;
174
- if ( ! tokenValues . Contains ( name ) )
175
- {
176
- results . Add ( name ) ;
177
- }
178
- }
179
- return results ;
180
- }
181
-
182
- private HashSet < RuleContext > GetIdentifierContexts ( IReadOnlyList < RuleContext > ruleContexts )
183
- {
184
- var identifiers = new HashSet < RuleContext > ( ) ;
185
- foreach ( RuleContext ruleContext in ruleContexts )
186
- {
187
- var identifiersForThisContext = GetIdentifierContexts ( ruleContext ) ;
188
- identifiers . UnionWith ( identifiersForThisContext ) ;
189
- }
190
- return identifiers ;
191
- }
192
-
193
- private HashSet < RuleContext > GetIdentifierContexts ( RuleContext ruleContext )
194
- {
195
- // note: this looks like something that's already handled somewhere else...
196
-
197
- //Recursively work through the tree to get all IdentifierContexts
198
- var results = new HashSet < RuleContext > ( ) ;
199
- var children = GetChildren ( ruleContext ) ;
200
-
201
- foreach ( var child in children )
202
- {
203
- var context = child as VBAParser . IdentifierContext ;
204
- if ( context != null )
205
- {
206
- //var childName = Identifier.GetName((VBAParser.IdentifierContext)child);
207
- results . Add ( ( RuleContext ) child ) ;
208
- }
209
- else
210
- {
211
- if ( ! ( child is TerminalNodeImpl ) )
212
- {
213
- results . UnionWith ( GetIdentifierContexts ( ( RuleContext ) child ) ) ;
214
- }
215
- }
216
- }
217
- return results ;
218
- }
219
-
220
- private static IEnumerable < IParseTree > GetChildren ( IParseTree tree )
221
- {
222
- var result = new List < IParseTree > ( ) ;
223
- for ( var index = 0 ; index < tree . ChildCount ; index ++ )
224
- {
225
- result . Add ( tree . GetChild ( index ) ) ;
226
- }
227
- return result ;
143
+ return allIdentifiers . ToList ( ) ;
228
144
}
229
145
private bool IsProceduralContext ( RuleContext context )
230
146
{
0 commit comments