Exemplo n.º 1
0
        // *DO NOT DELETE*
        // This is used by Ruleset Editor from ManagedSourceCodeAnalysis.dll.
        public IReadOnlyDictionary <string, IEnumerable <DiagnosticDescriptor> > GetAllDiagnosticDescriptors(IVsHierarchy hierarchyOpt)
        {
            if (hierarchyOpt == null)
            {
                return(_diagnosticService.GetAllDiagnosticDescriptors(null));
            }

            // Analyzers are only supported for C# and VB currently.
            var projectsWithHierarchy = _workspace.ProjectTracker.Projects
                                        .Where(p => p.Language == LanguageNames.CSharp || p.Language == LanguageNames.VisualBasic)
                                        .Where(p => p.Hierarchy == hierarchyOpt)
                                        .Select(p => _workspace.CurrentSolution.GetProject(p.Id));

            if (projectsWithHierarchy.Count() <= 1)
            {
                return(_diagnosticService.GetAllDiagnosticDescriptors(projectsWithHierarchy.FirstOrDefault()));
            }
            else
            {
                // Multiple workspace projects map to the same hierarchy, return a union of descriptors for all projects.
                // For example, this can happen for web projects where we create on the fly projects for aspx files.
                var descriptorsMap = ImmutableDictionary.CreateBuilder <string, IEnumerable <DiagnosticDescriptor> >();
                foreach (var project in projectsWithHierarchy)
                {
                    var newDescriptorTuples = _diagnosticService.GetAllDiagnosticDescriptors(project);
                    foreach (var kvp in newDescriptorTuples)
                    {
                        IEnumerable <DiagnosticDescriptor> existingDescriptors;
                        if (descriptorsMap.TryGetValue(kvp.Key, out existingDescriptors))
                        {
                            descriptorsMap[kvp.Key] = existingDescriptors.Concat(kvp.Value).Distinct();
                        }
                        else
                        {
                            descriptorsMap[kvp.Key] = kvp.Value;
                        }
                    }
                }

                return(descriptorsMap.ToImmutable());
            }
        }