4
4
using Rubberduck . Parsing ;
5
5
using Rubberduck . Parsing . Grammar ;
6
6
using Rubberduck . Parsing . Symbols ;
7
- using Rubberduck . Parsing . VBA ;
8
7
using Rubberduck . UI ;
9
8
using Rubberduck . VBEditor ;
10
9
using Rubberduck . VBEditor . Extensions ;
@@ -106,7 +105,6 @@ private void AdjustReferences(IEnumerable<IdentifierReference> references)
106
105
{
107
106
foreach ( var reference in references . Where ( item => item . Context != _model . TargetDeclaration . Context ) )
108
107
{
109
- dynamic proc = reference . Context ;
110
108
var module = reference . QualifiedModuleName . Component . CodeModule ;
111
109
VBAParser . ArgumentListContext argumentList = null ;
112
110
var callStmt = ParserRuleContextHelper . GetParent < VBAParser . CallStmtContext > ( reference . Context ) ;
@@ -121,55 +119,52 @@ private void AdjustReferences(IEnumerable<IdentifierReference> references)
121
119
122
120
private void RewriteCall ( VBAParser . ArgumentListContext paramList , CodeModule module )
123
121
{
124
- List < string > paramNames = new List < string > ( ) ;
122
+ var argValues = new List < string > ( ) ;
125
123
if ( paramList . positionalOrNamedArgumentList ( ) . positionalArgumentOrMissing ( ) != null )
126
124
{
127
- paramNames . AddRange ( paramList . positionalOrNamedArgumentList ( ) . positionalArgumentOrMissing ( ) . Select ( p =>
125
+ argValues . AddRange ( paramList . positionalOrNamedArgumentList ( ) . positionalArgumentOrMissing ( ) . Select ( p =>
128
126
{
129
127
if ( p is VBAParser . SpecifiedPositionalArgumentContext )
130
128
{
131
129
return ( ( VBAParser . SpecifiedPositionalArgumentContext ) p ) . positionalArgument ( ) . GetText ( ) ;
132
130
}
133
- else
134
- {
135
- return string . Empty ;
136
- }
131
+
132
+ return string . Empty ;
137
133
} ) . ToList ( ) ) ;
138
134
}
139
135
if ( paramList . positionalOrNamedArgumentList ( ) . namedArgumentList ( ) != null )
140
136
{
141
- paramNames . AddRange ( paramList . positionalOrNamedArgumentList ( ) . namedArgumentList ( ) . namedArgument ( ) . Select ( p => p . GetText ( ) ) . ToList ( ) ) ;
137
+ argValues . AddRange ( paramList . positionalOrNamedArgumentList ( ) . namedArgumentList ( ) . namedArgument ( ) . Select ( p => p . GetText ( ) ) . ToList ( ) ) ;
142
138
}
143
139
if ( paramList . positionalOrNamedArgumentList ( ) . requiredPositionalArgument ( ) != null )
144
140
{
145
- paramNames . Add ( paramList . positionalOrNamedArgumentList ( ) . requiredPositionalArgument ( ) . GetText ( ) ) ;
141
+ argValues . Add ( paramList . positionalOrNamedArgumentList ( ) . requiredPositionalArgument ( ) . GetText ( ) ) ;
146
142
}
147
143
148
144
var lineCount = paramList . Stop . Line - paramList . Start . Line + 1 ; // adjust for total line count
149
145
150
- var newContent = module . Lines [ paramList . Start . Line , lineCount ] . Replace ( " _" + Environment . NewLine , string . Empty ) . RemoveExtraSpacesLeavingIndentation ( ) ;
151
-
152
- var parameterIndex = 0 ;
153
- var currentStringIndex = 0 ;
146
+ var newContent = module . Lines [ paramList . Start . Line , lineCount ] ;
147
+ newContent = newContent . Remove ( paramList . Start . Column , paramList . GetText ( ) . Length ) ;
154
148
155
- for ( var i = 0 ; i < paramNames . Count && parameterIndex < _model . Parameters . Count ; i ++ )
149
+ var reorderedArgValues = new List < string > ( ) ;
150
+ foreach ( var param in _model . Parameters )
156
151
{
157
- var parameterStringIndex = newContent . IndexOf ( paramNames . ElementAt ( i ) , currentStringIndex , StringComparison . Ordinal ) ;
158
-
159
- if ( parameterStringIndex <= - 1 ) { continue ; }
160
-
161
- var oldParameterString = paramNames . ElementAt ( i ) ;
162
- var newParameterString = paramNames . ElementAt ( _model . Parameters . ElementAt ( parameterIndex ) . Index ) ;
163
- var beginningSub = newContent . Substring ( 0 , parameterStringIndex ) ;
164
- var replaceSub = newContent . Substring ( parameterStringIndex ) . Replace ( oldParameterString , newParameterString ) ;
165
-
166
- newContent = beginningSub + replaceSub ;
152
+ var argAtIndex = argValues . ElementAtOrDefault ( param . Index ) ;
153
+ if ( argAtIndex != null )
154
+ {
155
+ reorderedArgValues . Add ( argAtIndex ) ;
156
+ }
157
+ }
167
158
168
- parameterIndex ++ ;
169
- currentStringIndex = beginningSub . Length + newParameterString . Length ;
159
+ // property let/set and paramarrays
160
+ for ( var index = reorderedArgValues . Count ; index < argValues . Count ; index ++ )
161
+ {
162
+ reorderedArgValues . Add ( argValues [ index ] ) ;
170
163
}
171
164
172
- module . ReplaceLine ( paramList . Start . Line , newContent ) ;
165
+ newContent = newContent . Insert ( paramList . Start . Column , string . Join ( ", " , reorderedArgValues ) ) ;
166
+
167
+ module . ReplaceLine ( paramList . Start . Line , newContent . Replace ( " _" + Environment . NewLine , string . Empty ) ) ;
173
168
module . DeleteLines ( paramList . Start . Line + 1 , lineCount - 1 ) ;
174
169
}
175
170
0 commit comments