/// <summary> /// Gets a value indicating whether the given rule is enabled for the given document. /// </summary> /// <param name="analyzer"> /// The analyzer which contains the rule. /// </param> /// <param name="ruleName"> /// The rule to check. /// </param> /// <returns> /// Returns true if the rule is enabled; otherwise false. /// </returns> public bool IsRuleEnabled(SourceAnalyzer analyzer, string ruleName) { Param.RequireNotNull(analyzer, "analyzer"); Param.RequireValidString(ruleName, "ruleName"); this.enabledRulesLock.AcquireReaderLock(Timeout.Infinite); try { this.InitializeEnabledRules(); if (this.enabledRules != null) { Dictionary <string, Rule> enabledRulesForAnalyzer = null; if (this.enabledRules.TryGetValue(analyzer, out enabledRulesForAnalyzer) && enabledRulesForAnalyzer != null) { return(enabledRulesForAnalyzer.ContainsKey(ruleName)); } } } finally { this.enabledRulesLock.ReleaseReaderLock(); } return(false); }
/// <summary> /// Loads a property which no longer exists, and translates it into an enabled or /// disabled rule. /// </summary> /// <param name="settings"> /// The settings. /// </param> /// <param name="analyzerId"> /// The ID of the analyzer owning the property. /// </param> /// <param name="propertyName"> /// The name of legacy property. /// </param> /// <param name="nodeText"> /// The property value. /// </param> private static void LoadLegacyAnalyzerSetting(Settings settings, string analyzerId, string propertyName, string nodeText) { Param.AssertNotNull(settings, "settings"); Param.AssertValidString(analyzerId, "analyzerId"); Param.AssertValidString(propertyName, "propertyName"); Param.AssertNotNull(nodeText, "nodeText"); switch (analyzerId) { case "StyleCop.CSharp.DocumentationRules": if (propertyName == "RequireValueTags") { SourceAnalyzer analyzer = settings.Core.GetAnalyzer(analyzerId); if (analyzer != null) { AddInPropertyCollection settingsForAnalyzer = settings.GetAddInSettings(analyzer); if (settingsForAnalyzer == null) { settingsForAnalyzer = new AddInPropertyCollection(analyzer); settings.SetAddInSettings(settingsForAnalyzer); } bool enabled = nodeText != "0"; AddOrUpdateLegacyBooleanProperty("PropertyDocumentationMustHaveValue", enabled, settingsForAnalyzer, analyzer.PropertyDescriptors); AddOrUpdateLegacyBooleanProperty("PropertyDocumentationMustHaveValueText", enabled, settingsForAnalyzer, analyzer.PropertyDescriptors); } } break; } }
/// <summary> /// Initializes a new instance. /// </summary> public PropertyPage(SourceAnalyzer analyzer) : this() { m_analyzer = analyzer; namingRulesPage.Page = this; customRulesPage.Page = this; }
/// <summary> /// Initializes settings from specified document. /// </summary> public void Initialize(SourceAnalyzer analyzer, CodeDocument document) { InitializeCommon(analyzer, document); InitializeDerivings(analyzer, document); InitializeBlockAt(analyzer, document); InitializeEnglishOnly(analyzer, document); InitializeCheckLength(analyzer, document); }
/// <summary> /// Runs the list of analyzers against the given document. /// </summary> /// <param name="document"> /// The document to analyze. /// </param> /// <param name="parser"> /// The parser that created the document. /// </param> /// <param name="analyzers"> /// The list of analyzers to run against the document. /// </param> private void RunAnalyzers(CodeDocument document, SourceParser parser, IEnumerable <SourceAnalyzer> analyzers) { Param.AssertNotNull(document, "document"); Param.AssertNotNull(parser, "parser"); Param.Ignore(analyzers, "analyzers"); StyleCopTrace.In(document, parser, analyzers); if (analyzers != null) { if (parser.SkipAnalysisForDocument(document.SourceCode)) { string format = string.Format(CultureInfo.CurrentCulture, "Skipping: {0} - {1}", document.SourceCode.Project.Location.SubstringAfterLast('\\'), GetRelativeFileName(document.SourceCode)); this.data.Core.SignalOutput(MessageImportance.Normal, format); } else { // Loop through each of the parser's analyzers. // Only call analyzers that are also in the enabled list. foreach (SourceAnalyzer analyzer in parser.Analyzers) { SourceAnalyzer localAnalyzer = analyzer; if (analyzers.Any(enabledAnalyzers => enabledAnalyzers.Id == localAnalyzer.Id)) { // Make sure the user hasn't cancelled us. if (this.data.Core.Cancel) { break; } SourceParser.ClearAnalyzerTags(document); try { if (analyzer.DoAnalysis(document)) { analyzer.AnalyzeDocument(document); } } catch (Exception ex) { StringBuilder details = new StringBuilder(); details.AppendLine(string.Format(CultureInfo.CurrentCulture, "Exception thrown by analyzer '{0}' while processing '{1}'.", analyzer.Name, document.SourceCode.Path)); // Add exception message for help on bugfix. if (!string.IsNullOrEmpty(ex.Message)) { details.AppendLine(string.Format(CultureInfo.CurrentCulture, "Exception message : {0}", ex.Message)); } this.data.Core.SignalOutput(MessageImportance.High, details.ToString()); throw; } } } } } StyleCopTrace.Out(); }
/// <summary> /// Sets the given property on the given parser. /// </summary> /// <param name="settings">The settings collection.</param> /// <param name="analyzerId">The ID of the analyzer.</param> /// <param name="propertyName">The name of the property to set.</param> /// <param name="nodeText">The text of the setting from the settings file.</param> private static void LoadAnalyzerSetting(Settings settings, string analyzerId, string propertyName, string nodeText) { Param.AssertNotNull(settings, "settings"); Param.AssertValidString(analyzerId, "analyzerId"); Param.AssertValidString(propertyName, "propertyName"); Param.AssertValidString(nodeText, "nodeText"); SourceAnalyzer analyzer = settings.Core.GetAnalyzer(analyzerId); if (analyzer != null) { PropertyDescriptor <bool> propertyDescriptor = analyzer.PropertyDescriptors[propertyName] as PropertyDescriptor <bool>; if (propertyDescriptor != null) { settings.SetAddInSettingInternal(analyzer, new BooleanProperty(propertyDescriptor, nodeText != "0")); } } }
/// <summary> /// Loads analyzer settings from the document. /// </summary> /// <param name="documentRoot">The root node of the settings document.</param> /// <param name="settings">Stores the settings.</param> private static void LoadAnalyzerSettings(XmlNode documentRoot, Settings settings) { Param.AssertNotNull(documentRoot, "documentRoot"); Param.AssertNotNull(settings, "settings"); XmlNodeList analyzerNodes = documentRoot.SelectNodes("Analyzers/Analyzer"); if (analyzerNodes != null && analyzerNodes.Count > 0) { foreach (XmlNode analyzerNode in analyzerNodes) { XmlAttribute analyzerId = analyzerNode.Attributes["AnalyzerId"]; if (analyzerId != null && !string.IsNullOrEmpty(analyzerId.Value)) { string analyzerName = ConvertLegacyAddInName(analyzerId.Value); // Get the analyzer instance. SourceAnalyzer analyzerInstance = settings.Core.GetAnalyzer(analyzerName); if (analyzerInstance != null) { // Get the analyzer settings object for this analyzer or create a new one. AddInPropertyCollection settingsForAnalyzer = settings.GetAddInSettings(analyzerInstance); if (settingsForAnalyzer == null) { settingsForAnalyzer = new AddInPropertyCollection(analyzerInstance); settings.SetAddInSettings(settingsForAnalyzer); } // Load the settings for this analyzer. XmlNode analyzerSettingsNode = analyzerNode["AnalyzerSettings"]; if (analyzerSettingsNode != null) { LoadPropertyCollection(analyzerSettingsNode, settingsForAnalyzer, analyzerInstance.PropertyDescriptors, null); } // Load any rule settings for the analyzer. LoadRulesSettings(analyzerNode, settingsForAnalyzer, analyzerInstance.PropertyDescriptors); } } } } }
/// <summary> /// Loads the valid prefixes from the given node.. /// </summary> /// <param name="validPrefixesNode">The node containing the prefixes.</param> /// <param name="settings">The settings collection.</param> private static void LoadValidPrefixes(XmlNode validPrefixesNode, Settings settings) { Param.AssertNotNull(validPrefixesNode, "validPrefixesNode"); Param.AssertNotNull(settings, "settings"); string[] prefixes = validPrefixesNode.InnerText.Split(','); // Get the analyzer. SourceAnalyzer analyzer = settings.Core.GetAnalyzer("StyleCop.CSharp.NamingRules"); if (analyzer != null) { // Get the property descriptor. CollectionPropertyDescriptor propertyDescriptor = analyzer.PropertyDescriptors["Hungarian"] as CollectionPropertyDescriptor; if (propertyDescriptor != null) { settings.SetAddInSettingInternal(analyzer, new CollectionProperty(propertyDescriptor, prefixes)); } } }
/// <summary> /// Enables or disables all ruels for the given analyzers. /// </summary> /// <param name="disabledAnalyzersNode">The node representing the analyzer.</param> /// <param name="settings">The settings.</param> /// <param name="enabled">Indicates whether to enable or disable the rules.</param> private static void EnableDisableAnalyzerRules(XmlNode disabledAnalyzersNode, Settings settings, bool enabled) { Param.AssertNotNull(disabledAnalyzersNode, "disabledAnalyzersNode"); Param.AssertNotNull(settings, "settings"); Param.Ignore(enabled); string[] ids = disabledAnalyzersNode.InnerText.Split(','); foreach (string id in ids) { string analyzerId43 = MapAnalyzerId(id); if (analyzerId43 != null) { SourceAnalyzer analyzer = settings.Core.GetAnalyzer(analyzerId43); if (analyzer != null) { ICollection <string> rules = MapAnalyzerToRules(id); if (rules != null) { AddInPropertyCollection settingsForAnalyzer = settings.GetAddInSettings(analyzer); if (settingsForAnalyzer == null) { settingsForAnalyzer = new AddInPropertyCollection(analyzer); settings.SetAddInSettings(settingsForAnalyzer); } foreach (string rule in rules) { AddBooleanProperty(rule + "#Enabled", enabled, settingsForAnalyzer, analyzer.PropertyDescriptors); } } } } } }
/// <summary> /// Gets example summary text for destructor. /// </summary> public static string GetExampleSummaryTextForDestructor(SourceAnalyzer customDocumentationAnalyzer) { return (string)typeof(DocumentationRules).InvokeMember( "GetExampleSummaryTextForDestructor", BindingFlags.Static | BindingFlags.NonPublic | BindingFlags.InvokeMethod, null, customDocumentationAnalyzer, null); }
/// <summary> /// Gets example summary text for constructor. /// </summary> public static string GetExampleSummaryTextForConstructor(SourceAnalyzer customDocumentationAnalyzer, ICodeUnit constructor) { string type = (constructor.Parent is Struct) ? "struct" : "class"; return (string)typeof(DocumentationRules).InvokeMember( "GetExampleSummaryTextForConstructorType", BindingFlags.Static | BindingFlags.NonPublic | BindingFlags.InvokeMethod, null, customDocumentationAnalyzer, new object[] { constructor, type }); }
/// <summary> /// Initializes custom analyzer based on the standard one. /// </summary> public static void InitializeCustomAnalyzer( StyleCopCore originalCore, StyleCopCore customCore, string originalAnalyzerId, SourceAnalyzer customAnalyzer) { Dictionary<string, SourceAnalyzer> originalAnalyzers = (Dictionary<string, SourceAnalyzer>)typeof(StyleCopCore).InvokeMember( "analyzers", BindingFlags.Instance | BindingFlags.NonPublic | BindingFlags.GetField, null, originalCore, null); SourceAnalyzer originalAnalyzer = originalAnalyzers[originalAnalyzerId]; Dictionary<string, Rule> originalRules = (Dictionary<string, Rule>)typeof(StyleCopAddIn).InvokeMember( "rules", BindingFlags.Instance | BindingFlags.NonPublic | BindingFlags.GetField, null, originalAnalyzer, null); typeof(StyleCopAddIn).InvokeMember( "core", BindingFlags.Instance | BindingFlags.NonPublic | BindingFlags.SetField, null, customAnalyzer, new object[] { customCore }); Dictionary<string, Rule> customRules = new Dictionary<string, Rule>(); foreach (KeyValuePair<string, Rule> pair in originalRules) { Rule originalRule = pair.Value; Rule customRule = (Rule)typeof(Rule).InvokeMember( "Rule", BindingFlags.Instance | BindingFlags.NonPublic | BindingFlags.CreateInstance, null, customCore, new object[] { originalRule.Name, originalRule.Namespace, originalRule.CheckId, originalRule.Context, originalRule.Warning, originalRule.Description, originalRule.RuleGroup, true, false }); customRules[pair.Key] = customRule; } typeof(StyleCopAddIn).InvokeMember( "rules", BindingFlags.Instance | BindingFlags.NonPublic | BindingFlags.SetField, null, customAnalyzer, new object[] { customRules }); CustomCsParser customParser = new CustomCsParser(); typeof(SourceAnalyzer).InvokeMember( "parser", BindingFlags.Instance | BindingFlags.NonPublic | BindingFlags.SetField, null, customAnalyzer, new object[] { customParser }); }
/// <summary> /// Initializes common settings. /// </summary> private void InitializeCommon(SourceAnalyzer analyzer, CodeDocument document) { m_names = new Dictionary<string, string>(); m_examples = new Dictionary<string, string>(); m_regulars = new Dictionary<string, Regex>(); string words = SettingsManager.GetValue<string>( analyzer, document.Settings, NamingSettings.Words); foreach (string setting in NamingSettings.GetCommon()) { string name = SettingsManager.GetFriendlyName(analyzer, setting); m_names.Add(setting, name); string value = SettingsManager.GetValue<string>(analyzer, document.Settings, setting); if (String.IsNullOrEmpty(value)) { m_examples.Add(setting, null); m_regulars.Add(setting, null); } else { string example = NamingMacro.BuildExample(value); m_examples.Add(setting, example); Regex regex = NamingMacro.BuildRegex(value, words); m_regulars.Add(setting, regex); } } }
private void FillAnalyzerRules(SourceAnalyzer analyzer, TreeNode analyzerNode) { Param.AssertNotNull(analyzer, "analyzer"); Param.AssertNotNull(analyzerNode, "analyzerNode"); // Iterate through each rule in the analyzer and add a checkbox for each. foreach (Rule rule in analyzer.AddInRules) { // Only show rules which can be disabled. if (rule.CanDisable) { // Get or create the rule group node for this rule, if necessary. TreeNode ruleParentNode = analyzerNode; if (!string.IsNullOrEmpty(rule.RuleGroup)) { ruleParentNode = FindMatchingRuleGroupNode(analyzerNode.Nodes, rule.RuleGroup); if (ruleParentNode == null) { ruleParentNode = new TreeNode(rule.RuleGroup); ruleParentNode.ImageKey = ruleParentNode.SelectedImageKey = RuleGroupNode; InsertIntoSortedTree(analyzerNode.Nodes, ruleParentNode); } } // Create a node for this rule. TreeNode ruleNode = new TreeNode(string.Concat(rule.CheckId, ": ", rule.Name)); ruleNode.ImageKey = ruleNode.SelectedImageKey = RuleNode; ruleNode.Tag = rule; InsertIntoSortedTree(ruleParentNode.Nodes, ruleNode); this.InitializeRuleCheckedState(rule, ruleNode); } } }
/// <summary> /// Initializes setting for using English characters only. /// </summary> private void InitializeEnglishOnly(SourceAnalyzer analyzer, CodeDocument document) { string definition = SettingsManager.GetValue<string>( analyzer, document.Settings, NamingSettings.EnglishOnly); m_englishOnly = new EnglishOnly().ConvertTo(definition); }
/// <summary> /// Initializes setting for blocking @ character. /// </summary> private void InitializeBlockAt(SourceAnalyzer analyzer, CodeDocument document) { string definition = SettingsManager.GetValue<string>( analyzer, document.Settings, NamingSettings.BlockAt); m_blockAt = new BlockAt().ConvertTo(definition); }
/// <summary> /// Initializes setting for checking derived classes. /// </summary> private void InitializeDerivings(SourceAnalyzer analyzer, CodeDocument document) { m_derivings = new List<string>(); string derivings = SettingsManager.GetValue<string>( analyzer, document.Settings, NamingSettings.Derivings); m_derivings.AddRange( derivings.Split( new[] { ' ' }, StringSplitOptions.RemoveEmptyEntries)); }
/// <summary> /// Gets a value indicating whether the given rule is enabled for the given document. /// </summary> /// <param name="analyzer"> /// The analyzer which contains the rule. /// </param> /// <param name="ruleName"> /// The rule to check. /// </param> /// <returns> /// Returns true if the rule is enabled; otherwise false. /// </returns> public bool IsRuleEnabled(SourceAnalyzer analyzer, string ruleName) { Param.RequireNotNull(analyzer, "analyzer"); Param.RequireValidString(ruleName, "ruleName"); this.enabledRulesLock.AcquireReaderLock(Timeout.Infinite); try { this.InitializeEnabledRules(); if (this.enabledRules != null) { Dictionary<string, Rule> enabledRulesForAnalyzer = null; if (this.enabledRules.TryGetValue(analyzer, out enabledRulesForAnalyzer) && enabledRulesForAnalyzer != null) { return enabledRulesForAnalyzer.ContainsKey(ruleName); } } } finally { this.enabledRulesLock.ReleaseReaderLock(); } return false; }
/// <summary> /// Initializes setting for checking name length. /// </summary> private void InitializeCheckLength(SourceAnalyzer analyzer, CodeDocument document) { string definition = SettingsManager.GetValue<string>( analyzer, document.Settings, NamingSettings.CheckLength); m_checkLength = new CheckLength().ConvertTo(definition); string exceptions = SettingsManager.GetValue<string>( analyzer, document.Settings, NamingSettings.CheckLengthExceptions); m_checkLengthExceptions = new HashSet<string>( exceptions .Split(new[] { ' ' }, StringSplitOptions.RemoveEmptyEntries) .Select(word => word.ToLowerInvariant())); }
/// <summary> /// Initializes the rule analyzer. /// </summary> /// <param name="sourceAnalyzer">The source analyzer.</param> public void Initialize(SourceAnalyzer sourceAnalyzer) { this.SourceAnalyzer = sourceAnalyzer; }