Example #1
0
        internal static string GetPropertyName(AttributeFieldSource source)
        {
            var fieldName = source.FieldName;
            var propName  = (fieldName[0] == '_') ? fieldName.Substring(1) : fieldName;

            return(propName.ToUpperOnlyFirst());
        }
        public void Execute(GeneratorExecutionContext context)
        {
            var attrCode = new MetaTagPropertyGeneratorAttributeTemplate().TransformText();

            context.AddSource(AttributeName + ".cs", attrCode);

            try
            {
                if (context.SyntaxReceiver is not SyntaxReceiver receiver)
                {
                    return;
                }

                foreach (var classDeclaration in receiver.Targets)
                {
                    var model      = context.Compilation.GetSemanticModel(classDeclaration.SyntaxTree);
                    var typeSymbol = model.GetDeclaredSymbol(classDeclaration);
                    if (typeSymbol is null)
                    {
                        continue;
                    }

                    var candidateFields = classDeclaration.Members.OfType <FieldDeclarationSyntax>()
                                          .Select(field => (field, model))
                                          .SelectMany(x => x.field.Declaration.Variables.Select(v => (x.field, symbol: x.model.GetDeclaredSymbol(v) as IFieldSymbol)))
                                          .Where(x => x.symbol?.GetAttributes().Any(y => y.AttributeClass?.Name is nameof(MetaTagPropertyGenerator) or AttributeName) ?? false)
                                          .ToArray();

                    var fieldSource = candidateFields.Select(x => AttributeFieldSource.Create(model, x.field)).ToArray();
                    if (fieldSource is null)
                    {
                        continue;
                    }

                    var template = new CodeTemplate(classDeclaration, fieldSource !)
                    {
                        Namespace = typeSymbol.ContainingNamespace.ToDisplayString(),
                    };

                    var text = template.TransformText();
                    context.AddSource(typeSymbol.GenerateHintName(), text);
                }
            }
            catch (Exception ex)
            {
                System.Diagnostics.Trace.WriteLine(ex.ToString());
            }
        }
Example #3
0
 internal static bool IsBuiltInType(AttributeFieldSource source) => source.FieldType == AttributeFieldSource.FieldDeclarationType.BuiltIn;
Example #4
0
 internal static string GetOptionId(AttributeFieldSource source) => "0x" + source.Id.ToString("x4");
Example #5
0
 internal static string GetOptionKey(AttributeFieldSource source) => source.Key;
Example #6
0
 internal static string GetMethodName(AttributeFieldSource source)
 => "GetTagValue_" + (source.FieldType == AttributeFieldSource.FieldDeclarationType.BuiltIn ? source.TypeName : "int");
Example #7
0
 internal static string GetLoadedFlagName(AttributeFieldSource source) => source.FieldName + "_loaded";
Example #8
0
 internal static string GetBackingFieldName(AttributeFieldSource source) => source.FieldName;
Example #9
0
 internal static string GetFieldTypeShortName(AttributeFieldSource source) => source.TypeName.Split('.').Last();
Example #10
0
 internal static string GetFieldTypeFullName(AttributeFieldSource source) => source.TypeName;