Skip to content

Commit cb279a5

Browse files
committed
Correctly remove parameters from contexts resolved into "IndexExpression"s (i.e. calls with paramers around the args).
1 parent 86d006b commit cb279a5

File tree

3 files changed

+91
-0
lines changed

3 files changed

+91
-0
lines changed

RetailCoder.VBE/Refactorings/RemoveParameters/RemoveParametersRefactoring.cs

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
using System;
22
using System.Collections.Generic;
33
using System.Linq;
4+
using Antlr4.Runtime;
45
using Antlr4.Runtime.Misc;
56
using Microsoft.Vbe.Interop;
67
using Rubberduck.Common;
@@ -103,6 +104,16 @@ private void AdjustReferences(IEnumerable<IdentifierReference> references, Decla
103104
{
104105
argumentList = CallStatement.GetArgumentList(callStmt);
105106
}
107+
108+
if (argumentList == null)
109+
{
110+
var indexExpression = ParserRuleContextHelper.GetParent<VBAParser.IndexExprContext>(reference.Context);
111+
if (indexExpression != null)
112+
{
113+
argumentList = ParserRuleContextHelper.GetChild<VBAParser.ArgumentListContext>(indexExpression);
114+
}
115+
}
116+
106117
if (argumentList == null) { continue; }
107118
RemoveCallParameter(argumentList, module);
108119
}

Rubberduck.Parsing/Symbols/ParserRuleContextHelper.cs

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,5 +47,24 @@ public static string GetText(ParserRuleContext context, ICharStream stream)
4747
// Gets the original source, without "synthetic" text such as "<EOF>".
4848
return stream.GetText(new Interval(context.Start.StartIndex, context.Stop.StopIndex));
4949
}
50+
51+
public static T GetChild<T>(RuleContext context)
52+
{
53+
if (context == null)
54+
{
55+
return default(T);
56+
}
57+
58+
for (var index = 0; index < context.ChildCount; index++)
59+
{
60+
var child = context.GetChild(index);
61+
if (context.GetChild(index) is T)
62+
{
63+
return (T)child;
64+
}
65+
}
66+
67+
return default(T);
68+
}
5069
}
5170
}

RubberduckTests/Refactoring/RemoveParametersTests.cs

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -675,6 +675,67 @@ End Sub
675675
Assert.AreEqual(expectedCode, module.Lines());
676676
}
677677

678+
[TestMethod]
679+
public void RemoveParametersRefactoring_ClientReferencesAreUpdated_FirstParam_ParensAroundCall()
680+
{
681+
//Input
682+
const string inputCode =
683+
@"Private Sub bar()
684+
Dim x As Integer
685+
Dim y As Integer
686+
y = foo(x, 42)
687+
Debug.Print y, x
688+
End Sub
689+
690+
Private Function foo(ByRef a As Integer, ByVal b As Integer) As Integer
691+
a = b
692+
foo = a + b
693+
End Function";
694+
var selection = new Selection(8, 20, 8, 20);
695+
696+
//Expectation
697+
const string expectedCode =
698+
@"Private Sub bar()
699+
Dim x As Integer
700+
Dim y As Integer
701+
y = foo(42)
702+
Debug.Print y, x
703+
End Sub
704+
705+
Private Function foo(ByVal b As Integer) As Integer
706+
a = b
707+
foo = a + b
708+
End Function";
709+
710+
//Arrange
711+
var builder = new MockVbeBuilder();
712+
VBComponent component;
713+
var vbe = builder.BuildFromSingleStandardModule(inputCode, out component, selection);
714+
var module = component.CodeModule;
715+
var mockHost = new Mock<IHostApplication>();
716+
mockHost.SetupAllProperties();
717+
var parser = MockParser.Create(vbe.Object, new RubberduckParserState(new Mock<ISinks>().Object));
718+
719+
parser.Parse(new CancellationTokenSource());
720+
if (parser.State.Status >= ParserState.Error) { Assert.Inconclusive("Parser Error"); }
721+
722+
var qualifiedSelection = new QualifiedSelection(new QualifiedModuleName(component), selection);
723+
724+
//Specify Param(s) to remove
725+
var model = new RemoveParametersModel(parser.State, qualifiedSelection, null);
726+
model.Parameters[0].IsRemoved = true;
727+
728+
//SetupFactory
729+
var factory = SetupFactory(model);
730+
731+
//Act
732+
var refactoring = new RemoveParametersRefactoring(vbe.Object, factory.Object);
733+
refactoring.Refactor(qualifiedSelection);
734+
735+
//Assert
736+
Assert.AreEqual(expectedCode, module.Lines());
737+
}
738+
678739
[TestMethod]
679740
public void RemoveParametersRefactoring_ClientReferencesAreUpdated_LastParam()
680741
{

0 commit comments

Comments
 (0)