Beispiel #1
0
        public sealed override async Task RegisterCodeFixesAsync(CodeFixContext context)
        {
            SyntaxNode root = await context.GetSyntaxRootAsync().ConfigureAwait(false);

            if (!TryFindFirstAncestorOrSelf(root, context.Span, out EnumDeclarationSyntax enumDeclaration))
            {
                return;
            }

            foreach (Diagnostic diagnostic in context.Diagnostics)
            {
                switch (diagnostic.Id)
                {
                case DiagnosticIdentifiers.AddNewLineBeforeEnumMember:
                {
                    CodeAction codeAction = CodeAction.Create(
                        "Add new line",
                        cancellationToken => AddNewLineBeforeEnumMemberRefactoring.RefactorAsync(context.Document, enumDeclaration, cancellationToken),
                        GetEquivalenceKey(diagnostic));

                    context.RegisterCodeFix(codeAction, diagnostic);
                    break;
                }

                case DiagnosticIdentifiers.SortEnumMembers:
                {
                    CodeAction codeAction = CodeAction.Create(
                        $"Sort '{enumDeclaration.Identifier}' members",
                        cancellationToken => SortEnumMembersAsync(context.Document, enumDeclaration, cancellationToken),
                        GetEquivalenceKey(diagnostic));

                    context.RegisterCodeFix(codeAction, diagnostic);
                    break;
                }
                }
            }
        }
        public sealed override async Task RegisterCodeFixesAsync(CodeFixContext context)
        {
            SyntaxNode root = await context.GetSyntaxRootAsync().ConfigureAwait(false);

            if (!TryFindFirstAncestorOrSelf(root, context.Span, out EnumDeclarationSyntax enumDeclaration))
            {
                return;
            }

            foreach (Diagnostic diagnostic in context.Diagnostics)
            {
                switch (diagnostic.Id)
                {
                case DiagnosticIdentifiers.AddNewLineBeforeEnumMember:
                {
                    CodeAction codeAction = CodeAction.Create(
                        "Add newline",
                        cancellationToken => AddNewLineBeforeEnumMemberRefactoring.RefactorAsync(context.Document, enumDeclaration, cancellationToken),
                        GetEquivalenceKey(diagnostic));

                    context.RegisterCodeFix(codeAction, diagnostic);
                    break;
                }

                case DiagnosticIdentifiers.SortEnumMembers:
                {
                    CodeAction codeAction = CodeAction.Create(
                        $"Sort '{enumDeclaration.Identifier}' members",
                        cancellationToken => SortEnumMembersAsync(context.Document, enumDeclaration, cancellationToken),
                        GetEquivalenceKey(diagnostic));

                    context.RegisterCodeFix(codeAction, diagnostic);
                    break;
                }

                case DiagnosticIdentifiers.EnumShouldDeclareExplicitValues:
                {
                    SemanticModel semanticModel = await context.GetSemanticModelAsync().ConfigureAwait(false);

                    INamedTypeSymbol enumSymbol = semanticModel.GetDeclaredSymbol(enumDeclaration, context.CancellationToken);

                    EnumSymbolInfo enumInfo = EnumSymbolInfo.Create(enumSymbol);

                    ImmutableArray <ulong> values = enumInfo
                                                    .Fields
                                                    .Where(f => f.HasValue && ((EnumMemberDeclarationSyntax)f.Symbol.GetSyntax(context.CancellationToken)).EqualsValue != null)
                                                    .Select(f => f.Value)
                                                    .ToImmutableArray();

                    Optional <ulong> optional = FlagsUtility <ulong> .Instance.GetUniquePowerOfTwo(values);

                    if (!optional.HasValue ||
                        !ConvertHelpers.CanConvert(optional.Value, enumSymbol.EnumUnderlyingType.SpecialType))
                    {
                        return;
                    }

                    CodeAction codeAction = CodeAction.Create(
                        "Declare explicit values",
                        ct => DeclareExplicitValueAsync(context.Document, enumDeclaration, enumSymbol, values, semanticModel, ct),
                        GetEquivalenceKey(diagnostic));

                    context.RegisterCodeFix(codeAction, diagnostic);
                    break;
                }
                }
            }
        }