Skip to content

Commit

Permalink
fixes rubberduck-vba#2179 - makes DoNotShow severity also apply to Pa…
Browse files Browse the repository at this point in the history
…rseTree inspections.
  • Loading branch information
retailcoder committed Sep 4, 2016
1 parent 86feaa9 commit 3c023cf
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 25 deletions.
52 changes: 35 additions & 17 deletions RetailCoder.VBE/Inspections/Inspector.cs
Original file line number Diff line number Diff line change
Expand Up @@ -30,13 +30,12 @@ public Inspector(IGeneralConfigService configService, IEnumerable<IInspection> i

private void ConfigServiceSettingsChanged(object sender, EventArgs e)
{
UpdateInspectionSeverity();
var config = _configService.LoadConfiguration();
UpdateInspectionSeverity(config);
}

private void UpdateInspectionSeverity()
private void UpdateInspectionSeverity(Configuration config)
{
var config = _configService.LoadConfiguration();

foreach (var inspection in _inspections)
{
foreach (var setting in config.UserSettings.CodeInspectionSettings.CodeInspections)
Expand All @@ -57,12 +56,14 @@ public async Task<IEnumerable<ICodeInspectionResult>> FindIssuesAsync(Rubberduck
}

state.OnStatusMessageUpdate(RubberduckUI.CodeInspections_Inspecting);
UpdateInspectionSeverity();

var config = _configService.LoadConfiguration();
UpdateInspectionSeverity(config);

var allIssues = new ConcurrentBag<ICodeInspectionResult>();

// Prepare ParseTreeWalker based inspections
var parseTreeWalkResults = GetParseTreeResults(state);
var parseTreeWalkResults = GetParseTreeResults(config, state);
foreach (var parseTreeInspection in _inspections.Where(inspection => inspection.Severity != CodeInspectionSeverity.DoNotShow && inspection is IParseTreeInspection))
{
(parseTreeInspection as IParseTreeInspection).ParseTreeResults = parseTreeWalkResults;
Expand All @@ -86,21 +87,22 @@ public async Task<IEnumerable<ICodeInspectionResult>> FindIssuesAsync(Rubberduck
return allIssues;
}

private ParseTreeResults GetParseTreeResults(RubberduckParserState state)
private ParseTreeResults GetParseTreeResults(Configuration config, RubberduckParserState state)
{
var result = new ParseTreeResults();
var settings = config.UserSettings.CodeInspectionSettings;

foreach (var componentTreePair in state.ParseTrees)
{
/*
Need to reinitialize these for each and every ParseTree we process, since the results are aggregated in the instances themselves
before moving them into the ParseTreeResults after qualifying them
*/
var obsoleteCallStatementListener = new ObsoleteCallStatementInspection.ObsoleteCallStatementListener();
var obsoleteLetStatementListener = new ObsoleteLetStatementInspection.ObsoleteLetStatementListener();
var emptyStringLiteralListener = new EmptyStringLiteralInspection.EmptyStringLiteralListener();
var argListWithOneByRefParamListener = new ProcedureCanBeWrittenAsFunctionInspection.ArgListWithOneByRefParamListener();
var malformedAnnotationListenter = new MalformedAnnotationInspection.MalformedAnnotationStatementListener();
var obsoleteCallStatementListener = IsDisabled<ObsoleteCallStatementInspection>(settings) ? null : new ObsoleteCallStatementInspection.ObsoleteCallStatementListener();
var obsoleteLetStatementListener = IsDisabled<ObsoleteLetStatementInspection>(settings) ? null : new ObsoleteLetStatementInspection.ObsoleteLetStatementListener();
var emptyStringLiteralListener = IsDisabled<EmptyStringLiteralInspection>(settings) ? null : new EmptyStringLiteralInspection.EmptyStringLiteralListener();
var argListWithOneByRefParamListener = IsDisabled<ProcedureCanBeWrittenAsFunctionInspection>(settings) ? null : new ProcedureCanBeWrittenAsFunctionInspection.ArgListWithOneByRefParamListener();
var malformedAnnotationListenter = IsDisabled<MalformedAnnotationInspection>(settings) ? null : new MalformedAnnotationInspection.MalformedAnnotationStatementListener();

var combinedListener = new CombinedParseTreeListener(new IParseTreeListener[]{
obsoleteCallStatementListener,
Expand All @@ -112,15 +114,31 @@ private ParseTreeResults GetParseTreeResults(RubberduckParserState state)

ParseTreeWalker.Default.Walk(combinedListener, componentTreePair.Value);

result.ArgListsWithOneByRefParam = result.ArgListsWithOneByRefParam.Concat(argListWithOneByRefParamListener.Contexts.Select(context => new QualifiedContext(componentTreePair.Key, context)));
result.EmptyStringLiterals = result.EmptyStringLiterals.Concat(emptyStringLiteralListener.Contexts.Select(context => new QualifiedContext(componentTreePair.Key, context)));
result.ObsoleteLetContexts = result.ObsoleteLetContexts.Concat(obsoleteLetStatementListener.Contexts.Select(context => new QualifiedContext(componentTreePair.Key, context)));
result.ObsoleteCallContexts = result.ObsoleteCallContexts.Concat(obsoleteCallStatementListener.Contexts.Select(context => new QualifiedContext(componentTreePair.Key, context)));
result.MalformedAnnotations = result.MalformedAnnotations.Concat(malformedAnnotationListenter.Contexts.Select(context => new QualifiedContext<VBAParser.AnnotationContext>(componentTreePair.Key, context)));
result.ArgListsWithOneByRefParam = argListWithOneByRefParamListener == null
? Enumerable.Empty<QualifiedContext>()
: result.ArgListsWithOneByRefParam.Concat(argListWithOneByRefParamListener.Contexts.Select(context => new QualifiedContext(componentTreePair.Key, context)));
result.EmptyStringLiterals = emptyStringLiteralListener == null
? Enumerable.Empty<QualifiedContext>()
: result.EmptyStringLiterals.Concat(emptyStringLiteralListener.Contexts.Select(context => new QualifiedContext(componentTreePair.Key, context)));
result.ObsoleteLetContexts = obsoleteLetStatementListener == null
? Enumerable.Empty<QualifiedContext>()
: result.ObsoleteLetContexts.Concat(obsoleteLetStatementListener.Contexts.Select(context => new QualifiedContext(componentTreePair.Key, context)));
result.ObsoleteCallContexts = obsoleteCallStatementListener == null
? Enumerable.Empty<QualifiedContext>()
: result.ObsoleteCallContexts.Concat(obsoleteCallStatementListener.Contexts.Select(context => new QualifiedContext(componentTreePair.Key, context)));
result.MalformedAnnotations = malformedAnnotationListenter == null
? Enumerable.Empty<QualifiedContext>()
: result.MalformedAnnotations.Concat(malformedAnnotationListenter.Contexts.Select(context => new QualifiedContext<VBAParser.AnnotationContext>(componentTreePair.Key, context)));
}
return result;
}

private bool IsDisabled<TInspection>(CodeInspectionSettings config) where TInspection : IInspection
{
var setting = config.GetSetting<TInspection>();
return setting != null && setting.Severity == CodeInspectionSeverity.DoNotShow;
}

public void Dispose()
{
if (_configService != null)
Expand Down
9 changes: 2 additions & 7 deletions RetailCoder.VBE/Settings/CodeInspectionSettings.cs
Original file line number Diff line number Diff line change
Expand Up @@ -36,14 +36,9 @@ public CodeInspectionSettings(HashSet<CodeInspectionSetting> inspections, Whitel
RunInspectionsOnSuccessfulParse = runInspectionsOnParse;
}

public CodeInspectionSetting GetSetting(Type inspection)
public CodeInspectionSetting GetSetting<TInspection>() where TInspection : IInspection
{
var proto = Convert.ChangeType(Activator.CreateInstance(inspection), inspection);
var existing = CodeInspections.FirstOrDefault(s => s.Name.Equals(proto.GetType().ToString()));
if (existing != null) return existing;
var setting = new CodeInspectionSetting(proto as IInspectionModel);
CodeInspections.Add(setting);
return setting;
return CodeInspections.FirstOrDefault(s => s.Name.Equals(typeof(TInspection).Name.ToString()));
}
}

Expand Down
2 changes: 1 addition & 1 deletion Rubberduck.Parsing/VBA/CombinedParseTreeListener.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ public class CombinedParseTreeListener : IParseTreeListener
private readonly IReadOnlyList<IParseTreeListener> _listeners;
public CombinedParseTreeListener(IEnumerable<IParseTreeListener> listeners)
{
_listeners = listeners.ToList();
_listeners = listeners.Where(listener => listener != null).ToList();
}

public void EnterEveryRule(ParserRuleContext ctx)
Expand Down

0 comments on commit 3c023cf

Please sign in to comment.