private static BlockSyntax GenerateWrapingCode(List <ExpressionSyntax> assigmentExpressions, Accessor firstOutputAccessor)
        {
            BlockSyntax body;

            if (firstOutputAccessor.Name == Accessor.SpecialNameThis)
            {
                var assigmentStatements = assigmentExpressions.Select(x => SyntaxFactory.ExpressionStatement(x));
                body = SyntaxFactory.Block(assigmentStatements);
            }
            else
            {
                var statements = new List <StatementSyntax>();

                // {}
                var initializerSyntax = SyntaxFactoryEx.ObjectInitializerExpression(assigmentExpressions);

                // = new accessor.Type.Name() initializerSyntax
                var objectCreationSyntax = SyntaxFactory.EqualsValueClause(SyntaxFactory.ObjectCreationExpression(SyntaxFactory.IdentifierName(firstOutputAccessor.Type.Name), SyntaxFactory.ArgumentList().WithTrailingTrivia(SyntaxFactory.LineFeed), initializerSyntax));

                // var result objectCreationSyntax
                var resultSyntax = SyntaxFactory.LocalDeclarationStatement(SyntaxFactory.VariableDeclaration(SyntaxFactory.IdentifierName("var"), SyntaxFactory.SingletonSeparatedList <VariableDeclaratorSyntax>(SyntaxFactory.VariableDeclarator("result").WithInitializer(objectCreationSyntax))));
                statements.Add(resultSyntax);

                //  return resultSyntax;
                var returnResultSyntaxt = SyntaxFactory.ReturnStatement(SyntaxFactory.IdentifierName("result"));
                statements.Add(returnResultSyntaxt);

                body = SyntaxFactory.Block(statements);
            }

            return(body);
        }
Exemplo n.º 2
0
        private static CompilationUnitSyntax GenerateSyntax(BasicData data, IEnumerable <DependecyData> contexDependecies)
        {
            var compilationUnit = SyntaxFactory.CompilationUnit();

            compilationUnit = compilationUnit.AddUsings(data.Usings);

            var @namespace       = SyntaxFactoryEx.NamespaceDeclaration(data.Namespace);
            var classDeclaration = SyntaxFactoryEx.InternalClassDeclaration(data.HandlerName);

            var typeArgumentList = new List <TypeSyntax>();

            typeArgumentList.Add(SyntaxFactory.IdentifierName(data.CommandName + data.CommandTypeArguments));
            if (data.ReturnType != null)
            {
                typeArgumentList.Add(data.ReturnType);
            }
            classDeclaration = classDeclaration.AddBaseListTypes(SyntaxFactory.SimpleBaseType(SyntaxFactory.GenericName(SyntaxFactory.Identifier("IRequestHandler")).AddTypeArgumentListArguments(typeArgumentList.ToArray())));

            var constructor  = GenerateConstructor(data.HandlerName, contexDependecies);
            var handleMethod = GenerateHandleMethod(data);
            var members      = new MemberDeclarationSyntax[] { constructor, handleMethod };

            classDeclaration = classDeclaration.AddMembers(members);
            @namespace       = @namespace.AddMembers(classDeclaration);
            compilationUnit  = compilationUnit.AddMembers(@namespace);
            var code = compilationUnit.NormalizeWhitespace();

            return(code);
        }
Exemplo n.º 3
0
        private static ConstructorDeclarationSyntax GenerateConstructor(string name, IEnumerable <DependecyData> contexDependecies)
        {
            var parameters  = contexDependecies.Select(x => SyntaxFactoryEx.Parameter(x.Name.ToLowerFirst(), x.Type));
            var constructor = SyntaxFactory.ConstructorDeclaration(SyntaxFactory.Identifier(name))
                              .AddModifiers(SyntaxFactory.Token(SyntaxKind.PublicKeyword))
                              .AddParameterListParameters(parameters.ToArray())
                              .WithBody(SyntaxFactory.Block());

            //.AddBodyStatements(SyntaxFactory.Block());
            return(constructor);
        }
        private static List <ExpressionSyntax> GenerateAssigmentExpressions(IEnumerable <Match> matches)
        {
            var assigmentExpressions = new List <ExpressionSyntax>();

            foreach (var match in matches)
            {
                TypeData leftType = match.LeftAccessor.Type;
                var      left     = GenerateExpression(match.LeftAccessor);
                var      right    = GenerateExpression(match.RightAccessor);

                if (leftType.IsCollection && leftType.Arguments.Count() == 1)
                {
                    var arg = leftType.Arguments.First();
                    if (!arg.IsImmutable)
                    {
                        // x => new arg.Name(x)
                        var lambda = SyntaxFactory.SimpleLambdaExpression(SyntaxFactoryEx.Parameter("x"), SyntaxFactory.ObjectCreationExpression(SyntaxFactory.IdentifierName(arg.Name), SyntaxFactoryEx.ArgumentListWithOneArgument(SyntaxFactory.IdentifierName("x")), null));

                        // rightExpression.Select(lambda)
                        right = SyntaxFactory.InvocationExpression(SyntaxFactory.MemberAccessExpression(SyntaxKind.SimpleMemberAccessExpression, right, SyntaxFactory.IdentifierName("Select")), SyntaxFactoryEx.ArgumentListWithOneArgument(lambda));
                    }
                }

                if (!match.LeftAccessor.Type.IsImmutable)
                {
                    if (match.LeftAccessor.Type.IsCollection && match.LeftAccessor.Type.IsInterface)
                    {
                        // rightExpression.ToList()
                        right = SyntaxFactory.InvocationExpression(SyntaxFactory.MemberAccessExpression(SyntaxKind.SimpleMemberAccessExpression, right, SyntaxFactory.IdentifierName("ToList")));
                    }
                    else
                    {
                        if (match.LeftAccessor.Type.IsArray)
                        {
                            // rightExpression.ToArray()
                            right = SyntaxFactory.InvocationExpression(SyntaxFactory.MemberAccessExpression(SyntaxKind.SimpleMemberAccessExpression, right, SyntaxFactory.IdentifierName("ToArray")));
                        }
                        else
                        {
                            // new LeftReferenceType(rightExpression)
                            right = SyntaxFactory.ObjectCreationExpression(SyntaxFactory.IdentifierName(match.LeftAccessor.Type.Name), SyntaxFactoryEx.ArgumentListWithOneArgument(right), null);
                        }
                    }
                }


                assigmentExpressions.Add(SyntaxFactory.AssignmentExpression(SyntaxKind.SimpleAssignmentExpression, left, right));
            }

            return(assigmentExpressions);
        }
        public static CompilationUnitSyntax CreateParameterObjectClass(ParameterObject parameterObject, IEnumerable <ParameterSyntax> parameters)
        {
            var compilationUnit = SyntaxFactory.CompilationUnit();

            compilationUnit = compilationUnit.AddUsings(parameterObject.Usings);
            var @namespace       = SyntaxFactoryEx.NamespaceDeclaration(parameterObject.Namespace);
            var classDeclaration = SyntaxFactoryEx.InternalClassDeclaration(parameterObject.Name);

            var members = new List <MemberDeclarationSyntax>();

            foreach (var property in parameterObject.Properties)
            {
                var propertyDeclaration = SyntaxFactory.PropertyDeclaration(SyntaxFactory.ParseTypeName(property.Type), property.PropertyName)
                                          .AddModifiers(SyntaxFactory.Token(SyntaxKind.PublicKeyword))
                                          .AddAccessorListAccessors(
                    SyntaxFactory.AccessorDeclaration(SyntaxKind.GetAccessorDeclaration).WithSemicolonToken(SyntaxFactory.Token(SyntaxKind.SemicolonToken)),
                    SyntaxFactory.AccessorDeclaration(SyntaxKind.SetAccessorDeclaration).WithSemicolonToken(SyntaxFactory.Token(SyntaxKind.SemicolonToken)));

                members.Add(propertyDeclaration);
            }

            var body = new List <StatementSyntax>();

            foreach (var property in parameterObject.Properties)
            {
                var assignment = SyntaxFactory.ExpressionStatement
                                 (
                    SyntaxFactory.AssignmentExpression
                    (
                        SyntaxKind.SimpleAssignmentExpression,
                        SyntaxFactory.IdentifierName(property.PropertyName),
                        SyntaxFactory.IdentifierName(property.ParameterName)
                    )
                                 );
                body.Add(assignment);
            }

            var constructor = SyntaxFactory.ConstructorDeclaration(SyntaxFactory.Identifier(parameterObject.Name))
                              .AddModifiers(SyntaxFactory.Token(SyntaxKind.PublicKeyword))
                              .AddParameterListParameters(parameters.ToArray())
                              .AddBodyStatements(body.ToArray());

            members.Add(constructor);

            classDeclaration = classDeclaration.AddMembers(members.ToArray());
            @namespace       = @namespace.AddMembers(classDeclaration);
            compilationUnit  = compilationUnit.AddMembers(@namespace);
            var code = compilationUnit.NormalizeWhitespace();

            return(code);
        }
Exemplo n.º 6
0
        protected override SyntaxNode TryUpdateParentOfUpdatedNode(SyntaxNode parent, SyntaxNode newNameNode, SyntaxEditor editor, ISyntaxFacts syntaxFacts)
        {
            if (newNameNode.IsKind(SyntaxKind.DiscardDesignation) &&
                parent.IsKind(SyntaxKind.DeclarationPattern, out DeclarationPatternSyntax declarationPattern) &&
                ((CSharpParseOptions)parent.SyntaxTree.Options).LanguageVersion.IsCSharp9OrAbove())
            {
                var trailingTrivia = declarationPattern.Type.GetTrailingTrivia()
                                     .AddRange(newNameNode.GetLeadingTrivia())
                                     .AddRange(newNameNode.GetTrailingTrivia());

                return(SyntaxFactoryEx.TypePattern(declarationPattern.Type).WithTrailingTrivia(trailingTrivia));
            }

            return(null);
        }
Exemplo n.º 7
0
        private static MethodDeclarationSyntax GenerateHandleMethod(BasicData data)
        {
            var handleMethodReturnType = SyntaxFactory.GenericName("Task").AddTypeArgumentListArguments(data.ReturnType);

            if (data.ReturnType == null)
            {
                handleMethodReturnType = SyntaxFactory.GenericName("Task").AddTypeArgumentListArguments(SyntaxFactory.IdentifierName("Unit"));
            }
            var handleMethod = SyntaxFactory.MethodDeclaration(handleMethodReturnType, "Handle");

            handleMethod = handleMethod.AddModifiers(SyntaxFactory.Token(SyntaxKind.PublicKeyword), SyntaxFactory.Token(SyntaxKind.AsyncKeyword));
            handleMethod = handleMethod.AddParameterListParameters(SyntaxFactoryEx.Parameter(data.CommandParameterNameInHandleMethod, data.CommandName + data.CommandTypeArguments),
                                                                   SyntaxFactoryEx.Parameter("cancellationToken", "CancellationToken"));
            handleMethod = handleMethod.WithBody(SyntaxFactory.Block());
            return(handleMethod);
        }
        public static CSharpSyntaxNode GenerateSyntax(CSharpSyntaxNode syntaxNode, IEnumerable <Match> matches, Accessor firstOutputAccessor)
        {
            List <ExpressionSyntax> assigmentExpressions = GenerateAssigmentExpressions(matches);

            switch (syntaxNode)
            {
            case ObjectCreationExpressionSyntax objectCreation:
                var initializerSyntax     = SyntaxFactoryEx.ObjectInitializerExpression(assigmentExpressions);
                var updatedObjectCreation = objectCreation.WithInitializer(initializerSyntax);
                return(updatedObjectCreation);

            case BaseMethodDeclarationSyntax baseMethod:
                BlockSyntax body          = GenerateWrapingCode(assigmentExpressions, firstOutputAccessor);
                var         updatedMethod = baseMethod.WithBody(body).WithTrailingTrivia(baseMethod.Body.GetTrailingTrivia());
                return(updatedMethod);
            }

            throw new NotImplementedException();
        }
        private static SyntaxNode TransformArgumentListToTuple(SemanticModel semanticModel, SeparatedSyntaxList <ArgumentSyntax> arguments)
        {
            SeparatedSyntaxList <ArgumentSyntax> processedArguments = default;

            for (var i = 0; i < arguments.Count; i++)
            {
                var argument = arguments[i];

                var argumentTypeInfo = semanticModel.GetTypeInfo(argument.Expression);
                if (!Equals(argumentTypeInfo.Type, argumentTypeInfo.ConvertedType))
                {
                    var expectedType = SyntaxFactory.ParseTypeName(argumentTypeInfo.ConvertedType.ToDisplayString());
                    argument = argument.WithExpression(SyntaxFactory.CastExpression(expectedType, argument.Expression));
                }

                processedArguments = processedArguments.Add(argument);
            }

            return(SyntaxFactoryEx.TupleExpression(processedArguments));
        }
        private static SyntaxNode TransformGenericNameToTuple(SemanticModel semanticModel, GenericNameSyntax genericName)
        {
            var implementationType = typeof(SeparatedSyntaxListWrapper <> .AutoWrapSeparatedSyntaxList <>).MakeGenericType(typeof(TupleElementSyntaxWrapper), SyntaxWrapperHelper.GetWrappedType(typeof(TupleElementSyntaxWrapper)));
            var tupleElements      = (SeparatedSyntaxListWrapper <TupleElementSyntaxWrapper>)Activator.CreateInstance(implementationType);

            foreach (var typeArgument in genericName.TypeArgumentList.Arguments)
            {
                if (IsValueTuple(semanticModel, typeArgument))
                {
                    var tupleTypeSyntax = (TypeSyntax)GetReplacementNode(semanticModel, typeArgument);
                    tupleElements = tupleElements.Add(SyntaxFactoryEx.TupleElement(tupleTypeSyntax));
                }
                else
                {
                    tupleElements = tupleElements.Add(SyntaxFactoryEx.TupleElement(typeArgument));
                }
            }

            return(SyntaxFactoryEx.TupleType(tupleElements));
        }
Exemplo n.º 11
0
        private static CompilationUnitSyntax GenerateSyntax(BasicData data)
        {
            var compilationUnit = SyntaxFactory.CompilationUnit();

            compilationUnit = compilationUnit.AddUsings(data.Usings, data.Namespace);

            var @namespace       = SyntaxFactoryEx.NamespaceDeclaration(data.Namespace);
            var classDeclaration = SyntaxFactoryEx.PublicClassDeclaration(data.CommandName);

            if (data.ReturnType != null)
            {
                classDeclaration = classDeclaration.AddBaseListTypes(SyntaxFactory.SimpleBaseType(SyntaxFactory.GenericName(SyntaxFactory.Identifier("IRequest")).AddTypeArgumentListArguments(data.ReturnType)));
            }
            else
            {
                classDeclaration = classDeclaration.AddBaseListTypes(SyntaxFactory.SimpleBaseType(SyntaxFactory.IdentifierName("IRequest")));
            }

            @namespace      = @namespace.AddMembers(classDeclaration);
            compilationUnit = compilationUnit.AddMembers(@namespace);
            var code = compilationUnit.NormalizeWhitespace();

            return(code);
        }