private string GetParameterName(ISyntaxFactsService service, int index)
            {
                if (Arguments.IsDefault || index >= Arguments.Length)
                {
                    return(string.Empty);
                }

                return(service.GetNameForArgument(Arguments[index]));
            }
Exemplo n.º 2
0
            private string GetParameterName(ISyntaxFactsService service, int index)
            {
                if (index >= this.Arguments?.Count)
                {
                    return(string.Empty);
                }

                return(service.GetNameForArgument(this.Arguments?[index]));
            }
            > GetArgumentInsertPositionForMethodCandidates(
            TArgumentSyntax argumentOpt,
            SemanticModel semanticModel,
            ISyntaxFactsService syntaxFacts,
            SeparatedSyntaxList <TArgumentSyntax> arguments,
            ImmutableArray <IMethodSymbol> methodCandidates
            )
        {
            var comparer = syntaxFacts.StringComparer;
            var methodsAndArgumentToAdd = ArrayBuilder <
                ArgumentInsertPositionData <TArgumentSyntax>
                > .GetInstance();

            foreach (var method in methodCandidates.OrderBy(m => m.Parameters.Length))
            {
                if (method.IsNonImplicitAndFromSource())
                {
                    var isNamedArgument = !string.IsNullOrWhiteSpace(
                        syntaxFacts.GetNameForArgument(argumentOpt)
                        );

                    if (isNamedArgument || NonParamsParameterCount(method) < arguments.Count)
                    {
                        var argumentToAdd = DetermineFirstArgumentToAdd(
                            semanticModel,
                            syntaxFacts,
                            comparer,
                            method,
                            arguments
                            );

                        if (argumentToAdd != null)
                        {
                            if (argumentOpt != null && argumentToAdd != argumentOpt)
                            {
                                // We were trying to fix a specific argument, but the argument we want
                                // to fix is something different.  That means there was an error earlier
                                // than this argument.  Which means we're looking at a non-viable
                                // constructor or method.  Skip this one.
                                continue;
                            }

                            methodsAndArgumentToAdd.Add(
                                new ArgumentInsertPositionData <TArgumentSyntax>(
                                    method,
                                    argumentToAdd,
                                    arguments.IndexOf(argumentToAdd)
                                    )
                                );
                        }
                    }
                }
            }

            return(methodsAndArgumentToAdd.ToImmutableAndFree());
        }
Exemplo n.º 4
0
        private static TArgumentSyntax DetermineFirstArgumentToAdd(
            SemanticModel semanticModel,
            ISyntaxFactsService syntaxFacts,
            StringComparer comparer,
            IMethodSymbol method,
            SeparatedSyntaxList <TArgumentSyntax> arguments)
        {
            var compilation          = semanticModel.Compilation;
            var methodParameterNames = new HashSet <string>(comparer);

            methodParameterNames.AddRange(method.Parameters.Select(p => p.Name));

            for (int i = 0, n = arguments.Count; i < n; i++)
            {
                var argument     = arguments[i];
                var argumentName = syntaxFacts.GetNameForArgument(argument);

                if (!string.IsNullOrWhiteSpace(argumentName))
                {
                    // If the user provided an argument-name and we don't have any parameters that
                    // match, then this is the argument we want to add a parameter for.
                    if (!methodParameterNames.Contains(argumentName))
                    {
                        return(argument);
                    }
                }
                else
                {
                    // Positional argument.  If the position is beyond what the method supports,
                    // then this definitely is an argument we could add.
                    if (i >= method.Parameters.Length)
                    {
                        if (method.Parameters.LastOrDefault()?.IsParams == true)
                        {
                            // Last parameter is a params.  We can't place any parameters past it.
                            return(null);
                        }

                        return(argument);
                    }

                    // Now check the type of the argument versus the type of the parameter.  If they
                    // don't match, then this is the argument we should make the parameter for.
                    var expressionOfArgument = syntaxFacts.GetExpressionOfArgument(argument);
                    if (expressionOfArgument is null)
                    {
                        return(null);
                    }
                    var argumentTypeInfo = semanticModel.GetTypeInfo(expressionOfArgument);
                    var isNullLiteral    = syntaxFacts.IsNullLiteralExpression(expressionOfArgument);
                    var isDefaultLiteral = syntaxFacts.IsDefaultLiteralExpression(expressionOfArgument);

                    if (argumentTypeInfo.Type == null && argumentTypeInfo.ConvertedType == null)
                    {
                        // Didn't know the type of the argument.  We shouldn't assume it doesn't
                        // match a parameter.  However, if the user wrote 'null' and it didn't
                        // match anything, then this is the problem argument.
                        if (!isNullLiteral && !isDefaultLiteral)
                        {
                            continue;
                        }
                    }

                    var parameter = method.Parameters[i];

                    if (!TypeInfoMatchesType(
                            compilation, argumentTypeInfo, parameter.Type,
                            isNullLiteral, isDefaultLiteral))
                    {
                        if (TypeInfoMatchesWithParamsExpansion(
                                compilation, argumentTypeInfo, parameter,
                                isNullLiteral, isDefaultLiteral))
                        {
                            // The argument matched if we expanded out the params-parameter.
                            // As the params-parameter has to be last, there's nothing else to
                            // do here.
                            return(null);
                        }

                        return(argument);
                    }
                }
            }

            return(null);
        }
Exemplo n.º 5
0
 private string GetArgumentName(TArgumentSyntax argument, ISyntaxFactsService syntaxFactsService)
 => syntaxFactsService.GetNameForArgument(argument);