Skip to content

Instantly share code, notes, and snippets.

@jamesfoster
Created June 20, 2014 17:49
Show Gist options
  • Save jamesfoster/883d7e51bdb280308eb4 to your computer and use it in GitHub Desktop.
Save jamesfoster/883d7e51bdb280308eb4 to your computer and use it in GitHub Desktop.
Respect the RulesEnabledByDefault setting
---
Project/Src/StyleCop/AnalyzersOptions.cs | 15 +++++++++++--
Project/Src/StyleCop/Settings/WritableSettings.cs | 26 +++++++++++++++++------
2 files changed, 33 insertions(+), 8 deletions(-)
diff --git a/Project/Src/StyleCop/AnalyzersOptions.cs b/Project/Src/StyleCop/AnalyzersOptions.cs
index 384bf30..56b11d5 100644
--- a/Project/Src/StyleCop/AnalyzersOptions.cs
+++ b/Project/Src/StyleCop/AnalyzersOptions.cs
@@ -831,7 +831,11 @@ namespace StyleCop
Rule rule = ruleNode.Tag as Rule;
bool overridden = false;
- if (analyzer != null)
+ if (!tabControl.MergedSettings.RulesEnabledByDefault)
+ {
+ overridden = ruleNode.Checked;
+ }
+ else
{
// Create a property representing the current value of the selection.
string propertyName = rule.Name + "#Enabled";
@@ -1252,7 +1256,14 @@ namespace StyleCop
BooleanProperty enabledDisabledSetting = analyzer.GetRuleSetting(this.tabControl.MergedSettings, rule.Name, "Enabled") as BooleanProperty;
if (enabledDisabledSetting == null)
{
- ruleNode.Checked = rule.EnabledByDefault;
+ if (this.tabControl.MergedSettings.RulesEnabledByDefault)
+ {
+ ruleNode.Checked = rule.EnabledByDefault;
+ }
+ else
+ {
+ ruleNode.Checked = false;
+ }
}
else
{
diff --git a/Project/Src/StyleCop/Settings/WritableSettings.cs b/Project/Src/StyleCop/Settings/WritableSettings.cs
index 920b406..baedecd 100644
--- a/Project/Src/StyleCop/Settings/WritableSettings.cs
+++ b/Project/Src/StyleCop/Settings/WritableSettings.cs
@@ -287,11 +287,14 @@ namespace StyleCop
/// <param name="nodeNameAttribute">
/// An optional name attribute value for the new property node.
/// </param>
+ /// <param name="rulesEnabledByDefault">
+ /// Indicates whether rules are enabled by default.
+ /// </param>
/// <returns>
/// Returns true if at least one property was saved.
/// </returns>
private static bool SavePropertyCollection(
- XmlNode rootNode, string nodeName, PropertyCollection properties, PropertyCollection parentProperties, bool aggregate, string nodeNameAttribute)
+ XmlNode rootNode, string nodeName, PropertyCollection properties, PropertyCollection parentProperties, bool aggregate, string nodeNameAttribute, bool rulesEnabledByDefault)
{
Param.AssertNotNull(rootNode, "rootNode");
Param.AssertValidString(nodeName, "nodeName");
@@ -338,12 +341,12 @@ namespace StyleCop
skip = true;
}
}
- else if (property.IsDefault)
+ else if (PropertyIsDefault(property, rulesEnabledByDefault))
{
skip = true;
}
}
- else if (property.IsDefault)
+ else if (PropertyIsDefault(property, rulesEnabledByDefault))
{
skip = true;
}
@@ -378,6 +381,17 @@ namespace StyleCop
return propertyWritten;
}
+ private static bool PropertyIsDefault(PropertyValue property, bool rulesEnabledByDefault)
+ {
+ if (property.PropertyName.EndsWith("#Enabled") && !rulesEnabledByDefault)
+ {
+ var booleanProperty = property as BooleanProperty;
+ return booleanProperty == null || booleanProperty.Value == false;
+ }
+
+ return property.IsDefault;
+ }
+
/// <summary>
/// Saves a single property value.
/// </summary>
@@ -550,7 +564,7 @@ namespace StyleCop
parentGlobalSettings = parentSettings.GlobalSettings;
}
- SavePropertyCollection(rootElement, "GlobalSettings", settingsToSave.GlobalSettings, parentGlobalSettings, true, null);
+ SavePropertyCollection(rootElement, "GlobalSettings", settingsToSave.GlobalSettings, parentGlobalSettings, true, null, true);
}
// Add the parser settings if there are any.
@@ -577,7 +591,7 @@ namespace StyleCop
parentParserSettings = parentSettings.GetAddInSettings(parserSettings.AddIn);
}
- if (SavePropertyCollection(parserNode, "ParserSettings", parserSettings, parentParserSettings, true, null))
+ if (SavePropertyCollection(parserNode, "ParserSettings", parserSettings, parentParserSettings, true, null, true))
{
parsersNode.AppendChild(parserNode);
parserSettingsAdded = true;
@@ -615,7 +629,7 @@ namespace StyleCop
parentAnalyzerSettings = parentSettings.GetAddInSettings(analyzerSettings.AddIn);
}
- if (SavePropertyCollection(analyzerNode, "AnalyzerSettings", analyzerSettings, parentAnalyzerSettings, true, null))
+ if (SavePropertyCollection(analyzerNode, "AnalyzerSettings", analyzerSettings, parentAnalyzerSettings, true, null, settingsToSave.RulesEnabledByDefault))
{
analyzersNode.AppendChild(analyzerNode);
analyzerSettingsAdded = true;
--
1.9.2.msysgit.0
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment