示例#1
0
        private static async Task TestFixAllAsync(Stopwatch stopwatch, Solution solution, ImmutableDictionary <ProjectId, ImmutableArray <Diagnostic> > diagnostics, CancellationToken cancellationToken)
        {
            Console.WriteLine("Calculating fixes");

            var codeFixers = GetAllCodeFixers().SelectMany(x => x.Value).Distinct();

            var equivalenceGroups = new List <CodeFixEquivalenceGroup>();

            foreach (var codeFixer in codeFixers)
            {
                equivalenceGroups.AddRange(await CodeFixEquivalenceGroup.CreateAsync(codeFixer, diagnostics, solution, cancellationToken).ConfigureAwait(true));
            }

            Console.WriteLine($"Found {equivalenceGroups.Count} equivalence groups.");

            Console.WriteLine("Calculating changes");

            foreach (var fix in equivalenceGroups)
            {
                try
                {
                    stopwatch.Restart();
                    Console.WriteLine($"Calculating fix for {fix.CodeFixEquivalenceKey} using {fix.FixAllProvider} for {fix.NumberOfDiagnostics} instances.");
                    await fix.GetOperationsAsync(cancellationToken).ConfigureAwait(true);

                    WriteLine($"Calculating changes completed in {stopwatch.ElapsedMilliseconds}ms. This is {fix.NumberOfDiagnostics / stopwatch.Elapsed.TotalSeconds:0.000} instances/second.", ConsoleColor.Yellow);
                }
                catch (Exception ex)
                {
                    // Report thrown exceptions
                    WriteLine($"The fix '{fix.CodeFixEquivalenceKey}' threw an exception after {stopwatch.ElapsedMilliseconds}ms:", ConsoleColor.Yellow);
                    WriteLine(ex.ToString(), ConsoleColor.Yellow);
                }
            }
        }
        private static void TestFixAll(Stopwatch stopwatch, Solution solution, ImmutableList <Diagnostic> diagnostics)
        {
            Console.WriteLine("Calculating fixes");

            var codeFixers = GetAllCodeFixers().SelectMany(x => x.Value).Distinct();

            var equivalenceGroups = new List <CodeFixEquivalenceGroup>();

            foreach (var codeFixer in codeFixers)
            {
                equivalenceGroups.AddRange(CodeFixEquivalenceGroup.CreateAsync(codeFixer, diagnostics, solution).GetAwaiter().GetResult());
            }

            Console.WriteLine($"Found {equivalenceGroups.Count} equivalence groups.");

            Console.WriteLine("Calculating changes");

            foreach (var fix in equivalenceGroups)
            {
                try
                {
                    stopwatch.Restart();
                    Console.WriteLine($"Calculating fix for {fix.CodeFixEquivalenceKey}.");
                    fix.GetOperationsAsync(CancellationToken.None).GetAwaiter().GetResult();
                    WriteLine($"Calculating changes completed in {stopwatch.ElapsedMilliseconds}ms", ConsoleColor.Yellow);
                }
                catch (Exception ex)
                {
                    // Report thrown exceptions
                    WriteLine($"The fix '{fix.CodeFixEquivalenceKey}' threw an exception:", ConsoleColor.Yellow);
                    WriteLine(ex.ToString(), ConsoleColor.Yellow);
                }
            }
        }
        private static async Task TestFixAllAsync(Stopwatch stopwatch, Solution solution, ImmutableDictionary <ProjectId, ImmutableArray <Diagnostic> > diagnostics, bool applyChanges, CancellationToken cancellationToken)
        {
            Console.WriteLine("Calculating fixes");

            var codeFixers = GetAllCodeFixers().SelectMany(x => x.Value).Distinct();

            var equivalenceGroups = new List <CodeFixEquivalenceGroup>();

            foreach (var codeFixer in codeFixers)
            {
                equivalenceGroups.AddRange(await CodeFixEquivalenceGroup.CreateAsync(codeFixer, diagnostics, solution, cancellationToken).ConfigureAwait(true));
            }

            Console.WriteLine($"Found {equivalenceGroups.Count} equivalence groups.");
            if (applyChanges && equivalenceGroups.Count > 1)
            {
                Console.Error.WriteLine("/apply can only be used with a single equivalence group.");
                return;
            }

            Console.WriteLine("Calculating changes");

            foreach (var fix in equivalenceGroups)
            {
                try
                {
                    stopwatch.Restart();
                    Console.WriteLine($"Calculating fix for {fix.CodeFixEquivalenceKey} using {fix.FixAllProvider} for {fix.NumberOfDiagnostics} instances.");
                    var operations = await fix.GetOperationsAsync(cancellationToken).ConfigureAwait(true);

                    if (applyChanges)
                    {
                        var applyOperations = operations.OfType <ApplyChangesOperation>().ToList();
                        if (applyOperations.Count > 1)
                        {
                            Console.Error.WriteLine("/apply can only apply a single code action operation.");
                        }
                        else if (applyOperations.Count == 0)
                        {
                            Console.WriteLine("No changes were found to apply.");
                        }
                        else
                        {
                            applyOperations[0].Apply(solution.Workspace, cancellationToken);
                        }
                    }

                    WriteLine($"Calculating changes completed in {stopwatch.ElapsedMilliseconds}ms. This is {fix.NumberOfDiagnostics / stopwatch.Elapsed.TotalSeconds:0.000} instances/second.", ConsoleColor.Yellow);
                }
                catch (Exception ex)
                {
                    // Report thrown exceptions
                    WriteLine($"The fix '{fix.CodeFixEquivalenceKey}' threw an exception after {stopwatch.ElapsedMilliseconds}ms:", ConsoleColor.Yellow);
                    WriteLine(ex.ToString(), ConsoleColor.Yellow);
                }
            }
        }