Skip to content

Commit

Permalink
Implemented ObsoleteGlobalInspection; closes #239
Browse files Browse the repository at this point in the history
  • Loading branch information
retailcoder committed Feb 22, 2015
1 parent 6782fe1 commit 6124559
Show file tree
Hide file tree
Showing 7 changed files with 108 additions and 1 deletion.
9 changes: 9 additions & 0 deletions RetailCoder.VBE/Inspections/InspectionNames.Designer.cs

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 3 additions & 0 deletions RetailCoder.VBE/Inspections/InspectionNames.resx
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,9 @@
<data name="ObsoleteComment" xml:space="preserve">
<value>Use of obsolete Rem comment syntax</value>
</data>
<data name="ObsoleteGlobal" xml:space="preserve">
<value>Use of obsolete Global access modifier</value>
</data>
<data name="ObsoleteLet" xml:space="preserve">
<value>Use of obsolete Let statement</value>
</data>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
using System;
using System.Collections.Generic;
using System.Linq;
using Antlr4.Runtime;
Expand Down
39 changes: 39 additions & 0 deletions RetailCoder.VBE/Inspections/ObsoleteGlobalInspection.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
using System.Collections.Generic;
using System.Linq;
using Antlr4.Runtime;
using Rubberduck.VBA;
using Rubberduck.VBA.Grammar;
using Rubberduck.VBA.Nodes;
using Rubberduck.VBA.ParseTreeListeners;

namespace Rubberduck.Inspections
{
public class ObsoleteGlobalInspection : IInspection
{
public ObsoleteGlobalInspection()
{
Severity = CodeInspectionSeverity.Hint;
}

public string Name { get { return InspectionNames.ObsoleteGlobal; } }
public CodeInspectionType InspectionType { get { return CodeInspectionType.LanguageOpportunities; } }
public CodeInspectionSeverity Severity { get; set; }

public IEnumerable<CodeInspectionResultBase> GetInspectionResults(IEnumerable<VBComponentParseResult> parseResult)
{
foreach (var result in parseResult)
{
var statements = (result.ParseTree.GetContexts<DeclarationSectionListener, ParserRuleContext>(new DeclarationSectionListener(result.QualifiedName)))
.Select(context => context.Context).ToList();
var module = result;
foreach (var inspectionResult in
statements.OfType<VBParser.VisibilityContext>()
.Where(context => context.GetText() == Tokens.Global)
.Select(context => new ObsoleteGlobalInspectionResult(Name, Severity, new QualifiedContext<ParserRuleContext>(module.QualifiedName, context.Parent as ParserRuleContext))))
{
yield return inspectionResult;
}
}
}
}
}
50 changes: 50 additions & 0 deletions RetailCoder.VBE/Inspections/ObsoleteGlobalInspectionResult.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
using System;
using System.Collections.Generic;
using System.Linq;
using Antlr4.Runtime;
using Microsoft.Vbe.Interop;
using Rubberduck.Extensions;
using Rubberduck.VBA;

namespace Rubberduck.Inspections
{
public class ObsoleteGlobalInspectionResult : CodeInspectionResultBase
{
public ObsoleteGlobalInspectionResult(string inspection, CodeInspectionSeverity type, QualifiedContext<ParserRuleContext> context)
: base(inspection, type, context.QualifiedName, context.Context)
{
}

public override IDictionary<string, Action<VBE>> GetQuickFixes()
{
return new Dictionary<string, Action<VBE>>
{
{"Replace 'Global' access modifier with 'Public'", ChangeAccessModifier}
};
}

private void ChangeAccessModifier(VBE vbe)
{
var module = vbe.FindCodeModules(QualifiedName).SingleOrDefault();
if (module == null)
{
return;
}

var selection = Context.GetSelection();

// remove line continuations to compare against Context:
var originalCodeLines = module.get_Lines(selection.StartLine, selection.LineCount)
.Replace("\r\n", " ")
.Replace("_", string.Empty);
var originalInstruction = Context.GetText();

module.DeleteLines(selection.StartLine, selection.LineCount);

var newInstruction = Tokens.Public + ' ' + Context.GetText().Replace(Tokens.Global + ' ', string.Empty);
var newCodeLines = originalCodeLines.Replace(originalInstruction, newInstruction);

module.InsertLines(selection.StartLine, newCodeLines);
}
}
}
2 changes: 2 additions & 0 deletions RetailCoder.VBE/Rubberduck.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,8 @@
<Compile Include="Inspections\NonReturningFunctionInspectionResult.cs" />
<Compile Include="Inspections\ObsoleteCallStatementInspection.cs" />
<Compile Include="Inspections\ObsoleteCallStatementUsageInspectionResult.cs" />
<Compile Include="Inspections\ObsoleteGlobalInspection.cs" />
<Compile Include="Inspections\ObsoleteGlobalInspectionResult.cs" />
<Compile Include="Inspections\ObsoleteLetStatementInspection.cs" />
<Compile Include="Inspections\ObsoleteLetStatementUsageInspectionResult.cs" />
<Compile Include="Inspections\ObsoleteTypeHintInspection.cs" />
Expand Down
5 changes: 5 additions & 0 deletions RetailCoder.VBE/VBA/ParseTreeListeners/DeclarationListener.cs
Original file line number Diff line number Diff line change
Expand Up @@ -144,6 +144,11 @@ public override void EnterVariableStmt(VBParser.VariableStmtContext context)
}
}

public override void EnterVisibility(VBParser.VisibilityContext context)
{
_members.Add(new QualifiedContext<ParserRuleContext>(_qualifiedName, context));
}

public override void EnterEnumerationStmt(VBParser.EnumerationStmtContext context)
{
_members.Add(new QualifiedContext<ParserRuleContext>(_qualifiedName, context));
Expand Down

0 comments on commit 6124559

Please sign in to comment.