Esempio n. 1
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));
        }