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