コード例 #1
0
            protected override Action <ITextControl> ExecutePsiTransaction(ISolution solution, IProgressIndicator progress)
            {
                if (myIsSerialized)
                {
                    AttributeUtil.RemoveAttributeFromAllDeclarations(myFieldDeclaration, KnownTypes.SerializeField);
                    if (myFieldDeclaration.GetAccessRights() == AccessRights.PUBLIC)
                    {
                        AttributeUtil.AddAttributeToEntireDeclaration(myMultipleFieldDeclaration,
                                                                      PredefinedType.NONSERIALIZED_ATTRIBUTE_CLASS, EmptyArray <AttributeValue> .Instance, myModule,
                                                                      myElementFactory);
                    }
                }
                else
                {
                    if (myFieldDeclaration.IsStatic)
                    {
                        ModifiersUtil.SetStatic(myMultipleFieldDeclaration, false);
                    }
                    if (myFieldDeclaration.IsReadonly)
                    {
                        ModifiersUtil.SetReadonly(myMultipleFieldDeclaration, false);
                    }

                    AttributeUtil.RemoveAttributeFromAllDeclarations(myFieldDeclaration,
                                                                     PredefinedType.NONSERIALIZED_ATTRIBUTE_CLASS);
                    if (myFieldDeclaration.GetAccessRights() != AccessRights.PUBLIC)
                    {
                        AttributeUtil.AddAttributeToEntireDeclaration(myMultipleFieldDeclaration,
                                                                      KnownTypes.SerializeField, EmptyArray <AttributeValue> .Instance, myModule, myElementFactory);
                    }
                }

                return(null);
            }
コード例 #2
0
            protected override Action <ITextControl> ExecutePsiTransaction(ISolution solution, IProgressIndicator progress)
            {
                if (myIsSerialized)
                {
                    AttributeUtil.RemoveAttributeFromSingleDeclaration(myFieldDeclaration, KnownTypes.SerializeField);
                    if (myFieldDeclaration.GetAccessRights() == AccessRights.PUBLIC)
                    {
                        AttributeUtil.AddAttributeToSingleDeclaration(myFieldDeclaration,
                                                                      PredefinedType.NONSERIALIZED_ATTRIBUTE_CLASS, myModule, myElementFactory);
                    }
                }
                else
                {
                    if (myFieldDeclaration.IsStatic)
                    {
                        myFieldDeclaration.SetStatic(false);
                    }

                    if (myFieldDeclaration.IsReadonly)
                    {
                        myFieldDeclaration.SetReadonly(false);
                    }

                    AttributeUtil.RemoveAttributeFromSingleDeclaration(myFieldDeclaration,
                                                                       PredefinedType.NONSERIALIZED_ATTRIBUTE_CLASS);
                    if (myFieldDeclaration.GetAccessRights() != AccessRights.PUBLIC)
                    {
                        AttributeUtil.AddAttributeToSingleDeclaration(myFieldDeclaration, KnownTypes.SerializeField,
                                                                      myModule, myElementFactory);
                    }
                }

                return(null);
            }
コード例 #3
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());
        }
コード例 #4
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)
            });
        }
        public static Action <ITextControl> Execute([CanBeNull] IPropertyDeclaration propertyDeclaration, ISolution solution, CSharpElementFactory elementFactory)
        {
            if (propertyDeclaration == null)
            {
                return(null);
            }

            var fieldDeclaration = AutomaticToBackingFieldAction.Execute(propertyDeclaration);

            AttributeUtil.AddAttributeToSingleDeclaration(fieldDeclaration, KnownTypes.SerializeField, propertyDeclaration.GetPsiModule(), elementFactory);
            return(AutomaticToBackingFieldAction.PostExecute(propertyDeclaration, fieldDeclaration, solution));
        }
コード例 #6
0
            protected override Action <ITextControl> ExecutePsiTransaction(ISolution solution, IProgressIndicator progress)
            {
                var values = new[]
                {
                    new Pair <string, AttributeValue>("menuName", new AttributeValue(new ConstantValue($"Create {myClassLikeDeclaration.DeclaredName}", myModule))),
                    new Pair <string, AttributeValue>("fileName", new AttributeValue(new ConstantValue(myClassLikeDeclaration.DeclaredName, myModule))),
                    new Pair <string, AttributeValue>("order", new AttributeValue(new ConstantValue(0, myModule))),
                };

                var attribute = AttributeUtil.AddAttributeToSingleDeclaration(myClassLikeDeclaration, KnownTypes.CreateAssetMenuAttribute, EmptyArray <AttributeValue> .Instance,
                                                                              values, myModule, myElementFactory);

                return(attribute.CreateHotspotSession());
            }
コード例 #7
0
            protected override Action <ITextControl> ExecutePsiTransaction(ISolution solution,
                                                                           IProgressIndicator progress)
            {
                if (myExistingAttribute != null)
                {
                    myFieldDeclaration.RemoveAttribute(myExistingAttribute);
                }
                else
                {
                    AttributeUtil.AddAttributeToSingleDeclaration(myFieldDeclaration, KnownTypes.HideInInspector,
                                                                  myPsiModule, myElementFactory);
                }

                return(null);
            }
コード例 #8
0
            protected override Action <ITextControl> ExecutePsiTransaction(ISolution solution, IProgressIndicator progress)
            {
                var fieldDeclaration = (IFieldDeclaration)myMultipleFieldDeclaration.Declarators[0];

                if (myExistingAttribute != null)
                {
                    CSharpSharedImplUtil.RemoveAttribute(fieldDeclaration, myExistingAttribute);
                }
                else
                {
                    AttributeUtil.AddAttributeToAllDeclarations(fieldDeclaration, KnownTypes.HideInInspector,
                                                                myModule, myElementFactory);
                }

                return(null);
            }
コード例 #9
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 });
        }
コード例 #10
0
        private bool HasRequireComponentWithSameType(IType type, IClassDeclaration classDeclaration)
        {
            var existingAttributes = AttributeUtil.GetAttributes(classDeclaration, KnownTypes.RequireComponent);

            foreach (var attribute in existingAttributes)
            {
                var argument = attribute.Arguments.FirstOrDefault();

                if (argument?.Value is ITypeofExpression typeofExpression)
                {
                    if (typeofExpression.ArgumentType.Equals(type))
                    {
                        return(true);
                    }
                }
            }

            return(false);
        }
コード例 #11
0
            protected override Action <ITextControl> ExecutePsiTransaction(
                ISolution solution, IProgressIndicator progress)
            {
                IAttribute attribute;

                if (mySelectedFieldDeclaration == null)
                {
                    attribute = AttributeUtil.AddAttributeToEntireDeclaration(myMultipleFieldDeclaration,
                                                                              myAttributeTypeName, myAttributeValues, null, myModule, myElementFactory);
                }
                else
                {
                    attribute = AttributeUtil.AddAttributeToSingleDeclaration(mySelectedFieldDeclaration,
                                                                              myAttributeTypeName, myAttributeValues, null, myModule, myElementFactory);
                }

                if (myAttributeValues.Length == 0)
                {
                    return(null);
                }

                return(attribute.CreateHotspotSession());
            }
コード例 #12
0
 protected override Action <ITextControl> ExecutePsiTransaction(ISolution solution, IProgressIndicator progress)
 {
     AttributeUtil.AddAttributeToSingleDeclaration(myClassDeclaration, KnownTypes.RequireComponent,
                                                   new[] { new AttributeValue(myType) }, myClassDeclaration.GetPsiModule(), myFactory, true);
     return(null);
 }