private static async Task <Document> ReplaceEmptyInitializationBlock(Document document,
                                                                             InitializerExpressionSyntax objectInitializer, CancellationToken cancellationToken,
                                                                             SemanticModel semanticModel, IMappingSourceFinder mappingSourceFinder, SyntaxGenerator syntaxGenerator)
        {
            var contextAssembly   = semanticModel.FindContextAssembly(objectInitializer);
            var oldObjCreation    = objectInitializer.FindContainer <ObjectCreationExpressionSyntax>();
            var createdObjectType = ModelExtensions.GetTypeInfo(semanticModel, oldObjCreation).Type;
            var mappingEngine     = await MappingEngine.Create(document, cancellationToken, semanticModel.FindContextAssembly(objectInitializer));

            var newObjectCreation = mappingEngine.AddInitializerWithMapping(oldObjCreation, mappingSourceFinder, createdObjectType);

            return(await document.ReplaceNodes(oldObjCreation, newObjectCreation, cancellationToken));
        }
        private static async Task <Document> ReplaceWithMappingBody(Document document, LambdaExpressionSyntax lambda, SemanticModel semanticModel, IMappingMatcher mappingMatcher, CancellationToken cancellationToken)
        {
            var methodSymbol      = (IMethodSymbol)semanticModel.GetSymbolInfo(lambda).Symbol;
            var createdObjectType = methodSymbol.Parameters.First().Type;
            var contextAssembly   = semanticModel.FindContextAssembly(lambda);
            var mappingEngine     = await MappingEngine.Create(document, cancellationToken, contextAssembly);

            var propertiesToSet = ObjectHelper.GetFieldsThaCanBeSetPublicly(createdObjectType, contextAssembly);
            var statements      = mappingEngine.MapUsingSimpleAssignment(propertiesToSet, mappingMatcher, globalTargetAccessor: SyntaxFactory.IdentifierName(GetParameterIdentifier(lambda)))
                                  .Select(x => x.AsStatement().WithTrailingTrivia(SyntaxFactory.EndOfLine("\r\n")));

            var newLambda = UpdateLambdaBody(lambda, SyntaxFactory.Block(statements)).WithAdditionalAnnotations(Formatter.Annotation);

            return(await document.ReplaceNodes(lambda, newLambda, 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);
        }
        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);
        }