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); }
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); }