Exemplo n.º 1
0
        private static IDeclaredElement TryFindDeclaration([NotNull] IInvocationExpression expression, [NotNull] string propertyName,
                                                           [NotNull] string requiredTypeName, [NotNull] string requiredMethodName, out string name)
        {
            name = null;
            var baseClass = expression.GetContainingNode <IClassLikeDeclaration>();

            if (baseClass == null)
            {
                return(null);
            }
            var result = TryFindIdDeclarationInClassLikeDeclaration(baseClass, propertyName, requiredTypeName, requiredMethodName, out name);

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

            var next = ClassLikeDeclarationNavigator.GetByNestedTypeDeclaration(baseClass);

            while (next != null)
            {
                baseClass = next;
                result    = TryFindIdDeclarationInClassLikeDeclaration(baseClass, propertyName, requiredTypeName, requiredMethodName, out name);
                if (result != null)
                {
                    return(result);
                }

                next = ClassLikeDeclarationNavigator.GetByNestedTypeDeclaration(baseClass);
            }

            return(null);
        }
        public bool IsAvailable(IUserDataHolder cache)
        {
            var identifier = myDataProvider.GetSelectedElement <ICSharpIdentifier>();

            var classLikeDeclaration = ClassLikeDeclarationNavigator.GetByNameIdentifier(identifier);

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

            ITypeElement declaredElement = classLikeDeclaration.DeclaredElement;

            if (declaredElement.DerivesFrom(KnownTypes.EditorWindow))
            {
                return(false);
            }

            if (declaredElement.DerivesFrom(KnownTypes.Editor))
            {
                return(false);
            }

            var existingAttribute = classLikeDeclaration.GetAttribute(KnownTypes.CreateAssetMenuAttribute);

            return(existingAttribute == null && declaredElement.DerivesFromScriptableObject());
        }
        public bool IsAvailable(IUserDataHolder cache)
        {
            var identifier = myDataProvider.GetSelectedElement <ICSharpIdentifier>();

            var classLikeDeclaration = ClassLikeDeclarationNavigator.GetByNameIdentifier(identifier);

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

            var existingAttribute = classLikeDeclaration.GetAttribute(KnownTypes.CreateAssetMenu);

            return(existingAttribute == null && UnityApi.IsDescendantOfScriptableObject(classLikeDeclaration.DeclaredElement));
        }
        protected override void Analyze(IAttribute element, ElementProblemAnalyzerData data, IHighlightingConsumer consumer)
        {
            var attributeTypeElement = element.TypeReference?.Resolve().DeclaredElement as ITypeElement;

            if (attributeTypeElement == null)
            {
                return;
            }

            if (Equals(attributeTypeElement.GetClrName(), KnownTypes.InitializeOnLoadAttribute))
            {
                var classLikeDeclaration = ClassLikeDeclarationNavigator.GetByAttribute(element);
                if (classLikeDeclaration != null && classLikeDeclaration.ConstructorDeclarations.All(c => !c.IsStatic))
                {
                    // Unity doesn't report a warning if there isn't a static constructor, so just highlight
                    // the attribute as dead code.
                    consumer.AddHighlighting(new RedundantInitializeOnLoadAttributeWarning(element));
                }
            }
            else if (Equals(attributeTypeElement.GetClrName(), KnownTypes.InitializeOnLoadMethodAttribute) ||
                     Equals(attributeTypeElement.GetClrName(), KnownTypes.RuntimeInitializeOnLoadMethodAttribute))
            {
                var methodDeclaration = MethodDeclarationNavigator.GetByAttribute(element);
                if (methodDeclaration == null)
                {
                    return;
                }

                var predefinedType  = myPredefinedTypeCache.GetOrCreatePredefinedType(element.GetPsiModule());
                var methodSignature = new MethodSignature(predefinedType.Void, true);

                if (!methodDeclaration.IsStatic)
                {
                    consumer.AddHighlighting(new InvalidStaticModifierWarning(methodDeclaration, methodSignature));
                }
                if (!methodDeclaration.Type.IsVoid())
                {
                    consumer.AddHighlighting(new InvalidReturnTypeWarning(methodDeclaration, methodSignature));
                }
                if (methodDeclaration.ParameterDeclarationsEnumerable.Any())
                {
                    consumer.AddHighlighting(new InvalidSignatureWarning(methodDeclaration, methodSignature));
                }
            }
        }
Exemplo n.º 5
0
        protected override void Analyze(IAttribute element, ElementProblemAnalyzerData data, IHighlightingConsumer consumer)
        {
            if (!(element.TypeReference?.Resolve().DeclaredElement is ITypeElement attributeTypeElement))
            {
                return;
            }

            if (Equals(attributeTypeElement.GetClrName(), KnownTypes.InitializeOnLoadAttribute))
            {
                var classLikeDeclaration = ClassLikeDeclarationNavigator.GetByAttribute(element);
                if (classLikeDeclaration != null && classLikeDeclaration.ConstructorDeclarations.All(c => !c.IsStatic))
                {
                    // Unity doesn't report a warning if there isn't a static constructor, so just highlight
                    // the attribute as dead code.
                    consumer.AddHighlighting(new RedundantInitializeOnLoadAttributeWarning(element));
                }
            }
        }
Exemplo n.º 6
0
        private static IClassLikeDeclaration GetTopLevelClassLikeDeclaration([NotNull] IInvocationExpression expression)
        {
            var baseClass = expression.GetContainingNode <IClassLikeDeclaration>();

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

            var next = ClassLikeDeclarationNavigator.GetByNestedTypeDeclaration(baseClass);

            while (next != null)
            {
                baseClass = next;
                next      = ClassLikeDeclarationNavigator.GetByNestedTypeDeclaration(baseClass);
            }

            return(baseClass);
        }
Exemplo n.º 7
0
 public override bool IsAvailable(IUserDataHolder cache)
 {
     this.Declaration = ClassLikeDeclarationNavigator.GetByNameIdentifier(this._provider.GetSelectedElement <ICSharpIdentifier>());
     return(this.Declaration != null);
 }
 public CreateStaticConstructorFromUsageAction(IAttribute attribute)
     : base(reference: null)
 {
     myClassLikeDeclaration = ClassLikeDeclarationNavigator.GetByAttribute(attribute);
 }