/// <summary> /// Loads the settings from the document. /// </summary> /// <param name="document">The settings document.</param> /// <param name="settings">Stores the settings.</param> public static void Load(XmlDocument document, Settings settings) { Param.AssertNotNull(document, "document"); Param.AssertNotNull(settings, "settings"); // If the PublicAndProtectedOnly property exists on the Documentation analyzer, rename it to IgnorePrivates. V41Settings.ChangeAnalyzerSettingName( document, "StyleCop.CSharp.Documentation", "PublicAndProtectedOnly", "IgnorePrivates"); // Add the global settings if there are any. XmlNode globalSettingsNode = document.DocumentElement["GlobalSettings"]; if (globalSettingsNode != null) { LoadPropertyCollection( globalSettingsNode, settings.GlobalSettings, settings.Core.PropertyDescriptors, null); } // Load the parser settings. LoadParserSettings(document, settings); // Load the analyzers under this parser. LoadAnalyzerSettings(document, settings); }
/// <summary> /// Loads the settings from the document. /// </summary> /// <param name="document"> /// The settings document. /// </param> /// <param name="settings"> /// Stores the settings. /// </param> public static void Load(XmlDocument document, Settings settings) { Param.AssertNotNull(document, "document"); Param.AssertNotNull(settings, "settings"); Load(document.DocumentElement, settings); }
/// <summary> /// Initializes a new instance of the SettingsMerger class. /// </summary> /// <param name="localSettings"> /// The settings which should be merged. /// </param> /// <param name="environment"> /// The environment in which StyleCop is running. /// </param> public SettingsMerger(Settings localSettings, StyleCopEnvironment environment) { Param.RequireNotNull(localSettings, "localSettings"); Param.RequireNotNull(environment, "environment"); this.localSettings = localSettings; this.environment = environment; }
/// <summary> /// Initializes a new instance of the StyleCopObjectConsole class. /// </summary> /// <param name="environment"> /// The environment. /// </param> /// <param name="defaultSettings"> /// The default settings to use, or null to allow each project to specify its own settings. /// </param> /// <param name="addInPaths"> /// The list of paths to search under for parser and analyzer addins. /// Can be null if no addin paths are provided. /// </param> /// <param name="loadFromDefaultPath"> /// Indicates whether to load addins /// from the default application path. /// </param> public StyleCopObjectConsole(ObjectBasedEnvironment environment, Settings defaultSettings, ICollection<string> addInPaths, bool loadFromDefaultPath) : this(environment, defaultSettings, addInPaths, loadFromDefaultPath, null) { Param.Ignore(environment); Param.Ignore(defaultSettings); Param.Ignore(addInPaths); Param.Ignore(loadFromDefaultPath); }
/// <summary> /// Initializes a new instance of the SettingsComparer class. /// </summary> /// <param name="localSettings">The local settings.</param> /// <param name="parentSettings">The parent setting to merge with the local settings, or null.</param> public SettingsComparer(Settings localSettings, Settings parentSettings) { Param.RequireNotNull(localSettings, "localSettings"); Param.Ignore(parentSettings); this.localSettings = localSettings; this.parentSettings = parentSettings; }
/// <summary> /// Loads the settings from the document. /// </summary> /// <param name="document"> /// The settings document. /// </param> /// <param name="settings"> /// Stores the settings. /// </param> public static void Load(XmlDocument document, Settings settings) { Param.AssertNotNull(document, "document"); Param.AssertNotNull(settings, "settings"); // Move the StatementMustNotUseUnnecessaryParenthesis rule from the ReadabilityRules analyzer to the MaintainabilityRules analyzer // if it exists. MoveRuleToNewAnalyzer( document, "Microsoft.SourceAnalysis.CSharp.ReadabilityRules", "Microsoft.SourceAnalysis.CSharp.MaintainabilityRules", "StatementMustNotUseUnnecessaryParenthesis"); // If the PublicAndProtectedOnly property exists on the DocumentationRules analyzer, rename it to IgnorePrivates. V102Settings.ChangeAnalyzerSettingName(document, "Microsoft.SourceAnalysis.CSharp.DocumentationRules", "PublicAndProtectedOnly", "IgnorePrivates"); // Forward this call to the V4.3 rule class for parsing. V104Settings.Load(document, settings); }
/// <summary> /// Loads parser 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 LoadParserSettings(XmlNode documentRoot, Settings settings) { Param.AssertNotNull(documentRoot, "documentRoot"); Param.AssertNotNull(settings, "settings"); XmlNodeList parsersNodes = documentRoot.SelectNodes("Parsers/Parser"); if (parsersNodes != null && parsersNodes.Count > 0) { foreach (XmlNode parserNode in parsersNodes) { XmlAttribute parserId = parserNode.Attributes["ParserId"]; if (parserId != null && !string.IsNullOrEmpty(parserId.Value)) { string parserName = ConvertLegacyAddInName(parserId.Value); // Get the parser instance. SourceParser parserInstance = settings.Core.GetParser(parserName); if (parserInstance != null) { // Get the parser settings object for this parser or create a new one. AddInPropertyCollection settingsForParser = settings.GetAddInSettings(parserInstance); if (settingsForParser == null) { settingsForParser = new AddInPropertyCollection(parserInstance); settings.SetAddInSettings(settingsForParser); } // Load the settings for this parser. XmlNode parserSettingsNode = parserNode["ParserSettings"]; if (parserSettingsNode != null) { LoadPropertyCollection(parserSettingsNode, settingsForParser, parserInstance.PropertyDescriptors, null); } // Load any rule settings for the parser. LoadRulesSettings(parserNode, settingsForParser, parserInstance.PropertyDescriptors); } } } } }
/// <summary> /// Initializes a new instance of the StyleCopObjectConsole class. /// </summary> /// <param name="environment">The environment.</param> /// <param name="defaultSettings">The default settings to use, or null to allow each project to specify its own settings.</param> /// <param name="addInPaths">The list of paths to search under for parser and analyzer addins. /// Can be null if no addin paths are provided.</param> /// <param name="loadFromDefaultPath">Indicates whether to load addins /// from the default application path.</param> /// <param name="hostTag">An optional tag which can be set by the host.</param> public StyleCopObjectConsole( ObjectBasedEnvironment environment, Settings defaultSettings, ICollection<string> addInPaths, bool loadFromDefaultPath, object hostTag) { Param.RequireNotNull(environment, "environment"); Param.Ignore(defaultSettings); Param.Ignore(addInPaths); Param.Ignore(loadFromDefaultPath); Param.Ignore(hostTag); this.Core = new StyleCopCore(environment, hostTag); this.Core.Initialize(addInPaths, loadFromDefaultPath); this.Core.WriteResultsCache = false; this.InitCore(); this.defaultSettings = defaultSettings; }
/// <summary> /// Loads the settings from the document. /// </summary> /// <param name="documentRoot">The root node of the settings document.</param> /// <param name="settings">Stores the settings.</param> public static void Load(XmlNode documentRoot, Settings settings) { Param.AssertNotNull(documentRoot, "documentRoot"); Param.AssertNotNull(settings, "settings"); // Add the global settings if there are any. XmlNode globalSettingsNode = documentRoot["GlobalSettings"]; if (globalSettingsNode != null) { LoadPropertyCollection( globalSettingsNode, settings.GlobalSettings, settings.Core.PropertyDescriptors, null); } // Load the parser settings. LoadParserSettings(documentRoot, settings); // Load the analyzers under this parser. LoadAnalyzerSettings(documentRoot, settings); // Load the collection of excluded files. LoadFileLists(documentRoot, settings); }
internal UsingSettings(StyleCopAddIn sourceAnalyzer, Settings settings) { this.settingsManager = ServiceLocator.GetService<SettingsManager>(); this.AliasShouldBeLast = this.settingsManager.GetSetting<bool>("AliasShouldBeLast", sourceAnalyzer, settings); string usingDirectiveGroups = this.settingsManager.GetSetting<string>("UsingDirectiveGroups", sourceAnalyzer, settings); this.GroupPrefixes = new List<IList<string>>(); foreach (string groupValue in usingDirectiveGroups.Split(UsingSettings.GroupSeparator)) { this.GroupPrefixes.Add(groupValue.Split(UsingSettings.UsingSeparator)); } foreach (IList<string> prefixList in this.GroupPrefixes) { foreach (string prefix in prefixList) { if (prefix == null) throw new InvalidOperationException("using group cannot be empty"); if (string.IsNullOrEmpty(prefix.Trim())) throw new InvalidOperationException("using group cannot be empty"); } } }
/// <summary> /// Determines the type of merge to perform, based on the local settings file. /// </summary> /// <param name="settings"> /// The settings file /// </param> /// <param name="environment"> /// The environment. /// </param> /// <returns> /// Returns the merge type. /// </returns> private static string DetermineMergeType(Settings settings, StyleCopEnvironment environment) { Param.AssertNotNull(settings, "settings"); Param.Ignore(environment); StringProperty mergeTypeProperty = settings.GlobalSettings.GetProperty(SettingsMerger.MergeSettingsFilesProperty) as StringProperty; string mergeType = SettingsMerger.MergeStyleParent; if (mergeTypeProperty != null) { mergeType = mergeTypeProperty.Value; } // If the merge style is set to link but the current environment doesn't support linking, change it to parent. if ((environment == null || !environment.SupportsLinkedSettings) && string.CompareOrdinal(mergeType, SettingsMerger.MergeStyleLinked) == 0) { mergeType = SettingsMerger.MergeStyleParent; } return mergeType; }
/// <summary> /// Processes the given element. /// </summary> /// <param name="element">The element being visited.</param> /// <param name="settings">The settings.</param> private void CheckForRegionsInElement(Element element, Settings settings) { Param.AssertNotNull(element, "element"); Param.AssertNotNull(settings, "settings"); // If the DoNotUseRegions setting is enabled, then skip this check as the region // will be discovered during the overall regions rule check. if (settings.DoNotPlaceRegionsWithinElements && !settings.DoNotUseRegions && !element.Generated) { if (element.ElementType == ElementType.Method || element.ElementType == ElementType.Accessor || element.ElementType == ElementType.Constructor || element.ElementType == ElementType.Destructor || element.ElementType == ElementType.Field) { for (RegionDirective region = element.FindFirstDescendent<RegionDirective>(); region != null; region = region.FindNextDescendentOf<RegionDirective>(element)) { // If this token is an opening region directive, this is a violation. if (!region.Generated && !region.IsGeneratedCodeRegion) { this.AddViolation(element, region.LineNumber, Rules.DoNotPlaceRegionsWithinElements); } } } } }
/// <summary> /// Checks a region. /// </summary> /// <param name="region">The region to check.</param> /// <param name="parentElement">The parent element.</param> /// <param name="settings">The current settings.</param> private void CheckRegion(RegionDirective region, Element parentElement, Settings settings) { Param.AssertNotNull(region, "region"); Param.AssertNotNull(parentElement, "parentElement"); Param.AssertNotNull(settings, "settings"); if (settings.DoNotUseRegions) { if (!region.Generated && !region.IsGeneratedCodeRegion) { // There should not be any regions in the code. this.AddViolation(parentElement, region.LineNumber, Rules.DoNotUseRegions); } } }
/// <summary> /// Checks the given element. /// </summary> /// <param name="element">The element being visited.</param> /// <param name="settings">The current settings.</param> /// <returns>Returns true to continue, or false to stop the walker.</returns> private bool VisitElement(Element element, Settings settings) { Param.AssertNotNull(element, "element"); Param.Ignore(settings); this.CheckMethodParameters(element); this.CheckForRegionsInElement(element, settings); return true; }
/// <summary> /// Loads a pre-version 4.1 settings document. /// </summary> /// <param name="document"> /// The settings to load. /// </param> /// <param name="settings"> /// The object where the settings will be stored. /// </param> internal static void Load(XmlDocument document, Settings settings) { Param.AssertNotNull(document, "document"); Param.AssertNotNull(settings, "settings"); foreach (XmlNode child in document.DocumentElement.ChildNodes) { switch (child.Name) { case "StyleCopDisabledAnalyzers": EnableDisableAnalyzerRules(child, settings, false); break; case "StyleCopExplicitlyEnabledAnalyzers": EnableDisableAnalyzerRules(child, settings, true); break; case "AnalyzeDesignerFiles": LoadAnalyzeDesignerFilesSetting(settings, child.InnerText); break; case "PublicAndProtectedOnly": LoadAnalyzerSetting(settings, "StyleCop.CSharp.DocumentationRules", "IgnorePrivates", child.InnerText); LoadAnalyzerSetting(settings, "StyleCop.CSharp.DocumentationRules", "IgnoreInternals", child.InnerText); break; case "IncludeFields": LoadAnalyzerSetting(settings, "StyleCop.CSharp.DocumentationRules", "IncludeFields", child.InnerText); break; case "GeneratedCodeElementOrder": LoadAnalyzerSetting(settings, "StyleCop.CSharp.OrderingRules", "GeneratedCodeElementOrder", child.InnerText); break; case "RequireValueTags": LoadLegacyAnalyzerSetting(settings, "StyleCop.CSharp.DocumentationRules", "RequireValueTags", child.InnerText); break; case "GlobalSettingsFilePath": PropertyDescriptor<string> propertyDescriptor = settings.Core.PropertyDescriptors[SettingsMerger.MergeSettingsFilesProperty] as PropertyDescriptor<string>; if (propertyDescriptor != null) { settings.GlobalSettings.Add(new StringProperty(propertyDescriptor, SettingsMerger.MergeStyleLinked)); } propertyDescriptor = settings.Core.PropertyDescriptors[SettingsMerger.LinkedSettingsProperty] as PropertyDescriptor<string>; if (propertyDescriptor != null) { settings.GlobalSettings.Add(new StringProperty(propertyDescriptor, child.InnerText)); } break; case "StyleCopHungarian": LoadValidPrefixes(child, settings); break; } } }
/// <summary> /// Scans all parent paths above the path containing the given settings file, and merges all settings together. /// </summary> /// <param name="originalSettings"> /// The original settings to merge. /// </param> /// <param name="mergeOriginal"> /// Indicates whether the merge the original settings with the parent settings files. /// </param> /// <returns> /// Returns the merged settings. /// </returns> private Settings FindMergedSettingsThroughParentPaths(Settings originalSettings, bool mergeOriginal) { Param.AssertNotNull(originalSettings, "originalSettings"); Param.Ignore(mergeOriginal); if (!originalSettings.DefaultSettings) { bool defaultSettings = false; string parentSettingsPath = this.environment.GetParentSettingsPath(originalSettings.Location); if (string.IsNullOrEmpty(parentSettingsPath) && !originalSettings.DefaultSettings) { defaultSettings = true; parentSettingsPath = this.environment.GetDefaultSettingsPath(); } if (!string.IsNullOrEmpty(parentSettingsPath)) { Settings mergedParentSettings = this.environment.GetSettings(parentSettingsPath, !defaultSettings); mergedParentSettings.DefaultSettings = defaultSettings; if (mergedParentSettings != null) { if (mergeOriginal) { Settings mergedSettings = MergeSettings(mergedParentSettings, originalSettings); mergedSettings.DefaultSettings = defaultSettings; return mergedSettings; } return mergedParentSettings; } } } return mergeOriginal ? originalSettings : null; }
/// <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> /// Loads files specified in file lists. /// </summary> /// <param name="documentRoot"> /// The root node of the settings document. /// </param> /// <param name="settings"> /// Stores the settings. /// </param> private static void LoadFileLists(XmlNode documentRoot, Settings settings) { Param.AssertNotNull(documentRoot, "documentRoot"); Param.AssertNotNull(settings, "settings"); XmlNodeList fileListNodes = documentRoot.SelectNodes("SourceFileList"); foreach (XmlNode fileListNode in fileListNodes) { XmlNodeList fileNodes = fileListNode.SelectNodes("SourceFile"); if (fileNodes.Count > 0) { Settings settingsForFileList = new Settings(settings.Core); XmlNode settingsNode = fileListNode.SelectSingleNode("Settings"); if (settingsNode != null) { V104Settings.Load(settingsNode, settingsForFileList); } SourceFileListSettings sourceFileListSettings = new SourceFileListSettings(settingsForFileList); foreach (XmlNode fileNode in fileNodes) { if (!string.IsNullOrEmpty(fileNode.InnerText)) { sourceFileListSettings.AddFile(fileNode.InnerText); } } settings.AddSourceFileList(sourceFileListSettings); } } }
/// <summary> /// The control must be initialized by calling this method during the host's OnLoad event. /// </summary> /// <param name="hostInstance"> /// Interface implemented by the host object. /// </param> /// <param name="propertyPages"> /// The array of pages to display on the tab control. /// </param> /// <param name="settings"> /// The settings to read from and write to. /// </param> /// <param name="coreInstance"> /// The StyleCop core instance. /// </param> /// <param name="contextItem"> /// The context for the property control. /// </param> internal void Initialize( IPropertyControlHost hostInstance, IList<IPropertyControlPage> propertyPages, WritableSettings settings, StyleCopCore coreInstance, params object[] contextItem) { Param.AssertNotNull(hostInstance, "hostInstance"); Param.Assert(propertyPages != null && propertyPages.Count > 0, "propertyPages", "Cannot be null or empty"); Param.AssertNotNull(settings, "settings"); Param.AssertNotNull(coreInstance, "coreInstance"); Param.Ignore(contextItem); // Make sure we haven't already been intialized. if (this.host != null) { throw new StyleCopException(Strings.PropertyControlAlreadyInitialized); } this.host = hostInstance; this.pageInterfaces = propertyPages; this.localSettings = settings; this.core = coreInstance; this.context = contextItem; // Set the contents of the parent settings file. SettingsMerger merger = new SettingsMerger(this.localSettings, this.core.Environment); this.parentSettings = merger.ParentMergedSettings; this.mergedSettings = merger.MergedSettings; // Set up the settings comparer. this.settingsComparer = new SettingsComparer(this.localSettings, this.parentSettings); // Make sure the context is non-null. if (this.context == null) { this.context = new object[] { }; } this.tabPages = new TabPage[propertyPages.Count]; this.pages = new UserControl[propertyPages.Count]; // Add each of the property pages. int pageCount = 0; // Initialize the settings pages. for (int i = 0; i < propertyPages.Count; ++i) { this.pages[pageCount] = (UserControl)this.pageInterfaces[i]; TabPage tabPage = new TabPage(this.pageInterfaces[i].TabName); this.tabPages[pageCount] = tabPage; tabPage.Controls.Add(this.pages[i]); this.Controls.Add(tabPage); this.pages[i].Dock = DockStyle.Fill; this.SizePage(i); // The first page has already been initialized. this.pageInterfaces[i].Initialize(this); ++pageCount; } // Activate the first page. if (this.TabPages[0] != null) { this.SelectedTab = this.tabPages[0]; this.pageInterfaces[0].Activate(true); } this.SizeChanged += this.OnSizeChanged; }
/// <summary> /// Called when the parent settings have changed. /// </summary> public void RefreshMergedSettings() { // Set the contents of the parent settings file. SettingsMerger merger = new SettingsMerger(this.localSettings, this.core.Environment); this.parentSettings = merger.ParentMergedSettings; this.mergedSettings = merger.MergedSettings; // Set up the settings comparer. this.settingsComparer = new SettingsComparer(this.localSettings, this.parentSettings); for (int i = 0; i < this.pageInterfaces.Count; ++i) { if (this.pageInterfaces[i] != null) { this.pageInterfaces[i].RefreshSettingsOverrideState(); } } }
/// <summary> /// Saves the Settings provided into the XmlDocument. /// </summary> /// <param name="document"> /// The root document. /// </param> /// <param name="environment"> /// The environment that StyleCop is running under, if any. /// </param> /// <param name="rootElement"> /// The element to save our settings to. /// </param> /// <param name="settingsToSave"> /// The settings to save. /// </param> private void SaveSettingsIntoXmlDocument(XmlDocument document, StyleCopEnvironment environment, XmlElement rootElement, Settings settingsToSave) { // Get the parent settings if there are any. SettingsMerger merger = new SettingsMerger(settingsToSave, environment); Settings parentSettings = merger.ParentMergedSettings; // Add the global settings if there are any. if (settingsToSave.GlobalSettings != null && settingsToSave.GlobalSettings.Count > 0) { // Get the global settings from the parent. PropertyCollection parentGlobalSettings = null; if (parentSettings != null) { parentGlobalSettings = parentSettings.GlobalSettings; } SavePropertyCollection(rootElement, "GlobalSettings", settingsToSave.GlobalSettings, parentGlobalSettings, true, null); } // Add the parser settings if there are any. if (settingsToSave.ParserSettings.Count > 0) { bool parserSettingsAdded = false; XmlElement parsersNode = document.CreateElement("Parsers"); foreach (AddInPropertyCollection parserSettings in settingsToSave.ParserSettings) { // Add the settings for this parser if there are any. if (parserSettings.Count > 0) { // Create a node for this parser. XmlElement parserNode = document.CreateElement("Parser"); XmlAttribute parserIdAttribute = document.CreateAttribute("ParserId"); parserIdAttribute.Value = parserSettings.AddIn.Id; parserNode.Attributes.Append(parserIdAttribute); // Get the parser settings from the parent. PropertyCollection parentParserSettings = null; if (parentSettings != null) { parentParserSettings = parentSettings.GetAddInSettings(parserSettings.AddIn); } if (SavePropertyCollection(parserNode, "ParserSettings", parserSettings, parentParserSettings, true, null)) { parsersNode.AppendChild(parserNode); parserSettingsAdded = true; } } } if (parserSettingsAdded) { rootElement.AppendChild(parsersNode); } } // Add the analyzer settings if there are any. if (settingsToSave.AnalyzerSettings.Count > 0) { bool analyzerSettingsAdded = false; XmlElement analyzersNode = document.CreateElement("Analyzers"); foreach (AddInPropertyCollection analyzerSettings in settingsToSave.AnalyzerSettings) { // Add the settings for this analyzer if there are any. if (analyzerSettings.Count > 0) { // Create a node for this analzyer. XmlElement analyzerNode = document.CreateElement("Analyzer"); XmlAttribute analyzerIdAttribute = document.CreateAttribute("AnalyzerId"); analyzerIdAttribute.Value = analyzerSettings.AddIn.Id; analyzerNode.Attributes.Append(analyzerIdAttribute); // Get the analyzer settings from the parent. PropertyCollection parentAnalyzerSettings = null; if (parentSettings != null) { parentAnalyzerSettings = parentSettings.GetAddInSettings(analyzerSettings.AddIn); } if (SavePropertyCollection(analyzerNode, "AnalyzerSettings", analyzerSettings, parentAnalyzerSettings, true, null)) { analyzersNode.AppendChild(analyzerNode); analyzerSettingsAdded = true; } } } if (analyzerSettingsAdded) { rootElement.AppendChild(analyzersNode); } } // Add the sourcefilelists settings if there are any. if (settingsToSave.SourceFileLists.Count > 0) { foreach (SourceFileListSettings sourceFileListSettings in settingsToSave.SourceFileLists) { XmlElement sourceFileListNode = document.CreateElement("SourceFileList"); foreach (string sourceFileListSetting in sourceFileListSettings.SourceFiles) { XmlElement sourceFileNode = document.CreateElement("SourceFile"); sourceFileNode.InnerText = sourceFileListSetting; sourceFileListNode.AppendChild(sourceFileNode); } XmlElement settingsNode = document.CreateElement("Settings"); this.SaveSettingsIntoXmlDocument(document, environment, settingsNode, sourceFileListSettings.Settings); sourceFileListNode.AppendChild(settingsNode); rootElement.AppendChild(sourceFileListNode); } } }
/// <summary> /// Merges two settings files together. /// </summary> /// <param name="originalSettings"> /// The original settings. /// </param> /// <param name="overridingSettings"> /// The settings which are overriding the original settings. /// </param> /// <returns> /// Returns the merged settings. /// </returns> private static Settings MergeSettings(Settings originalSettings, Settings overridingSettings) { Param.AssertNotNull(originalSettings, "originalSettings"); Param.AssertNotNull(overridingSettings, "overridingSettings"); //// TODO Not sure why this has to be true //// TODO Also where are we getting a different core from? Debug.Assert(originalSettings.Core == overridingSettings.Core, "The settings must come from the same core instance."); // Create a new merged settings object. Settings mergedSettings = new Settings(originalSettings.Core); // Merge the global settings together. MergePropertyCollections(originalSettings.GlobalSettings, overridingSettings.GlobalSettings, mergedSettings.GlobalSettings); // Merge the parser settings together. Loop through the settings for each parser in the original settings. foreach (AddInPropertyCollection originalParserSettings in originalSettings.ParserSettings) { // Try to find settings for this parser in the overriding settings. AddInPropertyCollection overridingParserSettings = overridingSettings.GetAddInSettings(originalParserSettings.AddIn); // Create a new merged parser settings object. AddInPropertyCollection mergedParserSettings = new AddInPropertyCollection(originalParserSettings.AddIn); mergedSettings.SetAddInSettings(mergedParserSettings); // Merge the parser settings together. MergePropertyCollections(originalParserSettings, overridingParserSettings, mergedParserSettings); } // Now loop through the settings for each parser in the overriding settings. If there are any parser // settings here that aren't in the merged settings, then copy the settings directly from the overriding settings. // This means that there were no settings for this parser in the original settings. foreach (AddInPropertyCollection overridingParserSettings in overridingSettings.ParserSettings) { AddInPropertyCollection mergedParserSettings = mergedSettings.GetAddInSettings(overridingParserSettings.AddIn); if (mergedParserSettings == null) { mergedSettings.SetAddInSettings((AddInPropertyCollection)overridingParserSettings.Clone()); } } // Merge the analyzer settings together. Loop through the settings for each analyzer in the original settings. foreach (AddInPropertyCollection originalAnalyzerSettings in originalSettings.AnalyzerSettings) { // Try to find settings for this analyzer in the overriding settings. AddInPropertyCollection overridingAnalyzerSettings = overridingSettings.GetAddInSettings(originalAnalyzerSettings.AddIn); // Create a new merged analyzer settings object. AddInPropertyCollection mergedAnalyzerSettings = new AddInPropertyCollection(originalAnalyzerSettings.AddIn); mergedSettings.SetAddInSettings(mergedAnalyzerSettings); // Merge the analyer settings together. MergePropertyCollections(originalAnalyzerSettings, overridingAnalyzerSettings, mergedAnalyzerSettings); } // Now loop through the settings for each analyzer in the overriding settings. If there are any analyzer // settings here that aren't in the merged settings, then copy the settings directly from the overriding settings. // This means that there were no settings for this analyzer in the original settings. foreach (AddInPropertyCollection overridingAnalyzerSettings in overridingSettings.AnalyzerSettings) { AddInPropertyCollection mergedAnalyzerSettings = mergedSettings.GetAddInSettings(overridingAnalyzerSettings.AddIn); if (mergedAnalyzerSettings == null) { mergedSettings.SetAddInSettings((AddInPropertyCollection)overridingAnalyzerSettings.Clone()); } } // Merge the write times together. Set the write time of the merged settings file to the most // recent write time of the two file which were merged. if (originalSettings.WriteTime.CompareTo(overridingSettings.WriteTime) > 0) { mergedSettings.WriteTime = originalSettings.WriteTime; } else { mergedSettings.WriteTime = overridingSettings.WriteTime; } return mergedSettings; }
/// <summary> /// Finds a linked settings document and merges it with the given settings. /// </summary> /// <param name="originalSettings"> /// The original settings. /// </param> /// <param name="mergeOriginal"> /// Indicates whether the merge the original settings with the linked settings. /// </param> /// <returns> /// Returns the merged settings. /// </returns> /// <remarks> /// This method is designed to work only in file-based environments. /// </remarks> private Settings FindMergedSettingsThroughLinkedSettings(Settings originalSettings, bool mergeOriginal) { Param.AssertNotNull(originalSettings, "originalSettings"); Param.Ignore(mergeOriginal); StringProperty linkedSettingsProperty = originalSettings.GlobalSettings.GetProperty(SettingsMerger.LinkedSettingsProperty) as StringProperty; if (linkedSettingsProperty != null && !string.IsNullOrEmpty(linkedSettingsProperty.Value)) { string linkedSettingsFile = Environment.ExpandEnvironmentVariables(linkedSettingsProperty.Value); if (linkedSettingsFile.StartsWith(".", StringComparison.Ordinal) || !linkedSettingsFile.Contains("\\")) { linkedSettingsFile = Utils.MakeAbsolutePath(Path.GetDirectoryName(originalSettings.Location), linkedSettingsFile); } if (File.Exists(linkedSettingsFile)) { Settings mergedLinkedSettings = this.environment.GetSettings(linkedSettingsFile, true); if (mergedLinkedSettings != null) { if (mergeOriginal) { return MergeSettings(mergedLinkedSettings, originalSettings); } return mergedLinkedSettings; } } } // The linked settings do not exist. Just return the original settings. return originalSettings; }
/// <summary> /// Visits one code unit in the document. /// </summary> /// <param name="codeUnit">The item being visited.</param> /// <param name="parentElement">The parent element, if any.</param> /// <param name="parentStatement">The parent statement, if any.</param> /// <param name="parentExpression">The parent expression, if any.</param> /// <param name="parentClause">The parent query clause, if any.</param> /// <param name="parentToken">The parent token, if any.</param> /// <param name="settings">The settings.</param> /// <returns>Returns true to continue, or false to stop the walker.</returns> private bool VisitCodeUnit( CodeUnit codeUnit, Element parentElement, Statement parentStatement, Expression parentExpression, QueryClause parentClause, Token parentToken, Settings settings) { Param.AssertNotNull(codeUnit, "codeUnit"); Param.Ignore(parentElement, parentStatement, parentExpression, parentClause, parentToken); Param.AssertNotNull(settings, "settings"); if (codeUnit.CodeUnitType == CodeUnitType.Element) { return this.VisitElement((Element)codeUnit, settings); } else if (codeUnit.CodeUnitType == CodeUnitType.Expression) { return this.VisitExpression((Expression)codeUnit, parentElement); } else if (codeUnit.Is(LexicalElementType.Token)) { Token token = (Token)codeUnit; if (token.TokenType == TokenType.Type && !token.Parent.Is(TokenType.Type)) { // Check that the type is using the built-in types, if applicable. this.CheckBuiltInType((TypeToken)token, parentElement); } else if (token.TokenType == TokenType.String) { // Check that the string is not using the empty string "" syntax. this.CheckEmptyString(token, parentElement); } } else if (codeUnit.Is(PreprocessorType.Region)) { this.CheckRegion((RegionDirective)codeUnit, parentElement, settings); } else if (codeUnit.Is(LexicalElementType.Comment)) { this.CheckForEmptyComments((Comment)codeUnit, parentElement); } return !this.Cancel; }
/// <summary> /// Loads parser settings from the document. /// </summary> /// <param name="document"> /// The settings document. /// </param> /// <param name="settings"> /// Stores the settings. /// </param> private static void LoadParserSettings(XmlDocument document, Settings settings) { Param.AssertNotNull(document, "document"); Param.AssertNotNull(settings, "settings"); XmlNodeList parsersNodes = document.DocumentElement.SelectNodes("Parsers/Parser"); if (parsersNodes != null && parsersNodes.Count > 0) { foreach (XmlNode parserNode in parsersNodes) { XmlAttribute parserId = parserNode.Attributes["ParserId"]; if (parserId != null && !string.IsNullOrEmpty(parserId.Value)) { string parserName = parserId.Value; if (parserName.Equals("Microsoft.SourceAnalysis.CSharp.CsParser", StringComparison.Ordinal)) { parserName = "StyleCop.CSharp.CsParser"; } // Get the parser instance. SourceParser parserInstance = settings.Core.GetParser(parserName); if (parserInstance != null) { // Get the parser settings object for this parser or create a new one. AddInPropertyCollection settingsForParser = settings.GetAddInSettings(parserInstance); if (settingsForParser == null) { settingsForParser = new AddInPropertyCollection(parserInstance); settings.SetAddInSettings(settingsForParser); } // Load the settings for this parser. XmlNode parserSettingsNode = parserNode["ParserSettings"]; if (parserSettingsNode != null) { LoadPropertyCollection(parserSettingsNode, settingsForParser, parserInstance.PropertyDescriptors, null); } // Load any rule settings for the parser. LoadRulesSettings(parserNode, settingsForParser, parserInstance.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> /// Loads analyzer settings from the document. /// </summary> /// <param name="document"> /// The settings document. /// </param> /// <param name="settings"> /// Stores the settings. /// </param> private static void LoadAnalyzerSettings(XmlDocument document, Settings settings) { Param.AssertNotNull(document, "document"); Param.AssertNotNull(settings, "settings"); XmlNodeList analyzerNodes = document.DocumentElement.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 analyzerId43 = MapAnalyzerId(analyzerId.Value); // Get the analyzer instance for this mapped analyzer ID. SourceAnalyzer analyzerInstance = settings.Core.GetAnalyzer(analyzerId43); 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, analyzerId.Value); } } } } } }
/// <summary> /// Gets the list of valid prefixes for the given project. /// </summary> /// <param name="settings">The settings for the document being parsed.</param> /// <returns>Returns the list of prefixes.</returns> private Dictionary<string, string> GetPrefixes(Settings settings) { Param.Ignore(settings); Dictionary<string, string> validPrefixes = new Dictionary<string, string>(); if (settings != null) { // Get the allowed hungarian prefixes from the local settings file. CollectionProperty list = this.GetSetting(settings, NamingRules.AllowedPrefixesProperty) as CollectionProperty; if (list != null && list.Count > 0) { foreach (string value in list) { if (!string.IsNullOrEmpty(value) && !validPrefixes.ContainsKey(value)) { validPrefixes.Add(value, value); } } } } return validPrefixes; }
/// <summary> /// Gets the settings given the path to the local settings. /// </summary> /// <param name="settingsPath">The path to the settings to load.</param> /// <param name="merge">Indicates whether to merge the settings with parent settings before returning them.</param> /// <param name="exception">Returns an exception if one occured while loading the settings.</param> /// <returns>Returns the settings.</returns> public override Settings GetSettings(string settingsPath, bool merge, out Exception exception) { Param.RequireValidString(settingsPath, "settingsPath"); Param.Ignore(merge); Param.Ignore(merge); // Load the settings file. Settings settings = this.LoadSettingsDocument(settingsPath, true, out exception); if (merge) { // If there are no local settings, create an empty settings file pointing // at the location where we expected the local settings to be. This // will allow us to do a parent merge from this location. if (settings == null) { settings = new Settings(this.Core, settingsPath, Path.GetDirectoryName(settingsPath)); } // Merge the file and return it. SettingsMerger merger = new SettingsMerger(settings, this); settings = merger.MergedSettings; } return settings; }
/// <summary> /// Analyzes or fixes the given document. /// </summary> /// <param name="document">The document.</param> private void ProcessDocument(ICodeDocument document) { Param.AssertNotNull(document, "document"); CsDocument csdocument = document.AsCsDocument(); Settings settings = new Settings(); settings.DoNotUseRegions = this.IsRuleEnabled(document, Rules.DoNotUseRegions.ToString()); settings.DoNotPlaceRegionsWithinElements = this.IsRuleEnabled(document, Rules.DoNotPlaceRegionsWithinElements.ToString()); if (csdocument != null && !csdocument.Generated) { // Checks various formatting rules. csdocument.WalkCodeModel<Settings>(this.VisitCodeUnit, settings); // Check statement formatting rules. this.CheckStatementFormattingRulesForElement(csdocument); // Check the class member rules. this.CheckClassMemberRulesForElements(csdocument, null, null); } }