Beispiel #1
0
 public static ICollection <string> GetTags(this IAttributesOwner type)
 {
     return(type.GetAttributeInstances(new CLRTypeName(typeof(TagsAttribute).FullName), true)
            .SelectMany(x => x.PositionParameters(), (x, v) => v.ConstantValue.Value.ToString())
            .Distinct()
            .ToList());
 }
        /// <summary>Codes the annotation attribute.</summary>
        /// <param name="attributesOwner">The attributes owner.</param>
        /// <returns>Returns the annotation attribute.</returns>
        private CodeAnnotationAttribute GetAnnotation([NotNull] IAttributesOwner attributesOwner)
        {
            if (attributesOwner == null)
            {
                throw new ArgumentNullException("attributesOwner");
            }

            var instances = attributesOwner.GetAttributeInstances(true);

            foreach (var attributeInstance in instances)
            {
                var shortName = attributeInstance.GetClrName().ShortName;
                if (shortName == "NotNullAttribute")
                {
                    return(CodeAnnotationAttribute.NotNull);
                }

                if (shortName == "CanBeNullAttribute")
                {
                    return(CodeAnnotationAttribute.CanBeNull);
                }
            }

            return(CodeAnnotationAttribute.NotSet);
        }
        private bool markedWithAttributeMatch(IDeclaration declaration)
        {
            if (string.IsNullOrEmpty(_markedWithAttribute))
            {
                return(true);
            }

            if (_markedWithAttributeType == null)
            {
                return(false);
            }

            IAttributesOwner attributesOwner = declaration.DeclaredElement as IAttributesOwner;

            if (attributesOwner == null)
            {
                return(false);
            }

            foreach (IAttributeInstance attribute in attributesOwner.GetAttributeInstances(false))
            {
                if (Equals(attribute.GetAttributeType().GetTypeElement(), _markedWithAttributeType))
                {
                    return(true);
                }
            }
            return(false);
        }
Beispiel #4
0
 private void HandleContainerElement(
     IHighlightingConsumer consumer,
     [CanBeNull] IAttributesOwner element,
     [CanBeNull] ITypeUsage typeNodeForHighlighting)
 {
     if (typeNodeForHighlighting != null && IsItemNotNull(element))
         consumer.AddHighlighting(new StaticNullabilityItemTypeHighlighting(typeNodeForHighlighting, "[ItemNotNull]"));
 }
Beispiel #5
0
 private void HandleElement(
     IHighlightingConsumer consumer,
     [CanBeNull] IAttributesOwner element,
     [CanBeNull] ITreeNode typeNodeForHighlighting)
 {
     if (typeNodeForHighlighting != null && IsNotNull(element))
         consumer.AddHighlighting(new StaticNullabilityTypeHighlighting(typeNodeForHighlighting, "[NotNull]"));
 }
Beispiel #6
0
        public static string GetSubjectString(this IAttributesOwner type)
        {
            var attribute = type.GetAttributeInstances(new CLRTypeName(typeof(SubjectAttribute).FullName), true)
                            .FirstOrDefault();

            if (attribute == null)
            {
                var containingType = type.GetContainingType();
                if (containingType == null)
                {
                    return(null);
                }

                return(containingType.GetSubjectString());
            }

            if (attribute.PositionParameters().Any(x => x.IsBadValue))
            {
                return(null);
            }

            if (attribute.PositionParameters()
                .Where(x => x.IsType)
                .Select(x => x.TypeValue as IDeclaredType)
                .Any(x => x.IsInvalid()))
            {
                return(null);
            }

            try
            {
                var parameters = attribute.PositionParameters()
                                 .Select(x =>
                {
                    if (x.IsType)
                    {
                        var declaredType = (IDeclaredType)x.TypeValue;
#if RESHARPER_6
                        return(declaredType.GetClrName().ShortName);
#else
                        return(new CLRTypeName(declaredType.GetCLRName()).ShortName);
#endif
                    }

                    return((string)x.ConstantValue.Value);
                })
                                 .ToArray();

                return(String.Join(" ", parameters));
            }
            catch (Exception)
            {
                return(null);
            }
        }
Beispiel #7
0
        private void AppendNullness([NotNull] IAttributesOwner attributesOwner)
        {
            string nullnessName = GetNullnessName(attributesOwner);

            if (nullnessName == null)
            {
                return;
            }

            AppendText("[", null);
            AppendText(nullnessName, _options.UseReSharperColors ? HighlightingAttributeIds.TYPE_CLASS_ATTRIBUTE : VsHighlightingAttributeIds.Classes);
            AppendText("] ", null);
        }
        public static string GetSubjectString(this IAttributesOwner type)
        {
            var attribute = GetSubjectAttribute(type);

            if (attribute == null)
            {
                var containingType = type.GetContainingType();
                if (containingType == null)
                {
                    return(null);
                }

                return(containingType.GetSubjectString());
            }

            if (attribute.PositionParameters().Any(x => x.IsBadValue))
            {
                return(null);
            }

            if (attribute.PositionParameters()
                .Where(x => x.IsType)
                .Select(x => x.TypeValue as IDeclaredType)
                .Any(x => x.IsInvalid()))
            {
                return(null);
            }

            try
            {
                var parameters = attribute.PositionParameters()
                                 .Select(x =>
                {
                    if (x.IsType)
                    {
                        var declaredType = (IDeclaredType)x.TypeValue;
                        return(declaredType.GetClrName().ShortName);
                    }

                    return((string)x.ConstantValue.Value);
                })
                                 .ToArray();

                return(String.Join(" ", parameters));
            }
            catch (Exception)
            {
                return(null);
            }
        }
Beispiel #9
0
        private string GetNullnessName([NotNull] IAttributesOwner attributesOwner)
        {
            CodeAnnotationNullableValue?nullableValue = _codeAnnotationsCache.GetNullableAttribute(attributesOwner);

            switch (nullableValue)
            {
            case CodeAnnotationNullableValue.NOT_NULL:
                return(_notNullAttributeName);

            case CodeAnnotationNullableValue.CAN_BE_NULL:
                return(_canBeNullAttributeName);

            default:
                return(null);
            }
        }
        public static ICollection <string> GetTags(this IAttributesOwner type)
        {
            return(type.GetAttributeInstances(new CLRTypeName(new TagsAttributeFullName()), true)
                   .SelectMany(x => x.PositionParameters())
                   .SelectMany(x =>
            {
                if (x.IsArray)
                {
                    return x.ArrayValue.Select(av => av.ConstantValue);
                }

                return new[] { x.ConstantValue };
            })
                   .Select(v => v.Value.ToString())
                   .Distinct()
                   .ToList());
        }
Beispiel #11
0
        public static bool IsIgnored(this IDeclaredElement element)
        {
            IAttributesOwner attributeOwner = element as IAttributesOwner;

            if (attributeOwner == null)
            {
                return(false);
            }

            IInitializerOwnerDeclaration initializer = element as IInitializerOwnerDeclaration;
            bool hasInitializer = true;

            if (initializer != null)
            {
                hasInitializer = initializer.Initializer != null;
            }

            return(!hasInitializer || attributeOwner.HasAttributeInstance(new CLRTypeName(typeof(IgnoreAttribute).FullName), false));
        }
Beispiel #12
0
        public static ICollection <JetTuple <MvcKind, string, IAttributeInstance> > GetMvcKinds(
            [NotNull] this IAttributesOwner element)
        {
            CodeAnnotationsCache codeAnnotations = element.GetPsiServices().GetCodeAnnotationsCache();

            return(element
                   .GetAttributeInstances(false)
                   .SelectMany(attr =>
                               MvcKinds.Where(pair => codeAnnotations.IsAnnotationAttribute(attr, pair.Key))
                               .Select(pair => JetTuple.Of
                                       (
                                           pair.Value,
                                           MvcKindAnonymousPropertyInitializers.ContainsKey(pair.Value)
                                        ? MvcKindAnonymousPropertyInitializers[pair.Value](attr)
                                        : null,
                                           attr
                                       )))
                   .ToList());
        }
 public static bool HasFrequentlyCalledMethodAttribute(IAttributesOwner attributesOwner)
 {
     return(HasSpecificAttribute(attributesOwner, "FrequentlyCalledMethodAttribute"));
 }
 public static bool HasPerformanceSensitiveAttribute(IAttributesOwner attributesOwner)
 {
     return(HasSpecificAttribute(attributesOwner, "PerformanceCharacteristicsHintAttribute"));
 }
 public static bool HasGeneratedCodeAttribute(this IAttributesOwner attributesOwner)
 {
     return(attributesOwner.HasAttributeInstance(PredefinedType.CODEDOM_GENERATED_CODE_ATTRIBUTE_CLASS, inherit: false));
 }
 private bool IsItemNotNull([CanBeNull] IAttributesOwner attributesOwner) =>
 _containerElementNullnessProvider.GetInfo(attributesOwner) == CodeAnnotationNullableValue.NOT_NULL;
        private IEnumerable<StaticAttributeWrapper> GetAttributesForAttributeOwner(IAttributesOwner owner)
        {
            if (!owner.IsValid())
                yield break;

            foreach (IAttributeInstance attribute in owner.GetAttributeInstances(false))
            {
                if (IsAttributeInstanceValid(attribute))
                    yield return new StaticAttributeWrapper(this, attribute);
            }
        }
 public static bool HasSpecificAttribute(IAttributesOwner attributesOwner, string name)
 {
     return(attributesOwner.GetAttributeInstances(true)
            .Any(t => t.GetClrName().ShortName.Equals(name)));
 }