public CSharpSyntaxNode Convert(NewExpression node) { if (node.Arguments.Count == 1 && node.Arguments[0].Kind == NodeKind.ObjectLiteralExpression) { ObjectLiteralExpression literaExpression = node.Arguments[0] as ObjectLiteralExpression; ObjectCreationExpressionSyntax csObjNewExpr = SyntaxFactory.ObjectCreationExpression(node.Type.ToCsNode <TypeSyntax>()); InitializerExpressionSyntax csInitExpr = SyntaxFactory.InitializerExpression(SyntaxKind.ObjectInitializerExpression); foreach (PropertyAssignment prop in literaExpression.Properties) { csInitExpr = csInitExpr.AddExpressions(SyntaxFactory.AssignmentExpression( SyntaxKind.SimpleAssignmentExpression, prop.Name.ToCsNode <ExpressionSyntax>(), prop.Initializer.ToCsNode <ExpressionSyntax>())); } return(csObjNewExpr.WithInitializer(csInitExpr).AddArgumentListArguments()); } if (node.TypeArguments.Count > 0) { return(SyntaxFactory .ObjectCreationExpression(SyntaxFactory .GenericName(this.StripType(node.Type.Text)) .AddTypeArgumentListArguments(node.TypeArguments.ToCsNodes <TypeSyntax>())) .AddArgumentListArguments(this.ToArgumentList(node.Arguments))); } else { return(SyntaxFactory .ObjectCreationExpression(node.Type.ToCsNode <TypeSyntax>()) .AddArgumentListArguments(this.ToArgumentList(node.Arguments))); } }
private async Task <Document> AddDefaultValuesAsync(Document document, AttributeSyntax attribute, MethodDeclarationSyntax method, InlineDataMustMatchTheoryParameters.ParameterArrayStyleType arrayStyle, CancellationToken cancellationToken) { var editor = await DocumentEditor.CreateAsync(document, cancellationToken).ConfigureAwait(false); InitializerExpressionSyntax arrayInitializer = null; if (arrayStyle == InlineDataMustMatchTheoryParameters.ParameterArrayStyleType.Initializer) { arrayInitializer = (InitializerExpressionSyntax)attribute.DescendantNodes().First(n => n.IsKind(SyntaxKind.ArrayInitializerExpression)); } var originalInitializer = arrayInitializer; int i = originalInitializer?.Expressions.Count ?? attribute.ArgumentList?.Arguments.Count ?? 0; for (; i < method.ParameterList.Parameters.Count; i++) { var defaultExpression = (ExpressionSyntax)CreateDefaultValueSyntax(editor, method.ParameterList.Parameters[i].Type); if (arrayInitializer != null) { arrayInitializer = arrayInitializer.AddExpressions(defaultExpression); } else { editor.AddAttributeArgument(attribute, defaultExpression); } } if (arrayInitializer != null) { editor.ReplaceNode(originalInitializer, arrayInitializer); } return(editor.GetChangedDocument()); }
public override SyntaxNode VisitInitializerExpression(InitializerExpressionSyntax node) { var parentProperty = node.FirstAncestorOrSelf <PropertyDeclarationSyntax>(); var parentInvocation = node.FirstAncestorOrSelf <InvocationExpressionSyntax>(); if (parentProperty != null && parentProperty.Identifier.Text == "SelectorExpression" || parentInvocation != null && parentInvocation.ToString().Contains("SelectorExpression")) { var initializerExpressions = GenerateInitializerExpressions(_metadata, "", "p.").ToList(); var expressionsWithSeparators = node.AddExpressions(initializerExpressions.ToArray()).Expressions.GetWithSeparators(); // This section is here only to format code well. Namely, in initializer expression, to add comma token after each expression and newline after comma. var list = new List <SyntaxNodeOrToken>(); var expressionTrailingTrivia = new List <SyntaxTrivia>(); foreach (var item in expressionsWithSeparators.ToList()) { // This is required if we have a custom code trivia which is attached to expression node, but should be attached after comma-token. if (item.IsToken) { expressionTrailingTrivia.Add(SyntaxExtenders.EndOfLineTrivia); list.Add(item.AsToken().WithTrailingTrivia(expressionTrailingTrivia.ToArray())); expressionTrailingTrivia.Clear(); } else { expressionTrailingTrivia = item.GetTrailingTrivia().ToList(); list.Add(item.WithTrailingTrivia()); } } if (list.Any() && list.Last().IsNode) { var item = list.Last(); list.Remove(item); list.Add(item.WithTrailingTrivia(expressionTrailingTrivia)); } return(node.WithExpressions(SyntaxFactory.SeparatedList <ExpressionSyntax>(list))); } return(base.VisitInitializerExpression(node)); }
private async Task <Document> ImplementAllSetters(Document document, InitializerExpressionSyntax expression, CancellationToken cancellationToken) { var createExpression = expression.Parent as ObjectCreationExpressionSyntax; var semanticModel = await document.GetSemanticModelAsync(cancellationToken); var typeSymbol = semanticModel.GetTypeInfo(createExpression, cancellationToken).Type; var missingprops = GetMissingProperties(expression, typeSymbol); var newExpression = expression.AddExpressions(missingprops.Select(x => SyntaxFactory.AssignmentExpression(SyntaxKind.SimpleAssignmentExpression, SyntaxFactory.IdentifierName(x.PropertyName()), x.Parameters.First().Type.DefaultExpression(x.PropertyName()))).Cast <ExpressionSyntax>().ToArray()); var root = await document.GetSyntaxRootAsync(); var newroot = root.ReplaceNode(expression, newExpression); return(document.WithSyntaxRoot(newroot)); }
private static InitializerExpressionSyntax CreateInitializer(ObjectCreationExpressionSyntax objectCreation, ExpressionStatementSyntax[] expressionStatements) { InitializerExpressionSyntax initializer = objectCreation.Initializer ?? SyntaxFactory.InitializerExpression(SyntaxKind.ObjectInitializerExpression); var expressions = new AssignmentExpressionSyntax[expressionStatements.Length]; for (int i = 0; i < expressionStatements.Length; i++) { var assignment = (AssignmentExpressionSyntax)expressionStatements[i].Expression; var memberAccess = (MemberAccessExpressionSyntax)assignment.Left; expressions[i] = assignment.ReplaceNode(memberAccess, memberAccess.Name); } return(initializer .AddExpressions(expressions) .WithFormatterAnnotation()); }
private static SyntaxNode ImplementAllSettersFromExpression(InitializerExpressionSyntax expression, string sourcename, ITypeSymbol targetTypeInfo, SemanticModel semanticModel, ITypeSymbol sourceType) { var missingprops = GetMissingProperties(expression, targetTypeInfo); var newproperties = sourceType.GetMembers().Where(x => x.Kind == SymbolKind.Property).Cast <IPropertySymbol>().Where(x => x.IsMissing(missingprops)); var newExpression = expression.AddExpressions( newproperties.Select(x => SyntaxFactory.AssignmentExpression(SyntaxKind.SimpleAssignmentExpression, SyntaxFactory.IdentifierName(x.Name), SyntaxFactory.MemberAccessExpression( SyntaxKind.SimpleMemberAccessExpression, SyntaxFactory.IdentifierName(sourcename), SyntaxFactory.IdentifierName(x.Name)))) .Cast <ExpressionSyntax>().ToArray()); return(newExpression); }
private static SyntaxNode ImplementAllSettersFromExpression(InitializerExpressionSyntax expression, string sourcename, ITypeSymbol targetTypeInfo, ITypeSymbol sourceType) { var missingprops = GetMissingProperties(expression, targetTypeInfo); var newproperties = sourceType.GetBaseTypesAndThis().SelectMany(x => x.GetMembers()).Where(x => x.Kind == SymbolKind.Property).Cast<IPropertySymbol>().Where(x => x.IsMissing(missingprops)); var newExpression = expression.AddExpressions( newproperties.Select(x => SyntaxFactory.AssignmentExpression(SyntaxKind.SimpleAssignmentExpression, SyntaxFactory.IdentifierName(x.Name), SyntaxFactory.MemberAccessExpression( SyntaxKind.SimpleMemberAccessExpression, SyntaxFactory.IdentifierName(sourcename), SyntaxFactory.IdentifierName(x.Name)))) .Cast<ExpressionSyntax>().ToArray()); return newExpression; }