Пример #1
0
        private static Task <Document> RefactorAsync(
            Document document,
            ExpressionSyntax expression,
            object constantValue,
            CancellationToken cancellationToken)
        {
            ExpressionSyntax newExpression = CSharpFactory.LiteralExpression(constantValue);

            return(document.ReplaceNodeAsync(expression, newExpression.WithTriviaFrom(expression), cancellationToken));
        }
        public static void ComputeRefactoring(RefactoringContext context, LiteralExpressionSyntax literalExpression)
        {
            HexNumericLiteralExpressionInfo info = SyntaxInfo.HexNumericLiteralExpressionInfo(literalExpression);

            if (!info.Success)
            {
                return;
            }

            LiteralExpressionSyntax newLiteralExpression = CSharpFactory.LiteralExpression(info.Value);

            context.RegisterRefactoring(
                $"Replace '{info.Text}' with '{newLiteralExpression}'",
                cancellationToken => RefactorAsync(context.Document, literalExpression, newLiteralExpression, cancellationToken));
        }
        public static void ComputeRefactoring(RefactoringContext context, LiteralExpressionSyntax literalExpression)
        {
            HexNumericLiteralExpressionInfo info = SyntaxInfo.HexNumericLiteralExpressionInfo(literalExpression);

            if (!info.Success)
            {
                return;
            }

            LiteralExpressionSyntax newLiteralExpression = CSharpFactory.LiteralExpression(info.Value);

            context.RegisterRefactoring(
                $"Convert to '{newLiteralExpression}'",
                cancellationToken => RefactorAsync(context.Document, literalExpression, newLiteralExpression, cancellationToken),
                RefactoringIdentifiers.ConvertHexadecimalLiteralToDecimalLiteral);
        }
        private static EnumMemberDeclarationSyntax CreateEnumMember(INamedTypeSymbol enumSymbol, string name, object value)
        {
            EqualsValueClauseSyntax equalsValue = null;

            if (value != null)
            {
                equalsValue = EqualsValueClause(CSharpFactory.LiteralExpression(value));
            }

            name = NameGenerator.Default.EnsureUniqueMemberName(name, enumSymbol);

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

            return(EnumMemberDeclaration(
                       default(SyntaxList <AttributeListSyntax>),
                       identifier,
                       equalsValue));
        }
Пример #5
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));
        }
Пример #6
0
        public static async Task ComputeRefactoringAsync(RefactoringContext context, ExpressionSyntax expression)
        {
            if (expression is LiteralExpressionSyntax)
            {
                return;
            }

            SemanticModel semanticModel = await context.GetSemanticModelAsync().ConfigureAwait(false);

            Optional <object> optional = semanticModel.GetConstantValue(expression, context.CancellationToken);

            if (!optional.HasValue)
            {
                return;
            }

            ExpressionSyntax newExpression = CSharpFactory.LiteralExpression(optional.Value);

            context.RegisterRefactoring(
                $"Replace expression with '{newExpression}'",
                cancellationToken => RefactorAsync(context.Document, expression, newExpression, cancellationToken));
        }