Exemple #1
0
        /// <summary>
        /// Gets the rules.
        /// </summary>
        /// <param name="analyzer">
        /// The analyzer.
        /// </param>
        /// <returns>
        /// A list of all the rules for the <see cref="SourceAnalyzer"/>.
        /// </returns>
        private static List <StyleCopRule> GetRules(SourceAnalyzer analyzer)
        {
            XmlDocument         xmlDocument     = StyleCopCore.LoadAddInResourceXml(analyzer.GetType(), null);
            List <StyleCopRule> xmlDefinedRules = GetRulesFromXml(xmlDocument);

            return(xmlDefinedRules);
        }
        /// <summary>
        /// Initializes settings from specified document.
        /// </summary>
        public void Initialize(SourceAnalyzer analyzer, CodeDocument document)
        {
            IndentOptions = GetOptionsData <IndentOptionsData>(
                analyzer,
                document,
                Rules.CheckAllowedIndentationCharacters);

            LastLineOptions = GetOptionsData <LastLineOptionsData>(
                analyzer,
                document,
                Rules.CheckWhetherLastCodeLineIsEmpty);

            CharLimitOptions = GetOptionsData <CharLimitOptionsData>(
                analyzer,
                document,
                Rules.CodeLineMustNotBeLongerThan);

            MethodSizeOptions = GetOptionsData <LimitOptionsData>(
                analyzer,
                document,
                Rules.MethodMustNotContainMoreLinesThan);

            PropertySizeOptions = GetOptionsData <LimitOptionsData>(
                analyzer,
                document,
                Rules.PropertyMustNotContainMoreLinesThan);

            FileSizeOptions = GetOptionsData <LimitOptionsData>(
                analyzer,
                document,
                Rules.FileMustNotContainMoreLinesThan);
        }
        /// <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);
                }
            }
        }
Exemple #4
0
 private void btnAnalyzeSources_Click(object sender, EventArgs e)
 {
     if (Lists.Sources.Count > 0)
     {
         SourceAnalyzer analyzer = new SourceAnalyzer();
         analyzer.ShowDialog();
     }
 }
 /// <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>
        /// Initializes a new instance.
        /// </summary>
        public PropertyPage(SourceAnalyzer analyzer)
            : this()
        {
            m_analyzer = analyzer;

            namingRulesPage.Page = this;
            customRulesPage.Page = this;
        }
        /// <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);
        }
Exemple #8
0
 /// <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>
        /// 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);
        }
Exemple #10
0
        /// <summary>
        /// Gets property descriptor with existing check.
        /// </summary>
        private static PropertyDescriptor GetDescriptor(SourceAnalyzer analyzer, string settingName)
        {
            PropertyDescriptor descriptor = analyzer.PropertyDescriptors[settingName];

            if (descriptor == null)
            {
                throw new InvalidDataException("Setting " + settingName + " is not registered as a known one.");
            }

            return(descriptor);
        }
Exemple #11
0
        /// <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>
        /// Gets options data for specified rule.
        /// </summary>
        private static T GetOptionsData <T>(SourceAnalyzer analyzer, CodeDocument document, Rules rule) where T : ICustomRuleOptionsData
        {
            CustomRule customRule = CustomRules.Get(rule);
            T          data       = (T)customRule.CreateOptionsData();

            string settingValue = SettingsManager.GetValue <string>(
                analyzer,
                document.Settings,
                customRule.SettingName);

            data.ConvertFromValue(settingValue);
            return(data);
        }
        /// <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>
        /// 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()));
        }
Exemple #15
0
        /// <summary>
        /// Gets property value for specified setting.
        /// </summary>
        public static T GetValue <T>(SourceAnalyzer analyzer, Settings settings, string settingName)
        {
            T customValue;

            if (GetCustomValue(analyzer, settingName, out customValue))
            {
                return(customValue);
            }

            PropertyValue <T> setting = (PropertyValue <T>)analyzer.GetSetting(settings, settingName);

            if (setting == null)
            {
                PropertyDescriptor <T> descriptor = (PropertyDescriptor <T>)GetDescriptor(analyzer, settingName);
                return(descriptor.DefaultValue);
            }

            return(setting.Value);
        }
Exemple #16
0
        /// <summary>
        /// Gets customized value if it is possible.
        /// </summary>
        private static bool GetCustomValue <T>(SourceAnalyzer analyzer, string settingName, out T value)
        {
            if (analyzer is StyleCopPlusRules)
            {
                StyleCopPlusRules styleCopPlus = (StyleCopPlusRules)analyzer;
                if (styleCopPlus.SpecialRunningParameters != null)
                {
                    if (styleCopPlus.SpecialRunningParameters.CustomSettings != null)
                    {
                        if (styleCopPlus.SpecialRunningParameters.CustomSettings.ContainsKey(settingName))
                        {
                            value = (T)styleCopPlus.SpecialRunningParameters.CustomSettings[settingName];
                            return(true);
                        }
                    }
                }
            }

            value = default(T);
            return(false);
        }
        private void EnsureDiagnostics()
        {
            if (_diagnostics == null)
            {
                _diagnostics = new ObservableCollection <DiagnosticModel>();

                switch (Type)
                {
                case ItemType.CSharp:
                    var analyzer = new SourceAnalyzer();
                    foreach (var d in analyzer.GetDiagnostics(AbsolutePath))
                    {
                        _diagnostics.Add(new DiagnosticModel(d));
                    }
                    break;

                default:
                    // only C# files are supported (for now).
                    break;
                }
            }
        }
        /// <summary>
        /// Analyzes rules tree directly from UI.
        /// </summary>
        public static void Initialize(PropertyControl tabControl)
        {
            IPropertyControlPage analyzersOptions = tabControl.Pages[0];
            TreeView             tree             = (TreeView)analyzersOptions.GetType().InvokeMember(
                "analyzeTree",
                BindingFlags.Instance | BindingFlags.NonPublic | BindingFlags.GetField,
                null,
                analyzersOptions,
                null);

            s_ruleNodes = new Dictionary <string, Dictionary <string, TreeNode> >();

            foreach (TreeNode parserNode in tree.Nodes)
            {
                foreach (TreeNode analyzerNode in parserNode.Nodes)
                {
                    SourceAnalyzer analyzer = (SourceAnalyzer)analyzerNode.Tag;
                    s_ruleNodes[analyzer.Id] = new Dictionary <string, TreeNode>();
                    AnalyzeRuleNodes(s_ruleNodes[analyzer.Id], analyzerNode);
                }
            }
        }
Exemple #19
0
        /// <summary>
        /// Checks whether original rule is enabled.
        /// </summary>
        private void CheckOriginalRule(CodeDocument document, string analyzerId, Rules rule)
        {
            string ruleName = rule.ToString();

            if (!m_parent.IsRuleEnabled(document, ruleName))
            {
                return;
            }

            SourceAnalyzer analyzer = m_parent.Core.GetAnalyzer(analyzerId);

            if (!analyzer.IsRuleEnabled(document, ruleName))
            {
                return;
            }

            string message = String.Format(
                Resources.ExtendedRuleConflictError,
                m_parent.GetRule(ruleName).CheckId,
                analyzer.GetRule(ruleName).CheckId);

            throw new Exception(message);
        }
        /// <summary>
        /// Gets the rules.
        /// </summary>
        /// <param name="analyzer">
        /// The analyzer.
        /// </param>
        /// <returns>
        /// A list of all the rules for the <see cref="SourceAnalyzer"/>.
        /// </returns>
        private static List<StyleCopRule> GetRules(SourceAnalyzer analyzer)
        {
            XmlDocument xmlDocument = StyleCopCore.LoadAddInResourceXml(analyzer.GetType(), null);
            List<StyleCopRule> xmlDefinedRules = GetRulesFromXml(xmlDocument);

            return xmlDefinedRules;
        }
    /// <summary>
    /// Fills the given analyzer node with it's rules.
    /// </summary>
    /// <param name="analyzer">Source analyzer</param>
    /// <param name="analyzerIter">Analyzer node iterator.</param>
    private void FillAnalyzerRules(SourceAnalyzer analyzer, Gtk.TreeIter analyzerIter)
    {
      IEnumerable<Rule> addinRules = null;

      // We must use reflection at the moment to get all analyzer rules.
      PropertyInfo prop = analyzer.GetType().GetProperty("AddInRules", BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance);
      if (prop != null && prop.CanRead)
      {
        addinRules = prop.GetValue(analyzer, null) as IEnumerable<Rule>;
      }

      if (addinRules != null)
      {
        Dictionary<string, Gtk.TreeIter> tempRuleGroups = new Dictionary<string, Gtk.TreeIter>();

        foreach (Rule rule in addinRules)
        {
          // Only show rules which can be disabled.
          if (rule.CanDisable)
          {
            // Get or create the rule group node for this rule, if necessary.
            Gtk.TreeIter ruleParentIter = analyzerIter;
            if (!string.IsNullOrEmpty(rule.RuleGroup))
            {
              if (!tempRuleGroups.TryGetValue(rule.RuleGroup, out ruleParentIter))
              {
                ruleParentIter = this.rulesStore.AppendValues(analyzerIter, false, null, rule, null, false);
                tempRuleGroups.Add(rule.RuleGroup, ruleParentIter);
              }
            }

            bool isChecked = rule.EnabledByDefault;
            BooleanProperty enabledDisabledSetting = analyzer.GetRuleSetting(this.SettingsHandler.MergedSettings, rule.Name, "Enabled") as BooleanProperty;
            if (enabledDisabledSetting != null)
            {
              isChecked = enabledDisabledSetting.Value;
            }

            bool isOverridden = this.DetectRuleOverride(analyzer, rule, isChecked, false);
            Gtk.TreeIter ruleIter = this.rulesStore.AppendValues(ruleParentIter, isChecked, null, rule, analyzer, isOverridden);
            this.SetParentIterValues(ruleIter, isChecked, null);

            if (isOverridden)
            {
              this.rulesStore.SetValue(ruleParentIter, (int)TreeStoreColumns.Overridden, true);
              this.rulesStore.SetValue(analyzerIter, (int)TreeStoreColumns.Overridden, true);
            }
          }
        }
      }
    }
    /// <summary>
    /// Checks if the given analyzer rule setting is overridden.
    /// </summary>
    /// <param name="analyzer">Source analyzer of the rule to check.</param>
    /// <param name="ruleToCheck">The rule that should be checked.</param>
    /// <param name="isChecked">Is the check box checked or unchecked?</param>
    /// <param name="hasOverriddenChilds">Value that is used for cells that don't have a analyzer.</param>
    /// <returns>True if the rule is overridden, false otherwise.</returns>
    private bool DetectRuleOverride(SourceAnalyzer analyzer, Rule ruleToCheck, bool isChecked, bool hasOverriddenChilds)
    {
      bool overridden = false;

      if (analyzer != null)
      {
        // Create a property representing the current value of the selection.
        string propertyName = ruleToCheck.Name + "#Enabled";
        BooleanProperty localValue = new BooleanProperty(analyzer, propertyName, isChecked);

        // Compare this with the parent value.
        overridden = this.SettingsHandler.SettingsComparer.IsAddInSettingOverwritten(analyzer, propertyName, localValue);
      }
      else
      {
        overridden = hasOverriddenChilds;
      }

      return overridden;
    }
 /// <summary>
 /// Initializes a new instance of the CompanyInformation class.
 /// </summary>
 /// <param name="analyzer">The analyzer that this settings page is attached to.</param>
 public CompanyInformation(DocumentationRules analyzer)
     : this()
 {
     Param.RequireNotNull(analyzer, "analyzer");
     this.analyzer = analyzer;
 }
 /// <summary>
 /// Initializes a new instance of the ValidPrefixes class.
 /// </summary>
 /// <param name="analyzer">
 /// The analyzer that this settings page is attached to.
 /// </param>
 public ValidPrefixes(NamingRules analyzer)
     : this()
 {
     Param.AssertNotNull(analyzer, "analyzer");
     this.analyzer = analyzer;
 }
Exemple #25
0
 /// <summary>
 /// Initializes a new instance of the CompanyInformation class.
 /// </summary>
 /// <param name="analyzer">
 /// The analyzer that this settings page is attached to.
 /// </param>
 public CompanyInformation(DocumentationRules analyzer)
     : this()
 {
     Param.RequireNotNull(analyzer, "analyzer");
     this.analyzer = analyzer;
 }
 /// <summary>
 /// Initializes a new instance of the ValidPrefixes class.
 /// </summary>
 /// <param name="analyzer">
 /// The analyzer that this settings page is attached to.
 /// </param>
 public ValidPrefixes(NamingRules analyzer)
     : this()
 {
     Param.AssertNotNull(analyzer, "analyzer");
     this.analyzer = analyzer;
 }
 /// <summary>
 /// Initializes a new instance of the <see cref="MonoDevelop.StyleCop.CompanyInformationOptionsPanel"/> class.
 /// </summary>
 public CompanyInformationOptionsPanel()
 {
   this.Build();
   this.analyzer = ProjectUtilities.Instance.Core.GetAnalyzer("StyleCop.CSharp.DocumentationRules");
 }
Exemple #28
0
        /// <summary>
        /// Gets friendly name for specified setting.
        /// </summary>
        public static string GetFriendlyName(SourceAnalyzer analyzer, string settingName)
        {
            PropertyDescriptor descriptor = GetDescriptor(analyzer, settingName);

            return(descriptor.FriendlyName);
        }
Exemple #29
0
        private void menuItem7_Click(object sender, EventArgs e)
        {
            SourceAnalyzer snc = new SourceAnalyzer();

            snc.Show();
        }
Exemple #30
0
        /// <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 a new instance of the <see cref="MonoDevelop.StyleCop.ValidPrefixesOptionsPanel"/> class.
    /// </summary>
    public ValidPrefixesOptionsPanel()
    {
      this.Build();

      this.analyzer = ProjectUtilities.Instance.Core.GetAnalyzer("StyleCop.CSharp.NamingRules");
    }