public static async Task <Document> RefactorAsync(
            Document document,
            InvocationExpressionSyntax invocationExpression,
            CancellationToken cancellationToken)
        {
            SemanticModel semanticModel = await document.GetSemanticModelAsync(cancellationToken).ConfigureAwait(false);

            MemberDeclarationSyntax memberDeclaration = invocationExpression.FirstAncestor <MemberDeclarationSyntax>();

            Debug.Assert(memberDeclaration != null, "");

            if (memberDeclaration != null)
            {
                BaseTypeDeclarationSyntax typeDeclaration = memberDeclaration.FirstAncestor <BaseTypeDeclarationSyntax>();

                Debug.Assert(typeDeclaration != null, "");

                if (typeDeclaration != null)
                {
                    MemberInvocationExpression memberInvocation = MemberInvocationExpression.Create(invocationExpression);

                    string fieldName = NameGenerator.Default.EnsureUniqueLocalName("_regex", semanticModel, invocationExpression.SpanStart, cancellationToken: cancellationToken);

                    MemberAccessExpressionSyntax newMemberAccess = memberInvocation.MemberAccessExpression.WithExpression(IdentifierName(Identifier(fieldName).WithRenameAnnotation()));

                    ArgumentListPair pair = RewriteArgumentLists(memberInvocation.ArgumentList, semanticModel, cancellationToken);

                    InvocationExpressionSyntax newInvocationExpression = invocationExpression
                                                                         .WithExpression(newMemberAccess)
                                                                         .WithArgumentList(pair.ArgumentList1);

                    MemberDeclarationSyntax newTypeDeclaration = typeDeclaration.ReplaceNode(invocationExpression, newInvocationExpression);

                    TypeSyntax regexType = semanticModel.GetTypeByMetadataName(MetadataNames.System_Text_RegularExpressions_Regex).ToMinimalTypeSyntax(semanticModel, typeDeclaration.SpanStart);

                    FieldDeclarationSyntax fieldDeclaration = FieldDeclaration(
                        (ShouldBeStatic(memberDeclaration, semanticModel, cancellationToken))
                            ? Modifiers.PrivateStaticReadOnly()
                            : Modifiers.PrivateReadOnly(),
                        regexType,
                        Identifier(fieldName),
                        EqualsValueClause(
                            ObjectCreationExpression(regexType, pair.ArgumentList2)));

                    SyntaxList <MemberDeclarationSyntax> newMembers = newTypeDeclaration
                                                                      .GetMembers()
                                                                      .InsertMember(fieldDeclaration, MemberDeclarationComparer.ByKind);

                    newTypeDeclaration = newTypeDeclaration.WithMembers(newMembers);

                    return(await document.ReplaceNodeAsync(typeDeclaration, newTypeDeclaration, cancellationToken).ConfigureAwait(false));
                }
            }

            return(document);
        }
コード例 #2
0
        public static async Task <Document> RefactorAsync(
            Document document,
            LockStatementSyntax lockStatement,
            CancellationToken cancellationToken = default)
        {
            MemberDeclarationSyntax containingMember = lockStatement.FirstAncestor <MemberDeclarationSyntax>();

            Debug.Assert(containingMember != null);

            if (containingMember == null)
            {
                return(document);
            }

            TypeDeclarationSyntax containingType = containingMember.FirstAncestor <TypeDeclarationSyntax>();

            Debug.Assert(containingType != null);

            if (containingType == null)
            {
                return(document);
            }

            SyntaxList <MemberDeclarationSyntax> members = containingType.Members;

            int index = members.IndexOf(containingMember);

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

            string name = NameGenerator.Default.EnsureUniqueLocalName(
                LockObjectName,
                semanticModel,
                lockStatement.Expression.SpanStart,
                cancellationToken: cancellationToken);

            LockStatementSyntax newLockStatement = lockStatement
                                                   .WithExpression(IdentifierName(Identifier(name).WithRenameAnnotation()));

            MemberDeclarationSyntax newContainingMember = containingMember
                                                          .ReplaceNode(lockStatement, newLockStatement);

            bool isStatic = SyntaxInfo.ModifierListInfo(containingMember).IsStatic;

            FieldDeclarationSyntax field = CreateFieldDeclaration(name, isStatic).WithFormatterAnnotation();

            SyntaxList <MemberDeclarationSyntax> newMembers = members.ReplaceAt(index, newContainingMember);

            newMembers = MemberDeclarationInserter.Default.Insert(newMembers, field);

            MemberDeclarationSyntax newNode = containingType.WithMembers(newMembers);

            return(await document.ReplaceNodeAsync(containingType, newNode, cancellationToken).ConfigureAwait(false));
        }
コード例 #3
0
        public static async Task <Document> RefactorAsync(
            Document document,
            InvocationExpressionSyntax invocationExpression,
            CancellationToken cancellationToken)
        {
            SemanticModel semanticModel = await document.GetSemanticModelAsync(cancellationToken).ConfigureAwait(false);

            MemberDeclarationSyntax memberDeclaration = invocationExpression.FirstAncestor <MemberDeclarationSyntax>();

            Debug.Assert(memberDeclaration != null, "");

            if (memberDeclaration != null)
            {
                TypeDeclarationSyntax typeDeclaration = memberDeclaration.FirstAncestor <TypeDeclarationSyntax>();

                Debug.Assert(typeDeclaration != null, "");

                if (typeDeclaration != null)
                {
                    SimpleMemberInvocationExpressionInfo invocationInfo = SyntaxInfo.SimpleMemberInvocationExpressionInfo(invocationExpression);

                    string fieldName = NameGenerator.Default.EnsureUniqueLocalName("_regex", semanticModel, invocationExpression.SpanStart, cancellationToken: cancellationToken);

                    MemberAccessExpressionSyntax newMemberAccess = invocationInfo.MemberAccessExpression.WithExpression(IdentifierName(Identifier(fieldName).WithRenameAnnotation()));

                    ArgumentListPair pair = RewriteArgumentLists(invocationInfo.ArgumentList, semanticModel, cancellationToken);

                    InvocationExpressionSyntax newInvocationExpression = invocationExpression
                                                                         .WithExpression(newMemberAccess)
                                                                         .WithArgumentList(pair.ArgumentList1);

                    TypeDeclarationSyntax newTypeDeclaration = typeDeclaration.ReplaceNode(invocationExpression, newInvocationExpression);

                    TypeSyntax regexType = ParseTypeName("System.Text.RegularExpressions.Regex").WithSimplifierAnnotation();

                    FieldDeclarationSyntax fieldDeclaration = FieldDeclaration(
                        Modifiers.Private_Static_ReadOnly(),
                        regexType,
                        Identifier(fieldName),
                        EqualsValueClause(
                            ObjectCreationExpression(regexType, pair.ArgumentList2)));

                    SyntaxList <MemberDeclarationSyntax> newMembers = MemberDeclarationInserter.Default.Insert(newTypeDeclaration.Members, fieldDeclaration);

                    newTypeDeclaration = newTypeDeclaration.WithMembers(newMembers);

                    return(await document.ReplaceNodeAsync(typeDeclaration, newTypeDeclaration, cancellationToken).ConfigureAwait(false));
                }
            }

            return(document);
        }