예제 #1
0
        public static async Task <Document> RefactorAsync(
            Document document,
            EnumMemberDeclarationSyntax enumMemberDeclaration,
            CancellationToken cancellationToken)
        {
            SemanticModel semanticModel = await document.GetSemanticModelAsync(cancellationToken).ConfigureAwait(false);

            IFieldSymbol enumMemberSymbol = semanticModel.GetDeclaredSymbol(enumMemberDeclaration, cancellationToken);

            ImmutableArray <EnumFieldInfo> infos = EnumFieldInfo.CreateRange(enumMemberSymbol.ContainingType);

            ExpressionSyntax value = enumMemberDeclaration.EqualsValue?.Value;

            var info = new EnumFieldInfo(enumMemberSymbol);

            List <EnumFieldInfo> values = info.Decompose(infos);

            values.Sort((f, g) =>
            {
                if (f.IsComposite())
                {
                    if (g.IsComposite())
                    {
                        return(((IComparable)f.Value).CompareTo((IComparable)g.Value));
                    }
                    else
                    {
                        return(-1);
                    }
                }
                else if (g.IsComposite())
                {
                    return(1);
                }

                return(((IComparable)f.Value).CompareTo((IComparable)g.Value));
            });

            BinaryExpressionSyntax newValue = CSharpFactory.BitwiseOrExpression(values[0].ToIdentifierName(), values[1].ToIdentifierName());

            for (int i = 2; i < values.Count; i++)
            {
                newValue = CSharpFactory.BitwiseOrExpression(newValue, values[i].ToIdentifierName());
            }

            newValue = newValue.WithFormatterAnnotation();

            return(await document.ReplaceNodeAsync(value, newValue, cancellationToken).ConfigureAwait(false));
        }