Skip to content

Commit 9b2ddce

Browse files
committed
Event quick fix works
1 parent b94d23f commit 9b2ddce

File tree

2 files changed

+106
-2
lines changed

2 files changed

+106
-2
lines changed

RetailCoder.VBE/Inspections/ParameterCanBeByValInspectionResult.cs

Lines changed: 38 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,11 @@ public PassParameterByValueQuickFix(RubberduckParserState state, Declaration tar
4545

4646
public override void Fix()
4747
{
48-
if (_state.AllUserDeclarations.FindInterfaceMembers().Contains(_target.ParentDeclaration))
48+
if (_target.ParentDeclaration.DeclarationType == DeclarationType.Event)
49+
{
50+
FixEventMethods();
51+
}
52+
else if (_state.AllUserDeclarations.FindInterfaceMembers().Contains(_target.ParentDeclaration))
4953
{
5054
FixInterfaceMethods();
5155
}
@@ -65,7 +69,6 @@ private void FixInterfaceMethods()
6569
.ToList();
6670

6771
var parameterIndex = declarationParameters.IndexOf(_target);
68-
6972
if (parameterIndex == -1)
7073
{
7174
return; // should only happen if the parse results are stale; prevents a crash in that case
@@ -89,6 +92,39 @@ private void FixInterfaceMethods()
8992
declarationParameters[parameterIndex].QualifiedSelection);
9093
}
9194

95+
private void FixEventMethods()
96+
{
97+
var declarationParameters =
98+
_state.AllUserDeclarations.Where(declaration => declaration.DeclarationType == DeclarationType.Parameter &&
99+
declaration.ParentDeclaration == _target.ParentDeclaration)
100+
.OrderBy(o => o.Selection.StartLine)
101+
.ThenBy(t => t.Selection.StartColumn)
102+
.ToList();
103+
104+
var parameterIndex = declarationParameters.IndexOf(_target);
105+
if (parameterIndex == -1)
106+
{
107+
return; // should only happen if the parse results are stale; prevents a crash in that case
108+
}
109+
110+
var handlers = _state.AllUserDeclarations.FindHandlersForEvent(_target.ParentDeclaration).Select(s => s.Item2).ToList();
111+
foreach (var handler in handlers)
112+
{
113+
var parameters =
114+
_state.AllUserDeclarations.Where(declaration => declaration.DeclarationType == DeclarationType.Parameter &&
115+
declaration.ParentDeclaration == handler)
116+
.OrderBy(o => o.Selection.StartLine)
117+
.ThenBy(t => t.Selection.StartColumn)
118+
.ToList();
119+
120+
FixMethod((VBAParser.ArgContext)parameters[parameterIndex].Context,
121+
parameters[parameterIndex].QualifiedSelection);
122+
}
123+
124+
FixMethod((VBAParser.ArgContext)declarationParameters[parameterIndex].Context,
125+
declarationParameters[parameterIndex].QualifiedSelection);
126+
}
127+
92128
private void FixMethod(VBAParser.ArgContext context, QualifiedSelection qualifiedSelection)
93129
{
94130
var selectionLength = context.BYREF() == null ? 0 : 6;

RubberduckTests/Inspections/ParameterCanBeByValInspectionTests.cs

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -503,6 +503,7 @@ Private Sub abc_Foo(ByRef arg1 As Integer)
503503
var project = builder.ProjectBuilder("TestProject1", vbext_ProjectProtection.vbext_pp_none)
504504
.AddComponent("Class1", vbext_ComponentType.vbext_ct_ClassModule, inputCode1)
505505
.AddComponent("Class2", vbext_ComponentType.vbext_ct_ClassModule, inputCode2)
506+
.AddComponent("Class3", vbext_ComponentType.vbext_ct_ClassModule, inputCode2)
506507
.Build();
507508
var vbe = builder.AddProject(project).Build();
508509

@@ -538,6 +539,7 @@ Private Sub abc_Foo(ByVal arg1 As Integer)
538539
var project = builder.ProjectBuilder("TestProject1", vbext_ProjectProtection.vbext_pp_none)
539540
.AddComponent("Class1", vbext_ComponentType.vbext_ct_ClassModule, inputCode1)
540541
.AddComponent("Class2", vbext_ComponentType.vbext_ct_ClassModule, inputCode2)
542+
.AddComponent("Class3", vbext_ComponentType.vbext_ct_ClassModule, inputCode2)
541543
.Build();
542544
var vbe = builder.AddProject(project).Build();
543545

@@ -574,6 +576,7 @@ Private Sub abc_Foo(ByRef arg1 As Integer)
574576
var project = builder.ProjectBuilder("TestProject1", vbext_ProjectProtection.vbext_pp_none)
575577
.AddComponent("Class1", vbext_ComponentType.vbext_ct_ClassModule, inputCode1)
576578
.AddComponent("Class2", vbext_ComponentType.vbext_ct_ClassModule, inputCode2)
579+
.AddComponent("Class3", vbext_ComponentType.vbext_ct_ClassModule, inputCode2)
577580
.Build();
578581
var vbe = builder.AddProject(project).Build();
579582

@@ -610,6 +613,7 @@ Private Sub abc_Foo(ByRef arg1 As Integer, ByRef arg2 As Integer)
610613
var project = builder.ProjectBuilder("TestProject1", vbext_ProjectProtection.vbext_pp_none)
611614
.AddComponent("Class1", vbext_ComponentType.vbext_ct_ClassModule, inputCode1)
612615
.AddComponent("Class2", vbext_ComponentType.vbext_ct_ClassModule, inputCode2)
616+
.AddComponent("Class3", vbext_ComponentType.vbext_ct_ClassModule, inputCode2)
613617
.Build();
614618
var vbe = builder.AddProject(project).Build();
615619

@@ -851,6 +855,70 @@ Private Sub IClass1_DoSomething(ByVal a As Integer, ByRef b As Integer)
851855
Assert.AreEqual(expectedCode3, module3.Lines());
852856
}
853857

858+
[TestMethod]
859+
[TestCategory("Inspections")]
860+
public void ParameterCanBeByVal_EVentMember_MultipleParams_OneCanBeByVal_QuickFixWorks()
861+
{
862+
//Input
863+
const string inputCode1 =
864+
@"Public Event Foo(ByRef a As Integer, ByRef b As Integer)";
865+
const string inputCode2 =
866+
@"Private WithEvents abc As Class1
867+
868+
Private Sub abc_Foo(ByRef a As Integer, ByRef b As Integer)
869+
a = 42
870+
End Sub";
871+
const string inputCode3 =
872+
@"Private WithEvents abc As Class1
873+
874+
Private Sub abc_Foo(ByRef a As Integer, ByRef b As Integer)
875+
End Sub";
876+
877+
//Expected
878+
const string expectedCode1 =
879+
@"Public Event Foo(ByRef a As Integer, ByVal b As Integer)";
880+
const string expectedCode2 =
881+
@"Private WithEvents abc As Class1
882+
883+
Private Sub abc_Foo(ByRef a As Integer, ByVal b As Integer)
884+
a = 42
885+
End Sub";
886+
const string expectedCode3 =
887+
@"Private WithEvents abc As Class1
888+
889+
Private Sub abc_Foo(ByRef a As Integer, ByVal b As Integer)
890+
End Sub";
891+
892+
//Arrange
893+
var builder = new MockVbeBuilder();
894+
var project = builder.ProjectBuilder("TestProject1", vbext_ProjectProtection.vbext_pp_none)
895+
.AddComponent("Class1", vbext_ComponentType.vbext_ct_ClassModule, inputCode1)
896+
.AddComponent("Class2", vbext_ComponentType.vbext_ct_ClassModule, inputCode2)
897+
.AddComponent("Class3", vbext_ComponentType.vbext_ct_ClassModule, inputCode3)
898+
.Build();
899+
900+
var module1 = project.Object.VBComponents.Item("Class1").CodeModule;
901+
var module2 = project.Object.VBComponents.Item("Class2").CodeModule;
902+
var module3 = project.Object.VBComponents.Item("Class3").CodeModule;
903+
var vbe = builder.AddProject(project).Build();
904+
905+
var mockHost = new Mock<IHostApplication>();
906+
mockHost.SetupAllProperties();
907+
var parser = MockParser.Create(vbe.Object, new RubberduckParserState(vbe.Object, new Mock<ISinks>().Object));
908+
909+
parser.Parse(new CancellationTokenSource());
910+
if (parser.State.Status >= ParserState.Error) { Assert.Inconclusive("Parser Error"); }
911+
912+
var inspection = new ParameterCanBeByValInspection(parser.State);
913+
var inspectionResults = inspection.GetInspectionResults();
914+
915+
inspectionResults.Single().QuickFixes.Single(s => s is PassParameterByValueQuickFix).Fix();
916+
917+
Assert.AreEqual(expectedCode1, module1.Lines());
918+
Assert.AreEqual(expectedCode2, module2.Lines());
919+
Assert.AreEqual(expectedCode3, module3.Lines());
920+
}
921+
854922
[TestMethod]
855923
[TestCategory("Inspections")]
856924
public void ParameterCanBeByVal_IgnoreQuickFixWorks()

0 commit comments

Comments
 (0)