forked from rubberduck-vba/Rubberduck
-
Notifications
You must be signed in to change notification settings - Fork 12
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Implemented ObsoleteGlobalInspection; closes #239
- Loading branch information
1 parent
6782fe1
commit 6124559
Showing
7 changed files
with
108 additions
and
1 deletion.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
50
RetailCoder.VBE/Inspections/ObsoleteGlobalInspectionResult.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters