private async Task <Document> UseLocalVariablesAsParameters(Document document, IInvocation invocation, bool generateNamedParameters, CancellationToken cancellationToken) { var semanticModel = await document.GetSemanticModelAsync(cancellationToken); var mappingSourceFinder = new LocalScopeMappingSourceFinder(semanticModel, invocation.SourceNode); return(await CodeFixHelper.FixInvocationWithParameters(document, invocation, generateNamedParameters, semanticModel, mappingSourceFinder, cancellationToken)); }
private async Task <Document> InitizalizeWithLocals(Document document, InitializerExpressionSyntax objectInitializer, CancellationToken cancellationToken) { var generator = SyntaxGenerator.GetGenerator(document); var semanticModel = await document.GetSemanticModelAsync(cancellationToken); var mappingSourceFinder = new LocalScopeMappingSourceFinder(semanticModel, objectInitializer); return(await ReplaceEmptyInitializationBlock(document, objectInitializer, cancellationToken, semanticModel, mappingSourceFinder, generator)); }
private static IEnumerable<SyntaxNode> GenerateMappingCode(IMethodSymbol methodSymbol, SyntaxGenerator generator, SemanticModel semanticModel) { var mappingEngine = new MappingEngine(semanticModel, generator); if (SymbolHelper.IsPureMappingFunction(methodSymbol)) { var source = methodSymbol.Parameters[0]; var targetType = methodSymbol.ReturnType; var newExpression = mappingEngine.MapExpression((ExpressionSyntax)generator.IdentifierName(source.Name), source.Type, targetType); return new[] { generator.ReturnStatement(newExpression) }; } //TODO: Pure mapping with multiple parameters if (SymbolHelper.IsUpdateParameterFunction(methodSymbol)) { var source = methodSymbol.Parameters[0]; var target = methodSymbol.Parameters[1]; var targets = ObjectHelper.GetFieldsThaCanBeSetPublicly(target.Type); var sourceFinder = new ObjectMembersMappingSourceFinder(source.Type, generator.IdentifierName(source.Name), generator); return MappingHelper.MapUsingSimpleAssignment(generator, semanticModel, targets, sourceFinder, globalTargetAccessor: generator.IdentifierName(target.Name)); } if (SymbolHelper.IsUpdateThisObjectFunction(methodSymbol)) { var source = methodSymbol.Parameters[0]; var sourceFinder = new ObjectMembersMappingSourceFinder(source.Type, generator.IdentifierName(source.Name), generator); var targets = ObjectHelper.GetFieldsThaCanBeSetPrivately(methodSymbol.ContainingType); return MappingHelper.MapUsingSimpleAssignment(generator, semanticModel, targets, sourceFinder); } if (SymbolHelper.IsMultiParameterUpdateThisObjectFunction(methodSymbol)) { var sourceFinder = new LocalScopeMappingSourceFinder(semanticModel, methodSymbol.Parameters); var targets = ObjectHelper.GetFieldsThaCanBeSetPrivately(methodSymbol.ContainingType); return MappingHelper.MapUsingSimpleAssignment(generator, semanticModel, targets, sourceFinder); } if (SymbolHelper.IsMappingConstructor(methodSymbol)) { var source = methodSymbol.Parameters[0]; var sourceFinder = new ObjectMembersMappingSourceFinder(source.Type, generator.IdentifierName(source.Name), generator); var targets = ObjectHelper.GetFieldsThaCanBeSetFromConstructor(methodSymbol.ContainingType); return MappingHelper.MapUsingSimpleAssignment(generator, semanticModel, targets, sourceFinder); } if (SymbolHelper.IsMultiParameterMappingConstructor(methodSymbol)) { var sourceFinder = new LocalScopeMappingSourceFinder(semanticModel, methodSymbol.Parameters); var targets = ObjectHelper.GetFieldsThaCanBeSetFromConstructor(methodSymbol.ContainingType); return MappingHelper.MapUsingSimpleAssignment(generator, semanticModel, targets, sourceFinder); } return Enumerable.Empty<SyntaxNode>(); }
private static IEnumerable <SyntaxNode> GenerateMappingCode(IMethodSymbol methodSymbol, SyntaxGenerator generator, MappingGenerator mappingGenerator, SemanticModel semanticModel) { if (SymbolHelper.IsPureMappingFunction(methodSymbol)) { var source = methodSymbol.Parameters[0]; var targetType = methodSymbol.ReturnType; return(mappingGenerator.MapTypes(source.Type, targetType, generator.IdentifierName(source.Name))); } var isMappingConstructor = SymbolHelper.IsMappingConstructor(methodSymbol); if (SymbolHelper.IsUpdateThisObjectFunction(methodSymbol) || isMappingConstructor) { var source = methodSymbol.Parameters[0]; var targetType = methodSymbol.ContainingType; return(mappingGenerator.MapTypes(source.Type, targetType, generator.IdentifierName(source.Name), generator.ThisExpression(), targetExists: true, isConstructorContext: isMappingConstructor)); } if (SymbolHelper.IsUpdateParameterFunction(methodSymbol)) { var source = methodSymbol.Parameters[0]; var target = methodSymbol.Parameters[1]; return(mappingGenerator.MapTypes(source.Type, target.Type, generator.IdentifierName(source.Name), generator.IdentifierName(target.Name), targetExists: true)); } var isMultiParameterConstructor = SymbolHelper.IsMultiParameterUpdateThisObjectFunction(methodSymbol); if (isMultiParameterConstructor || SymbolHelper.IsMultiParameterMappingConstructor(methodSymbol)) { var sourceFinder = new LocalScopeMappingSourceFinder(semanticModel, methodSymbol.Parameters); return(ObjectHelper.GetPublicPropertySymbols(methodSymbol.ContainingType) .Where(property => property.SetMethod != null || (property.CanBeSetOnlyFromConstructor() && isMultiParameterConstructor)) .Select(property => new { source = sourceFinder.FindMappingSource(property.Name, property.Type), target = new MappingElement() { Expression = SyntaxFactory.IdentifierName(property.Name), ExpressionType = property.Type } }) .Where(x => x.source != null) .SelectMany(pair => mappingGenerator.Map(pair.source, pair.target))); } return(Enumerable.Empty <SyntaxNode>()); }
private async Task <Document> UseLocalVariablesAsParameters(Document document, IInvocation invocation, bool generateNamedParameters, CancellationToken cancellationToken) { var semanticModel = await document.GetSemanticModelAsync(cancellationToken); var mappingSourceFinder = new LocalScopeMappingSourceFinder(semanticModel, invocation.SourceNode); var overloadParameterSets = invocation.GetOverloadParameterSets(semanticModel); if (overloadParameterSets != null) { var parametersMatch = MethodHelper.FindBestParametersMatch(mappingSourceFinder, overloadParameterSets); if (parametersMatch != null) { var syntaxGenerator = SyntaxGenerator.GetGenerator(document); var argumentList = parametersMatch.ToArgumentListSyntax(syntaxGenerator, generateNamedParameters); return(await document.ReplaceNodes(invocation.SourceNode, invocation.WithArgumentList(argumentList), cancellationToken)); } } return(document); }
private async Task <Document> InitizalizeWithLocals(Document document, InitializerExpressionSyntax objectInitializer, CancellationToken cancellationToken) { var semanticModel = await document.GetSemanticModelAsync(cancellationToken); var objectCreationExpression = objectInitializer.FindContainer <ObjectCreationExpressionSyntax>(); var createdObjectType = ModelExtensions.GetTypeInfo(semanticModel, objectCreationExpression).Type; var mappingSourceFinder = new LocalScopeMappingSourceFinder(semanticModel, objectInitializer); var propertiesToSet = ObjectHelper.GetPublicPropertySymbols(createdObjectType).Where(x => x.SetMethod?.DeclaredAccessibility == Accessibility.Public); var initExpressions = propertiesToSet.Aggregate(objectInitializer.Expressions, (expr, property) => { var mappingSource = mappingSourceFinder.FindMappingSource(property.Name, property.Type); if (mappingSource != null) { var assignmentExpression = SyntaxFactory.AssignmentExpression(SyntaxKind.SimpleAssignmentExpression, SyntaxFactory.IdentifierName(property.Name), mappingSource.Expression); return(expr.Add(assignmentExpression)); } return(expr); }); return(await document.ReplaceNodes(objectInitializer, objectInitializer.WithExpressions(initExpressions), cancellationToken)); }
private string GetArgumentListWithLocalVariables(Document document, IInvocation invocation, bool generateNamedParameters, SemanticModel semanticModel) { var mappingSourceFinder = new LocalScopeMappingSourceFinder(semanticModel, invocation.SourceNode); var syntaxGenerator = SyntaxGenerator.GetGenerator(document); var overloadParameterSets = invocation.GetOverloadParameterSets(semanticModel); if (overloadParameterSets != null) { var contextAssembly = semanticModel.FindContextAssembly(invocation.SourceNode); var mappingEngine = new MappingEngine(semanticModel, syntaxGenerator, contextAssembly); var parametersMatch = MethodHelper.FindBestParametersMatch(mappingSourceFinder, overloadParameterSets); if (parametersMatch != null) { var argumentList = parametersMatch.ToArgumentListSyntax(mappingEngine, generateNamedParameters); var chunks = argumentList.Arguments.Select(a => a.ToString()); return(string.Join(", ", chunks)); } } return(null); }
private static IEnumerable <SyntaxNode> GenerateMappingCode(IMethodSymbol methodSymbol, SyntaxGenerator generator, SemanticModel semanticModel) { if (SymbolHelper.IsIdentityMappingFunction(methodSymbol)) { var cloneMappingEngine = new CloneMappingEngine(semanticModel, generator, methodSymbol.ContainingAssembly); var source = methodSymbol.Parameters[0]; var targetType = methodSymbol.ReturnType; var newExpression = cloneMappingEngine.MapExpression((ExpressionSyntax)generator.IdentifierName(source.Name), source.Type, targetType); return(new[] { generator.ReturnStatement(newExpression).WithAdditionalAnnotations(Formatter.Annotation) }); } var mappingEngine = new MappingEngine(semanticModel, generator, methodSymbol.ContainingAssembly); if (SymbolHelper.IsPureMappingFunction(methodSymbol)) { var source = methodSymbol.Parameters[0]; var targetType = methodSymbol.ReturnType; var newExpression = mappingEngine.MapExpression((ExpressionSyntax)generator.IdentifierName(source.Name), source.Type, targetType); return(new[] { generator.ReturnStatement(newExpression).WithAdditionalAnnotations(Formatter.Annotation) }); } if (SymbolHelper.IsMultiParameterPureFunction(methodSymbol)) { var targetType = methodSymbol.ReturnType; var sourceFinder = new LocalScopeMappingSourceFinder(semanticModel, methodSymbol); var newExpression = mappingEngine.AddInitializerWithMapping((ObjectCreationExpressionSyntax)generator.ObjectCreationExpression(targetType), sourceFinder, targetType); return(new[] { generator.ReturnStatement(newExpression).WithAdditionalAnnotations(Formatter.Annotation) }); } if (SymbolHelper.IsUpdateParameterFunction(methodSymbol)) { var source = methodSymbol.Parameters[0]; var target = methodSymbol.Parameters[1]; var targets = ObjectHelper.GetFieldsThaCanBeSetPublicly(target.Type, methodSymbol.ContainingAssembly); var sourceFinder = new ObjectMembersMappingSourceFinder(source.Type, generator.IdentifierName(source.Name), generator); return(mappingEngine.MapUsingSimpleAssignment(generator, targets, sourceFinder, globalTargetAccessor: generator.IdentifierName(target.Name))); } if (SymbolHelper.IsUpdateThisObjectFunction(methodSymbol)) { var source = methodSymbol.Parameters[0]; var sourceFinder = new ObjectMembersMappingSourceFinder(source.Type, generator.IdentifierName(source.Name), generator); var targets = ObjectHelper.GetFieldsThaCanBeSetPrivately(methodSymbol.ContainingType); return(mappingEngine.MapUsingSimpleAssignment(generator, targets, sourceFinder)); } if (SymbolHelper.IsMultiParameterUpdateThisObjectFunction(methodSymbol)) { var sourceFinder = new LocalScopeMappingSourceFinder(semanticModel, methodSymbol.Parameters); var targets = ObjectHelper.GetFieldsThaCanBeSetPrivately(methodSymbol.ContainingType); return(mappingEngine.MapUsingSimpleAssignment(generator, targets, sourceFinder)); } if (SymbolHelper.IsMappingConstructor(methodSymbol)) { var source = methodSymbol.Parameters[0]; var sourceFinder = new ObjectMembersMappingSourceFinder(source.Type, generator.IdentifierName(source.Name), generator); var targets = ObjectHelper.GetFieldsThaCanBeSetFromConstructor(methodSymbol.ContainingType); return(mappingEngine.MapUsingSimpleAssignment(generator, targets, sourceFinder)); } if (SymbolHelper.IsMultiParameterMappingConstructor(methodSymbol)) { var sourceFinder = new LocalScopeMappingSourceFinder(semanticModel, methodSymbol.Parameters); var targets = ObjectHelper.GetFieldsThaCanBeSetFromConstructor(methodSymbol.ContainingType); return(mappingEngine.MapUsingSimpleAssignment(generator, targets, sourceFinder)); } return(Enumerable.Empty <SyntaxNode>()); }