Skip to content

Commit c7a6e1a

Browse files
committed
Merge pull request #902 from Hosch250/UseMeaningfulNameInspection
Use meaningful name inspection
2 parents 23bc446 + b522309 commit c7a6e1a

File tree

5 files changed

+127
-0
lines changed

5 files changed

+127
-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: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
using System.Collections.Generic;
2+
using System.Linq;
3+
using Rubberduck.Parsing.VBA;
4+
using Rubberduck.UI;
5+
using Rubberduck.VBEditor.VBEInterfaces.RubberduckCodePane;
6+
7+
namespace Rubberduck.Inspections
8+
{
9+
public class UseMeaningfulNameInspection : IInspection
10+
{
11+
private readonly ICodePaneWrapperFactory _wrapperFactory;
12+
13+
public UseMeaningfulNameInspection()
14+
{
15+
_wrapperFactory = new CodePaneWrapperFactory();
16+
Severity = CodeInspectionSeverity.Suggestion;
17+
}
18+
19+
public string Name { get { return "UseMeaningfulNameInspection"; } }
20+
public string Description { get { return InspectionsUI.UseMeaningfulNameInspection; } }
21+
public CodeInspectionType InspectionType { get { return CodeInspectionType.MaintainabilityAndReadabilityIssues; } }
22+
public CodeInspectionSeverity Severity { get; set; }
23+
24+
public IEnumerable<CodeInspectionResultBase> GetInspectionResults(RubberduckParserState state)
25+
{
26+
var issues = state.AllDeclarations
27+
.Where(declaration => !declaration.IsBuiltIn &&
28+
(declaration.IdentifierName.Length < 3 ||
29+
char.IsDigit(declaration.IdentifierName.Last()) ||
30+
!declaration.IdentifierName.Any(c => new[] {'a', 'e', 'i', 'o', 'u', 'y'}.Contains(c))
31+
))
32+
.Select(issue => new UseMeaningfulNameInspectionResult(this, issue, state, _wrapperFactory, new MessageBox()))
33+
.ToList();
34+
35+
return issues;
36+
}
37+
}
38+
}
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
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 parserState, ICodePaneWrapperFactory wrapperFactory, IMessageBox messageBox)
18+
: base(inspection, string.Format(inspection.Description, target.IdentifierName), target)
19+
{
20+
_quickFixes = new[]
21+
{
22+
new RenameDeclarationQuickFix(target.Context, target.QualifiedSelection, target, parserState, wrapperFactory, messageBox),
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 _parserState;
36+
private readonly ICodePaneWrapperFactory _wrapperFactory;
37+
private readonly IMessageBox _messageBox;
38+
39+
public RenameDeclarationQuickFix(ParserRuleContext context, QualifiedSelection selection, Declaration target, RubberduckParserState parserState, ICodePaneWrapperFactory wrapperFactory, IMessageBox messageBox)
40+
: base(context, selection, string.Format(RubberduckUI.Rename_DeclarationType, RubberduckUI.ResourceManager.GetString("DeclarationType_" + target.DeclarationType, RubberduckUI.Culture)))
41+
{
42+
_target = target;
43+
_parserState = parserState;
44+
_wrapperFactory = wrapperFactory;
45+
_messageBox = messageBox;
46+
}
47+
48+
public override void Fix()
49+
{
50+
var vbe = Selection.QualifiedName.Project.VBE;
51+
52+
using (var view = new RenameDialog())
53+
{
54+
var factory = new RenamePresenterFactory(vbe, view, _parserState, _messageBox, _wrapperFactory);
55+
var refactoring = new RenameRefactoring(factory, new ActiveCodePaneEditor(vbe, _wrapperFactory), _messageBox);
56+
refactoring.Refactor(_target);
57+
}
58+
}
59+
60+
public override bool CanFixInModule { get { return false; } }
61+
public override bool CanFixInProject { get { return false; } }
62+
}
63+
}

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)