private static MatchedParameterList FindParametersMatch(ArgumentListSyntax invalidArgumentList, IEnumerable <ImmutableArray <IParameterSymbol> > overloadParameterSets, SemanticModel semanticModel, SyntaxGenerator syntaxGenerator) { var sourceFinder = CreateSourceFinderBasedOnInvalidArgument(invalidArgumentList, semanticModel, syntaxGenerator); var parametersMatch = MethodHelper.FindBestParametersMatch(sourceFinder, overloadParameterSets); return(parametersMatch); }
protected virtual MappingElement TryToCreateMappingExpression(MappingElement source, ITypeSymbol targetType, MappingPath mappingPath) { //TODO: If source expression is method or constructor invocation then we should extract local variable and use it im mappings as a reference var namedTargetType = targetType as INamedTypeSymbol; if (namedTargetType != null) { var directlyMappingConstructor = namedTargetType.Constructors.FirstOrDefault(c => c.Parameters.Length == 1 && c.Parameters[0].Type.Equals(source.ExpressionType)); if (directlyMappingConstructor != null) { var constructorParameters = SyntaxFactory.ArgumentList().AddArguments(SyntaxFactory.Argument(source.Expression)); var creationExpression = syntaxGenerator.ObjectCreationExpression(targetType, constructorParameters.Arguments); return(new MappingElement() { ExpressionType = targetType, Expression = (ExpressionSyntax)creationExpression }); } } if (MappingHelper.IsMappingBetweenCollections(targetType, source.ExpressionType)) { return(new MappingElement() { ExpressionType = targetType, Expression = MapCollections(source.Expression, source.ExpressionType, targetType, mappingPath.Clone()) as ExpressionSyntax }); } var subMappingSourceFinder = new ObjectMembersMappingSourceFinder(source.ExpressionType, source.Expression, syntaxGenerator); if (namedTargetType != null) { //maybe there is constructor that accepts parameter matching source properties var constructorOverloadParameterSets = namedTargetType.Constructors.Select(x => x.Parameters); var matchedOverload = MethodHelper.FindBestParametersMatch(subMappingSourceFinder, constructorOverloadParameterSets); if (matchedOverload != null) { var creationExpression = syntaxGenerator.ObjectCreationExpression(targetType, matchedOverload.ToArgumentListSyntax(this).Arguments); return(new MappingElement() { ExpressionType = targetType, Expression = (ExpressionSyntax)creationExpression }); } } var objectCreationExpressionSyntax = ((ObjectCreationExpressionSyntax)syntaxGenerator.ObjectCreationExpression(targetType)); return(new MappingElement() { ExpressionType = targetType, Expression = AddInitializerWithMapping(objectCreationExpressionSyntax, subMappingSourceFinder, targetType, mappingPath) }); }
private ArgumentListSyntax FindMappingConstructorParameters(ITypeSymbol targetType, ITypeSymbol sourceType, ObjectMembersMappingSourceFinder mappingSourceFinder, ExpressionSyntax globalSourceAccessor) { if (targetType is INamedTypeSymbol namedType) { var directlyMappingConstructor = namedType.Constructors.FirstOrDefault(c => c.Parameters.Length == 1 && c.Parameters[0].Type == sourceType); if (directlyMappingConstructor != null) { return(SyntaxFactory.ArgumentList().AddArguments(SyntaxFactory.Argument(globalSourceAccessor))); } var constructorOverloadParameterSets = namedType.Constructors.Select(x => x.Parameters); return(MethodHelper.FindBestParametersMatch(mappingSourceFinder, constructorOverloadParameterSets)?.ToArgumentListSyntax(generator)); } return(null); }
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 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); }
public static async Task <Document> FixInvocationWithParameters(Document document, IInvocation invocation, bool generateNamedParameters, SemanticModel semanticModel, IMappingSourceFinder mappingSourceFinder, CancellationToken cancellationToken) { 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); return(await document.ReplaceNodes(invocation.SourceNode, invocation.WithArgumentList(argumentList), cancellationToken)); } } return(document); }