Skip to content

Commit

Permalink
Fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
OctopBP committed Mar 18, 2024
1 parent 8151e78 commit a3fcef2
Show file tree
Hide file tree
Showing 8 changed files with 54 additions and 35 deletions.
28 changes: 16 additions & 12 deletions UnityAttributes/GenConstructor/GenConstructorGenerator.Attribute.cs
Original file line number Diff line number Diff line change
@@ -1,16 +1,20 @@
using UnityAttributes.Common;

namespace UnityAttributes.GenConstructor;

public partial class GenConstructorGenerator {
public const string AttributeName = "GenConstructorAttribute";
readonly string attributeText = @$"{Const.AUTO_GENERATED_TEXT}
public partial class GenConstructorGenerator
{
public const string ATTRIBUTE_NAME = "GenConstructorAttribute";
public const string ATTRIBUTE_TEXT =
$$"""
/// <auto-generated />
namespace UnityAttributes
{
[global::System.AttributeUsage(global::System.AttributeTargets.Class, Inherited = true, AllowMultiple = false)]
internal sealed class {{ATTRIBUTE_NAME}} : global::System.Attribute
{
public {{ATTRIBUTE_NAME}}() { }
}
}
{generatedCodeAttribute}
[global::System.AttributeUsage(global::System.AttributeTargets.Class, Inherited = true, AllowMultiple = false)]
internal sealed class {AttributeName} : global::System.Attribute
{{
public {AttributeName}() {{ }}
}}
";
""";
}
4 changes: 2 additions & 2 deletions UnityAttributes/GenConstructor/GenConstructorGenerator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ public partial class GenConstructorGenerator : ISourceGenerator
public void Initialize(GeneratorInitializationContext context)
{
context.RegisterForPostInitialization(i => {
i.AddSource($"{AttributeName}.g.cs", attributeText);
i.AddSource($"{ATTRIBUTE_NAME}.g.cs", ATTRIBUTE_TEXT);
i.AddSource($"{IgnoreAttributeName}.g.cs", ignoreAttributeText);
});
context.RegisterForSyntaxNotifications(() => new SyntaxReceiver());
Expand Down Expand Up @@ -116,7 +116,7 @@ public void OnVisitSyntaxNode(GeneratorSyntaxContext context)

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

if (!haveAttribute) return;
Expand Down
12 changes: 7 additions & 5 deletions UnityAttributes/MonoReadonly/MonoReadonlyGenerator.Attribute.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,14 @@ public partial class MonoReadonlyGenerator
static readonly string generatedCodeAttribute = typeof(MonoReadonlyGenerator).GeneratedCodeAttribute();

readonly string attributeText = @$"{Const.AUTO_GENERATED_TEXT}
{generatedCodeAttribute}
[global::System.AttributeUsage(global::System.AttributeTargets.Field, Inherited = true, AllowMultiple = false)]
internal class {AttributeName} : global::System.Attribute
namespace UnityAttributes
{{
public {AttributeName}() {{ }}
{generatedCodeAttribute}
[global::System.AttributeUsage(global::System.AttributeTargets.Field, Inherited = true, AllowMultiple = false)]
internal class {AttributeName} : global::System.Attribute
{{
public {AttributeName}() {{ }}
}}
}}
";
}
11 changes: 7 additions & 4 deletions UnityAttributes/PublicAccessor/PublicAccessorAttribute.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,14 @@ public static class PublicAccessorAttribute
public const string ATTRIBUTE_TEXT =
$$"""
/// <auto-generated />
[global::System.AttributeUsage(global::System.AttributeTargets.Field, Inherited = true, AllowMultiple = false)]
internal sealed class {{ATTRIBUTE_FULL_NAME}} : global::System.Attribute
namespace UnityAttributes
{
public {{ATTRIBUTE_FULL_NAME}}() { }
[global::System.AttributeUsage(global::System.AttributeTargets.Field, Inherited = true, AllowMultiple = false)]
internal sealed class {{ATTRIBUTE_FULL_NAME}} : global::System.Attribute
{
public {{ATTRIBUTE_FULL_NAME}}() { }
}
}
""";
}
9 changes: 7 additions & 2 deletions UnityAttributes/PublicAccessor/PublicAccessorGenerator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ public void Initialize(IncrementalGeneratorInitializationContext context)

private static bool IsSyntaxTargetForGeneration(SyntaxNode node)
{
return node is not ClassDeclarationSyntax;
return node is ClassDeclarationSyntax;
}

private static ClassToProcess? GetSemanticTargetForGeneration(GeneratorSyntaxContext ctx, CancellationToken token)
Expand Down Expand Up @@ -65,6 +65,11 @@ private static bool IsSyntaxTargetForGeneration(SyntaxNode node)
}
}
}

if (fieldToProcess.Count == 0)
{
return null;
}

return new ClassToProcess(classTypeSymbol, fieldToProcess, classDeclarationSymbol.GetNamespace());
}
Expand All @@ -90,7 +95,7 @@ private static string GenerateCode(ClassToProcess classToProcess)
builder.OpenBrackets();
}

builder.AppendIdent().Append(methodVisibility).Append(" partial class ").Append(classToProcess.FieldSymbol.Name);
builder.AppendIdent().Append(methodVisibility).Append(" partial class ").AppendLine(classToProcess.FieldSymbol.Name);
builder.OpenBrackets();

foreach (var field in classToProcess.Fields)
Expand Down
12 changes: 7 additions & 5 deletions UnityAttributes/Readonly/ReadonlyGenerator.Attribute.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,14 @@ public partial class ReadonlyGenerator
static readonly string generatedCodeAttribute = typeof(ReadonlyGenerator).GeneratedCodeAttribute();

readonly string attributeText = @$"{Const.AUTO_GENERATED_TEXT}
{generatedCodeAttribute}
[global::System.AttributeUsage(global::System.AttributeTargets.Field, Inherited = true, AllowMultiple = false)]
internal class {AttributeName} : global::System.Attribute
namespace UnityAttributes
{{
public {AttributeName}() {{ }}
{generatedCodeAttribute}
[global::System.AttributeUsage(global::System.AttributeTargets.Field, Inherited = true, AllowMultiple = false)]
internal class {AttributeName} : global::System.Attribute
{{
public {AttributeName}() {{ }}
}}
}}
";
}
11 changes: 7 additions & 4 deletions UnityAttributes/Singleton/SingletonGenerator.Attribute.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,14 @@ public partial class SingletonGenerator {
public const string AttributeName = "SingletonAttribute";
readonly string attributeText = @$"{Const.AUTO_GENERATED_TEXT}
{generatedCodeAttribute}
[global::System.AttributeUsage(global::System.AttributeTargets.Class, Inherited = true, AllowMultiple = false)]
internal sealed class {AttributeName} : global::System.Attribute
namespace UnityAttributes
{{
public {AttributeName}() {{ }}
{generatedCodeAttribute}
[global::System.AttributeUsage(global::System.AttributeTargets.Class, Inherited = true, AllowMultiple = false)]
internal sealed class {AttributeName} : global::System.Attribute
{{
public {AttributeName}() {{ }}
}}
}}
";
}
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.3.0.0</Version>
<Version>1.3.1</Version>
<TargetFramework>netstandard2.0</TargetFramework>
<RootNamespace>UnityAttributes</RootNamespace>
<LangVersion>latest</LangVersion>
Expand Down

0 comments on commit a3fcef2

Please sign in to comment.