Ejemplo n.º 1
0
        private static Task <Document> RefactorAsync(
            Document document,
            EnumDeclarationSyntax enumDeclaration,
            INamedTypeSymbol enumSymbol,
            ulong?value,
            CancellationToken cancellationToken)
        {
            EqualsValueClauseSyntax equalsValue = null;

            if (value != null)
            {
                equalsValue = EqualsValueClause(CSharpFactory.NumericLiteralExpression(value.Value, enumSymbol.EnumUnderlyingType.SpecialType));
            }

            string name = NameGenerator.Default.EnsureUniqueMemberName(DefaultNames.EnumMember, enumSymbol);

            SyntaxToken identifier = Identifier(name).WithRenameAnnotation();

            EnumMemberDeclarationSyntax newEnumMember = EnumMemberDeclaration(
                default(SyntaxList <AttributeListSyntax>),
                identifier,
                equalsValue);

            EnumDeclarationSyntax newNode = enumDeclaration.AddMembers(newEnumMember);

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

            List <ulong> valuesList = values.ToList();

            for (int i = 0; i < members.Count; i++)
            {
                if (members[i].EqualsValue == null)
                {
                    Optional <ulong> optional = FlagsUtility <ulong> .Instance.GetUniquePowerOfTwo(valuesList, startFromHighestExistingValue);

                    if (optional.HasValue &&
                        ConvertHelpers.CanConvertFromUInt64(optional.Value, enumSymbol.EnumUnderlyingType.SpecialType))
                    {
                        valuesList.Add(optional.Value);

                        EqualsValueClauseSyntax equalsValue = EqualsValueClause(
                            Token(TriviaList(ElasticSpace), SyntaxKind.EqualsToken, TriviaList(ElasticSpace)),
                            CSharpFactory.NumericLiteralExpression(optional.Value, enumSymbol.EnumUnderlyingType.SpecialType));

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

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

            EnumDeclarationSyntax newNode = enumDeclaration.WithMembers(members);

            return(await document.ReplaceNodeAsync(enumDeclaration, newNode, cancellationToken).ConfigureAwait(false));
        }