Skip to content

Commit 9925238

Browse files
committed
Resolved the local copy formatting error
Resovled the local copy formatting error and also modfied to retain any comments (On the Sub/Function line) . Improved final result to match the indentation scheme of the original first line.
1 parent 323f0ba commit 9925238

File tree

3 files changed

+148
-25
lines changed

3 files changed

+148
-25
lines changed

Rubberduck.Inspections/QuickFixes/AssignedByValParameterMakeLocalCopyQuickFix.cs

Lines changed: 21 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@
1313
using Rubberduck.Parsing.Inspections.Resources;
1414
using Rubberduck.Parsing.Rewriter;
1515
using Rubberduck.Parsing.VBA;
16+
using static Rubberduck.Parsing.Grammar.VBAParser;
17+
using System.Diagnostics;
1618

1719
namespace Rubberduck.Inspections.QuickFixes
1820
{
@@ -106,16 +108,31 @@ private void ReplaceAssignedByValParameterReferences(IModuleRewriter rewriter, D
106108

107109
private void InsertLocalVariableDeclarationAndAssignment(IModuleRewriter rewriter, Declaration target, string localIdentifier)
108110
{
109-
var localVariableDeclaration = $"{Environment.NewLine}{Tokens.Dim} {localIdentifier} {Tokens.As} {target.AsTypeName}{Environment.NewLine}";
110-
111+
var localVariableDeclaration = $"{Tokens.Dim} {localIdentifier} {Tokens.As} {target.AsTypeName}";
112+
111113
var requiresAssignmentUsingSet =
112114
target.References.Any(refItem => VariableRequiresSetAssignmentEvaluator.RequiresSetAssignment(refItem, _parserState));
113115

114116
var localVariableAssignment = string.Format("{0}{1}",
115-
requiresAssignmentUsingSet ? "Set " : string.Empty,
117+
requiresAssignmentUsingSet ? $"{Tokens.Set} " : string.Empty,
116118
$"{localIdentifier} = {target.IdentifierName}");
117119

118-
rewriter.InsertBefore(((ParserRuleContext)target.Context.Parent).Stop.TokenIndex + 1, localVariableDeclaration + localVariableAssignment);
120+
var endOfStmtCtxt = ParserRuleContextHelper.GetChild<EndOfStatementContext>(target.Context.Parent.Parent);
121+
var eosContent = endOfStmtCtxt.GetText();
122+
var idxLastNewLine = eosContent.LastIndexOf(Environment.NewLine);
123+
var comment = eosContent.Substring(0, idxLastNewLine);
124+
var endOfStmtNewLineContent = eosContent.Substring(idxLastNewLine);
125+
126+
Debug.Assert(target.Context.Parent is ArgListContext);
127+
128+
var insertCtxt = (ParserRuleContext)ParserRuleContextHelper.GetChild<AsTypeClauseContext>(target.Context.Parent.Parent);
129+
if(insertCtxt == null)
130+
{
131+
insertCtxt = (ParserRuleContext)target.Context.Parent;
132+
}
133+
134+
rewriter.Remove(endOfStmtCtxt);
135+
rewriter.InsertAfter(insertCtxt.Stop.TokenIndex, $"{comment}{endOfStmtNewLineContent}{localVariableDeclaration}" + $"{endOfStmtNewLineContent}{localVariableAssignment}{endOfStmtNewLineContent}");
119136
}
120137
}
121138
}

RubberduckTests/Inspections/AssignedByValParameterInspectionTests.cs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ public class AssignedByValParameterInspectionTests
1515
public void AssignedByValParameter_ReturnsResult_Sub()
1616
{
1717
const string inputCode =
18-
@"Public Sub Foo(ByVal arg1 As String)
18+
@"Public Sub Foo(ByVal arg1 As String)
1919
Let arg1 = ""test""
2020
End Sub";
2121

@@ -35,7 +35,7 @@ public void AssignedByValParameter_ReturnsResult_Sub()
3535
public void AssignedByValParameter_ReturnsResult_Function()
3636
{
3737
const string inputCode =
38-
@"Function Foo(ByVal arg1 As Integer) As Boolean
38+
@"Function Foo(ByVal arg1 As Integer) As Boolean
3939
Let arg1 = 9
4040
End Function";
4141

@@ -55,7 +55,7 @@ public void AssignedByValParameter_ReturnsResult_Function()
5555
public void AssignedByValParameter_ReturnsResult_MultipleParams()
5656
{
5757
const string inputCode =
58-
@"Public Sub Foo(ByVal arg1 As String, ByVal arg2 As Integer)
58+
@"Public Sub Foo(ByVal arg1 As String, ByVal arg2 As Integer)
5959
Let arg1 = ""test""
6060
Let arg2 = 9
6161
End Sub";
@@ -76,7 +76,7 @@ public void AssignedByValParameter_ReturnsResult_MultipleParams()
7676
public void AssignedByValParameter_DoesNotReturnResult()
7777
{
7878
const string inputCode =
79-
@"Public Sub Foo(ByVal arg1 As String)
79+
@"Public Sub Foo(ByVal arg1 As String)
8080
End Sub";
8181

8282
var vbe = MockVbeBuilder.BuildFromSingleStandardModule(inputCode, out _);
@@ -95,7 +95,7 @@ public void AssignedByValParameter_DoesNotReturnResult()
9595
public void AssignedByValParameter_Ignored_DoesNotReturnResult_Sub()
9696
{
9797
const string inputCode =
98-
@"'@Ignore AssignedByValParameter
98+
@"'@Ignore AssignedByValParameter
9999
Public Sub Foo(ByVal arg1 As String)
100100
Let arg1 = ""test""
101101
End Sub";
@@ -116,7 +116,7 @@ Public Sub Foo(ByVal arg1 As String)
116116
public void AssignedByValParameter_ReturnsResult_SomeAssignedByValParams()
117117
{
118118
const string inputCode =
119-
@"Public Sub Foo(ByVal arg1 As String, ByVal arg2 As Integer)
119+
@"Public Sub Foo(ByVal arg1 As String, ByVal arg2 As Integer)
120120
Let arg1 = ""test""
121121
122122
Dim var1 As Integer

RubberduckTests/QuickFixes/AssignedByValParameterMakeLocalCopyQuickFixTests.cs

Lines changed: 121 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -16,23 +16,129 @@ public class AssignedByValParameterMakeLocalCopyQuickFixTests
1616
{
1717
[Test]
1818
[Category("QuickFixes")]
19-
public void AssignedByValParameter_LocalVariableAssignment()
19+
public void AssignedByValParameter_LocalVariableAssignment_Sub()
2020
{
2121
var inputCode =
22-
@"Public Sub Foo(ByVal arg1 As String)
22+
@"Public Sub Foo(ByVal arg1 As String)
2323
Let arg1 = ""test""
2424
End Sub";
2525
var expectedCode =
26-
@"Public Sub Foo(ByVal arg1 As String)
27-
Dim localArg1 As String
28-
localArg1 = arg1
26+
@"Public Sub Foo(ByVal arg1 As String)
27+
Dim localArg1 As String
28+
localArg1 = arg1
2929
Let localArg1 = ""test""
3030
End Sub";
3131

3232
var quickFixResult = ApplyLocalVariableQuickFixToCodeFragment(inputCode);
3333
Assert.AreEqual(expectedCode, quickFixResult);
3434
}
3535

36+
[Test]
37+
[Category("QuickFixes")]
38+
public void AssignedByValParameter_LocalVariableAssignment_SubWithCommentn()
39+
{
40+
var inputCode =
41+
@"Public Sub Foo(ByVal arg1 As String) 'Comment is here _
42+
and here _
43+
and here
44+
Let arg1 = ""test""
45+
End Sub";
46+
var expectedCode =
47+
@"Public Sub Foo(ByVal arg1 As String) 'Comment is here _
48+
and here _
49+
and here
50+
Dim localArg1 As String
51+
localArg1 = arg1
52+
Let localArg1 = ""test""
53+
End Sub";
54+
55+
var quickFixResult = ApplyLocalVariableQuickFixToCodeFragment(inputCode);
56+
Assert.AreEqual(expectedCode, quickFixResult);
57+
}
58+
59+
[Test]
60+
[Category("QuickFixes")]
61+
public void AssignedByValParameter_LocalVariableAssignment_Function()
62+
{
63+
var inputCode =
64+
@"Public Function Foo(ByVal arg1 As String) As String
65+
arg1 = ""test""
66+
Foo = arg1
67+
End Function";
68+
var expectedCode =
69+
@"Public Function Foo(ByVal arg1 As String) As String
70+
Dim localArg1 As String
71+
localArg1 = arg1
72+
localArg1 = ""test""
73+
Foo = localArg1
74+
End Function";
75+
76+
var quickFixResult = ApplyLocalVariableQuickFixToCodeFragment(inputCode);
77+
Assert.AreEqual(expectedCode, quickFixResult);
78+
}
79+
80+
[Test]
81+
[Category("QuickFixes")]
82+
public void AssignedByValParameter_LocalVariableAssignment_FunctionWithComment()
83+
{
84+
var inputCode =
85+
@"Public Function Foo(ByVal arg1 As String) As String 'This is a comment
86+
arg1 = ""test""
87+
Foo = arg1
88+
End Function";
89+
var expectedCode =
90+
@"Public Function Foo(ByVal arg1 As String) As String 'This is a comment
91+
Dim localArg1 As String
92+
localArg1 = arg1
93+
localArg1 = ""test""
94+
Foo = localArg1
95+
End Function";
96+
97+
var quickFixResult = ApplyLocalVariableQuickFixToCodeFragment(inputCode);
98+
Assert.AreEqual(expectedCode, quickFixResult);
99+
}
100+
101+
[Test]
102+
[Category("QuickFixes")]
103+
public void AssignedByValParameter_LocalVariableAssignment_Property()
104+
{
105+
var inputCode =
106+
@"
107+
Option Explicit
108+
Private mBar as Long
109+
Public Property Let Foo(ByVal bar As Long)
110+
bar = 42
111+
bar = bar * 2
112+
mBar = bar
113+
End Property
114+
115+
Public Property Get Foo() As Long
116+
Dim bar as Long
117+
bar = 12
118+
Foo = mBar
119+
End Property
120+
";
121+
var expectedCode =
122+
@"
123+
Option Explicit
124+
Private mBar as Long
125+
Public Property Let Foo(ByVal bar As Long)
126+
Dim localBar As Long
127+
localBar = bar
128+
localBar = 42
129+
localBar = localBar * 2
130+
mBar = localBar
131+
End Property
132+
133+
Public Property Get Foo() As Long
134+
Dim bar as Long
135+
bar = 12
136+
Foo = mBar
137+
End Property
138+
";
139+
var quickFixResult = ApplyLocalVariableQuickFixToCodeFragment(inputCode);
140+
Assert.AreEqual(expectedCode, quickFixResult);
141+
}
36142
[Test]
37143
[Category("QuickFixes")]
38144
public void AssignedByValParameter_LocalVariableAssignment_ComplexFormat()
@@ -56,8 +162,8 @@ ByRef _
56162
bar, _
57163
ByRef barbecue _
58164
)
59-
Dim localFoo As Long
60-
localFoo = foo
165+
Dim localFoo As Long
166+
localFoo = foo
61167
localFoo = 4
62168
bar = barbecue * _
63169
bar + localFoo / barbecue
@@ -83,8 +189,8 @@ End Sub"
83189
var expectedCode =
84190
@"
85191
Public Sub Foo(ByVal arg1 As String)
86-
Dim localArg12 As String
87-
localArg12 = arg1
192+
Dim localArg12 As String
193+
localArg12 = arg1
88194
Dim fooVar, _
89195
localArg1 As Long
90196
Let localArg12 = ""test""
@@ -178,8 +284,8 @@ End Sub"
178284
var expectedCode =
179285
@"
180286
Public Sub Foo(ByVal target As Range)
181-
Dim localTarget As Range
182-
Set localTarget = target
287+
Dim localTarget As Range
288+
Set localTarget = target
183289
Set localTarget = Selection
184290
End Sub"
185291
;
@@ -201,8 +307,8 @@ End Sub"
201307
var expectedCode =
202308
@"
203309
Public Sub Foo(FirstArg As Long, ByVal arg1)
204-
Dim localArg1 As Variant
205-
localArg1 = arg1
310+
Dim localArg1 As Variant
311+
localArg1 = arg1
206312
localArg1 = Range(""A1: C4"")
207313
End Sub"
208314
;
@@ -236,8 +342,8 @@ Enum TestEnum
236342
End Enum
237343
238344
Public Sub Foo(FirstArg As Long, ByVal arg1 As TestEnum)
239-
Dim localArg1 As TestEnum
240-
localArg1 = arg1
345+
Dim localArg1 As TestEnum
346+
localArg1 = arg1
241347
localArg1 = EnumThree
242348
End Sub"
243349
;

0 commit comments

Comments
 (0)