public static async Task ComputeRefactoringAsync(RefactoringContext context, EnumDeclarationSyntax enumDeclaration)
        {
            EnumMemberDeclarationSyntax[] selectedMembers = GetSelectedMembers(enumDeclaration, context.Span).ToArray();

            if (selectedMembers.Length > 1)
            {
                SemanticModel semanticModel = await context.GetSemanticModelAsync().ConfigureAwait(false);

                List <IFieldSymbol> fieldSymbols = GetFieldSymbols(selectedMembers, semanticModel, context.CancellationToken);

                if (fieldSymbols.Count > 1)
                {
                    var enumSymbol = semanticModel.GetDeclaredSymbol(enumDeclaration, context.CancellationToken) as INamedTypeSymbol;

                    if (enumSymbol != null)
                    {
                        object combinedValue = GetCombinedValue(fieldSymbols, enumSymbol);

                        if (combinedValue != null &&
                            !EnumHelper.IsValueDefined(enumSymbol, combinedValue))
                        {
                            string name = GetCombinedName(fieldSymbols);

                            EnumMemberDeclarationSyntax newEnumMember = GenerateEnumHelper.CreateEnumMember(enumSymbol, name, combinedValue);

                            int insertIndex = enumDeclaration.Members.IndexOf(selectedMembers.Last()) + 1;

                            context.RegisterRefactoring(
                                $"Generate enum member '{name}'",
                                cancellationToken => RefactorAsync(context.Document, enumDeclaration, newEnumMember, insertIndex, cancellationToken));
                        }
                    }
                }
            }
        }
        private static async Task <Document> RefactorAsync(
            Document document,
            EnumDeclarationSyntax enumDeclaration,
            INamedTypeSymbol enumSymbol,
            object value,
            CancellationToken cancellationToken)
        {
            EnumMemberDeclarationSyntax newEnumMember = GenerateEnumHelper.CreateEnumMember(enumSymbol, "EnumMember", value);

            EnumDeclarationSyntax newNode = enumDeclaration.AddMembers(newEnumMember);

            return(await document.ReplaceNodeAsync(enumDeclaration, newNode, cancellationToken).ConfigureAwait(false));
        }
Exemple #3
0
        private static Task <Document> RefactorAsync(
            Document document,
            EnumDeclarationSyntax enumDeclaration,
            INamedTypeSymbol enumSymbol,
            object value,
            CancellationToken cancellationToken)
        {
            EnumMemberDeclarationSyntax newEnumMember = GenerateEnumHelper.CreateEnumMember(enumSymbol, Identifier.DefaultEnumMemberName, value);

            EnumDeclarationSyntax newNode = enumDeclaration.AddMembers(newEnumMember);

            return(document.ReplaceNodeAsync(enumDeclaration, newNode, cancellationToken));
        }
        private static async Task <Document> RefactorAsync(
            Document document,
            EnumDeclarationSyntax enumDeclaration,
            INamedTypeSymbol enumSymbol,
            bool startFromHighestExistingValue,
            CancellationToken cancellationToken)
        {
            SeparatedSyntaxList <EnumMemberDeclarationSyntax> members = enumDeclaration.Members;

            SemanticModel semanticModel = await document.GetSemanticModelAsync(cancellationToken).ConfigureAwait(false);

            SpecialType specialType = enumSymbol.EnumUnderlyingType.SpecialType;

            List <object> values = GenerateEnumHelper.GetExplicitValues(enumDeclaration, semanticModel, cancellationToken);

            for (int i = 0; i < members.Count; i++)
            {
                if (members[i].EqualsValue == null)
                {
                    Optional <object> optional = EnumHelper.GetUniquePowerOfTwo(specialType, values, startFromHighestExistingValue);

                    if (optional.HasValue)
                    {
                        values.Add(optional.Value);

                        EqualsValueClauseSyntax equalsValue = EqualsValueClause(CSharpFactory.ConstantExpression(optional.Value));

                        EnumMemberDeclarationSyntax newMember = members[i]
                                                                .WithEqualsValue(equalsValue)
                                                                .WithFormatterAnnotation();

                        members = members.ReplaceAt(i, newMember);
                    }
                    else
                    {
                        break;
                    }
                }
            }

            EnumDeclarationSyntax newNode = enumDeclaration.WithMembers(members);

            return(await document.ReplaceNodeAsync(enumDeclaration, newNode, cancellationToken).ConfigureAwait(false));
        }
        public static async Task ComputeRefactoringAsync(RefactoringContext context, EnumDeclarationSyntax enumDeclaration)
        {
            SemanticModel semanticModel = await context.GetSemanticModelAsync().ConfigureAwait(false);

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

            if (SymbolUtility.IsEnumWithFlagsAttribute(enumSymbol, semanticModel))
            {
                SeparatedSyntaxList <EnumMemberDeclarationSyntax> members = enumDeclaration.Members;

                if (members.Any(f => f.EqualsValue == null))
                {
                    SpecialType specialType = enumSymbol.EnumUnderlyingType.SpecialType;

                    List <object> values = GenerateEnumHelper.GetExplicitValues(enumDeclaration, semanticModel, context.CancellationToken);

                    Optional <object> optional = EnumHelper.GetUniquePowerOfTwo(specialType, values);

                    if (optional.HasValue)
                    {
                        context.RegisterRefactoring(
                            "Generate enum values",
                            cancellationToken => RefactorAsync(context.Document, enumDeclaration, enumSymbol, startFromHighestExistingValue: false, cancellationToken: cancellationToken));

                        if (members.Any(f => f.EqualsValue != null))
                        {
                            Optional <object> optional2 = EnumHelper.GetUniquePowerOfTwo(specialType, values, startFromHighestExistingValue: true);

                            if (optional2.HasValue &&
                                !optional.Value.Equals(optional2.Value))
                            {
                                context.RegisterRefactoring(
                                    $"Generate enum values (starting from {optional2.Value})",
                                    cancellationToken => RefactorAsync(context.Document, enumDeclaration, enumSymbol, startFromHighestExistingValue: true, cancellationToken: cancellationToken));
                            }
                        }
                    }
                }
            }
        }