|
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,70 +105,76 @@ 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);
|
113 | 111 | if (callStmt != null)
|
114 | 112 | {
|
115 | 113 | argumentList = CallStatement.GetArgumentList(callStmt);
|
116 | 114 | }
|
| 115 | + |
| 116 | + if (argumentList == null) |
| 117 | + { |
| 118 | + var indexExpression = ParserRuleContextHelper.GetParent<VBAParser.IndexExprContext>(reference.Context); |
| 119 | + if (indexExpression != null) |
| 120 | + { |
| 121 | + argumentList = ParserRuleContextHelper.GetChild<VBAParser.ArgumentListContext>(indexExpression); |
| 122 | + } |
| 123 | + } |
| 124 | + |
117 | 125 | if (argumentList == null) { continue; }
|
118 | 126 | RewriteCall(argumentList, module);
|
119 | 127 | }
|
120 | 128 | }
|
121 | 129 |
|
122 | 130 | private void RewriteCall(VBAParser.ArgumentListContext paramList, CodeModule module)
|
123 | 131 | {
|
124 |
| - List<string> paramNames = new List<string>(); |
| 132 | + var argValues = new List<string>(); |
125 | 133 | if (paramList.positionalOrNamedArgumentList().positionalArgumentOrMissing() != null)
|
126 | 134 | {
|
127 |
| - paramNames.AddRange(paramList.positionalOrNamedArgumentList().positionalArgumentOrMissing().Select(p => |
| 135 | + argValues.AddRange(paramList.positionalOrNamedArgumentList().positionalArgumentOrMissing().Select(p => |
128 | 136 | {
|
129 | 137 | if (p is VBAParser.SpecifiedPositionalArgumentContext)
|
130 | 138 | {
|
131 | 139 | return ((VBAParser.SpecifiedPositionalArgumentContext)p).positionalArgument().GetText();
|
132 | 140 | }
|
133 |
| - else |
134 |
| - { |
135 |
| - return string.Empty; |
136 |
| - } |
| 141 | + |
| 142 | + return string.Empty; |
137 | 143 | }).ToList());
|
138 | 144 | }
|
139 | 145 | if (paramList.positionalOrNamedArgumentList().namedArgumentList() != null)
|
140 | 146 | {
|
141 |
| - paramNames.AddRange(paramList.positionalOrNamedArgumentList().namedArgumentList().namedArgument().Select(p => p.GetText()).ToList()); |
| 147 | + argValues.AddRange(paramList.positionalOrNamedArgumentList().namedArgumentList().namedArgument().Select(p => p.GetText()).ToList()); |
142 | 148 | }
|
143 | 149 | if (paramList.positionalOrNamedArgumentList().requiredPositionalArgument() != null)
|
144 | 150 | {
|
145 |
| - paramNames.Add(paramList.positionalOrNamedArgumentList().requiredPositionalArgument().GetText()); |
| 151 | + argValues.Add(paramList.positionalOrNamedArgumentList().requiredPositionalArgument().GetText()); |
146 | 152 | }
|
147 | 153 |
|
148 | 154 | var lineCount = paramList.Stop.Line - paramList.Start.Line + 1; // adjust for total line count
|
149 | 155 |
|
150 |
| - var newContent = module.Lines[paramList.Start.Line, lineCount].Replace(" _" + Environment.NewLine, string.Empty).RemoveExtraSpacesLeavingIndentation(); |
| 156 | + var newContent = module.Lines[paramList.Start.Line, lineCount]; |
| 157 | + newContent = newContent.Remove(paramList.Start.Column, paramList.GetText().Length); |
151 | 158 |
|
152 |
| - var parameterIndex = 0; |
153 |
| - var currentStringIndex = 0; |
154 |
| - |
155 |
| - for (var i = 0; i < paramNames.Count && parameterIndex < _model.Parameters.Count; i++) |
| 159 | + var reorderedArgValues = new List<string>(); |
| 160 | + foreach (var param in _model.Parameters) |
156 | 161 | {
|
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; |
| 162 | + var argAtIndex = argValues.ElementAtOrDefault(param.Index); |
| 163 | + if (argAtIndex != null) |
| 164 | + { |
| 165 | + reorderedArgValues.Add(argAtIndex); |
| 166 | + } |
| 167 | + } |
167 | 168 |
|
168 |
| - parameterIndex++; |
169 |
| - currentStringIndex = beginningSub.Length + newParameterString.Length; |
| 169 | + // property let/set and paramarrays |
| 170 | + for (var index = reorderedArgValues.Count; index < argValues.Count; index++) |
| 171 | + { |
| 172 | + reorderedArgValues.Add(argValues[index]); |
170 | 173 | }
|
171 | 174 |
|
172 |
| - module.ReplaceLine(paramList.Start.Line, newContent); |
| 175 | + newContent = newContent.Insert(paramList.Start.Column, string.Join(", ", reorderedArgValues)); |
| 176 | + |
| 177 | + module.ReplaceLine(paramList.Start.Line, newContent.Replace(" _" + Environment.NewLine, string.Empty)); |
173 | 178 | module.DeleteLines(paramList.Start.Line + 1, lineCount - 1);
|
174 | 179 | }
|
175 | 180 |
|
@@ -244,34 +249,33 @@ private void AdjustSignatures(Declaration declaration)
|
244 | 249 |
|
245 | 250 | private void RewriteSignature(Declaration target, VBAParser.ArgListContext paramList, CodeModule module)
|
246 | 251 | {
|
247 |
| - var argList = paramList.arg(); |
248 |
| - |
249 |
| - var newContent = GetOldSignature(target); |
250 |
| - var lineNum = paramList.GetSelection().LineCount; |
| 252 | + var parameters = paramList.arg().Select((s, i) => new {Index = i, Text = s.GetText()}).ToList(); |
251 | 253 |
|
252 |
| - var parameterIndex = 0; |
253 |
| - var currentStringIndex = 0; |
254 |
| - |
255 |
| - for (var i = parameterIndex; i < _model.Parameters.Count; i++) |
| 254 | + var reorderedParams = new List<string>(); |
| 255 | + foreach (var param in _model.Parameters) |
256 | 256 | {
|
257 |
| - var oldParam = argList.ElementAt(parameterIndex).GetText(); |
258 |
| - var newParam = argList.ElementAt(_model.Parameters.ElementAt(parameterIndex).Index).GetText(); |
259 |
| - var parameterStringIndex = newContent.IndexOf(oldParam, currentStringIndex, StringComparison.Ordinal); |
260 |
| - |
261 |
| - if (parameterStringIndex > -1) |
| 257 | + var parameterAtIndex = parameters.SingleOrDefault(s => s.Index == param.Index); |
| 258 | + if (parameterAtIndex != null) |
262 | 259 | {
|
263 |
| - var beginningSub = newContent.Substring(0, parameterStringIndex); |
264 |
| - var replaceSub = newContent.Substring(parameterStringIndex).Replace(oldParam, newParam); |
265 |
| - |
266 |
| - newContent = beginningSub + replaceSub; |
267 |
| - |
268 |
| - parameterIndex++; |
269 |
| - currentStringIndex = beginningSub.Length + newParam.Length; |
| 260 | + reorderedParams.Add(parameterAtIndex.Text); |
270 | 261 | }
|
271 | 262 | }
|
272 | 263 |
|
273 |
| - module.ReplaceLine(paramList.Start.Line, newContent.Replace(" _" + Environment.NewLine, string.Empty)); |
274 |
| - module.DeleteLines(paramList.Start.Line + 1, lineNum - 1); |
| 264 | + // property let/set and paramarrays |
| 265 | + for (var index = reorderedParams.Count; index < parameters.Count; index++) |
| 266 | + { |
| 267 | + reorderedParams.Add(parameters[index].Text); |
| 268 | + } |
| 269 | + |
| 270 | + var signature = GetOldSignature(target); |
| 271 | + signature = signature.Remove(signature.IndexOf('(')); |
| 272 | + |
| 273 | + var asTypeText = target.AsTypeContext == null ? string.Empty : " " + target.AsTypeContext.GetText(); |
| 274 | + signature += '(' + string.Join(", ", reorderedParams) + ")" + (asTypeText == " " ? string.Empty : asTypeText); |
| 275 | + |
| 276 | + var lineCount = paramList.GetSelection().LineCount; |
| 277 | + module.ReplaceLine(paramList.Start.Line, signature.Replace(" _" + Environment.NewLine, string.Empty)); |
| 278 | + module.DeleteLines(paramList.Start.Line + 1, lineCount - 1); |
275 | 279 | }
|
276 | 280 |
|
277 | 281 | private string GetOldSignature(Declaration target)
|
|
0 commit comments