Skip to content

Commit 80d8363

Browse files
committed
Closes #31
1 parent 81b5b5f commit 80d8363

File tree

5 files changed

+124
-0
lines changed

5 files changed

+124
-0
lines changed

RetailCoder.VBE/Inspections/InspectionsUI.Designer.cs

Lines changed: 18 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

RetailCoder.VBE/Inspections/InspectionsUI.resx

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -123,4 +123,10 @@
123123
<data name="EmptyStringLiteralInspectionQuickFix" xml:space="preserve">
124124
<value>Replace '""' with 'vbNullString'.</value>
125125
</data>
126+
<data name="UseMeaningfulNameInspection" xml:space="preserve">
127+
<value>Suboptimal Name Detected.</value>
128+
</data>
129+
<data name="UseMeaningfulNameInspectionQuickFix" xml:space="preserve">
130+
<value>Rename</value>
131+
</data>
126132
</root>
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
using System.Collections.Generic;
2+
using System.Linq;
3+
using Rubberduck.Parsing.VBA;
4+
using Rubberduck.VBEditor.VBEInterfaces.RubberduckCodePane;
5+
6+
namespace Rubberduck.Inspections
7+
{
8+
public class UseMeaningfulNameInspection : IInspection
9+
{
10+
private readonly ICodePaneWrapperFactory _wrapperFactory;
11+
12+
public UseMeaningfulNameInspection()
13+
{
14+
_wrapperFactory = new CodePaneWrapperFactory();
15+
Severity = CodeInspectionSeverity.Suggestion;
16+
}
17+
18+
public string Name { get { return "UseMeaningfulNameInspection"; } }
19+
public string Description { get { return InspectionsUI.UseMeaningfulNameInspection; } }
20+
public CodeInspectionType InspectionType { get { return CodeInspectionType.MaintainabilityAndReadabilityIssues; } }
21+
public CodeInspectionSeverity Severity { get; set; }
22+
23+
public IEnumerable<CodeInspectionResultBase> GetInspectionResults(RubberduckParserState state)
24+
{
25+
var issues = state.AllDeclarations
26+
.Where(declaration => !declaration.IsBuiltIn &&
27+
(declaration.IdentifierName.Length < 3 ||
28+
char.IsDigit(declaration.IdentifierName.Last()) ||
29+
!declaration.IdentifierName.Any(c => new[] {'a', 'e', 'i', 'o', 'u', 'y'}.Contains(c))
30+
))
31+
.Select(issue => new UseMeaningfulNameInspectionResult(this, issue, state, _wrapperFactory))
32+
.ToList();
33+
34+
return issues;
35+
}
36+
}
37+
}
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
using System.Collections.Generic;
2+
using Antlr4.Runtime;
3+
using Rubberduck.Parsing.Symbols;
4+
using Rubberduck.Parsing.VBA;
5+
using Rubberduck.Refactorings.Rename;
6+
using Rubberduck.UI;
7+
using Rubberduck.UI.Refactorings;
8+
using Rubberduck.VBEditor;
9+
using Rubberduck.VBEditor.VBEInterfaces.RubberduckCodePane;
10+
11+
namespace Rubberduck.Inspections
12+
{
13+
public class UseMeaningfulNameInspectionResult : CodeInspectionResultBase
14+
{
15+
private readonly IEnumerable<CodeInspectionQuickFix> _quickFixes;
16+
17+
public UseMeaningfulNameInspectionResult(IInspection inspection, Declaration target, RubberduckParserState parseResult, ICodePaneWrapperFactory wrapperFactory)
18+
: base(inspection, string.Format(inspection.Description, target.IdentifierName), target)
19+
{
20+
_quickFixes = new[]
21+
{
22+
new RenameDeclarationQuickFix(target.Context, target.QualifiedSelection, target, parseResult, wrapperFactory),
23+
};
24+
}
25+
26+
public override IEnumerable<CodeInspectionQuickFix> QuickFixes { get { return _quickFixes; } }
27+
}
28+
29+
/// <summary>
30+
/// A code inspection quickfix that addresses a VBProject bearing the default name.
31+
/// </summary>
32+
public class RenameDeclarationQuickFix : CodeInspectionQuickFix
33+
{
34+
private readonly Declaration _target;
35+
private readonly RubberduckParserState _parseResult;
36+
private readonly ICodePaneWrapperFactory _wrapperFactory;
37+
38+
public RenameDeclarationQuickFix(ParserRuleContext context, QualifiedSelection selection, Declaration target, RubberduckParserState parseResult, ICodePaneWrapperFactory wrapperFactory)
39+
: base(context, selection, string.Format(RubberduckUI.Rename_DeclarationType, RubberduckUI.ResourceManager.GetString("DeclarationType_" + target.DeclarationType, RubberduckUI.Culture)))
40+
{
41+
_target = target;
42+
_parseResult = parseResult;
43+
_wrapperFactory = wrapperFactory;
44+
}
45+
46+
public override void Fix()
47+
{
48+
var vbe = Selection.QualifiedName.Project.VBE;
49+
50+
using (var view = new RenameDialog())
51+
{
52+
var factory = new RenamePresenterFactory(vbe, view, _parseResult, new MessageBox(), _wrapperFactory);
53+
var refactoring = new RenameRefactoring(factory, new ActiveCodePaneEditor(vbe, _wrapperFactory), new MessageBox());
54+
refactoring.Refactor(_target);
55+
}
56+
}
57+
58+
public override bool CanFixInModule { get { return false; } }
59+
public override bool CanFixInProject { get { return false; } }
60+
}
61+
}

RetailCoder.VBE/Rubberduck.csproj

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -303,6 +303,8 @@
303303
<DependentUpon>InspectionsUI.resx</DependentUpon>
304304
</Compile>
305305
<Compile Include="Inspections\UntypedFunctionUsageInspectionResult.cs" />
306+
<Compile Include="Inspections\UseMeaningfulNameInspection.cs" />
307+
<Compile Include="Inspections\UseMeaningfulNameInspectionResult.cs" />
306308
<Compile Include="Inspections\WriteOnlyPropertyInspection.cs" />
307309
<Compile Include="Inspections\UntypedFunctionUsageInspection.cs" />
308310
<Compile Include="Navigation\NavigateAllReferences.cs" />

0 commit comments

Comments
 (0)