Ejemplo n.º 1
0
        public static void CreateAndAddAnnotationAttribute([NotNull] IAttributesOwnerDeclaration ownerDeclaration, [NotNull] string newAttributeShortName)
        {
            var codeAnnotationConfig = ownerDeclaration.GetPsiServices().GetComponent <CodeAnnotationsConfiguration>();

            var pureAttributeType = codeAnnotationConfig.GetAttributeTypeForElement(ownerDeclaration, newAttributeShortName);

            if (pureAttributeType == null)
            {
                return;
            }

            var elementFactory = CSharpElementFactory.GetInstance(ownerDeclaration);
            var attribute      = elementFactory.CreateAttribute(pureAttributeType);

            ownerDeclaration.AddAttributeAfter(attribute, null);
        }
Ejemplo n.º 2
0
        /// <summary>Sets the attribute.</summary>
        /// <param name="owner">The owner.</param>
        /// <param name="attributeShortName">Short name of the attribute.</param>
        public static void SetAttribute([NotNull] IAttributesOwnerDeclaration owner, [NotNull] string attributeShortName)
        {
            if (owner == null)
            {
                throw new ArgumentNullException("owner");
            }

            if (!owner.IsValid())
            {
                return;
            }

            if (attributeShortName == null)
            {
                throw new ArgumentNullException("attributeShortName");
            }

            var psiServices = owner.GetPsiServices();

            if (psiServices == null)
            {
                throw new InvalidOperationException("psiServices");
            }

            var cache = psiServices.GetCodeAnnotationsCache();

            if (cache == null)
            {
                return;
            }

            var attributeTypeElement = cache.GetAttributeTypeForElement(owner, attributeShortName);

            if (attributeTypeElement == null)
            {
                return;
            }

            var factory = CSharpElementFactory.GetInstance(owner.GetPsiModule());

            var attribute = factory.CreateAttribute(attributeTypeElement);

            owner.AddAttributeAfter(attribute, null);
        }
        protected sealed override Func <CSharpElementFactory, IAttribute> CreateAttributeFactoryIfAvailable(
            IAttributesOwnerDeclaration attributesOwnerDeclaration,
            IPsiModule psiModule,
            out IAttribute attributeToRemove)
        {
            var attributeType = attributesOwnerDeclaration.GetPsiServices()
                                .GetComponent <CodeAnnotationsConfiguration>()
                                .GetAttributeTypeForElement(attributesOwnerDeclaration, AnnotationAttributeTypeName);

            if (attributeType != null && CanBeAnnotated(attributesOwnerDeclaration.DeclaredElement, attributesOwnerDeclaration, psiModule))
            {
                attributeToRemove = TryGetAttributeToReplace(attributesOwnerDeclaration);

                return(factory =>
                {
                    Debug.Assert(factory != null);

                    return factory.CreateAttribute(attributeType);
                });
            }

            attributeToRemove = null;
            return(null);
        }
        protected sealed override Func<CSharpElementFactory, IAttribute> CreateAttributeFactoryIfAvailable(
            IAttributesOwnerDeclaration attributesOwnerDeclaration,
            IPsiModule module,
            out IAttribute attributeToRemove)
        {
            var attributeType =
                attributesOwnerDeclaration.GetPsiServices()
                    .GetComponent<CodeAnnotationsConfiguration>()
                    .GetAttributeTypeForElement(attributesOwnerDeclaration, AnnotationAttributeTypeName);
            if (attributeType != null && CanBeAnnotated(attributesOwnerDeclaration.DeclaredElement, attributesOwnerDeclaration, module))
            {
                attributeToRemove = TryGetAttributeToReplace(attributesOwnerDeclaration);

                return factory =>
                {
                    Debug.Assert(factory != null);

                    return factory.CreateAttribute(attributeType);
                };
            }

            attributeToRemove = null;
            return null;
        }