private static Task <Document> RefactorAsync(
            Document document,
            EnumDeclarationSyntax enumDeclaration,
            TextSpan span,
            CancellationToken cancellationToken)
        {
            SyntaxTrivia trivia = enumDeclaration.FindTrivia(span.Start);

            SyntaxToken token = trivia.Token;

            EnumMemberDeclarationSyntax enumMemberDeclaration = token
                                                                .GetPreviousToken()
                                                                .Parent
                                                                .FirstAncestorOrSelf <EnumMemberDeclarationSyntax>();

            int enumMemberIndex = enumDeclaration.Members.IndexOf(enumMemberDeclaration);

            SyntaxTriviaList trailingTrivia = token.TrailingTrivia;

            SyntaxToken newToken = token.WithTrailingTrivia(trailingTrivia.Skip(trailingTrivia.IndexOf(trivia) + 1));

            EnumDeclarationSyntax newEnumDeclaration = enumDeclaration.ReplaceToken(token, newToken);

            var settings = new DocumentationCommentGeneratorSettings(ImmutableArray.Create(_leadingSlashesRegex.Replace(trivia.ToString(), "")));

            EnumMemberDeclarationSyntax newEnumMemberDeclaration = newEnumDeclaration.Members[enumMemberIndex].WithNewSingleLineDocumentationComment(settings);

            newEnumDeclaration = newEnumDeclaration.WithMembers(newEnumDeclaration.Members.ReplaceAt(enumMemberIndex, newEnumMemberDeclaration));

            return(document.ReplaceNodeAsync(enumDeclaration, newEnumDeclaration, cancellationToken));
        }
        private static Task <Document> RefactorAsync(
            Document document,
            EnumDeclarationSyntax enumDeclaration,
            SeparatedSyntaxListSelection <EnumMemberDeclarationSyntax> selectedMembers,
            string name,
            CancellationToken cancellationToken)
        {
            ExpressionSyntax expression = IdentifierName(selectedMembers.Last().Identifier.WithoutTrivia());

            for (int i = selectedMembers.LastIndex - 1; i >= selectedMembers.FirstIndex; i--)
            {
                expression = BitwiseOrExpression(
                    IdentifierName(selectedMembers.UnderlyingList[i].Identifier.WithoutTrivia()),
                    expression);
            }

            EnumMemberDeclarationSyntax newEnumMember = EnumMemberDeclaration(
                Identifier(name).WithRenameAnnotation(),
                EqualsValueClause(expression));

            EnumDeclarationSyntax newEnumDeclaration = enumDeclaration
                                                       .WithMembers(enumDeclaration.Members.Insert(selectedMembers.LastIndex + 1, newEnumMember));

            return(document.ReplaceNodeAsync(enumDeclaration, newEnumDeclaration, cancellationToken));
        }
Esempio n. 3
0
        private async Task <Document> ReorderEnum(Document document, EnumDeclarationSyntax enumDeclaration, CancellationToken cancellationToken)
        {
            var oldMembers = enumDeclaration.Members;
            var zeroValue  = oldMembers.Where(m =>
            {
                var value = m.EqualsValue?.Value;

                if (value != null && value.Kind() == SyntaxKind.NumericLiteralExpression && (int)((LiteralExpressionSyntax)value).Token.Value == 0)
                {
                    return(true);
                }

                return(false);
            }).First();

            var newMembers = oldMembers.Remove(zeroValue).Insert(0, zeroValue);

            var newEnum = enumDeclaration.WithMembers(newMembers);

            var root = await document.GetSyntaxRootAsync();

            var newRoot = root.ReplaceNode(enumDeclaration, newEnum);

            var newDocument = document.WithSyntaxRoot(newRoot);

            return(newDocument);
        }
Esempio n. 4
0
        private static async Task <Document> SortByValueAsync(
            Document document,
            EnumDeclarationSyntax enumDeclaration,
            ImmutableArray <EnumMemberDeclarationSyntax> selectedMembers,
            CancellationToken cancellationToken)
        {
            SeparatedSyntaxList <EnumMemberDeclarationSyntax> members = enumDeclaration.Members;

            int firstIndex = members.IndexOf(selectedMembers[0]);
            int lastIndex  = members.IndexOf(selectedMembers[selectedMembers.Length - 1]);

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

            var comparer = new EnumMemberDeclarationValueComparer(semanticModel, cancellationToken);

            IEnumerable <EnumMemberDeclarationSyntax> sorted = selectedMembers.OrderBy(f => f, comparer);

            SeparatedSyntaxList <EnumMemberDeclarationSyntax> newMembers = members
                                                                           .Take(firstIndex)
                                                                           .Concat(sorted)
                                                                           .Concat(members.Skip(lastIndex + 1))
                                                                           .ToSeparatedSyntaxList();

            MemberDeclarationSyntax newNode = enumDeclaration.WithMembers(newMembers);

            return(await document.ReplaceNodeAsync(enumDeclaration, newNode, cancellationToken).ConfigureAwait(false));
        }
        private static async Task <Document> DeclareExplicitValueAsync(
            Document document,
            EnumDeclarationSyntax enumDeclaration,
            INamedTypeSymbol enumSymbol,
            ImmutableArray <ulong> values,
            SemanticModel semanticModel,
            CancellationToken cancellationToken)
        {
            bool isFlags = enumSymbol.HasAttribute(MetadataNames.System_FlagsAttribute);

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

            SeparatedSyntaxList <EnumMemberDeclarationSyntax> members = enumDeclaration.Members;

            SeparatedSyntaxList <EnumMemberDeclarationSyntax> newMembers = members;

            for (int i = 0; i < members.Count; i++)
            {
                if (members[i].EqualsValue == null)
                {
                    IFieldSymbol fieldSymbol = semanticModel.GetDeclaredSymbol(members[i], cancellationToken);

                    ulong?value = null;

                    if (isFlags)
                    {
                        Optional <ulong> optional = FlagsUtility <ulong> .Instance.GetUniquePowerOfTwo(reservedValues);

                        if (optional.HasValue &&
                            ConvertHelpers.CanConvert(optional.Value, enumSymbol.EnumUnderlyingType.SpecialType))
                        {
                            value = optional.Value;
                        }
                    }
                    else
                    {
                        value = SymbolUtility.GetEnumValueAsUInt64(fieldSymbol.ConstantValue, enumSymbol);
                    }

                    if (value != null)
                    {
                        reservedValues.Add(value.Value);

                        EqualsValueClauseSyntax equalsValue = EqualsValueClause(NumericLiteralExpression(value.Value, enumSymbol.EnumUnderlyingType.SpecialType));

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

                        newMembers = newMembers.ReplaceAt(i, newMember);
                    }
                }
            }

            EnumDeclarationSyntax newEnumDeclaration = enumDeclaration.WithMembers(newMembers);

            return(await document.ReplaceNodeAsync(enumDeclaration, newEnumDeclaration, cancellationToken).ConfigureAwait(false));
        }
Esempio n. 6
0
        private static Task <Document> RefactorAsync(
            Document document,
            EnumDeclarationSyntax enumDeclaration,
            SeparatedSyntaxListSelection <EnumMemberDeclarationSyntax> selectedMembers,
            INamedTypeSymbol enumSymbol,
            string name,
            SemanticModel semanticModel,
            CancellationToken cancellationToken)
        {
            ExpressionSyntax            expression = null;
            EnumMemberDeclarationSyntax lastMember = null;

            using (IEnumerator <EnumMemberDeclarationSyntax> en = GetMembersToCombine().GetEnumerator())
            {
                if (en.MoveNext())
                {
                    lastMember = en.Current;
                    expression = IdentifierName(en.Current.Identifier.WithoutTrivia());

                    while (en.MoveNext())
                    {
                        expression = BitwiseOrExpression(
                            IdentifierName(en.Current.Identifier.WithoutTrivia()),
                            expression);
                    }
                }
            }

            EnumMemberDeclarationSyntax newEnumMember = EnumMemberDeclaration(
                Identifier(name).WithRenameAnnotation(),
                EqualsValueClause(expression));

            EnumDeclarationSyntax newEnumDeclaration = enumDeclaration
                                                       .WithMembers(enumDeclaration.Members.Insert(selectedMembers.UnderlyingList.IndexOf(lastMember) + 1, newEnumMember));

            return(document.ReplaceNodeAsync(enumDeclaration, newEnumDeclaration, cancellationToken));

            IEnumerable <EnumMemberDeclarationSyntax> GetMembersToCombine()
            {
                for (int i = selectedMembers.Count - 1; i >= 0; i--)
                {
                    IFieldSymbol symbol = semanticModel.GetDeclaredSymbol(selectedMembers[i], cancellationToken);

                    if (symbol.HasConstantValue)
                    {
                        ulong value = SymbolUtility.GetEnumValueAsUInt64(symbol.ConstantValue, enumSymbol);

                        if (!FlagsUtility <ulong> .Instance.IsComposite(value))
                        {
                            yield return(selectedMembers[i]);
                        }
                    }
                }
            }
        }
Esempio n. 7
0
        private static Task <Document> RefactorAsync(
            Document document,
            EnumDeclarationSyntax enumDeclaration,
            INamedTypeSymbol enumSymbol,
            SemanticModel semanticModel,
            CancellationToken cancellationToken)
        {
            ulong value = 0;

            SpecialType numericType = enumSymbol.EnumUnderlyingType.SpecialType;

            IEnumerable <EnumMemberDeclarationSyntax> newMembers = (enumSymbol.HasAttribute(MetadataNames.System_FlagsAttribute))
                ? enumDeclaration.Members.Select(CreateNewFlagsMember)
                : enumDeclaration.Members.Select(CreateNewMember);

            EnumDeclarationSyntax newEnumDeclaration = enumDeclaration.WithMembers(newMembers.ToSeparatedSyntaxList());

            return(document.ReplaceNodeAsync(enumDeclaration, newEnumDeclaration, cancellationToken));

            EnumMemberDeclarationSyntax CreateNewFlagsMember(EnumMemberDeclarationSyntax enumMember)
            {
                if (!ConvertHelpers.CanConvert(value, numericType))
                {
                    return(enumMember);
                }

                IFieldSymbol fieldSymbol = semanticModel.GetDeclaredSymbol(enumMember, cancellationToken);

                if (fieldSymbol.HasConstantValue &&
                    FlagsUtility <ulong> .Instance.IsComposite(SymbolUtility.GetEnumValueAsUInt64(fieldSymbol.ConstantValue, enumSymbol)))
                {
                    return(enumMember);
                }

                EnumMemberDeclarationSyntax newEnumMember = CreateNewEnumMember(enumMember, value, numericType);

                value = (value == 0) ? 1 : value * 2;

                return(newEnumMember);
            }

            EnumMemberDeclarationSyntax CreateNewMember(EnumMemberDeclarationSyntax enumMember)
            {
                if (!ConvertHelpers.CanConvert(value, numericType))
                {
                    return(enumMember);
                }

                EnumMemberDeclarationSyntax newEnumMember = CreateNewEnumMember(enumMember, value, numericType);

                value++;

                return(newEnumMember);
            }
        }
        private static Task <Document> RefactorAsync(
            Document document,
            EnumDeclarationSyntax enumDeclaration,
            EnumMemberDeclarationSyntax newEnumMember,
            int insertIndex,
            CancellationToken cancellationToken)
        {
            EnumDeclarationSyntax newNode = enumDeclaration.WithMembers(enumDeclaration.Members.Insert(insertIndex, newEnumMember));

            return(document.ReplaceNodeAsync(enumDeclaration, newNode, cancellationToken));
        }
        private static async Task <Document> RefactorAsync(
            Document document,
            EnumDeclarationSyntax enumDeclaration,
            EnumMemberDeclarationSyntax newEnumMember,
            int insertIndex,
            CancellationToken cancellationToken)
        {
            SemanticModel semanticModel = await document.GetSemanticModelAsync(cancellationToken).ConfigureAwait(false);

            EnumDeclarationSyntax newNode = enumDeclaration.WithMembers(enumDeclaration.Members.Insert(insertIndex, newEnumMember));

            return(await document.ReplaceNodeAsync(enumDeclaration, newNode, cancellationToken).ConfigureAwait(false));
        }
        private static Task <Document> SortByNameAsync(
            Document document,
            EnumDeclarationSyntax enumDeclaration,
            SeparatedSyntaxListSelection <EnumMemberDeclarationSyntax> selectedMembers,
            CancellationToken cancellationToken)
        {
            IEnumerable <EnumMemberDeclarationSyntax> sorted = selectedMembers.OrderBy(f => f, EnumMemberDeclarationNameComparer.Instance);

            SeparatedSyntaxList <EnumMemberDeclarationSyntax> newMembers = enumDeclaration
                                                                           .Members
                                                                           .ReplaceRange(selectedMembers.FirstIndex, selectedMembers.Count, sorted);

            MemberDeclarationSyntax newNode = enumDeclaration.WithMembers(newMembers);

            return(document.ReplaceNodeAsync(enumDeclaration, newNode, cancellationToken));
        }
        private static async Task <Document> SortEnumMembersAsync(
            Document document,
            EnumDeclarationSyntax enumDeclaration,
            CancellationToken cancellationToken)
        {
            SemanticModel semanticModel = await document.GetSemanticModelAsync(cancellationToken).ConfigureAwait(false);

            SpecialType enumSpecialType = semanticModel.GetDeclaredSymbol(enumDeclaration).EnumUnderlyingType.SpecialType;

            SeparatedSyntaxList <EnumMemberDeclarationSyntax> members = enumDeclaration.Members;

            SeparatedSyntaxList <EnumMemberDeclarationSyntax> newMembers = members
                                                                           .OrderBy(f => GetConstantValue(f, semanticModel, cancellationToken), EnumValueComparer.GetInstance(enumSpecialType))
                                                                           .ToSeparatedSyntaxList();

            if (AreSeparatedWithEmptyLine(members))
            {
                for (int i = 0; i < newMembers.Count; i++)
                {
                    newMembers = newMembers.ReplaceAt(i, newMembers[i].TrimLeadingTrivia());
                }

                for (int i = 0; i < newMembers.Count - 1; i++)
                {
                    SyntaxToken separator = newMembers.GetSeparator(i);

                    newMembers = newMembers.ReplaceSeparator(
                        separator,
                        separator.TrimTrailingTrivia().AppendToTrailingTrivia(new SyntaxTrivia[] { NewLine(), NewLine() }));
                }
            }

            if (newMembers.SeparatorCount == members.SeparatorCount - 1)
            {
                SyntaxNodeOrTokenList newMembersWithSeparators = newMembers.GetWithSeparators();

                newMembersWithSeparators = newMembersWithSeparators.Add(CommaToken());

                newMembers = newMembersWithSeparators.ToSeparatedSyntaxList <EnumMemberDeclarationSyntax>();
            }

            MemberDeclarationSyntax newNode = enumDeclaration
                                              .WithMembers(newMembers)
                                              .WithFormatterAnnotation();

            return(await document.ReplaceNodeAsync(enumDeclaration, newNode, cancellationToken).ConfigureAwait(false));
        }
Esempio n. 12
0
        public static async Task <Document> RefactorAsync(
            Document document,
            EnumDeclarationSyntax enumDeclaration,
            CancellationToken cancellationToken)
        {
            SemanticModel semanticModel = await document.GetSemanticModelAsync(cancellationToken).ConfigureAwait(false);

            SeparatedSyntaxList <EnumMemberDeclarationSyntax> newMembers = enumDeclaration.Members
                                                                           .OrderBy(f => GetConstantValue(f, semanticModel, cancellationToken), EnumMemberValueComparer.Instance)
                                                                           .ToSeparatedSyntaxList();

            MemberDeclarationSyntax newNode = enumDeclaration
                                              .WithMembers(newMembers)
                                              .WithFormatterAnnotation();

            return(await document.ReplaceNodeAsync(enumDeclaration, newNode, cancellationToken).ConfigureAwait(false));
        }
Esempio n. 13
0
        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 = GetExplicitValues(enumDeclaration, semanticModel, cancellationToken);

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

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

                        EqualsValueClauseSyntax equalsValue = EqualsValueClause(CSharpFactory.LiteralExpression(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));
        }
Esempio n. 14
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));
        }
Esempio n. 15
0
        private static async Task <Document> RefactorAsync(
            Document document,
            EnumDeclarationSyntax enumDeclaration,
            CancellationToken cancellationToken)
        {
            SemanticModel semanticModel = await document.GetSemanticModelAsync(cancellationToken).ConfigureAwait(false);

            INamedTypeSymbol symbol = semanticModel.GetDeclaredSymbol(enumDeclaration, cancellationToken);

            string name = NameGenerator.Default.EnsureUniqueMemberName("None", symbol);

            EnumMemberDeclarationSyntax enumMember = EnumMemberDeclaration(
                Identifier(name).WithRenameAnnotation(),
                NumericLiteralExpression(0));

            enumMember = enumMember.WithTrailingTrivia(NewLine());

            EnumDeclarationSyntax newNode = enumDeclaration.WithMembers(enumDeclaration.Members.Insert(0, enumMember));

            return(await document.ReplaceNodeAsync(enumDeclaration, newNode, cancellationToken).ConfigureAwait(false));
        }
Esempio n. 16
0
        private static Task <Document> SortByNameAsync(
            Document document,
            EnumDeclarationSyntax enumDeclaration,
            ImmutableArray <EnumMemberDeclarationSyntax> selectedMembers,
            CancellationToken cancellationToken)
        {
            SeparatedSyntaxList <EnumMemberDeclarationSyntax> members = enumDeclaration.Members;

            int firstIndex = members.IndexOf(selectedMembers[0]);
            int lastIndex  = members.IndexOf(selectedMembers[selectedMembers.Length - 1]);

            SeparatedSyntaxList <EnumMemberDeclarationSyntax> newMembers = members
                                                                           .Take(firstIndex)
                                                                           .Concat(selectedMembers.OrderBy(f => f, EnumMemberDeclarationNameComparer.Instance))
                                                                           .Concat(members.Skip(lastIndex + 1))
                                                                           .ToSeparatedSyntaxList();

            MemberDeclarationSyntax newNode = enumDeclaration.WithMembers(newMembers);

            return(document.ReplaceNodeAsync(enumDeclaration, newNode, cancellationToken));
        }
Esempio n. 17
0
        public static async Task <Document> RefactorAsync(
            Document document,
            EnumDeclarationSyntax enumDeclaration,
            string value,
            CancellationToken cancellationToken)
        {
            SemanticModel semanticModel = await document.GetSemanticModelAsync(cancellationToken).ConfigureAwait(false);

            INamedTypeSymbol symbol = semanticModel.GetDeclaredSymbol(enumDeclaration, cancellationToken);

            string name = NameGenerator.Default.EnsureUniqueEnumMemberName(DefaultNames.EnumMember, symbol);

            EnumMemberDeclarationSyntax enumMember = EnumMemberDeclaration(
                Identifier(name).WithRenameAnnotation(),
                ParseExpression(value));

            enumMember = enumMember.WithTrailingTrivia(NewLine());

            EnumDeclarationSyntax newNode = enumDeclaration.WithMembers(enumDeclaration.Members.Add(enumMember));

            return(await document.ReplaceNodeAsync(enumDeclaration, newNode, cancellationToken).ConfigureAwait(false));
        }
        private static async Task <Document> SortByValueAsync(
            Document document,
            EnumDeclarationSyntax enumDeclaration,
            SeparatedSyntaxListSelection <EnumMemberDeclarationSyntax> selectedMembers,
            CancellationToken cancellationToken)
        {
            SemanticModel semanticModel = await document.GetSemanticModelAsync(cancellationToken).ConfigureAwait(false);

            SpecialType enumSpecialType = semanticModel.GetDeclaredSymbol(enumDeclaration, cancellationToken).EnumUnderlyingType.SpecialType;

            var comparer = new EnumMemberDeclarationValueComparer(EnumValueComparer.GetInstance(enumSpecialType), semanticModel, cancellationToken);

            IEnumerable <EnumMemberDeclarationSyntax> sorted = selectedMembers.OrderBy(f => f, comparer);

            SeparatedSyntaxList <EnumMemberDeclarationSyntax> newMembers = enumDeclaration
                                                                           .Members
                                                                           .ReplaceRange(selectedMembers.FirstIndex, selectedMembers.Count, sorted);

            MemberDeclarationSyntax newNode = enumDeclaration.WithMembers(newMembers);

            return(await document.ReplaceNodeAsync(enumDeclaration, newNode, cancellationToken).ConfigureAwait(false));
        }
        private static Task <Document> RefactorAsync(
            Document document,
            EnumDeclarationSyntax enumDeclaration,
            SeparatedSyntaxListSelection <EnumMemberDeclarationSyntax> selection,
            CancellationToken cancellationToken)
        {
            SeparatedSyntaxList <EnumMemberDeclarationSyntax> newMembers = enumDeclaration.Members.ModifyRange(
                selection.FirstIndex,
                selection.Count,
                enumMember =>
            {
                return(enumMember
                       .WithEqualsValue(null)
                       .WithTrailingTrivia(enumMember.GetTrailingTrivia())
                       .WithFormatterAnnotation());
            })
                                                                           .ToSeparatedSyntaxList();

            EnumDeclarationSyntax newEnumDeclaration = enumDeclaration.WithMembers(newMembers);

            return(document.ReplaceNodeAsync(enumDeclaration, newEnumDeclaration, cancellationToken));
        }
Esempio n. 20
0
        private static Task <Document> RefactorAsync(
            Document document,
            EnumDeclarationSyntax enumDeclaration,
            SeparatedSyntaxListSelection <EnumMemberDeclarationSyntax> selection,
            CancellationToken cancellationToken)
        {
            SeparatedSyntaxList <EnumMemberDeclarationSyntax> members    = enumDeclaration.Members;
            SeparatedSyntaxList <EnumMemberDeclarationSyntax> newMembers = members;

            for (int i = selection.StartIndex; i <= selection.EndIndex; i++)
            {
                EnumMemberDeclarationSyntax newMember = members[i]
                                                        .WithEqualsValue(null)
                                                        .WithTrailingTrivia(members[i].GetTrailingTrivia())
                                                        .WithFormatterAnnotation();

                newMembers = newMembers.ReplaceAt(i, newMember);
            }

            EnumDeclarationSyntax newEnumDeclaration = enumDeclaration.WithMembers(newMembers);

            return(document.ReplaceNodeAsync(enumDeclaration, newEnumDeclaration, cancellationToken));
        }
        private EnumDeclarationSyntax AddEnumDocs(EnumDeclarationSyntax enumNode, string summaryText, Dictionary <string, string> memberDocs)
        {
            if (!summaryText.EndsWith('.'))
            {
                summaryText += ".";
            }
            var xmlPad            = XmlText(" ");
            var xmlNewLine        = XmlNewLine("\r\n");
            var xmlNewLineNoTrail = XmlText(XmlTextNewLine("\r\n", false));
            var seeTag            = XmlName("see");
            var docs = new XmlNodeSyntax[]
            {
                xmlPad,
                XmlSummaryElement(
                    xmlNewLine,
                    xmlPad,
                    XmlText(summaryText),
                    xmlNewLine,
                    xmlPad
                    ),
                xmlNewLine,
                xmlPad,
                XmlRemarksElement(
                    XmlText($"Autogenerated from "),
                    XmlSeeHrefElement(_url, $"{enumNode.Identifier.ValueText} docs"),
                    XmlText($".")
                    ),
                xmlNewLineNoTrail
            };

            XmlElementEndTag(seeTag);

            var nodes = enumNode.Members.Select(member => AddEnumMemberDocs(member, memberDocs[member.Identifier.ValueText]));

            enumNode = enumNode.WithMembers(SeparatedList(nodes));
            return(enumNode.WithLeadingTrivia(Trivia(DocumentationComment(docs))));
        }
Esempio n. 22
0
        private static async Task <Document> DeclareExplicitValueAsync(
            Document document,
            EnumDeclarationSyntax enumDeclaration,
            INamedTypeSymbol enumSymbol,
            bool isFlags,
            bool useBitShift,
            ImmutableArray <ulong> values,
            SemanticModel semanticModel,
            CancellationToken cancellationToken)
        {
            List <ulong> reservedValues = values.ToList();

            SeparatedSyntaxList <EnumMemberDeclarationSyntax> members = enumDeclaration.Members;

            SeparatedSyntaxList <EnumMemberDeclarationSyntax> newMembers = members;

            for (int i = 0; i < members.Count; i++)
            {
                if (members[i].EqualsValue == null)
                {
                    IFieldSymbol fieldSymbol = semanticModel.GetDeclaredSymbol(members[i], cancellationToken);

                    ulong?value = null;

                    if (isFlags)
                    {
                        Optional <ulong> optional = FlagsUtility <ulong> .Instance.GetUniquePowerOfTwo(reservedValues);

                        if (optional.HasValue &&
                            ConvertHelpers.CanConvertFromUInt64(optional.Value, enumSymbol.EnumUnderlyingType.SpecialType))
                        {
                            value = optional.Value;
                        }
                    }
                    else
                    {
                        value = SymbolUtility.GetEnumValueAsUInt64(fieldSymbol.ConstantValue, enumSymbol);
                    }

                    if (value != null)
                    {
                        reservedValues.Add(value.Value);

                        ExpressionSyntax expression;

                        if (useBitShift &&
                            value.Value > 1)
                        {
                            var power = (int)Math.Log(Convert.ToDouble(value.Value), 2);

                            expression = LeftShiftExpression(NumericLiteralExpression(1), NumericLiteralExpression(power));
                        }
                        else
                        {
                            expression = NumericLiteralExpression(value.Value, enumSymbol.EnumUnderlyingType.SpecialType);
                        }

                        EnumMemberDeclarationSyntax newMember = members[i].Update(
                            members[i].AttributeLists,
                            members[i].Modifiers,
                            members[i].Identifier.WithoutTrailingTrivia(),
                            EqualsValueClause(expression).WithTrailingTrivia(members[i].Identifier.TrailingTrivia));

                        newMembers = newMembers.ReplaceAt(i, newMember);
                    }
                }
            }

            EnumDeclarationSyntax newEnumDeclaration = enumDeclaration.WithMembers(newMembers);

            return(await document.ReplaceNodeAsync(enumDeclaration, newEnumDeclaration, cancellationToken).ConfigureAwait(false));
        }
Esempio n. 23
0
        public override SyntaxNode VisitEnumDeclaration(EnumDeclarationSyntax node)
        {
            node = (EnumDeclarationSyntax)base.VisitEnumDeclaration(node);

            return(node.WithMembers(SortMembers(node.Members)));
        }