5
5
using Rubberduck . Parsing . Symbols ;
6
6
using Rubberduck . UI . Refactorings ;
7
7
using Rubberduck . Common ;
8
- using System . Collections . Generic ;
9
8
using System . Windows . Forms ;
10
9
using Antlr4 . Runtime ;
11
10
using Rubberduck . Inspections . Abstract ;
@@ -36,9 +35,8 @@ public override void Fix(IInspectionResult result)
36
35
Debug . Assert ( null != ( ( ParserRuleContext ) result . Target . Context . Parent . Parent ) . GetChild < VBAParser . EndOfStatementContext > ( ) ) ;
37
36
38
37
_quickFixTarget = result . Target ;
39
- //var forbiddenNames = _parserState.DeclarationFinder.GetDeclarationsWithIdentifiersToAvoid(result.Target).Select(n => n.IdentifierName);
40
38
41
- var localIdentifier = PromptForLocalVariableName ( result . Target ) ; //, forbiddenNames.ToList());
39
+ var localIdentifier = PromptForLocalVariableName ( result . Target ) ;
42
40
if ( string . IsNullOrEmpty ( localIdentifier ) )
43
41
{
44
42
return ;
@@ -55,16 +53,16 @@ public override void Fix(IInspectionResult result)
55
53
public override bool CanFixInModule => false ;
56
54
public override bool CanFixInProject => false ;
57
55
58
- private string PromptForLocalVariableName ( Declaration target ) //, List<string> forbiddenNames)
56
+ private string PromptForLocalVariableName ( Declaration target )
59
57
{
60
58
IAssignedByValParameterQuickFixDialog view = null ;
61
59
try
62
60
{
63
- view = _dialogFactory . Create ( target . IdentifierName , target . DeclarationType . ToString ( ) , IsNameCollision ) ; //, forbiddenNames);
64
- view . NewName = GetDefaultLocalIdentifier ( target ) ; //, forbiddenNames);
61
+ view = _dialogFactory . Create ( target . IdentifierName , target . DeclarationType . ToString ( ) , IsNameCollision ) ;
62
+ view . NewName = GetDefaultLocalIdentifier ( target ) ;
65
63
view . ShowDialog ( ) ;
66
64
67
- if ( view . DialogResult == DialogResult . Cancel || ! IsValidVariableName ( view . NewName ) ) //, forbiddenNames))
65
+ if ( view . DialogResult == DialogResult . Cancel || ! IsValidVariableName ( view . NewName ) )
68
66
{
69
67
return string . Empty ;
70
68
}
@@ -78,42 +76,7 @@ private string PromptForLocalVariableName(Declaration target) //, List<string> f
78
76
}
79
77
80
78
private bool IsNameCollision ( string newName )
81
- {
82
- var conflicts = _parserState . DeclarationFinder . NewDeclarationNameConflictsWithExisting ( newName , _quickFixTarget ) ;
83
- return conflicts . Any ( ) ;
84
- //bool hasConflict = false;
85
-
86
- //var nameCollisions = _parserState.DeclarationFinder.MatchName(newName);
87
- //if (_quickFixTarget.DeclarationType == DeclarationType.EnumerationMember
88
- // || _quickFixTarget.DeclarationType == DeclarationType.UserDefinedTypeMember)
89
- //{
90
- // nameCollisions = nameCollisions
91
- // .Where(nc => nc.ParentDeclaration == _quickFixTarget.ParentDeclaration);
92
- //}
93
- //else
94
- //{
95
- // nameCollisions = _parserState.DeclarationFinder.MatchName(newName)
96
- // .Where(nc => nc.DeclarationType != DeclarationType.EnumerationMember
97
- // && nc.DeclarationType != DeclarationType.UserDefinedTypeMember);
98
- //}
99
-
100
- //if (nameCollisions.Any())
101
- //{
102
- // foreach (var conflictDec in nameCollisions)
103
- // {
104
- // var accessible = _parserState.DeclarationFinder.GetAccessibleUserDeclarations(_quickFixTarget);
105
- // if (accessible.Contains(conflictDec))
106
- // {
107
- // hasConflict = true;
108
- // }
109
- // else if (IsSubroutineOrProperty(_quickFixTarget))
110
- // {
111
- // hasConflict = conflictDec.ParentDeclaration == _quickFixTarget;
112
- // }
113
- // }
114
- //}
115
- //return hasConflict;
116
- }
79
+ => _parserState . DeclarationFinder . FindNewDeclarationNameConflicts ( newName , _quickFixTarget ) . Any ( ) ;
117
80
118
81
private static bool IsSubroutineOrProperty ( Declaration declaration )
119
82
{
@@ -122,30 +85,29 @@ private static bool IsSubroutineOrProperty(Declaration declaration)
122
85
|| declaration . DeclarationType == DeclarationType . Procedure ;
123
86
}
124
87
125
- private string GetDefaultLocalIdentifier ( Declaration target ) //, List<string> forbiddenNames)
88
+ private string GetDefaultLocalIdentifier ( Declaration target )
126
89
{
127
90
var newName = $ "local{ target . IdentifierName . CapitalizeFirstLetter ( ) } ";
128
- if ( IsValidVariableName ( newName ) ) //, forbiddenNames))
91
+ if ( IsValidVariableName ( newName ) )
129
92
{
130
93
return newName ;
131
94
}
132
95
133
96
for ( var attempt = 2 ; attempt < 10 ; attempt ++ )
134
97
{
135
98
var result = newName + attempt ;
136
- if ( IsValidVariableName ( result ) ) //, forbiddenNames))
99
+ if ( IsValidVariableName ( result ) )
137
100
{
138
101
return result ;
139
102
}
140
103
}
141
104
return newName ;
142
105
}
143
106
144
- private bool IsValidVariableName ( string variableName ) //, IEnumerable<string> forbiddenNames)
107
+ private bool IsValidVariableName ( string variableName )
145
108
{
146
109
return VariableNameValidator . IsValidName ( variableName )
147
- && ! IsNameCollision ( variableName ) ; //, StringComparison.InvariantCultureIgnoreCase));
148
- //&& !forbiddenNames.Any(name => name.Equals(variableName, StringComparison.InvariantCultureIgnoreCase));
110
+ && ! IsNameCollision ( variableName ) ;
149
111
}
150
112
151
113
private void ReplaceAssignedByValParameterReferences ( IModuleRewriter rewriter , Declaration target , string localIdentifier )
0 commit comments