Skip to content

Commit 734d986

Browse files
committed
Added an inspection, quickfix and tests. Added separate Step statement to the parser in order to use it in the inspection.
1 parent 72a53d3 commit 734d986

15 files changed

+3533
-3169
lines changed
Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
using Rubberduck.Inspections.Abstract;
2+
using System;
3+
using System.Collections.Generic;
4+
using System.Linq;
5+
using Rubberduck.Parsing.Inspections.Abstract;
6+
using Rubberduck.Parsing.Inspections.Resources;
7+
using Rubberduck.Parsing.VBA;
8+
using Rubberduck.Parsing.Grammar;
9+
using Antlr4.Runtime.Misc;
10+
using Antlr4.Runtime;
11+
using Rubberduck.Parsing;
12+
using Rubberduck.VBEditor;
13+
using Rubberduck.Inspections.Results;
14+
using static Rubberduck.Parsing.Grammar.VBAParser;
15+
16+
namespace Rubberduck.Inspections.Concrete
17+
{
18+
public sealed class StepOneIsRedundantInspection : ParseTreeInspectionBase
19+
{
20+
public StepOneIsRedundantInspection(RubberduckParserState state) : base(state) { }
21+
22+
public override Type Type => typeof(StepOneIsRedundantInspection);
23+
24+
public override CodeInspectionType InspectionType => CodeInspectionType.LanguageOpportunities;
25+
26+
public override IEnumerable<IInspectionResult> GetInspectionResults()
27+
{
28+
return Listener.Contexts
29+
.Where(result => !IsIgnoringInspectionResultFor(result.ModuleName, result.Context.Start.Line))
30+
.Select(result => new QualifiedContextInspectionResult(this,
31+
InspectionsUI.StepOneIsRedundantInspectionResultFormat,
32+
result));
33+
}
34+
35+
public override IInspectionListener Listener { get; } =
36+
new StepOneIsRedundantListener();
37+
}
38+
39+
public class StepOneIsRedundantListener : VBAParserBaseListener, IInspectionListener
40+
{
41+
private readonly List<QualifiedContext<ParserRuleContext>> _contexts = new List<QualifiedContext<ParserRuleContext>>();
42+
public IReadOnlyList<QualifiedContext<ParserRuleContext>> Contexts => _contexts;
43+
44+
public QualifiedModuleName CurrentModuleName
45+
{
46+
get;
47+
set;
48+
}
49+
50+
public void ClearContexts()
51+
{
52+
_contexts.Clear();
53+
}
54+
55+
public override void EnterForNextStmt([NotNull] VBAParser.ForNextStmtContext context)
56+
{
57+
StepStmtContext stepStatement = context.stepStmt();
58+
59+
if (stepStatement == null)
60+
{
61+
return;
62+
}
63+
64+
string stepText = stepStatement.expression().GetText();
65+
66+
if(stepText == "1")
67+
{
68+
_contexts.Add(new QualifiedContext<ParserRuleContext>(CurrentModuleName, stepStatement));
69+
}
70+
}
71+
}
72+
}
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
using Rubberduck.Inspections.Abstract;
2+
using Rubberduck.Inspections.Concrete;
3+
using Rubberduck.Parsing.Inspections.Abstract;
4+
using Rubberduck.Parsing.Inspections.Resources;
5+
using Rubberduck.Parsing.Rewriter;
6+
using Rubberduck.Parsing.VBA;
7+
8+
namespace Rubberduck.Inspections.QuickFixes
9+
{
10+
public class RemoveStepOneQuickFix : QuickFixBase
11+
{
12+
private readonly RubberduckParserState _state;
13+
14+
public RemoveStepOneQuickFix(RubberduckParserState state)
15+
: base(typeof(StepOneIsRedundantInspection))
16+
{
17+
_state = state;
18+
}
19+
20+
public override bool CanFixInProcedure => false;
21+
22+
public override bool CanFixInModule => false;
23+
24+
public override bool CanFixInProject => false;
25+
26+
public override string Description(IInspectionResult result)
27+
{
28+
return InspectionsUI.RemoveStepOneQuickFix;
29+
}
30+
31+
public override void Fix(IInspectionResult result)
32+
{
33+
IModuleRewriter rewriter = _state.GetRewriter(result.QualifiedSelection.QualifiedName);
34+
var context = result.Context;
35+
rewriter.Remove(context);
36+
}
37+
}
38+
}

Rubberduck.Inspections/Rubberduck.Inspections.csproj

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,7 @@
6868
<Compile Include="Concrete\EmptyForEachBlockInspection.cs" />
6969
<Compile Include="Concrete\EmptyForLoopBlockInspection.cs" />
7070
<Compile Include="Concrete\EmptyWhileWendBlockInspection.cs" />
71+
<Compile Include="Concrete\StepOneIsRedundantInspection.cs" />
7172
<Compile Include="Concrete\StopKeywordInspection.cs" />
7273
<Compile Include="Concrete\LineLabelNotUsedInspection.cs" />
7374
<Compile Include="Concrete\IntegerDataTypeInspection.cs" />
@@ -121,6 +122,7 @@
121122
<Compile Include="QuickFixes\ChangeDimToPrivateQuickFix.cs" />
122123
<Compile Include="QuickFixes\ChangeIntegerToLongQuickFix.cs" />
123124
<Compile Include="Abstract\QuickFixBase.cs" />
125+
<Compile Include="QuickFixes\RemoveStepOneQuickFix.cs" />
124126
<Compile Include="QuickFixes\RemoveStopKeywordQuickFix.cs" />
125127
<Compile Include="QuickFixes\SpecifyExplicitByRefModifierQuickFix.cs" />
126128
<Compile Include="QuickFixes\ChangeProcedureToFunctionQuickFix.cs" />

0 commit comments

Comments
 (0)