1
1
using Rubberduck . Inspections . Abstract ;
2
2
using System . Linq ;
3
- using Rubberduck . Parsing ;
4
3
using Rubberduck . VBEditor ;
5
4
using Rubberduck . Inspections . Resources ;
6
5
using Rubberduck . Parsing . Grammar ;
@@ -19,18 +18,27 @@ public class AssignedByValParameterMakeLocalCopyQuickFix : QuickFixBase
19
18
{
20
19
private readonly Declaration _target ;
21
20
private readonly IAssignedByValParameterQuickFixDialogFactory _dialogFactory ;
21
+ //<<<<<<< HEAD
22
22
private readonly RubberduckParserState _parserState ;
23
+ // private string[] _variableNamesAccessibleToProcedureContext;
24
+ //=======
25
+ private readonly IEnumerable < string > _forbiddenNames ;
26
+ //>>>>>>> rubberduck-vba/next
23
27
private string _localCopyVariableName ;
24
- private string [ ] _variableNamesAccessibleToProcedureContext ;
25
28
26
29
public AssignedByValParameterMakeLocalCopyQuickFix ( Declaration target , QualifiedSelection selection , RubberduckParserState parserState , IAssignedByValParameterQuickFixDialogFactory dialogFactory )
27
30
: base ( target . Context , selection , InspectionsUI . AssignedByValParameterMakeLocalCopyQuickFix )
28
31
{
29
32
_target = target ;
30
33
_dialogFactory = dialogFactory ;
34
+ //<<<<<<< HEAD
31
35
_parserState = parserState ;
32
- _variableNamesAccessibleToProcedureContext = GetUserDefinedNamesAccessibleToProcedureContext ( _target . Context . Parent . Parent ) ;
33
- SetValidLocalCopyVariableNameSuggestion ( ) ;
36
+ //_variableNamesAccessibleToProcedureContext = GetUserDefinedNamesAccessibleToProcedureContext(_target.Context.Parent.Parent);
37
+ //SetValidLocalCopyVariableNameSuggestion();
38
+ //=======
39
+ _forbiddenNames = GetIdentifierNamesAccessibleToProcedureContext ( target . Context . Parent . Parent ) ;
40
+ _localCopyVariableName = ComputeSuggestedName ( ) ;
41
+ //>>>>>>> rubberduck-vba/next
34
42
}
35
43
36
44
public override bool CanFixInModule { get { return false ; } }
@@ -52,10 +60,9 @@ public override void Fix()
52
60
53
61
private void RequestLocalCopyVariableName ( )
54
62
{
55
- using ( var view = _dialogFactory . Create ( _target . IdentifierName , _target . DeclarationType . ToString ( ) ) )
63
+ using ( var view = _dialogFactory . Create ( _target . IdentifierName , _target . DeclarationType . ToString ( ) , _forbiddenNames ) )
56
64
{
57
65
view . NewName = _localCopyVariableName ;
58
- view . IdentifierNamesAlreadyDeclared = _variableNamesAccessibleToProcedureContext ;
59
66
view . ShowDialog ( ) ;
60
67
IsCancelled = view . DialogResult == DialogResult . Cancel ;
61
68
if ( ! IsCancelled )
@@ -65,63 +72,73 @@ private void RequestLocalCopyVariableName()
65
72
}
66
73
}
67
74
68
- private void SetValidLocalCopyVariableNameSuggestion ( )
75
+ private string ComputeSuggestedName ( )
69
76
{
70
- _localCopyVariableName = "x" + _target . IdentifierName . CapitalizeFirstLetter ( ) ;
71
- if ( VariableNameIsValid ( _localCopyVariableName ) ) { return ; }
77
+ var newName = "local" + _target . IdentifierName . CapitalizeFirstLetter ( ) ;
78
+ if ( VariableNameIsValid ( newName ) )
79
+ {
80
+ return newName ;
81
+ }
72
82
73
- //If the initial suggestion is not valid, keep pre-pending x's until it is
74
- for ( int attempt = 2 ; attempt < 10 ; attempt ++ )
83
+ for ( var attempt = 2 ; attempt < 10 ; attempt ++ )
75
84
{
76
- _localCopyVariableName = "x" + _localCopyVariableName ;
77
- if ( VariableNameIsValid ( _localCopyVariableName ) )
85
+ var result = newName + attempt ;
86
+ if ( VariableNameIsValid ( result ) )
78
87
{
79
- return ;
88
+ return result ;
80
89
}
81
90
}
82
- //if "xxFoo" to "xxxxxxxxxxFoo" isn't unique, give up and go with the original suggestion.
83
- //The QuickFix will leave the code as-is unless it receives a name that is free of conflicts
84
- _localCopyVariableName = "x" + _target . IdentifierName . CapitalizeFirstLetter ( ) ;
91
+ return newName ;
85
92
}
86
93
87
94
private bool VariableNameIsValid ( string variableName )
88
95
{
89
- var validator = new VariableNameValidator ( variableName ) ;
90
- return validator . IsValidName ( )
91
- && ! _variableNamesAccessibleToProcedureContext
92
- . Any ( name => name . Equals ( variableName , System . StringComparison . InvariantCultureIgnoreCase ) ) ;
96
+ return VariableNameValidator . IsValidName ( variableName )
97
+ && ! _forbiddenNames . Any ( name => name . Equals ( variableName , System . StringComparison . InvariantCultureIgnoreCase ) ) ;
93
98
}
94
99
95
100
private void ReplaceAssignedByValParameterReferences ( )
96
101
{
97
102
var module = Selection . QualifiedName . Component . CodeModule ;
98
- foreach ( IdentifierReference identifierReference in _target . References )
103
+ foreach ( var identifierReference in _target . References )
99
104
{
100
105
module . ReplaceIdentifierReferenceName ( identifierReference , _localCopyVariableName ) ;
101
106
}
102
107
}
103
108
104
109
private void InsertLocalVariableDeclarationAndAssignment ( )
105
110
{
106
- var blocks = QuickFixHelper . GetBlockStmtContexts ( _target . Context . Parent . Parent ) ;
111
+ //<<<<<<< HEAD
112
+ //var blocks = QuickFixHelper.GetBlockStmtContexts(_target.Context.Parent.Parent);
113
+ //=======
114
+ var block = QuickFixHelper . GetBlockStmtContexts ( _target . Context . Parent . Parent ) . FirstOrDefault ( ) ;
115
+ if ( block == null )
116
+ {
117
+ return ;
118
+ }
119
+
120
+ //>>>>>>> rubberduck-vba/next
107
121
string [ ] lines = { BuildLocalCopyDeclaration ( ) , BuildLocalCopyAssignment ( ) } ;
108
122
var module = Selection . QualifiedName . Component . CodeModule ;
109
- module . InsertLines ( blocks . FirstOrDefault ( ) . Start . Line , lines ) ;
123
+ module . InsertLines ( block . Start . Line , lines ) ;
110
124
}
111
125
112
126
private string BuildLocalCopyDeclaration ( )
113
127
{
114
- return Tokens . Dim + " " + _localCopyVariableName + " " + Tokens . As
115
- + " " + _target . AsTypeName ;
128
+ return Tokens . Dim + " " + _localCopyVariableName + " " + Tokens . As + " " + _target . AsTypeName ;
116
129
}
117
130
118
131
private string BuildLocalCopyAssignment ( )
119
132
{
120
- return ( SymbolList . ValueTypes . Contains ( _target . AsTypeName ) ? string . Empty : Tokens . Set + " " )
133
+ return ( _target . AsTypeDeclaration is ClassModuleDeclaration ? Tokens . Set + " " : string . Empty )
121
134
+ _localCopyVariableName + " = " + _target . IdentifierName ;
122
135
}
123
136
124
- private string [ ] GetUserDefinedNamesAccessibleToProcedureContext ( RuleContext ruleContext )
137
+ //<<<<<<< HEAD
138
+ // private string[] GetUserDefinedNamesAccessibleToProcedureContext(RuleContext ruleContext)
139
+ //=======
140
+ private IEnumerable < string > GetIdentifierNamesAccessibleToProcedureContext ( RuleContext ruleContext )
141
+ //>>>>>>> rubberduck-vba/next
125
142
{
126
143
var allIdentifiers = new HashSet < string > ( ) ;
127
144
@@ -163,6 +180,7 @@ private string[] GetUserDefinedNamesAccessibleToProcedureContext(RuleContext rul
163
180
return allIdentifiers . ToArray ( ) ;
164
181
}
165
182
183
+ //<<<<<<< HEAD
166
184
private HashSet < string > GetVariableNamesFromRuleContexts ( RuleContext [ ] ruleContexts )
167
185
{
168
186
var tokenValues = typeof ( Tokens ) . GetFields ( ) . Select ( item => item . GetValue ( null ) ) . Cast < string > ( ) . Select ( item => item ) ;
@@ -185,23 +203,50 @@ private HashSet<RuleContext> GetIdentifierContexts(IReadOnlyList<RuleContext> ru
185
203
foreach ( RuleContext ruleContext in ruleContexts )
186
204
{
187
205
var identifiersForThisContext = GetIdentifierContexts ( ruleContext ) ;
206
+ //=======
207
+ // private IEnumerable<string> GetIdentifierNames(IEnumerable<RuleContext> ruleContexts)
208
+ // {
209
+ // var identifiers = new HashSet<string>();
210
+ // foreach (var identifiersForThisContext in ruleContexts.Select(GetIdentifierNames))
211
+ // {
212
+ //>>>>>>> rubberduck-vba/next
188
213
identifiers . UnionWith ( identifiersForThisContext ) ;
189
214
}
190
215
return identifiers ;
191
216
}
192
217
218
+ //<<<<<<< HEAD
193
219
private HashSet < RuleContext > GetIdentifierContexts ( RuleContext ruleContext )
220
+ //=======
221
+ // private static HashSet<string> GetIdentifierNames(RuleContext ruleContext)
222
+ //>>>>>>> rubberduck-vba/next
194
223
{
224
+ // note: this looks like something that's already handled somewhere else...
225
+
195
226
//Recursively work through the tree to get all IdentifierContexts
227
+ //<<<<<<< HEAD
196
228
var results = new HashSet < RuleContext > ( ) ;
229
+ //=======
230
+ // var results = new HashSet<string>();
231
+ // var tokenValues = typeof(Tokens).GetFields().Select(item => item.GetValue(null)).Cast<string>().Select(item => item).ToArray();
232
+ //>>>>>>> rubberduck-vba/next
197
233
var children = GetChildren ( ruleContext ) ;
198
234
199
- foreach ( IParseTree child in children )
235
+ foreach ( var child in children )
200
236
{
201
- if ( child is VBAParser . IdentifierContext )
237
+ var context = child as VBAParser . IdentifierContext ;
238
+ if ( context != null )
202
239
{
240
+ //<<<<<<< HEAD
203
241
var childName = Identifier . GetName ( ( VBAParser . IdentifierContext ) child ) ;
204
242
results . Add ( ( RuleContext ) child ) ;
243
+ //=======
244
+ // var childName = Identifier.GetName(context);
245
+ // if (!tokenValues.Contains(childName))
246
+ // {
247
+ // results.Add(childName);
248
+ // }
249
+ //>>>>>>> rubberduck-vba/next
205
250
}
206
251
else
207
252
{
@@ -214,12 +259,12 @@ private HashSet<RuleContext> GetIdentifierContexts(RuleContext ruleContext)
214
259
return results ;
215
260
}
216
261
217
- private static List < IParseTree > GetChildren ( RuleContext ruleCtx )
262
+ private static IEnumerable < IParseTree > GetChildren ( IParseTree tree )
218
263
{
219
264
var result = new List < IParseTree > ( ) ;
220
- for ( int index = 0 ; index < ruleCtx . ChildCount ; index ++ )
265
+ for ( var index = 0 ; index < tree . ChildCount ; index ++ )
221
266
{
222
- result . Add ( ruleCtx . GetChild ( index ) ) ;
267
+ result . Add ( tree . GetChild ( index ) ) ;
223
268
}
224
269
return result ;
225
270
}
0 commit comments