Skip to content

Commit 3fbaef7

Browse files
committed
Fix reorder params with parens around caller
1 parent 8941329 commit 3fbaef7

File tree

2 files changed

+71
-0
lines changed

2 files changed

+71
-0
lines changed

RetailCoder.VBE/Refactorings/ReorderParameters/ReorderParametersRefactoring.cs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -112,6 +112,16 @@ private void AdjustReferences(IEnumerable<IdentifierReference> references)
112112
{
113113
argumentList = CallStatement.GetArgumentList(callStmt);
114114
}
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+
115125
if (argumentList == null) { continue; }
116126
RewriteCall(argumentList, module);
117127
}

RubberduckTests/Refactoring/ReorderParametersTests.cs

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -352,6 +352,67 @@ End Sub
352352
Assert.AreEqual(expectedCode, module.Lines());
353353
}
354354

355+
[TestMethod]
356+
public void RemoveParametersRefactoring_ClientReferencesAreUpdated_ParensAroundCall()
357+
{
358+
//Input
359+
const string inputCode =
360+
@"Private Sub bar()
361+
Dim x As Integer
362+
Dim y As Integer
363+
y = foo(x, 42)
364+
Debug.Print y, x
365+
End Sub
366+
367+
Private Function foo(ByRef a As Integer, ByVal b As Integer) As Integer
368+
a = b
369+
foo = a + b
370+
End Function";
371+
var selection = new Selection(8, 20, 8, 20);
372+
373+
//Expectation
374+
const string expectedCode =
375+
@"Private Sub bar()
376+
Dim x As Integer
377+
Dim y As Integer
378+
y = foo(42, x)
379+
Debug.Print y, x
380+
End Sub
381+
382+
Private Function foo(ByVal b As Integer, ByRef a As Integer) As Integer
383+
a = b
384+
foo = a + b
385+
End Function";
386+
387+
//Arrange
388+
var builder = new MockVbeBuilder();
389+
VBComponent component;
390+
var vbe = builder.BuildFromSingleStandardModule(inputCode, out component, selection);
391+
var project = vbe.Object.VBProjects.Item(0);
392+
var module = project.VBComponents.Item(0).CodeModule;
393+
var mockHost = new Mock<IHostApplication>();
394+
mockHost.SetupAllProperties();
395+
var parser = MockParser.Create(vbe.Object, new RubberduckParserState(new Mock<ISinks>().Object));
396+
397+
parser.Parse(new CancellationTokenSource());
398+
if (parser.State.Status >= ParserState.Error) { Assert.Inconclusive("Parser Error"); }
399+
400+
var qualifiedSelection = new QualifiedSelection(new QualifiedModuleName(component), selection);
401+
402+
//set up model
403+
var model = new ReorderParametersModel(parser.State, qualifiedSelection, null);
404+
model.Parameters.Reverse();
405+
406+
var factory = SetupFactory(model);
407+
408+
//act
409+
var refactoring = new ReorderParametersRefactoring(vbe.Object, factory.Object, null);
410+
refactoring.Refactor(qualifiedSelection);
411+
412+
//assert
413+
Assert.AreEqual(expectedCode, module.Lines());
414+
}
415+
355416
[TestMethod]
356417
public void ReorderParametersRefactoring_ReorderNamedParams()
357418
{

0 commit comments

Comments
 (0)