/// <summary> /// Initializes the object instance. /// </summary> protected internal override void Initialize() { base.Initialize(); StyleCopVSPackage package = this.ServiceProvider.GetService(typeof(StyleCopVSPackage)) as StyleCopVSPackage; Debug.Assert(package != null, "Unable to locate the package"); this.core = package.Core; this.helper = package.Helper; }
/// <summary> /// Initializes the object instance. /// </summary> protected internal override void Initialize() { base.Initialize(); StyleCopVSPackage package = this.ServiceProvider.GetService(typeof(StyleCopVSPackage)) as StyleCopVSPackage; Debug.Assert(package != null, "Unable to locate the package"); this.core = package.Core; this.helper = package.Helper; }
/// <summary> /// Disposes the contents of the class. /// </summary> /// <param name="disposing">Indicates whether to dispose managed resources.</param> protected override void Dispose(bool disposing) { Param.Ignore(disposing); base.Dispose(disposing); if (disposing) { if (this.helper != null) { this.helper.Dispose(); this.helper = null; } this.core = null; } }
/// <summary> /// Determines whether the StyleCop menu items should be shown. /// </summary> /// <param name="helper">The analysis helper instance.</param> /// <param name="type">Indicates the type of solution artifacts to search.</param> /// <returns>The type of solution node selected.</returns> internal static bool SupportsStyleCop(AnalysisHelper helper, AnalysisType type) { Param.Ignore(helper, type); DTE applicationObject = GetDTE(); if (type == AnalysisType.Solution || type == AnalysisType.Project) { // Create a project enumerator for the correct VS project list. ProjectCollection enumerator = new ProjectCollection(); if (type == AnalysisType.Solution) { enumerator.SolutionProjects = applicationObject.Solution.Projects; } else { enumerator.SelectedProjects = (System.Collections.IEnumerable)applicationObject.ActiveSolutionProjects; } // Enumerate through the VS projects. foreach (Project project in enumerator) { if (project != null) { if (EnumerateProject( project, new ProjectInvoker(IsKnownProjectTypeVisitor), new ProjectItemInvoker(IsKnownFileTypeVisitor), helper, null) != null) { return true; } } } } else if (type == AnalysisType.Item || type == AnalysisType.Folder) { if (AreSelectedFilesKnownFileTypes(helper)) { return true; } } else if (type == AnalysisType.File) { Document document = applicationObject.ActiveDocument; if (document != null && !string.IsNullOrEmpty(document.FullName)) { string fileExtension = Path.GetExtension(document.FullName); if (fileExtension.Length > 0 && fileExtension.StartsWith(".", StringComparison.Ordinal)) { fileExtension = fileExtension.Substring(1, fileExtension.Length - 1); } if (IsKnownFileExtension(fileExtension, helper.Core)) { return true; } } } return false; }
/// <summary> /// Determines whether any of the selected files are known file types. /// </summary> /// <param name="helper">The analysis helper instance.</param> /// <returns>Returns true if one of the selected items is a known type or contains a known type.</returns> private static bool AreSelectedFilesKnownFileTypes(AnalysisHelper helper) { Param.AssertNotNull(helper, "helper"); DTE applicationObject = GetDTE(); // Check whether there are any selected files. if (applicationObject.SelectedItems.Count > 0) { foreach (SelectedItem selectedItem in applicationObject.SelectedItems) { if (selectedItem.ProjectItem != null && selectedItem.ProjectItem.ContainingProject != null) { if (EnumerateProjectItem( selectedItem.ProjectItem, selectedItem.ProjectItem.ContainingProject.Name, null, new ProjectItemInvoker(IsKnownFileTypeVisitor), helper, null) != null) { return true; } } else if (selectedItem.Project != null) { if (EnumerateProject( selectedItem.Project, null, new ProjectItemInvoker(IsKnownFileTypeVisitor), helper, null) != null) { return true; } } } } return false; }
/// <summary> /// Disposes the contents of the class. /// </summary> /// <param name="disposing">Indicates whether to dispose managed resources.</param> protected override void Dispose(bool disposing) { Param.Ignore(disposing); base.Dispose(disposing); if (disposing) { if (this.helper != null) { this.helper.Dispose(); this.helper = null; } this.core = null; } }
/// <summary> /// Creates the list of project files to be analyzed. /// </summary> /// <param name="core"><see cref="T:StyleCopCore">Core object</see> that hosts the environment.</param> /// <param name="type">The analyze type being performed.</param> /// <param name="analysisFilePath">The path to the initial file we are analyzing.</param> /// <param name="analysisHelper">The analysis helper.</param> /// <returns>Returns the list of projects.</returns> internal static IList<CodeProject> GetProjectList(StyleCopCore core, AnalysisType type, out string analysisFilePath, AnalysisHelper analysisHelper) { Param.AssertNotNull(core, "core"); Param.Ignore(type); // Create a list to store the list of code projects to be analyzed. List<CodeProject> codeProjects = new List<CodeProject>(); DTE applicationObject = GetDTE(); analysisFilePath = null; switch (type) { case AnalysisType.Project: case AnalysisType.Solution: // Create a project enumerator for the correct VS project list. ProjectCollection enumerator = new ProjectCollection(); if (type == AnalysisType.Solution) { enumerator.SolutionProjects = applicationObject.Solution.Projects; } else { enumerator.SelectedProjects = (IEnumerable)applicationObject.ActiveSolutionProjects; } // Enumerate through the VS projects. foreach (Project project in enumerator) { // We continue if we know the project type or if its a misc item or a solution folder if (project != null && (IsKnownProjectType(project, analysisHelper) || project.Kind == Constants.vsProjectKindMisc || project.Kind == Constants.vsProjectKindSolutionItems)) { EnumerateProject(project, AddCodeProject, AddFileToProject, codeProjects, null); } } break; case AnalysisType.Folder: case AnalysisType.Item: analysisFilePath = GetSelectedItemFiles(codeProjects); break; case AnalysisType.File: var document = applicationObject.ActiveDocument; if (document != null) { CodeProject codeProject = null; string projectPath = GetProjectPath(document.ProjectItem.ContainingProject); if (projectPath != null) { codeProject = new CodeProject(projectPath.GetHashCode(), projectPath, GetProjectConfiguration(document.ProjectItem.ContainingProject), TargetFrameworkVersion(document.ProjectItem.ContainingProject)); } else if (!string.IsNullOrEmpty(document.FullName)) { codeProject = new CodeProject(document.FullName.GetHashCode(), Path.GetDirectoryName(document.FullName), new StyleCop.Configuration(null)); } if (codeProject != null) { // If this is a designer.cs file (and so a dependant of another file) then we need to add it but also its parent and siblings. analysisFilePath = document.FullName; var allFilesForProjectItem = GetAllFilesForProjectItem(document.ProjectItem); foreach (var path in allFilesForProjectItem) { core.Environment.AddSourceCode(codeProject, path, null); } codeProjects.Add(codeProject); } } break; default: Debug.Fail("Unknown analysis type requested."); break; } return codeProjects; }
/// <summary> /// Determines whether the selected item is included in StyleCop analysis. /// </summary> /// <param name="helper">The analysis helper instance.</param> /// <param name="type">Indicates the type of solution artifacts to search.</param> /// <returns>True if the selected item would be included in analysis.</returns> internal static bool IsItemIncluded(AnalysisHelper helper, AnalysisType type) { Param.Ignore(helper, type); DTE applicationObject = GetDTE(); if (type == AnalysisType.Solution || type == AnalysisType.Project || type == AnalysisType.Folder) { return true; } if (type == AnalysisType.Item) { // Check whether there are any selected files. // More than 1 selected we just return true if (applicationObject.SelectedItems.Count == 0 || applicationObject.SelectedItems.Count > 1) { return true; } foreach (SelectedItem selectedItem in applicationObject.SelectedItems) { if (selectedItem.ProjectItem != null && selectedItem.ProjectItem.ContainingProject != null) { return CheckProjectItemIsIncluded(selectedItem.ProjectItem); } return true; } return false; } if (type == AnalysisType.File) { Document document = applicationObject.ActiveDocument; if (document != null && !string.IsNullOrEmpty(document.FullName)) { return CheckProjectItemIsIncluded(document.ProjectItem); } } return false; }
/// <summary> /// Determines whether the StyleCop menu items should be shown. /// </summary> /// <param name="helper">The analysis helper instance.</param> /// <param name="type">Indicates the type of solution artifacts to search.</param> /// <returns>The type of solution node selected.</returns> internal static bool SupportsStyleCop(AnalysisHelper helper, AnalysisType type) { Param.Ignore(helper, type); DTE applicationObject = GetDTE(); if (type == AnalysisType.Solution || type == AnalysisType.Project) { // Create a project enumerator for the correct VS project list. ProjectCollection enumerator = new ProjectCollection(); if (type == AnalysisType.Solution) { enumerator.SolutionProjects = applicationObject.Solution.Projects; } else { enumerator.SelectedProjects = (IEnumerable)applicationObject.ActiveSolutionProjects; } // Enumerate through the VS projects. foreach (Project project in enumerator) { if (project != null) { // If we've already cached a value for whether this project supports StyleCop, use it, since it is very // expensive to constantly scan massive unmanaged project trees through COM. This used to render Visual // Studio unusable in largely unmanaged solutions (#6662). bool isEnabled; if (ProjectEnabledCache.ContainsKey(project.UniqueName)) { isEnabled = ProjectEnabledCache[project.UniqueName]; } else { isEnabled = EnumerateProject(project, IsKnownProjectTypeVisitor, IsKnownFileTypeVisitor, helper, null) != null; ProjectEnabledCache.Add(project.UniqueName, isEnabled); } if (isEnabled) { return true; } } } return false; } else if (type == AnalysisType.Item || type == AnalysisType.Folder) { if (AreSelectedFilesKnownFileTypes(helper)) { return true; } } else if (type == AnalysisType.File) { Document document = applicationObject.ActiveDocument; if (document != null && !string.IsNullOrEmpty(document.FullName)) { string fileExtension = Path.GetExtension(document.FullName); if (fileExtension.Length > 0 && fileExtension.StartsWith(".", StringComparison.Ordinal)) { fileExtension = fileExtension.Substring(1, fileExtension.Length - 1); } if (IsKnownFileExtension(fileExtension, helper.Core)) { return true; } } } return false; }
/// <summary> /// Determines whether the project is a known project type. /// </summary> /// <param name="project">The project to add.</param> /// <param name="helper">The Analysis helper.</param> /// <returns>Returns True if its a known file type, or False otherwise.</returns> private static bool IsKnownProjectType(Project project, AnalysisHelper helper) { Param.AssertNotNull(project, "project"); Param.AssertNotNull(helper, "helper"); // If this project type exists in the list of known project types, then return true to indicate // that this is a known project type. if (helper.ProjectTypes != null) { Dictionary<string, string> properties; if (helper.ProjectTypes.TryGetValue(project.Kind, out properties)) { return true; } } return false; }