File: CodeFixes\FixAllOccurrences\AbstractFixAllCodeFixCodeAction.cs
Web Access
Project: src\src\Features\Core\Portable\Microsoft.CodeAnalysis.Features.csproj (Microsoft.CodeAnalysis.Features)
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.
 
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Threading;
using Microsoft.CodeAnalysis.CodeFixesAndRefactorings;
 
namespace Microsoft.CodeAnalysis.CodeFixes;
 
/// <summary>
/// Fix all code action for a code action registered by a <see cref="CodeFixProvider"/>.
/// </summary>
internal abstract class AbstractFixAllCodeFixCodeAction : AbstractFixAllCodeAction
{
    private static readonly HashSet<string> s_predefinedCodeFixProviderNames = GetPredefinedCodeFixProviderNames();
 
    protected AbstractFixAllCodeFixCodeAction(
        IFixAllState fixAllState, bool showPreviewChangesDialog)
        : base(fixAllState, showPreviewChangesDialog)
    {
    }
 
    protected sealed override IFixAllContext CreateFixAllContext(IFixAllState fixAllState, IProgress<CodeAnalysisProgress> progressTracker, CancellationToken cancellationToken)
        => new FixAllContext((FixAllState)fixAllState, progressTracker, cancellationToken);
 
    protected sealed override bool IsInternalProvider(IFixAllState fixAllState)
    {
        var exportAttributes = fixAllState.Provider.GetType().GetTypeInfo().GetCustomAttributes(typeof(ExportCodeFixProviderAttribute), false);
        if (exportAttributes?.Any() == true)
        {
            var exportAttribute = (ExportCodeFixProviderAttribute)exportAttributes.First();
            return !string.IsNullOrEmpty(exportAttribute.Name)
                && s_predefinedCodeFixProviderNames.Contains(exportAttribute.Name);
        }
 
        return false;
    }
 
    private static HashSet<string> GetPredefinedCodeFixProviderNames()
    {
        var names = new HashSet<string>();
 
        var fields = typeof(PredefinedCodeFixProviderNames).GetTypeInfo().DeclaredFields;
        foreach (var field in fields)
        {
            if (field.IsStatic)
            {
                names.Add((string)field.GetValue(null)!);
            }
        }
 
        return names;
    }
}