示例#1
0
        public bool IsAvailable(IUserDataHolder cache)
        {
            if (!myDataProvider.Project.IsUnityProject())
            {
                return(false);
            }

            var unityApi         = myDataProvider.Solution.GetComponent <UnityApi>();
            var fieldDeclaration = myDataProvider.GetSelectedElement <IFieldDeclaration>();

            if (fieldDeclaration == null || !unityApi.IsSerialisedField(fieldDeclaration.DeclaredElement))
            {
                return(false);
            }

            var existingAttribute = AttributeUtil.GetAttribute(fieldDeclaration, AttributeTypeName);

            if (existingAttribute != null && !IsRemoveActionAvailable)
            {
                return(false);
            }

            // Only for UnityObject types, not [Serialized] types
            var classDeclaration = fieldDeclaration.GetContainingTypeDeclaration();
            var classElement     = classDeclaration?.DeclaredElement;

            return(classElement.DerivesFromMonoBehaviour() || classElement.DerivesFromScriptableObject());
        }
示例#2
0
        public IEnumerable <IntentionAction> CreateBulbItems()
        {
            var fieldDeclaration         = myDataProvider.GetSelectedElement <IFieldDeclaration>();
            var multipleFieldDeclaration = MultipleFieldDeclarationNavigator.GetByDeclarator(fieldDeclaration);
            var unityApi = myDataProvider.Solution.GetComponent <UnityApi>();

            if (!unityApi.IsSerialisedField(fieldDeclaration?.DeclaredElement) || multipleFieldDeclaration == null)
            {
                return(EmptyList <IntentionAction> .Enumerable);
            }

            var existingAttribute = AttributeUtil.GetAttribute(fieldDeclaration, KnownTypes.HideInInspector);

            if (multipleFieldDeclaration.Declarators.Count == 1)
            {
                return(new ToggleHideInInspectorAll(multipleFieldDeclaration, myDataProvider.PsiModule,
                                                    myDataProvider.ElementFactory, existingAttribute).ToContextActionIntentions());
            }

            return(new[]
            {
                new ToggleHideInInspectorOne(fieldDeclaration, myDataProvider.PsiModule, myDataProvider.ElementFactory,
                                             existingAttribute).ToContextActionIntention(ourSubmenuAnchor),
                new ToggleHideInInspectorAll(multipleFieldDeclaration, myDataProvider.PsiModule,
                                             myDataProvider.ElementFactory, existingAttribute).ToContextActionIntention(ourSubmenuAnchor)
            });
        }
示例#3
0
        public IEnumerable <IntentionAction> CreateBulbItems()
        {
            var selectedFieldDeclaration = myDataProvider.GetSelectedElement <IFieldDeclaration>();
            var multipleFieldDeclaration = MultipleFieldDeclarationNavigator.GetByDeclarator(selectedFieldDeclaration);
            var unityApi = myDataProvider.Solution.GetComponent <UnityApi>();

            if (!unityApi.IsSerialisedField(selectedFieldDeclaration?.DeclaredElement) || multipleFieldDeclaration == null)
            {
                return(EmptyList <IntentionAction> .Enumerable);
            }

            var existingAttribute = AttributeUtil.GetAttribute(selectedFieldDeclaration, AttributeTypeName);

            var actionToApplyToEntireDeclaration = GetActionToApplyToEntireFieldDeclaration(multipleFieldDeclaration,
                                                                                            selectedFieldDeclaration, myDataProvider.PsiModule, myDataProvider.ElementFactory,
                                                                                            existingAttribute)
                                                   .ToContextActionIntention(myAnchor);

            // If we only have a single field in the declaration, then use the default action ("Add 'Attr'")
            // This is the most likely case
            if (multipleFieldDeclaration.Declarators.Count == 1)
            {
                return new[] { actionToApplyToEntireDeclaration }
            }
            ;

            var actionToExtractAndApply = GetActionToExtractAndApplyToSingleField(multipleFieldDeclaration,
                                                                                  selectedFieldDeclaration, myDataProvider.PsiModule, myDataProvider.ElementFactory,
                                                                                  existingAttribute)
                                          .ToContextActionIntention(myAnchor);

            // Only makes sense to apply to a single attribute, not all. E.g. you can't apply Range to all the fields
            // in a multiple
            if (SupportsSingleDeclarationOnly)
            {
                return new[] { actionToExtractAndApply }
            }
            ;

            // Change the order of main menu and submenu. If it's a layout attribute (e.g. 'Space'):
            // "Add 'Attr' before all fields" -> "Add 'Attr' before 'field'"
            // If it's an annotation attribute (e.g. 'Tooltip'):
            // "Add 'Attr' to 'field'" -> "Add 'Attr' to all fields"
            return(IsLayoutAttribute
                ? new[] { actionToApplyToEntireDeclaration, actionToExtractAndApply }
                : new[] { actionToExtractAndApply, actionToApplyToEntireDeclaration });
        }