protected override void Process()

in resharper/resharper-unity/src/Unity/CSharp/Feature/Services/Generate/Dots/GenerateRefFieldsAccessorsBuilder.cs [48:131]


        protected override void Process(CSharpGeneratorContext context, IProgressIndicator progress)
        {
            var selectedClassDeclaration = context.ClassDeclaration;
            var node = context.Anchor;
            var classLikeDeclaration = node?.GetContainingNode<IClassLikeDeclaration>();
            if (classLikeDeclaration == null)
                return;

            var fieldDeclaration = node?.GetContainingNode<IFieldDeclaration>();
            if (fieldDeclaration == null)
                return;

            var (referencedType, isReadOnly) = UnityApiExtensions.GetReferencedType(fieldDeclaration);
            if (referencedType == null)
                return;

            var shouldGenerateSetters = context.GetBooleanOption(GenerateSetters);
            var fieldName = fieldDeclaration.DeclaredElement;
            var factory = CSharpElementFactory.GetInstance(selectedClassDeclaration);

            var selectedGeneratorElements = context.InputElements.OfType<GeneratorDeclaredElement>();

            var isDerivesFromIAspect = referencedType.DerivesFrom(KnownTypes.IAspect);
            var getterFormat = isDerivesFromIAspect ? AspectGetterFormat : RefGetterFormat;
            var setterFormat = isDerivesFromIAspect ? AspectSetterFormat : RefSetterFormat;

            foreach (var generatorElement in selectedGeneratorElements)
            {
                var declaredElement = generatorElement.DeclaredElement;

                var fieldOrProperty = declaredElement as IField ?? (ITypeOwner?)(declaredElement as IProperty);

                if (fieldOrProperty == null)
                    continue;

                var fieldShortName = fieldOrProperty.ShortName;
                var generatedPropertyName = BakerGeneratorUtils.CalculateValueFieldName(fieldShortName, fieldOrProperty.GetContainingType()?.ShortName);
                var uniquePropertyName = NamingUtil.GetUniqueName(classLikeDeclaration.Body, generatedPropertyName, NamedElementKinds.Property, null,
                    element =>
                    {
                        //Skip other struct names and classes
                        return element is not ITypeElement;
                    });

                var propertyDeclaration = factory.CreatePropertyDeclaration(fieldOrProperty.Type, uniquePropertyName);
                propertyDeclaration.SetAccessRights(AccessRights.PUBLIC);
                
                var getterExpression = factory.CreateExpression(getterFormat, fieldName, fieldShortName);
                var generateGettersOnly = isReadOnly || !shouldGenerateSetters;
                
                if (isDerivesFromIAspect)
                {
                    switch (fieldOrProperty)
                    {
                        case IField field:
                            generateGettersOnly |= field.IsReadonly; 
                            break;
                        case IProperty property:
                            generateGettersOnly |= property.IsReadonly || property.Setter == null;
                            break;
                    }
                }

                if (generateGettersOnly)
                {
                    propertyDeclaration.SetBodyExpression(getterExpression);
                }
                else
                {
                    var getterBodyExpression = factory.CreateAccessorDeclaration(AccessorKind.GETTER, false);
                    getterBodyExpression.SetBodyExpression(getterExpression);
                    propertyDeclaration.AddAccessorDeclarationBefore(getterBodyExpression, null);
                    var setterBodyExpression = factory.CreateAccessorDeclaration(AccessorKind.SETTER, false);
                    var setterExpression = factory.CreateExpression(setterFormat, fieldName, fieldShortName);
                    setterBodyExpression.SetBodyExpression(setterExpression);
                    propertyDeclaration.AddAccessorDeclarationBefore(setterBodyExpression, null);
                }

                using (WriteLockCookie.Create())
                {
                    classLikeDeclaration.AddClassMemberDeclarationAfter(propertyDeclaration, fieldDeclaration);
                }
            }
        }