Exemplo n.º 1
0
        IEnumerable <INamedTypeSymbol> FindNearestTupleConstructionWithInferrableType(SyntaxNode root, SemanticModel semanticModel, int position, SignatureHelpTriggerInfo triggerInfo,
                                                                                      ITypeInferenceService typeInferrer, ISyntaxFactsService syntaxFacts, CancellationToken cancellationToken, out ExpressionSyntax targetExpression)
        {
            // Walk upward through TupleExpressionSyntax/ParenthsizedExpressionSyntax looking for a
            // place where we can infer a tuple type.
            ParenthesizedExpressionSyntax parenthesizedExpression = null;

            while (TryGetTupleExpression(triggerInfo.TriggerReason, root, position, syntaxFacts, cancellationToken, out var tupleExpression) ||
                   TryGetParenthesizedExpression(triggerInfo.TriggerReason, root, position, syntaxFacts, cancellationToken, out parenthesizedExpression))
            {
                targetExpression = (ExpressionSyntax)tupleExpression ?? parenthesizedExpression;
                var inferredTypes = typeInferrer.InferTypes(semanticModel, targetExpression.SpanStart, cancellationToken);

                var tupleTypes = inferredTypes.Where(t => t.IsTupleType).OfType <INamedTypeSymbol>().ToList();
                if (tupleTypes.Any())
                {
                    return(tupleTypes);
                }

                position = targetExpression.GetFirstToken().SpanStart;
            }

            targetExpression = null;
            return(null);
        }
Exemplo n.º 2
0
        public static INamedTypeSymbol InferDelegateType(
            this ITypeInferenceService typeInferenceService,
            SemanticModel semanticModel,
            int position,
            CancellationToken cancellationToken)
        {
            var types = typeInferenceService.InferTypes(semanticModel, position, cancellationToken);

            return(GetFirstDelegateType(semanticModel, types));
        }
Exemplo n.º 3
0
        public static INamedTypeSymbol InferDelegateType(
            this ITypeInferenceService typeInferenceService,
            SemanticModel semanticModel,
            SyntaxNode expression,
            CancellationToken cancellationToken)
        {
            var types         = typeInferenceService.InferTypes(semanticModel, expression, cancellationToken);
            var delegateTypes = types.Select(t => t.GetDelegateType(semanticModel.Compilation));

            return(delegateTypes.WhereNotNull().FirstOrDefault());
        }
 public static ImmutableArray <ITypeSymbol> InferTypes(
     this ITypeInferenceService service,
     SemanticModel semanticModel,
     int position,
     CancellationToken cancellationToken
     ) =>
 service.InferTypes(
     semanticModel,
     position,
     nameOpt: null,
     cancellationToken: cancellationToken
     );
Exemplo n.º 5
0
        public static ITypeSymbol InferType(this ITypeInferenceService typeInferenceService,
                                            SemanticModel semanticModel,
                                            int position,
                                            bool objectAsDefault,
                                            CancellationToken cancellationToken)
        {
            var types = typeInferenceService.InferTypes(semanticModel, position, cancellationToken)
                        .WhereNotNull();

            if (!types.Any())
            {
                return(objectAsDefault ? semanticModel.Compilation.ObjectType : null);
            }

            return(types.FirstOrDefault());
        }
Exemplo n.º 6
0
        public static ITypeSymbol InferType(
            this ITypeInferenceService typeInferenceService,
            SemanticModel semanticModel,
            int position,
            bool objectAsDefault,
            string nameOpt,
            CancellationToken cancellationToken)
        {
            var types = typeInferenceService.InferTypes(semanticModel, position, nameOpt, cancellationToken);

            if (types.Length == 0)
            {
                return(objectAsDefault ? semanticModel.Compilation.ObjectType : null);
            }

            return(types.FirstOrDefault());
        }
        public static ITypeSymbol?InferType(
            this ITypeInferenceService typeInferenceService,
            SemanticModel semanticModel,
            SyntaxNode expression,
            bool objectAsDefault,
            string?name,
            CancellationToken cancellationToken)
        {
            var types = typeInferenceService.InferTypes(semanticModel, expression, name, cancellationToken);

            if (types.Length == 0)
            {
                return(objectAsDefault ? semanticModel.Compilation.ObjectType : null);
            }

            return(types.First());
        }
Exemplo n.º 8
0
        private bool IsLambdaExpression(SemanticModel semanticModel, int position, SyntaxToken token, ITypeInferenceService typeInferrer, CancellationToken cancellationToken)
        {
            // Typing a generic type parameter, the tree might look like a binary expression around the < token.
            // If we infer a delegate type here (because that's what on the other side of the binop),
            // ignore it.
            if (token.Kind() == SyntaxKind.LessThanToken && token.Parent is BinaryExpressionSyntax)
            {
                return(false);
            }

            // We might be in the arguments to a parenthesized lambda
            if (token.Kind() == SyntaxKind.OpenParenToken || token.Kind() == SyntaxKind.CommaToken)
            {
                if (token.Parent != null && token.Parent is ParameterListSyntax)
                {
                    return(token.Parent.Parent != null && token.Parent.Parent is ParenthesizedLambdaExpressionSyntax);
                }
            }

            // Walk up a single level to allow for typing the beginning of a lambda:
            // new AssemblyLoadEventHandler(($$
            if (token.Kind() == SyntaxKind.OpenParenToken &&
                token.Parent.Kind() == SyntaxKind.ParenthesizedExpression)
            {
                position = token.Parent.SpanStart;
            }

            // WorkItem 834609: Automatic brace completion inserts the closing paren, making it
            // like a cast.
            if (token.Kind() == SyntaxKind.OpenParenToken &&
                token.Parent.Kind() == SyntaxKind.CastExpression)
            {
                position = token.Parent.SpanStart;
            }

            // If we're an argument to a function with multiple overloads,
            // open the builder if any overload takes a delegate at our argument position
            var inferredTypes = typeInferrer.InferTypes(semanticModel, position, cancellationToken: cancellationToken);

            return(inferredTypes.Any(type => type.GetDelegateType(semanticModel.Compilation).IsDelegateType()));
        }
        IEnumerable<INamedTypeSymbol> FindNearestTupleConstructionWithInferrableType(SyntaxNode root, SemanticModel semanticModel, int position, SignatureHelpTriggerInfo triggerInfo,
            ITypeInferenceService typeInferrer, ISyntaxFactsService syntaxFacts, CancellationToken cancellationToken, out ExpressionSyntax targetExpression)
        {
            // Walk upward through TupleExpressionSyntax/ParenthsizedExpressionSyntax looking for a 
            // place where we can infer a tuple type. 
            ParenthesizedExpressionSyntax parenthesizedExpression = null;
            while (TryGetTupleExpression(triggerInfo.TriggerReason, root, position, syntaxFacts, cancellationToken, out var tupleExpression) ||
                   TryGetParenthesizedExpression(triggerInfo.TriggerReason, root, position, syntaxFacts, cancellationToken, out parenthesizedExpression))
            {
                targetExpression = (ExpressionSyntax)tupleExpression ?? parenthesizedExpression;
                var inferredTypes = typeInferrer.InferTypes(semanticModel, targetExpression.SpanStart, cancellationToken);

                var tupleTypes = inferredTypes.Where(t => t.IsTupleType).OfType<INamedTypeSymbol>().ToList();
                if (tupleTypes.Any())
                {
                    return tupleTypes;
                }

                position = targetExpression.GetFirstToken().SpanStart;
            }

            targetExpression = null;
            return null;
        }
        private bool IsLambdaExpression(SemanticModel semanticModel, int position, SyntaxToken token, ITypeInferenceService typeInferrer, CancellationToken cancellationToken)
        {
            // Typing a generic type parameter, the tree might look like a binary expression around the < token.
            // If we infer a delegate type here (because that's what on the other side of the binop), 
            // ignore it.
            if (token.Kind() == SyntaxKind.LessThanToken && token.Parent is BinaryExpressionSyntax)
            {
                return false;
            }

            // We might be in the arguments to a parenthsized lambda
            if (token.Kind() == SyntaxKind.OpenParenToken || token.Kind() == SyntaxKind.CommaToken)
            {
                if (token.Parent != null && token.Parent is ParameterListSyntax)
                {
                    return token.Parent.Parent != null && token.Parent.Parent is ParenthesizedLambdaExpressionSyntax;
                }
            }

            // Walk up a single level to allow for typing the beginning of a lambda:
            // new AssemblyLoadEventHandler(($$
            if (token.Kind() == SyntaxKind.OpenParenToken &&
                token.Parent.Kind() == SyntaxKind.ParenthesizedExpression)
            {
                position = token.Parent.SpanStart;
            }

            // WorkItem 834609: Automatic brace completion inserts the closing paren, making it
            // like a cast.
            if (token.Kind() == SyntaxKind.OpenParenToken &&
                token.Parent.Kind() == SyntaxKind.CastExpression)
            {
                position = token.Parent.SpanStart;
            }

            // If we're an argument to a function with multiple overloads, 
            // open the builder if any overload takes a delegate at our argument position
            var inferredTypes = typeInferrer.InferTypes(semanticModel, position, cancellationToken: cancellationToken);

            return inferredTypes.Any(type => type.GetDelegateType(semanticModel.Compilation).IsDelegateType());
        }
 public ImmutableArray <ITypeSymbol> InferTypes(SemanticModel semanticModel, int position, string?name, CancellationToken cancellationToken)
 => UnderlyingObject.InferTypes(semanticModel, position, name, cancellationToken);