private static BlockSyntax CreateNewBlockParent(StatementSyntax statement, SemanticModel semanticModel, ObjectCreationExpressionSyntax objectCreationExpression, ISymbol variableSymbol) { var blockParent = statement.FirstAncestorOrSelf <BlockSyntax>(); var assignmentExpressions = ObjectInitializerAnalyzer.FindAssingmentExpressions(semanticModel, statement, variableSymbol); var newBlockParent = SyntaxFactory.Block() .WithLeadingTrivia(blockParent.GetLeadingTrivia()) .WithTrailingTrivia(blockParent.GetTrailingTrivia()) .WithAdditionalAnnotations(Formatter.Annotation); var newAssignmentExpressions = new List <ExpressionStatementSyntax>(); for (int i = 0; i < blockParent.Statements.Count; i++) { var blockStatement = blockParent.Statements[i]; if (blockStatement.Equals(statement)) { var initializationExpressions = new List <AssignmentExpressionSyntax>(); foreach (var expressionStatement in assignmentExpressions) { var assignmentExpression = expressionStatement.Expression as AssignmentExpressionSyntax; var memberAccess = assignmentExpression.Left as MemberAccessExpressionSyntax; var propertyIdentifier = memberAccess.Name as IdentifierNameSyntax; initializationExpressions.Add(SyntaxFactory.AssignmentExpression(SyntaxKind.SimpleAssignmentExpression, propertyIdentifier, assignmentExpression.Right)); } var initializers = SyntaxFactory.SeparatedList <ExpressionSyntax>(initializationExpressions); var newObjectCreationExpression = objectCreationExpression.WithInitializer( SyntaxFactory.InitializerExpression( SyntaxKind.ObjectInitializerExpression, SyntaxFactory.Token(SyntaxFactory.ParseLeadingTrivia(" "), SyntaxKind.OpenBraceToken, SyntaxFactory.ParseTrailingTrivia("\n")), initializers, SyntaxFactory.Token(SyntaxFactory.ParseLeadingTrivia(" "), SyntaxKind.CloseBraceToken, SyntaxFactory.ParseTrailingTrivia("")) )) .WithLeadingTrivia(objectCreationExpression.GetLeadingTrivia()) .WithTrailingTrivia(objectCreationExpression.GetTrailingTrivia()) .WithAdditionalAnnotations(Formatter.Annotation); var newLocalDeclarationStatement = statement.ReplaceNode(objectCreationExpression, newObjectCreationExpression) .WithLeadingTrivia(statement.GetLeadingTrivia()) .WithTrailingTrivia(statement.GetTrailingTrivia()) .WithAdditionalAnnotations(Formatter.Annotation); newBlockParent = newBlockParent.AddStatements(newLocalDeclarationStatement); i += initializationExpressions.Count; } else { newBlockParent = newBlockParent.AddStatements(blockStatement .WithLeadingTrivia(blockStatement.GetLeadingTrivia()) .WithTrailingTrivia(blockStatement.GetTrailingTrivia()) .WithAdditionalAnnotations(Formatter.Annotation)); } } return(newBlockParent); }
private static BlockSyntax CreateNewBlockParent(StatementSyntax statement, SemanticModel semanticModel, ObjectCreationExpressionSyntax objectCreationExpression, ISymbol variableSymbol) { var blockParent = statement.FirstAncestorOrSelf <BlockSyntax>(); var assignmentExpressions = ObjectInitializerAnalyzer.FindAssignmentExpressions(semanticModel, statement, variableSymbol); var newBlockParent = SyntaxFactory.Block() .WithLeadingTrivia(blockParent.GetLeadingTrivia()) .WithTrailingTrivia(blockParent.GetTrailingTrivia()) .WithAdditionalAnnotations(Formatter.Annotation); for (int i = 0; i < blockParent.Statements.Count; i++) { var blockStatement = blockParent.Statements[i]; if (blockStatement.Equals(statement)) { var initializationExpressions = new List <AssignmentExpressionSyntax>(); foreach (var expressionStatement in assignmentExpressions) { var assignmentExpression = expressionStatement.Expression as AssignmentExpressionSyntax; var memberAccess = assignmentExpression.Left as MemberAccessExpressionSyntax; var propertyIdentifier = memberAccess.Name as IdentifierNameSyntax; var newAssignmentExpression = SyntaxFactory.AssignmentExpression(SyntaxKind.SimpleAssignmentExpression, propertyIdentifier, assignmentExpression.Right); initializationExpressions.Add(newAssignmentExpression.WithTriviaFrom(expressionStatement)); } if (objectCreationExpression.Initializer != null) { var existentInitilizers = objectCreationExpression.Initializer.Expressions.Cast <AssignmentExpressionSyntax>() .Where(ae => { var propertyIdentifier = ae.Left.ToFullString().Trim(); return(initializationExpressions.All(ie => ie.Left.ToFullString().Trim() != propertyIdentifier)); }) .Select(ae => ae.WithoutTrivia()) .ToList(); initializationExpressions.InsertRange(0, existentInitilizers); } // Trailing trivia will be added before the separator if a simple separator list is used. This builds the separator token for expression // such that the trailing trivia from the original expression is added after the comma on the same line. var initializerSeparators = initializationExpressions.Take(initializationExpressions.Count - 1).Select(expr => SyntaxFactory.Token(SyntaxKind.CommaToken).WithTrailingTrivia(expr.GetTrailingTrivia())).ToList(); var lastInitializer = initializationExpressions.Last(); // Preserve the last initializer before rebuilding the list. // Get all but the last initializer without the trailing trivia. Trivia will be added after the separator from the list above. initializationExpressions = initializationExpressions.Take(initializationExpressions.Count - 1).Select(expr => expr.WithoutTrailingTrivia()).ToList(); initializationExpressions.Add(lastInitializer); // Add the last initializer with all of its trivia. var initializers = SyntaxFactory.SeparatedList <ExpressionSyntax>(initializationExpressions, initializerSeparators); var newObjectCreationExpression = objectCreationExpression.WithInitializer( SyntaxFactory.InitializerExpression( SyntaxKind.ObjectInitializerExpression, SyntaxFactory.Token(SyntaxFactory.ParseLeadingTrivia(" "), SyntaxKind.OpenBraceToken, SyntaxFactory.ParseTrailingTrivia(Environment.NewLine)), initializers, SyntaxFactory.Token(SyntaxFactory.ParseLeadingTrivia(" "), SyntaxKind.CloseBraceToken, SyntaxFactory.ParseTrailingTrivia("")) )) .WithLeadingTrivia(objectCreationExpression.GetLeadingTrivia()) .WithTrailingTrivia(objectCreationExpression.GetTrailingTrivia()) .WithAdditionalAnnotations(Formatter.Annotation); if (newObjectCreationExpression.ArgumentList?.Arguments.Count == 0) { newObjectCreationExpression = newObjectCreationExpression.WithArgumentList(null); } var newLocalDeclarationStatement = statement.ReplaceNode(objectCreationExpression, newObjectCreationExpression) .WithLeadingTrivia(statement.GetLeadingTrivia()) .WithTrailingTrivia(statement.GetTrailingTrivia()) .WithAdditionalAnnotations(Formatter.Annotation); newBlockParent = newBlockParent.AddStatements(newLocalDeclarationStatement); i += assignmentExpressions.Count; } else { newBlockParent = newBlockParent.AddStatements(blockStatement .WithLeadingTrivia(blockStatement.GetLeadingTrivia()) .WithTrailingTrivia(blockStatement.GetTrailingTrivia()) .WithAdditionalAnnotations(Formatter.Annotation)); } } return(newBlockParent); }
private static BlockSyntax CreateNewBlockParent(StatementSyntax statement, SemanticModel semanticModel, ObjectCreationExpressionSyntax objectCreationExpression, ISymbol variableSymbol) { var blockParent = statement.FirstAncestorOrSelf <BlockSyntax>(); var assignmentExpressions = ObjectInitializerAnalyzer.FindAssignmentExpressions(semanticModel, statement, variableSymbol); var newBlockParent = SyntaxFactory.Block() .WithLeadingTrivia(blockParent.GetLeadingTrivia()) .WithTrailingTrivia(blockParent.GetTrailingTrivia()) .WithAdditionalAnnotations(Formatter.Annotation); for (int i = 0; i < blockParent.Statements.Count; i++) { var blockStatement = blockParent.Statements[i]; if (blockStatement.Equals(statement)) { var initializationExpressions = new List <AssignmentExpressionSyntax>(); foreach (var expressionStatement in assignmentExpressions) { var assignmentExpression = expressionStatement.Expression as AssignmentExpressionSyntax; var memberAccess = assignmentExpression.Left as MemberAccessExpressionSyntax; var propertyIdentifier = memberAccess.Name as IdentifierNameSyntax; var newAssignmentExpression = SyntaxFactory.AssignmentExpression(SyntaxKind.SimpleAssignmentExpression, propertyIdentifier, assignmentExpression.Right); initializationExpressions.Add(newAssignmentExpression); } if (objectCreationExpression.Initializer != null) { var existentInitilizers = objectCreationExpression.Initializer.Expressions.Cast <AssignmentExpressionSyntax>() .Where(ae => { var propertyIdentifier = ae.Left.ToFullString().Trim(); return(initializationExpressions.All(ie => ie.Left.ToFullString().Trim() != propertyIdentifier)); }) .Select(ae => ae.WithoutTrivia()) .ToList(); initializationExpressions.InsertRange(0, existentInitilizers); } var initializers = SyntaxFactory.SeparatedList <ExpressionSyntax>(initializationExpressions); var newObjectCreationExpression = objectCreationExpression.WithInitializer( SyntaxFactory.InitializerExpression( SyntaxKind.ObjectInitializerExpression, SyntaxFactory.Token(SyntaxFactory.ParseLeadingTrivia(" "), SyntaxKind.OpenBraceToken, SyntaxFactory.ParseTrailingTrivia(Environment.NewLine)), initializers, SyntaxFactory.Token(SyntaxFactory.ParseLeadingTrivia(" "), SyntaxKind.CloseBraceToken, SyntaxFactory.ParseTrailingTrivia("")) )) .WithLeadingTrivia(objectCreationExpression.GetLeadingTrivia()) .WithTrailingTrivia(objectCreationExpression.GetTrailingTrivia()) .WithAdditionalAnnotations(Formatter.Annotation); if (newObjectCreationExpression.ArgumentList?.Arguments.Count == 0) { newObjectCreationExpression = newObjectCreationExpression.WithArgumentList(null); } var newLocalDeclarationStatement = statement.ReplaceNode(objectCreationExpression, newObjectCreationExpression) .WithLeadingTrivia(statement.GetLeadingTrivia()) .WithTrailingTrivia(statement.GetTrailingTrivia()) .WithAdditionalAnnotations(Formatter.Annotation); newBlockParent = newBlockParent.AddStatements(newLocalDeclarationStatement); i += initializationExpressions.Count; } else { newBlockParent = newBlockParent.AddStatements(blockStatement .WithLeadingTrivia(blockStatement.GetLeadingTrivia()) .WithTrailingTrivia(blockStatement.GetTrailingTrivia()) .WithAdditionalAnnotations(Formatter.Annotation)); } } return(newBlockParent); }