Exemple #1
0
        ParameterHintingResult HandleAttribute(SemanticModel semanticModel, SyntaxNode node, CancellationToken cancellationToken)
        {
            var info           = semanticModel.GetSymbolInfo(node, cancellationToken);
            var result         = new ParameterHintingResult(node.SpanStart);
            var resolvedMethod = info.Symbol as IMethodSymbol;

            if (resolvedMethod != null)
            {
                result.AddData(factory.CreateConstructorProvider(resolvedMethod));
            }
            result.AddRange(info.CandidateSymbols.OfType <IMethodSymbol>().Select(m => factory.CreateConstructorProvider(m)));
            return(result);
        }
Exemple #2
0
        ParameterHintingResult HandleTypeParameterCase(SemanticModel semanticModel, SyntaxNode node, CancellationToken cancellationToken)
        {
            var    result = new ParameterHintingResult(node.SpanStart);
            string typeName;
            var    gns = node as GenericNameSyntax;

            if (gns != null)
            {
                typeName = gns.Identifier.ToString();
            }
            else
            {
                typeName = node.ToString();
            }

            foreach (var cand in semanticModel.LookupSymbols(node.SpanStart).OfType <INamedTypeSymbol> ())
            {
                if (cand.TypeParameters.Length == 0)
                {
                    continue;
                }
                if (cand.Name == typeName || cand.GetFullName() == typeName)
                {
                    result.AddData(factory.CreateTypeParameterDataProvider(cand));
                }
            }

            if (result.Count == 0)
            {
                foreach (var cand in semanticModel.LookupSymbols(node.SpanStart).OfType <IMethodSymbol> ())
                {
                    if (cand.TypeParameters.Length == 0)
                    {
                        continue;
                    }
                    if (cand.Name == typeName)
                    {
                        result.AddData(factory.CreateTypeParameterDataProvider(cand));
                    }
                }
            }
            return(result);
        }
        ParameterHintingResult HandleAttribute(SemanticModel semanticModel, SyntaxNode node, CancellationToken cancellationToken)
        {
            var info           = semanticModel.GetSymbolInfo(node, cancellationToken);
            var result         = new ParameterHintingResult(node.SpanStart);
            var resolvedMethod = info.Symbol as IMethodSymbol;

            if (resolvedMethod != null)
            {
                foreach (var c in resolvedMethod.ContainingType.GetMembers().OfType <IMethodSymbol> ().Where(m => m.MethodKind == MethodKind.Constructor))
                {
                    result.AddData(factory.CreateConstructorProvider(c));
                }
            }
            else
            {
                result.AddRange(info.CandidateSymbols.OfType <IMethodSymbol> ().Select(factory.CreateConstructorProvider));
            }
            return(result);
        }
Exemple #4
0
        ParameterHintingResult HandleObjectCreationExpression(SemanticModel semanticModel, SyntaxNode node, CancellationToken cancellationToken)
        {
            // var info = semanticModel.GetSymbolInfo(node, cancellationToken);
            var result = new ParameterHintingResult(node.SpanStart);
            var within = semanticModel.GetEnclosingNamedTypeOrAssembly(node.SpanStart, cancellationToken);

            var targetTypeInfo = semanticModel.GetTypeInfo(node);

            if (targetTypeInfo.Type != null)
            {
                foreach (IMethodSymbol c in targetTypeInfo.Type.GetMembers().OfType <IMethodSymbol>().Where(m => m.MethodKind == MethodKind.Constructor))
                {
                    if (c.IsAccessibleWithin(within))
                    {
                        result.AddData(factory.CreateConstructorProvider(c));
                    }
                }
            }
            return(result);
        }
Exemple #5
0
        ParameterHintingResult HandleElementAccessExpression(SemanticModel semanticModel, ElementAccessExpressionSyntax node, CancellationToken cancellationToken)
        {
            var within = semanticModel.GetEnclosingNamedTypeOrAssembly(node.SpanStart, cancellationToken);

            var         targetTypeInfo = semanticModel.GetTypeInfo(node.Expression);
            ITypeSymbol type           = targetTypeInfo.Type;

            if (type == null)
            {
                return(ParameterHintingResult.Empty);
            }

            var result = new ParameterHintingResult(node.SpanStart);

            if (type.TypeKind == TypeKind.Array)
            {
                result.AddData(factory.CreateArrayDataProvider((IArrayTypeSymbol)type));
                return(result);
            }

            var addedProperties = new List <IPropertySymbol> ();

            for (; type != null; type = type.BaseType)
            {
                foreach (var indexer in type.GetMembers().OfType <IPropertySymbol> ().Where(p => p.IsIndexer))
                {
                    if (addedProperties.Any(added => SignatureComparer.HaveSameSignature(indexer, added, true)))
                    {
                        continue;
                    }

                    if (indexer.IsAccessibleWithin(within))
                    {
                        addedProperties.Add(indexer);
                        result.AddData(factory.CreateIndexerParameterDataProvider(indexer, node));
                    }
                }
            }
            return(result);
        }
        ParameterHintingResult HandleConstructorInitializer(SemanticModel semanticModel, SyntaxNode node, CancellationToken cancellationToken)
        {
            var info           = semanticModel.GetSymbolInfo(node, cancellationToken);
            var result         = new ParameterHintingResult(node.SpanStart);
            var resolvedMethod = info.Symbol as IMethodSymbol;

            if (resolvedMethod != null)
            {
                var type   = resolvedMethod.ContainingType;
                var within = semanticModel.GetEnclosingNamedTypeOrAssembly(node.SpanStart, cancellationToken);

                result.AddRange(type.GetMembers()
                                .OfType <IMethodSymbol> ()
                                .Where(m => m.MethodKind == MethodKind.Constructor && m.IsAccessibleWithin(within))
                                .Select(factory.CreateConstructorProvider));
            }
            else
            {
                result.AddRange(info.CandidateSymbols.OfType <IMethodSymbol> ().Select(factory.CreateConstructorProvider));
            }
            return(result);
        }
Exemple #7
0
        ParameterHintingResult HandleInvocationExpression(SemanticModel semanticModel, InvocationExpressionSyntax node, CancellationToken cancellationToken)
        {
            var info   = semanticModel.GetSymbolInfo(node, cancellationToken);
            var result = new ParameterHintingResult(node.SpanStart);

            var targetTypeInfo = semanticModel.GetTypeInfo(node.Expression);

            if (targetTypeInfo.Type != null && targetTypeInfo.Type.TypeKind == TypeKind.Delegate)
            {
                result.AddData(factory.CreateMethodDataProvider(targetTypeInfo.Type.GetDelegateInvokeMethod()));
                return(result);
            }

            var         within = semanticModel.GetEnclosingNamedTypeOrAssembly(node.SpanStart, cancellationToken);
            ITypeSymbol type;
            var         ma           = node.Expression as MemberAccessExpressionSyntax;
            string      name         = null;
            bool        staticLookup = false;

            if (ma != null)
            {
                staticLookup = semanticModel.GetSymbolInfo(ma.Expression).Symbol is ITypeSymbol;
                type         = semanticModel.GetTypeInfo(ma.Expression).Type;
                name         = info.Symbol?.Name ?? ma.Name.Identifier.ValueText;
            }
            else
            {
                type = within as ITypeSymbol;
                name = info.Symbol?.Name ?? node.Expression.ToString();
                var sym = semanticModel.GetEnclosingSymbol(node.SpanStart, cancellationToken);
                staticLookup = sym.IsStatic;
            }
            var addedMethods = new List <IMethodSymbol> ();
            var filterMethod = new HashSet <IMethodSymbol> ();

            for (; type != null; type = type.BaseType)
            {
                foreach (var method in type.GetMembers().OfType <IMethodSymbol> ().Concat(GetExtensionMethods(semanticModel, type, node, cancellationToken)).Where(m => m.Name == name))
                {
                    if (staticLookup && !method.IsStatic)
                    {
                        continue;
                    }
                    if (method.OverriddenMethod != null)
                    {
                        filterMethod.Add(method.OverriddenMethod);
                    }
                    if (filterMethod.Contains(method))
                    {
                        continue;
                    }
                    if (addedMethods.Any(added => SignatureComparer.HaveSameSignature(method, added, true)))
                    {
                        continue;
                    }
                    if (method.IsAccessibleWithin(within))
                    {
                        if (info.Symbol != null)
                        {
                            var smethod = (IMethodSymbol)info.Symbol;
                            if (smethod != null && smethod.OriginalDefinition == method)
                            {
                                continue;
                            }
                        }
                        addedMethods.Add(method);
                        result.AddData(factory.CreateMethodDataProvider(method));
                    }
                }
            }
            if (info.Symbol != null && !addedMethods.Contains(info.Symbol))
            {
                if (!staticLookup || info.Symbol.IsStatic)
                {
                    result.AddData(factory.CreateMethodDataProvider((IMethodSymbol)info.Symbol));
                }
            }
            return(result);
        }
Exemple #8
0
        ParameterHintingResult HandleInvocationExpression(SemanticModel semanticModel, InvocationExpressionSyntax node, CancellationToken cancellationToken)
        {
            var result = new ParameterHintingResult(node.SpanStart);

            var targetTypeInfo = semanticModel.GetTypeInfo(node.Expression);

            if (targetTypeInfo.Type != null && targetTypeInfo.Type.TypeKind == TypeKind.Delegate)
            {
                result.AddData(factory.CreateMethodDataProvider(targetTypeInfo.Type.GetDelegateInvokeMethod()));
                return(result);
            }

            var within = semanticModel.GetEnclosingNamedTypeOrAssembly(node.SpanStart, cancellationToken);

            if (within == null)
            {
                return(result);
            }

            var memberGroup         = semanticModel.GetMemberGroup(node.Expression, cancellationToken).OfType <IMethodSymbol> ();
            var matchedMethodSymbol = semanticModel.GetSymbolInfo(node, cancellationToken).Symbol as IMethodSymbol;

            // if the symbol could be bound, replace that item in the symbol list
            if (matchedMethodSymbol != null && matchedMethodSymbol.IsGenericMethod)
            {
                memberGroup = memberGroup.Select(m => matchedMethodSymbol.OriginalDefinition == m ? matchedMethodSymbol : m);
            }

            ITypeSymbol throughType = null;

            if (node.Expression is MemberAccessExpressionSyntax)
            {
                var throughExpression = ((MemberAccessExpressionSyntax)node.Expression).Expression;
                var throughSymbol     = semanticModel.GetSymbolInfo(throughExpression, cancellationToken).GetAnySymbol();

                // if it is via a base expression "base.", we know the "throughType" is the base class but
                // we need to be able to tell between "base.M()" and "new Base().M()".
                // currently, Access check methods do not differentiate between them.
                // so handle "base." primary-expression here by nulling out "throughType"
                if (!(throughExpression is BaseExpressionSyntax))
                {
                    throughType = semanticModel.GetTypeInfo(throughExpression, cancellationToken).Type;
                }

                var includeInstance = !throughExpression.IsKind(SyntaxKind.IdentifierName) ||
                                      semanticModel.LookupSymbols(throughExpression.SpanStart, name: throughSymbol.Name).Any(s => !(s is INamedTypeSymbol)) ||
                                      (!(throughSymbol is INamespaceOrTypeSymbol) && semanticModel.LookupSymbols(throughExpression.SpanStart, container: throughSymbol.ContainingType).Any(s => !(s is INamedTypeSymbol)));

                var includeStatic = throughSymbol is INamedTypeSymbol ||
                                    (throughExpression.IsKind(SyntaxKind.IdentifierName) &&
                                     semanticModel.LookupNamespacesAndTypes(throughExpression.SpanStart, name: throughSymbol.Name).Any(t => t.GetSymbolType() == throughType));

                memberGroup = memberGroup.Where(m => (m.IsStatic && includeStatic) || (!m.IsStatic && includeInstance));
            }
            else if (node.Expression is SimpleNameSyntax && node.IsInStaticContext())
            {
                memberGroup = memberGroup.Where(m => m.IsStatic);
            }

            var methodList = memberGroup.Where(member => member.IsAccessibleWithin(within, throughType)).ToList();

            memberGroup = methodList.Where(m => !IsHiddenByOtherMethod(m, methodList));
            foreach (var member in memberGroup)
            {
                result.AddData(factory.CreateMethodDataProvider(member));
            }
            return(result);
        }