Skip to content

Commit 83cce66

Browse files
committed
Add Hungarian notation inspection, whitelist quickfix.
1 parent 10e5f45 commit 83cce66

12 files changed

+331
-72
lines changed

RetailCoder.VBE/Inspections/HungarianNotationInspection.cs

Lines changed: 18 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,9 @@
99
using Rubberduck.Inspections.Results;
1010
using Rubberduck.Parsing.Symbols;
1111
using Rubberduck.Parsing.VBA;
12+
using Rubberduck.Settings;
13+
using Rubberduck.SettingsProvider;
14+
using Rubberduck.UI;
1215

1316
namespace Rubberduck.Inspections
1417
{
@@ -95,9 +98,14 @@ public sealed class HungarianNotationInspection : InspectionBase
9598

9699
#endregion
97100

98-
public HungarianNotationInspection(RubberduckParserState state, CodeInspectionSeverity defaultSeverity = CodeInspectionSeverity.Suggestion)
99-
: base(state, defaultSeverity)
101+
private readonly IMessageBox _messageBox;
102+
private readonly IPersistanceService<CodeInspectionSettings> _settings;
103+
104+
public HungarianNotationInspection(IMessageBox messageBox, RubberduckParserState state, IPersistanceService<CodeInspectionSettings> settings)
105+
: base(state, CodeInspectionSeverity.Suggestion)
100106
{
107+
_messageBox = messageBox;
108+
_settings = settings;
101109
}
102110

103111
public override string Description
@@ -112,11 +120,15 @@ public override CodeInspectionType InspectionType
112120

113121
public override IEnumerable<InspectionResultBase> GetInspectionResults()
114122
{
123+
var settings = _settings.Load(new CodeInspectionSettings()) ?? new CodeInspectionSettings();
124+
var whitelistedNames = settings.WhitelistedIdentifiers.Select(s => s.Identifier).ToList();
125+
115126
var hungarians = UserDeclarations
116-
.Where(declaration => TargetDeclarationTypes.Contains(declaration.DeclarationType) &&
117-
HungarianIdentifierRegex.IsMatch(declaration.IdentifierName))
118-
.Select(issue => new HungarianNotationInspectionResult(this, issue))
119-
.ToList();
127+
.Where(declaration => !whitelistedNames.Contains(declaration.IdentifierName) &&
128+
TargetDeclarationTypes.Contains(declaration.DeclarationType) &&
129+
HungarianIdentifierRegex.IsMatch(declaration.IdentifierName))
130+
.Select(issue => new IdentifierNameInspectionResult(this, issue, State, _messageBox, _settings))
131+
.ToList();
120132
return hungarians;
121133
}
122134
}
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Linq;
4+
using System.Text;
5+
using System.Threading.Tasks;
6+
using Antlr4.Runtime;
7+
using Rubberduck.Inspections.Abstract;
8+
using Rubberduck.Inspections.Resources;
9+
using Rubberduck.Parsing.Symbols;
10+
using Rubberduck.Settings;
11+
using Rubberduck.SettingsProvider;
12+
using Rubberduck.VBEditor;
13+
14+
namespace Rubberduck.Inspections.QuickFixes
15+
{
16+
public class AddIdentifierToWhiteListQuickFix : QuickFixBase
17+
{
18+
private readonly IPersistanceService<CodeInspectionSettings> _settings;
19+
private readonly Declaration _target;
20+
21+
public AddIdentifierToWhiteListQuickFix(ParserRuleContext context, QualifiedSelection selection, Declaration target, IPersistanceService<CodeInspectionSettings> settings)
22+
: base(context, selection, InspectionsUI.WhiteListIdentifierQuickFix)
23+
{
24+
_settings = settings;
25+
_target = target;
26+
}
27+
28+
public override void Fix()
29+
{
30+
var inspectionSettings = _settings.Load(new CodeInspectionSettings()) ?? new CodeInspectionSettings();
31+
var whitelist = inspectionSettings.WhitelistedIdentifiers;
32+
inspectionSettings.WhitelistedIdentifiers =
33+
whitelist.Concat(new[] { new WhitelistedIdentifierSetting(_target.IdentifierName) }).ToArray();
34+
_settings.Save(inspectionSettings);
35+
}
36+
}
37+
}

RetailCoder.VBE/Inspections/Resources/InspectionsUI.Designer.cs

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

RetailCoder.VBE/Inspections/Resources/InspectionsUI.resx

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -317,7 +317,7 @@ If the parameter can be null, ignore this inspection result; passing a null valu
317317
<data name="ProcedureShouldBeFunctionInspectionQuickFix" xml:space="preserve">
318318
<value>Implement as function and update usages.</value>
319319
</data>
320-
<data name="UseMeaningfulNameInspectionResultFormat" xml:space="preserve">
320+
<data name="IdentifierNameInspectionResultFormat" xml:space="preserve">
321321
<value>Consider renaming {0} '{1}'</value>
322322
</data>
323323
<data name="UseMeaningfulNameInspectionMeta" xml:space="preserve">
@@ -612,7 +612,7 @@ If the parameter can be null, ignore this inspection result; passing a null valu
612612
<data name="HungarianNotationInspectionName" xml:space="preserve">
613613
<value>Variable uses Hungarian notation.</value>
614614
</data>
615-
<data name="HungarianNotationInspectionResultFormat" xml:space="preserve">
616-
<value>Consider renaming {0} '{1}'</value>
615+
<data name="WhiteListIdentifierQuickFix" xml:space="preserve">
616+
<value>Add to whitelist</value>
617617
</data>
618618
</root>

RetailCoder.VBE/Inspections/Results/HungarianNotationInspectionResult.cs

Lines changed: 0 additions & 42 deletions
This file was deleted.

RetailCoder.VBE/Inspections/Results/UseMeaningfulNameInspectionResult.cs renamed to RetailCoder.VBE/Inspections/Results/IdentifierNameInspectionResult.cs

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,29 +5,32 @@
55
using Rubberduck.Inspections.Resources;
66
using Rubberduck.Parsing.Symbols;
77
using Rubberduck.Parsing.VBA;
8+
using Rubberduck.Settings;
9+
using Rubberduck.SettingsProvider;
810
using Rubberduck.UI;
911

1012
namespace Rubberduck.Inspections.Results
1113
{
12-
public class UseMeaningfulNameInspectionResult : InspectionResultBase
14+
public class IdentifierNameInspectionResult : InspectionResultBase
1315
{
1416
private readonly IEnumerable<QuickFixBase> _quickFixes;
1517

16-
public UseMeaningfulNameInspectionResult(IInspection inspection, Declaration target, RubberduckParserState parserState, IMessageBox messageBox)
18+
public IdentifierNameInspectionResult(IInspection inspection, Declaration target, RubberduckParserState parserState, IMessageBox messageBox, IPersistanceService<CodeInspectionSettings> settings)
1719
: base(inspection, target)
1820
{
1921
_quickFixes = new QuickFixBase[]
2022
{
2123
new RenameDeclarationQuickFix(target.Context, target.QualifiedSelection, target, parserState, messageBox),
22-
new IgnoreOnceQuickFix(Context, QualifiedSelection, Inspection.AnnotationName),
24+
new IgnoreOnceQuickFix(Context, target.QualifiedSelection, Inspection.AnnotationName),
25+
new AddIdentifierToWhiteListQuickFix(Context, target.QualifiedSelection, target, settings)
2326
};
2427
}
2528

2629
public override IEnumerable<QuickFixBase> QuickFixes { get { return _quickFixes; } }
2730

2831
public override string Description
2932
{
30-
get { return string.Format(InspectionsUI.UseMeaningfulNameInspectionResultFormat, RubberduckUI.ResourceManager.GetString("DeclarationType_" + Target.DeclarationType, UI.Settings.Settings.Culture), Target.IdentifierName).Captialize(); }
33+
get { return string.Format(InspectionsUI.IdentifierNameInspectionResultFormat, RubberduckUI.ResourceManager.GetString("DeclarationType_" + Target.DeclarationType, UI.Settings.Settings.Culture), Target.IdentifierName).Captialize(); }
3134
}
3235

3336
public override NavigateCodeEventArgs GetNavigationArgs()

RetailCoder.VBE/Inspections/UseMeaningfulNameInspection.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ public override IEnumerable<InspectionResultBase> GetInspectionResults()
3939
char.IsDigit(declaration.IdentifierName.Last()) ||
4040
!declaration.IdentifierName.Any(c =>
4141
"aeiouy".Any(a => string.Compare(a.ToString(), c.ToString(), StringComparison.OrdinalIgnoreCase) == 0))))
42-
.Select(issue => new UseMeaningfulNameInspectionResult(this, issue, State, _messageBox))
42+
.Select(issue => new IdentifierNameInspectionResult(this, issue, State, _messageBox, _settings))
4343
.ToList();
4444

4545
return issues;

RetailCoder.VBE/Rubberduck.csproj

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -367,13 +367,13 @@
367367
<Compile Include="Common\UndocumentedAttribute.cs" />
368368
<Compile Include="Inspections\HungarianNotationInspection.cs" />
369369
<Compile Include="Inspections\ImplicitDefaultMemberAssignmentInspection.cs" />
370+
<Compile Include="Inspections\QuickFixes\AddIdentifierToWhiteListQuickFix.cs" />
370371
<Compile Include="Inspections\Resources\InspectionsUI.Designer.cs">
371372
<AutoGen>True</AutoGen>
372373
<DesignTime>True</DesignTime>
373374
<DependentUpon>InspectionsUI.resx</DependentUpon>
374375
</Compile>
375376
<Compile Include="Inspections\Results\AggregateInspectionResult.cs" />
376-
<Compile Include="Inspections\Results\HungarianNotationInspectionResult.cs" />
377377
<Compile Include="Inspections\Results\ImplicitDefaultMemberAssignmentInspectionResult.cs" />
378378
<Compile Include="Inspections\QuickFixes\IntroduceLocalVariableQuickFix.cs" />
379379
<Compile Include="Inspections\QuickFixes\OptionExplicitQuickFix.cs" />
@@ -544,7 +544,7 @@
544544
<Compile Include="Inspections\Results\ProcedureCanBeWrittenAsFunctionInspectionResult.cs" />
545545
<Compile Include="Inspections\Results\UntypedFunctionUsageInspectionResult.cs" />
546546
<Compile Include="Inspections\UseMeaningfulNameInspection.cs" />
547-
<Compile Include="Inspections\Results\UseMeaningfulNameInspectionResult.cs" />
547+
<Compile Include="Inspections\Results\IdentifierNameInspectionResult.cs" />
548548
<Compile Include="Inspections\WriteOnlyPropertyInspection.cs" />
549549
<Compile Include="Inspections\UntypedFunctionUsageInspection.cs" />
550550
<Compile Include="Navigation\CodeExplorer\CodeExplorerCustomFolderViewModel.cs" />

RubberduckTests/Inspections/GeneralInspectionTests.cs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,9 @@ public void InspectionResultFormatStringsExist()
6060
typeof(ConstantNotUsedInspection).Name,
6161
typeof(ParameterNotUsedInspection).Name,
6262
typeof(ProcedureNotUsedInspection).Name,
63-
typeof(VariableNotUsedInspection).Name
63+
typeof(VariableNotUsedInspection).Name,
64+
typeof(UseMeaningfulNameInspection).Name,
65+
typeof(HungarianNotationInspection).Name
6466
};
6567

6668
var inspections = typeof(InspectionBase).Assembly.GetTypes()

0 commit comments

Comments
 (0)