Skip to content

Commit d122208

Browse files
committed
Comment out With blocks with TODO instead of removing them.
1 parent ca8beff commit d122208

File tree

2 files changed

+108
-1
lines changed

2 files changed

+108
-1
lines changed

Rubberduck.CodeAnalysis/QuickFixes/RemoveUnassignedVariableUsageQuickFix.cs

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Linq;
4+
using System.ServiceModel;
15
using Antlr4.Runtime;
26
using Rubberduck.Inspections.Abstract;
37
using Rubberduck.Inspections.Concrete;
@@ -23,8 +27,26 @@ public override void Fix(IInspectionResult result)
2327
{
2428
var rewriter = _state.GetRewriter(result.QualifiedSelection.QualifiedName);
2529

30+
if (result.Context.Parent.Parent is VBAParser.WithStmtContext withContext)
31+
{
32+
var lines = withContext.GetText().Replace("\r", string.Empty).Split('\n');
33+
// Assume that the End With is at the appropriate indentation level for the block. Note that this could
34+
// over-indent or under-indent some lines if statement separators are being used, but meh.
35+
var padding = new string(' ', lines.Last().IndexOf(Tokens.End, StringComparison.Ordinal));
36+
37+
var replacement = new List<string>
38+
{
39+
$"{Tokens.CommentMarker}TODO - {result.Description}",
40+
$"{Tokens.CommentMarker}{padding}{lines.First()}"
41+
};
42+
replacement.AddRange(lines.Skip(1)
43+
.Select(line => Tokens.CommentMarker + line));
44+
45+
rewriter.Replace(withContext, string.Join(Environment.NewLine, replacement));
46+
return;
47+
}
2648
var assignmentContext = result.Context.GetAncestor<VBAParser.LetStmtContext>() ??
27-
(ParserRuleContext)result.Context.GetAncestor<VBAParser.CallStmtContext>();
49+
(ParserRuleContext)result.Context.GetAncestor<VBAParser.CallStmtContext>();
2850

2951
rewriter.Remove(assignmentContext);
3052
}

RubberduckTests/QuickFixes/RemoveUnassignedVariableUsageQuickFixTests.cs

Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,5 +39,90 @@ Dim bb As Boolean
3939
Assert.AreEqual(expectedCode, state.GetRewriter(component).GetText());
4040
}
4141
}
42+
43+
// See https://github.com/rubberduck-vba/Rubberduck/issues/3636
44+
[Test]
45+
[Category("QuickFixes")]
46+
public void UnassignedVariableUsage_QuickFixWorksWithBlock()
47+
{
48+
const string inputCode =
49+
@"Sub test()
50+
Dim wb As Workbook
51+
With wb
52+
Debug.Print .Name
53+
Debug.Print .Name
54+
Debug.Print .Name
55+
End With
56+
End Sub";
57+
58+
const string expectedCode =
59+
@"Sub test()
60+
Dim wb As Workbook
61+
'TODO - {0}
62+
' With wb
63+
' Debug.Print .Name
64+
' Debug.Print .Name
65+
' Debug.Print .Name
66+
' End With
67+
End Sub";
68+
69+
var vbe = MockVbeBuilder.BuildFromSingleStandardModule(inputCode, out var component);
70+
using (var state = MockParser.CreateAndParse(vbe.Object))
71+
{
72+
var inspection = new UnassignedVariableUsageInspection(state);
73+
var inspectionResult = inspection.GetInspectionResults(CancellationToken.None).First();
74+
var expected = string.Format(expectedCode, inspectionResult.Description);
75+
76+
new RemoveUnassignedVariableUsageQuickFix(state).Fix(inspectionResult);
77+
var actual = state.GetRewriter(component).GetText();
78+
Assert.AreEqual(expected, actual);
79+
}
80+
}
81+
82+
[Test]
83+
[Category("QuickFixes")]
84+
public void UnassignedVariableUsage_QuickFixWorksNestedWithBlock()
85+
{
86+
const string inputCode =
87+
@"Sub test()
88+
Dim wb As Workbook
89+
Dim ws As Worksheet
90+
With wb
91+
Debug.Print .Name
92+
With ws
93+
Debug.Print .Name
94+
Debug.Print .Name
95+
Debug.Print .Name
96+
End With
97+
End With
98+
End Sub";
99+
100+
const string expectedCode =
101+
@"Sub test()
102+
Dim wb As Workbook
103+
Dim ws As Worksheet
104+
With wb
105+
Debug.Print .Name
106+
'TODO - {0}
107+
' With ws
108+
' Debug.Print .Name
109+
' Debug.Print .Name
110+
' Debug.Print .Name
111+
' End With
112+
End With
113+
End Sub";
114+
115+
var vbe = MockVbeBuilder.BuildFromSingleStandardModule(inputCode, out var component);
116+
using (var state = MockParser.CreateAndParse(vbe.Object))
117+
{
118+
var inspection = new UnassignedVariableUsageInspection(state);
119+
var inspectionResult = inspection.GetInspectionResults(CancellationToken.None).First();
120+
var expected = string.Format(expectedCode, inspectionResult.Description);
121+
122+
new RemoveUnassignedVariableUsageQuickFix(state).Fix(inspectionResult);
123+
var actual = state.GetRewriter(component).GetText();
124+
Assert.AreEqual(expected, actual);
125+
}
126+
}
42127
}
43128
}

0 commit comments

Comments
 (0)