Exemplo n.º 1
0
            public override async Task <CodeAction?> GetFixAsync(FixAllContext fixAllContext)
            {
                bool addUnsafe = fixAllContext.CodeActionEquivalenceKey.Contains(",AddUnsafe,");
                bool includeFixesWithAdditionalWork            = fixAllContext.CodeActionEquivalenceKey.Contains($",{ConvertToLibraryImportAnalyzer.MayRequireAdditionalWork},");
                ImmutableArray <Diagnostic> diagnosticsInScope = await fixAllContext.GetDiagnosticsInScopeAsync().ConfigureAwait(false);

                return(CodeAction.Create(addUnsafe ? SR.ConvertToLibraryImportAddUnsafe : SR.ConvertToLibraryImport,
                                         async ct =>
                {
                    HashSet <Project> projectsToAddUnsafe = new();
                    SolutionEditor solutionEditor = new SolutionEditor(fixAllContext.Solution);

                    foreach (var diagnostic in diagnosticsInScope)
                    {
                        bool mayRequireAdditionalWork = bool.TryParse(diagnostic.Properties[ConvertToLibraryImportAnalyzer.MayRequireAdditionalWork], out bool mayRequireAdditionalWorkValue)
                                ? mayRequireAdditionalWorkValue
                                : false;
                        if (mayRequireAdditionalWork && !includeFixesWithAdditionalWork)
                        {
                            // Don't fix any diagnostics that require additional work if the "fix all" command wasn't triggered from a location
                            // that was able to warn the user that additional work may be required.
                            continue;
                        }
                        DocumentId documentId = solutionEditor.OriginalSolution.GetDocumentId(diagnostic.Location.SourceTree) !;
                        DocumentEditor editor = await solutionEditor.GetDocumentEditorAsync(documentId, ct).ConfigureAwait(false);
                        SyntaxNode root = await diagnostic.Location.SourceTree.GetRootAsync(ct).ConfigureAwait(false);

                        // Get the syntax node tied to the diagnostic and check that it is a method declaration
                        if (root.FindNode(diagnostic.Location.SourceSpan) is not MethodDeclarationSyntax methodSyntax)
                        {
                            continue;
                        }

                        await ConvertToLibraryImport(editor, methodSyntax, mayRequireAdditionalWork, GetSuffixFromEquivalenceKey(fixAllContext.CodeActionEquivalenceKey), ct).ConfigureAwait(false);

                        // Record this project as a project we need to allow unsafe blocks in.
                        projectsToAddUnsafe.Add(solutionEditor.OriginalSolution.GetDocument(documentId).Project);
                    }

                    Solution solutionWithUpdatedSources = solutionEditor.GetChangedSolution();

                    if (addUnsafe)
                    {
                        foreach (var project in projectsToAddUnsafe)
                        {
                            solutionWithUpdatedSources = AddUnsafe(solutionWithUpdatedSources, project);
                        }
                    }
                    return solutionWithUpdatedSources;
                },
                                         equivalenceKey: fixAllContext.CodeActionEquivalenceKey));
            }