Skip to content

Commit bfc6720

Browse files
authored
Merge pull request #3717 from AnnaVel/step-one-inspection
Implicit/redundant Step statement inspections
2 parents eae405e + 4d31d9a commit bfc6720

19 files changed

+3876
-3169
lines changed
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
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 StepIsNotSpecifiedInspection : ParseTreeInspectionBase
19+
{
20+
public StepIsNotSpecifiedInspection(RubberduckParserState state) : base(state, CodeInspectionSeverity.DoNotShow) { }
21+
22+
public override CodeInspectionType InspectionType => CodeInspectionType.LanguageOpportunities;
23+
24+
protected override IEnumerable<IInspectionResult> DoGetInspectionResults()
25+
{
26+
return Listener.Contexts
27+
.Where(result => !IsIgnoringInspectionResultFor(result.ModuleName, result.Context.Start.Line))
28+
.Select(result => new QualifiedContextInspectionResult(this,
29+
InspectionsUI.StepIsNotSpecifiedInspectionResultFormat,
30+
result));
31+
}
32+
33+
public override IInspectionListener Listener { get; } =
34+
new StepIsNotSpecifiedListener();
35+
}
36+
37+
public class StepIsNotSpecifiedListener : VBAParserBaseListener, IInspectionListener
38+
{
39+
private readonly List<QualifiedContext<ParserRuleContext>> _contexts = new List<QualifiedContext<ParserRuleContext>>();
40+
public IReadOnlyList<QualifiedContext<ParserRuleContext>> Contexts => _contexts;
41+
42+
public QualifiedModuleName CurrentModuleName
43+
{
44+
get;
45+
set;
46+
}
47+
48+
public void ClearContexts()
49+
{
50+
_contexts.Clear();
51+
}
52+
53+
public override void EnterForNextStmt([NotNull] VBAParser.ForNextStmtContext context)
54+
{
55+
StepStmtContext stepStatement = context.stepStmt();
56+
57+
if (stepStatement == null)
58+
{
59+
_contexts.Add(new QualifiedContext<ParserRuleContext>(CurrentModuleName, context));
60+
}
61+
}
62+
}
63+
}
Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
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, CodeInspectionSeverity.Hint) { }
21+
22+
public override CodeInspectionType InspectionType => CodeInspectionType.LanguageOpportunities;
23+
24+
protected override IEnumerable<IInspectionResult> DoGetInspectionResults()
25+
{
26+
return Listener.Contexts
27+
.Where(result => !IsIgnoringInspectionResultFor(result.ModuleName, result.Context.Start.Line))
28+
.Select(result => new QualifiedContextInspectionResult(this,
29+
InspectionsUI.StepOneIsRedundantInspectionResultFormat,
30+
result));
31+
}
32+
33+
public override IInspectionListener Listener { get; } =
34+
new StepOneIsRedundantListener();
35+
}
36+
37+
public class StepOneIsRedundantListener : VBAParserBaseListener, IInspectionListener
38+
{
39+
private readonly List<QualifiedContext<ParserRuleContext>> _contexts = new List<QualifiedContext<ParserRuleContext>>();
40+
public IReadOnlyList<QualifiedContext<ParserRuleContext>> Contexts => _contexts;
41+
42+
public QualifiedModuleName CurrentModuleName
43+
{
44+
get;
45+
set;
46+
}
47+
48+
public void ClearContexts()
49+
{
50+
_contexts.Clear();
51+
}
52+
53+
public override void EnterForNextStmt([NotNull] VBAParser.ForNextStmtContext context)
54+
{
55+
StepStmtContext stepStatement = context.stepStmt();
56+
57+
if (stepStatement == null)
58+
{
59+
return;
60+
}
61+
62+
string stepText = stepStatement.expression().GetText();
63+
64+
if(stepText == "1")
65+
{
66+
_contexts.Add(new QualifiedContext<ParserRuleContext>(CurrentModuleName, stepStatement));
67+
}
68+
}
69+
}
70+
}
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
using Antlr4.Runtime;
2+
using Antlr4.Runtime.Tree;
3+
using Rubberduck.Inspections.Abstract;
4+
using Rubberduck.Inspections.Concrete;
5+
using Rubberduck.Parsing.Inspections.Abstract;
6+
using Rubberduck.Parsing.Inspections.Resources;
7+
using Rubberduck.Parsing.Rewriter;
8+
using Rubberduck.Parsing.VBA;
9+
using System;
10+
using System.Collections.Generic;
11+
using System.Linq;
12+
using System.Text;
13+
using System.Threading.Tasks;
14+
using static Rubberduck.Parsing.Grammar.VBAParser;
15+
16+
namespace Rubberduck.Inspections.QuickFixes
17+
{
18+
public class AddStepOneQuickFix : QuickFixBase
19+
{
20+
private readonly RubberduckParserState _state;
21+
22+
public AddStepOneQuickFix(RubberduckParserState state)
23+
: base(typeof(StepIsNotSpecifiedInspection))
24+
{
25+
_state = state;
26+
}
27+
28+
public override bool CanFixInProcedure => false;
29+
30+
public override bool CanFixInModule => false;
31+
32+
public override bool CanFixInProject => false;
33+
34+
public override string Description(IInspectionResult result)
35+
{
36+
return InspectionsUI.AddStepOneQuickFix;
37+
}
38+
39+
public override void Fix(IInspectionResult result)
40+
{
41+
IModuleRewriter rewriter = _state.GetRewriter(result.QualifiedSelection.QualifiedName);
42+
ForNextStmtContext context = result.Context as ForNextStmtContext;
43+
44+
int toExpressionEnd = this.GetToExpressionEnd(context);
45+
rewriter.InsertAfter(toExpressionEnd, " Step 1");
46+
}
47+
48+
private int GetToExpressionEnd(ForNextStmtContext context)
49+
{
50+
int toNodeIndex = context.TO().Symbol.TokenIndex;
51+
52+
foreach(ExpressionContext expressionChild in context.expression())
53+
{
54+
if (expressionChild.Stop.TokenIndex > toNodeIndex)
55+
{
56+
return expressionChild.Stop.TokenIndex;
57+
}
58+
}
59+
60+
throw new InvalidOperationException();
61+
}
62+
}
63+
}
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: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,8 @@
7171
<Compile Include="Concrete\BooleanAssignedInIfElseInspection.cs" />
7272
<Compile Include="Concrete\EmptyWhileWendBlockInspection.cs" />
7373
<Compile Include="Concrete\ObsoleteErrorSyntaxInspection.cs" />
74+
<Compile Include="Concrete\StepIsNotSpecifiedInspection.cs" />
75+
<Compile Include="Concrete\StepOneIsRedundantInspection.cs" />
7476
<Compile Include="Concrete\StopKeywordInspection.cs" />
7577
<Compile Include="Concrete\LineLabelNotUsedInspection.cs" />
7678
<Compile Include="Concrete\IntegerDataTypeInspection.cs" />
@@ -120,11 +122,13 @@
120122
<Compile Include="Properties\AssemblyInfo.cs" />
121123
<Compile Include="QuickFixes\ReplaceIfElseWithConditionalStatementQuickFix.cs" />
122124
<Compile Include="QuickFixes\AddIdentifierToWhiteListQuickFix.cs" />
125+
<Compile Include="QuickFixes\AddStepOneQuickFix.cs" />
123126
<Compile Include="QuickFixes\ApplicationWorksheetFunctionQuickFix.cs" />
124127
<Compile Include="QuickFixes\AssignedByValParameterMakeLocalCopyQuickFix.cs" />
125128
<Compile Include="QuickFixes\ChangeDimToPrivateQuickFix.cs" />
126129
<Compile Include="QuickFixes\ChangeIntegerToLongQuickFix.cs" />
127130
<Compile Include="Abstract\QuickFixBase.cs" />
131+
<Compile Include="QuickFixes\RemoveStepOneQuickFix.cs" />
128132
<Compile Include="QuickFixes\RemoveStopKeywordQuickFix.cs" />
129133
<Compile Include="QuickFixes\ReplaceObsoleteErrorStatementQuickFix.cs" />
130134
<Compile Include="QuickFixes\RestoreErrorHandlingQuickFix.cs" />

0 commit comments

Comments
 (0)