Skip to content

Commit

Permalink
Add GenConstructorIgnore attribute
Browse files Browse the repository at this point in the history
  • Loading branch information
OctopBP committed Oct 18, 2023
1 parent 21ebc1a commit 9b2c0f3
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 7 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
using UnityAttributes.Common;

namespace UnityAttributes.GenConstructor;

public partial class GenConstructorGenerator {
public const string IgnoreAttributeName = "GenConstructorIgnoreAttribute";
readonly string ignoreAttributeText = @$"{Const.AutoGeneratedText}
{generatedCodeAttribute}
[global::System.AttributeUsage(global::System.AttributeTargets.Field, Inherited = true, AllowMultiple = false)]
internal sealed class {IgnoreAttributeName} : global::System.Attribute
{{
public {IgnoreAttributeName}() {{ }}
}}
";
}
22 changes: 16 additions & 6 deletions UnityAttributes/GenConstructor/GenConstructorGenerator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,10 @@ public partial class GenConstructorGenerator : ISourceGenerator

public void Initialize(GeneratorInitializationContext context)
{
context.RegisterForPostInitialization(i => i.AddSource($"{AttributeName}.g.cs", attributeText));
context.RegisterForPostInitialization(i => {
i.AddSource($"{AttributeName}.g.cs", attributeText);
i.AddSource($"{IgnoreAttributeName}.g.cs", ignoreAttributeText);
});
context.RegisterForSyntaxNotifications(() => new SyntaxReceiver());
}

Expand Down Expand Up @@ -113,11 +116,9 @@ public void OnVisitSyntaxNode(GeneratorSyntaxContext context)
);

var haveAttribute =
classSymbol != null
&& classSymbol.GetAttributes().Any(ad =>
ad.AttributeClass != null
&& ad.AttributeClass.ToDisplayString() == GenConstructorGenerator.AttributeName
);
classSymbol?.GetAttributes().Any(ad =>
ad.AttributeClass?.ToDisplayString() == GenConstructorGenerator.AttributeName
) ?? false;

if (haveAttribute)
{
Expand All @@ -126,9 +127,18 @@ public void OnVisitSyntaxNode(GeneratorSyntaxContext context)
.OfType<FieldDeclarationSyntax>()
.SelectMany(field => field.Declaration.Variables)
.Select(variable => Unsafe.As<IFieldSymbol>(context.SemanticModel.GetDeclaredSymbol(variable)))
.Where(checkForIgnoreAttribute)
.ToArray();

classes.Add((classSymbol, fields));
}

return;

bool checkForIgnoreAttribute(IFieldSymbol fieldSymbol) {
var haveIgnoreAttribute = fieldSymbol?.GetAttributes()
.Any(ad => ad.AttributeClass?.ToDisplayString() == GenConstructorGenerator.IgnoreAttributeName) ?? false;
return !haveIgnoreAttribute;
}
}
}
2 changes: 1 addition & 1 deletion UnityAttributes/UnityAttributes.csproj
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<Version>1.1.1.0</Version>
<Version>1.1.2.0</Version>
<TargetFramework>netstandard2.0</TargetFramework>
<RootNamespace>UnityAttributes</RootNamespace>
<LangVersion>latest</LangVersion>
Expand Down

0 comments on commit 9b2c0f3

Please sign in to comment.