private static List <(AttributeInfo Attribute, List <FieldTypeAttributeInfo> Infos)> GetFieldTypeAttributesInfos(ImmutableArray <AttributeInfo> attributes,
                                                                                                                         FieldTypeAttributesRegister fieldAttributesRegister,
                                                                                                                         CancellationToken cancellationToken)
        {
            cancellationToken.ThrowIfCancellationRequested();
            var fieldInfosList = new List <(AttributeInfo, List <FieldTypeAttributeInfo>)>(capacity: attributes.Length);

            foreach (AttributeInfo attribute in attributes)
            {
                cancellationToken.ThrowIfCancellationRequested();
                var attributeInfos = fieldAttributesRegister.GetFieldTypeAttributeInfos(attribute.AttributeData.AttributeClass).ToList();

                if (attributeInfos.Count > 0)
                {
                    fieldInfosList.Add((attribute, attributeInfos));
                }
            }

            return(fieldInfosList);
        }
            public bool IsAttributeToRemove(AttributeSyntax attributeNode)
            {
                if (attributeNode.Equals(_remainingAttribute))
                {
                    return(false);
                }

                ITypeSymbol attributeType = _semanticModel.GetTypeInfo(attributeNode, _cancellationToken).Type;

                _cancellationToken.ThrowIfCancellationRequested();

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

                var attributeInfos = _attributesRegister.GetFieldTypeAttributeInfos(attributeType);

                return(attributeInfos.Any(_attributeToRemovePredicate));
            }
Beispiel #3
0
        private async Task <Document> ChangePropertyTypeToAttributeType(Document document, SyntaxNode root, AttributeSyntax attributeNode,
                                                                        PropertyDeclarationSyntax propertyNode, CancellationToken cancellationToken)
        {
            SemanticModel semanticModel = await document.GetSemanticModelAsync(cancellationToken).ConfigureAwait(false);

            ITypeSymbol attributeType = semanticModel.GetTypeInfo(attributeNode, cancellationToken).Type;

            cancellationToken.ThrowIfCancellationRequested();

            if (attributeType == null)
            {
                return(document);
            }

            PXContext pxContext = new PXContext(semanticModel.Compilation);
            FieldTypeAttributesRegister typeAttributesRegister = new FieldTypeAttributesRegister(pxContext);
            FieldTypeAttributeInfo?     attributeInfo          = typeAttributesRegister.GetFieldTypeAttributeInfos(attributeType)
                                                                 .FirstOrDefault(attrInfo => attrInfo.IsFieldAttribute);

            if (attributeInfo?.FieldType == null)
            {
                return(document);
            }

            SyntaxGenerator generator         = SyntaxGenerator.GetGenerator(document);
            TypeSyntax      replacingTypeNode = generator.TypeExpression(attributeInfo.Value.FieldType) as TypeSyntax;

            if (attributeInfo.Value.FieldType.IsValueType)
            {
                replacingTypeNode = generator.NullableTypeExpression(replacingTypeNode) as TypeSyntax;
            }

            cancellationToken.ThrowIfCancellationRequested();

            replacingTypeNode = replacingTypeNode.WithTrailingTrivia(propertyNode.Type.GetTrailingTrivia());
            var propertyModified = propertyNode.WithType(replacingTypeNode);
            var modifiedRoot     = root.ReplaceNode(propertyNode, propertyModified);

            return(document.WithSyntaxRoot(modifiedRoot));
        }