Example #1
0
        public virtual async Task <ImmutableDictionary <Project, ImmutableArray <Diagnostic> > > GetProjectDiagnosticsToFixAsync(FixAllContext fixAllContext)
        {
            var project = fixAllContext.Project;

            if (project != null)
            {
                switch (fixAllContext.Scope)
                {
                case FixAllScope.Project:
                    var diagnostics = await fixAllContext.GetDiagnosticsAsync(project).ConfigureAwait(false);

                    var kvp = SpecializedCollections.SingletonEnumerable(KeyValuePair.Create(project, diagnostics));
                    return(ImmutableDictionary.CreateRange(kvp));

                case FixAllScope.Solution:
                    // TODO: Parallelize the diagnostic computation once DiagnosticAnalyzerService is multithreaded.
                    // var projectsAndDiagnostics = new ConcurrentDictionary<Project, ImmutableArray<Diagnostic>>();

                    // var options = new ParallelOptions();
                    // options.CancellationToken = fixAllContext.CancellationToken;
                    // Parallel.ForEach(project.Solution.Projects, options, proj =>
                    // {
                    // });

                    var projectsAndDiagnostics = ImmutableDictionary.CreateBuilder <Project, ImmutableArray <Diagnostic> >();
                    var cancellationToken      = fixAllContext.CancellationToken;
                    foreach (var proj in project.Solution.Projects)
                    {
                        var projectDiagnostics = fixAllContext.GetDiagnosticsAsync(proj).WaitAndGetResult(cancellationToken);
                        if (projectDiagnostics.Any())
                        {
                            projectsAndDiagnostics.Add(proj, projectDiagnostics);
                        }
                    }

                    return(projectsAndDiagnostics.ToImmutable());
                }
            }

            return(ImmutableDictionary <Project, ImmutableArray <Diagnostic> > .Empty);
        }
Example #2
0
        public virtual async Task <ImmutableDictionary <Document, ImmutableArray <Diagnostic> > > GetDocumentDiagnosticsToFixAsync(FixAllContext fixAllContext)
        {
            IEnumerable <Document> documentsToFix = null;
            var document = fixAllContext.Document;
            var project  = fixAllContext.Project;

            var generatedCodeServices = project.Solution.Workspace.Services.GetService <IGeneratedCodeRecognitionService>();

            switch (fixAllContext.Scope)
            {
            case FixAllScope.Document:
                if (document != null && !generatedCodeServices.IsGeneratedCode(document))
                {
                    var diagnostics = await fixAllContext.GetDiagnosticsAsync(document).ConfigureAwait(false);

                    var kvp = SpecializedCollections.SingletonEnumerable(KeyValuePair.Create(document, diagnostics));
                    return(ImmutableDictionary.CreateRange(kvp));
                }

                break;

            case FixAllScope.Project:
                documentsToFix = project.Documents;
                break;

            case FixAllScope.Solution:
                documentsToFix = project.Solution.Projects
                                 .Where(p => p.Language == project.Language)
                                 .SelectMany(p => p.Documents);
                break;
            }

            if (documentsToFix != null && documentsToFix.Any())
            {
                var documentAndDiagnostics = ImmutableDictionary.CreateBuilder <Document, ImmutableArray <Diagnostic> >();
                var cancellationToken      = fixAllContext.CancellationToken;

                // TODO: Parallelize the diagnostic computation once DiagnosticAnalyzerService is multithreaded.

                // var documentAndDiagnostics = new ConcurrentDictionary<Document, ImmutableArray<Diagnostic>>();
                // var options = new ParallelOptions();
                // options.CancellationToken = fixAllContext.CancellationToken;
                // Parallel.ForEach(documentsToFix, options, doc =>
                // {
                // });

                foreach (var doc in documentsToFix)
                {
                    cancellationToken.ThrowIfCancellationRequested();
                    if (!generatedCodeServices.IsGeneratedCode(doc))
                    {
                        var documentDiagnostics = fixAllContext.GetDiagnosticsAsync(doc).WaitAndGetResult(cancellationToken);
                        if (documentDiagnostics.Any())
                        {
                            documentAndDiagnostics.Add(doc, documentDiagnostics);
                        }
                    }
                }

                return(documentAndDiagnostics.ToImmutable());
            }

            return(ImmutableDictionary <Document, ImmutableArray <Diagnostic> > .Empty);
        }