public static ClassDeclarationSyntax InjectToConstructor(SimpleNameSyntax typeName,
                                                          ClassDeclarationSyntax updatedClass, SyntaxToken fieldIdentifier)
 {
     updatedClass = AddField(updatedClass, typeName, fieldIdentifier);
     updatedClass = InjectInConstructor(updatedClass, typeName.WithoutTrivia(), fieldIdentifier);
     return(updatedClass);
 }
Example #2
0
        private async Task <Document> InjectInstance(Document document, ClassDeclarationSyntax classNode, SimpleNameSyntax typeName)
        {
            var fieldName       = "_" + ToVariableName(typeName);
            var fieldIdentifier = SyntaxFactory.Identifier(fieldName);

            ClassDeclarationSyntax updatedClass = classNode;
            var typeNameStr = typeName.GetText().ToString();

            updatedClass = updatedClass.ReplaceNode(typeName, SyntaxFactory.IdentifierName(fieldName));
            updatedClass = AddField(updatedClass, typeName, fieldIdentifier);
            updatedClass = InjectInConstructor(updatedClass, typeName.WithoutTrivia(), fieldIdentifier);

            var root = await document.GetSyntaxRootAsync();

            root = root.ReplaceNode(classNode, updatedClass);
            return(document.WithSyntaxRoot(root));
        }
Example #3
0
        private static TNode ConvertAccessor <TNode>(TNode node, SimpleNameSyntax nameNode, SimpleNameSyntax newNameNode,
                                                     string cancellationTokenParamName, IBodyFunctionReferenceAnalyzationResult bodyFuncReferenceResult,
                                                     Func <InvocationExpressionSyntax, InvocationExpressionSyntax> alterFn = null) where TNode : SyntaxNode
        {
            var expressionNode = nameNode.GetAccessorExpression();
            var invocation     = InvocationExpression(
                expressionNode.IsKind(SyntaxKind.SimpleAssignmentExpression)
                                                ? newNameNode.WithoutTrivia().WithoutAnnotations()
                                                : expressionNode.ReplaceNode(nameNode, newNameNode).WithoutTrivia().WithoutAnnotations()
                )
                                 .WithTriviaFrom(expressionNode)
                                 .AddCancellationTokenArgumentIf(cancellationTokenParamName, bodyFuncReferenceResult)
                                 .WithAdditionalAnnotations(nameNode.GetAnnotations(Annotations.TaskReturned))
                                 .AddAssignedValueAsArgument(expressionNode);

            if (alterFn != null)
            {
                invocation = alterFn(invocation);
            }
            return(node.ReplaceNode(expressionNode, invocation));
        }
        private static Task <Document> GetTransformedDocumentAsync(Document document, SyntaxNode root, SimpleNameSyntax node)
        {
            var qualifiedExpression =
                SyntaxFactory.MemberAccessExpression(SyntaxKind.SimpleMemberAccessExpression, ThisExpressionSyntax, node.WithoutTrivia().WithoutFormatting())
                .WithTriviaFrom(node)
                .WithoutFormatting();

            var newSyntaxRoot = root.ReplaceNode(node, qualifiedExpression);

            return(Task.FromResult(document.WithSyntaxRoot(newSyntaxRoot)));
        }