private ITypeElement GetRelatedTypeElementFromAttributeUsage(ICSharpLiteralExpression literalExpression)
        {
            var attribute = AttributeNavigator.GetByConstructorArgumentExpression(literalExpression);

            if (attribute == null)
            {
                return(null);
            }

            var argument = (ICSharpArgument)literalExpression.Parent.NotNull();

            if (argument.MatchingParameter == null)
            {
                return(null);
            }

            var parameter            = argument.MatchingParameter.Element;
            var constructor          = attribute.ConstructorReference.GetResolved <IConstructor>().NotNull();
            var literalArgumentIndex = constructor.Parameters.IndexOf(parameter);

            if (literalArgumentIndex == 0)
            {
                return(null);
            }

            var typeofExpression = attribute.Arguments[literalArgumentIndex - 1].Expression as ITypeofExpression;

            if (typeofExpression == null)
            {
                return(null);
            }

            return(((IDeclaredType)typeofExpression.ArgumentType).GetTypeElement());
        }
        public static string SuggestShortReferenceName([NotNull] IReference reference,
                                                       [NotNull] IDeclaredElement declaredElement)
        {
            var sourceName = declaredElement.GetSourceName();

            var treeNode = reference.GetTreeNode();

            if (declaredElement is ITypeElement)
            {
                var referenceName = treeNode as ITypeReferenceName;
                if (AttributeNavigator.GetByReferenceName(referenceName) != null &&
                    sourceName.EndsWith(FSharpImplUtil.AttributeSuffix, StringComparison.Ordinal) &&
                    sourceName != FSharpImplUtil.AttributeSuffix)
                {
                    sourceName = sourceName.TrimFromEnd(FSharpImplUtil.AttributeSuffix);
                }
            }

            return(SuggestShortReferenceName(sourceName, treeNode.Language));
        }
        public IReference[] GetReferences(ITreeNode element, IReference[] oldReferences)
        {
            var literal = element as ILiteralExpression;

            if (literal != null && literal.ConstantValue.Value is string)
            {
                var agument   = literal.Parent as IVBArgument;
                var attribute = AttributeNavigator.GetByArgument(agument);
                if (attribute != null)
                {
                    var @class = attribute.AttributeType.Reference.Resolve().DeclaredElement as IClass;
                    if (@class != null && Equals(@class.GetClrName(), XunitTestProvider.PropertyDataAttribute))
                    {
                        var typeElement = (from a in attribute.Arguments
                                           where a is INamedArgument && a.ArgumentName == "PropertyType"
                                           select GetTypeof(a.Expression as IGetTypeExpression)).FirstOrDefault();

                        var member = GetAppliedToMethodDeclaration(attribute);
                        if (member != null && member.DeclaredElement != null && typeElement == null)
                        {
                            typeElement = member.DeclaredElement.GetContainingType();
                        }

                        if (typeElement == null)
                        {
                            return(EmptyArray <IReference> .Instance);
                        }

                        var reference = new PropertyDataReference(typeElement, literal);

                        return(oldReferences != null && oldReferences.Length == 1 && Equals(oldReferences[0], reference)
                                   ? oldReferences
                                   : new IReference[] { reference });
                    }
                }
            }

            return(EmptyArray <IReference> .Instance);
        }
Exemplo n.º 4
0
        public ReferenceCollection GetReferences(ITreeNode element, ReferenceCollection oldReferences)
        {
//            Protocol.TraceLogger.Log(LoggingLevel.INFO, $"GetReferences: {element.GetSourceFile()?.Name}: Node Type: {element.NodeType}");
            if (element is ILiteralExpression literal && literal.ConstantValue.Value is string)
            {
                var argumentExpression = literal as ICSharpExpression;
                var attribute          = AttributeNavigator.GetByConstructorArgumentExpression(argumentExpression);

                if (attribute?.Name.Reference.Resolve().DeclaredElement is IClass @class && Equals(@class.GetClrName(), GivenAttribute))
                {
//                        attribute.DumpObj(1);
                    var stringRegexLiteral = attribute.ConstructorArgumentExpressions[0] as ILiteralExpression;
                    var stringToken        = stringRegexLiteral.Literal as CSharpGenericToken;
                    var regexString        = stringToken.GetText();

                    Protocol.TraceLogger.Log(LoggingLevel.INFO, $"Found GIVEN attribute with regex: {regexString}");
                    return(oldReferences);
                }
            }

            return(oldReferences);
        }
Exemplo n.º 5
0
        public IReference[] GetReferences(ITreeNode element, IReference[] oldReferences)
        {
            var literal = element as ILiteralExpression;

            if (literal != null && literal.ConstantValue.Value is string)
            {
                var attribute = AttributeNavigator.GetByConstructorArgumentExpression(literal as ICSharpExpression);
                if (attribute != null)
                {
                    var @class = attribute.Name.Reference.Resolve().DeclaredElement as IClass;
                    if (@class != null && Equals(@class.GetClrName(), XunitTestProvider.PropertyDataAttribute))
                    {
                        var typeElement = (from a in attribute.PropertyAssignments
                                           where a.PropertyNameIdentifier.Name == "PropertyType"
                                           select GetTypeof(a.Source as ITypeofExpression)).FirstOrDefault();

                        var member = MethodDeclarationNavigator.GetByAttribute(attribute);
                        if (member != null && member.DeclaredElement != null && typeElement == null)
                        {
                            typeElement = member.DeclaredElement.GetContainingType();
                        }

                        if (typeElement == null)
                        {
                            return(EmptyArray <IReference> .Instance);
                        }

                        var reference = new PropertyDataReference(typeElement, literal);

                        return(oldReferences != null && oldReferences.Length == 1 && Equals(oldReferences[0], reference)
                                   ? oldReferences
                                   : new IReference[] { reference });
                    }
                }
            }

            return(EmptyArray <IReference> .Instance);
        }
        private static IAttribute GetAttribute(ITreeNode treeNode)
        {
            var argument = GetArgument(treeNode);

            return(AttributeNavigator.GetByArgument(argument));
        }
 public static IFSharpArgumentsOwner GetByArgumentExpression([CanBeNull] IFSharpExpression param) =>
 (IFSharpArgumentsOwner)AppLikeExprNavigator.GetByArgumentExpression(param) ??
 AttributeNavigator.GetByExpression(param);