Skip to content

Commit

Permalink
Migrate PublicAccessor to IncrementalGenerator. Update CodeBuilder. U…
Browse files Browse the repository at this point in the history
…pdate code style.
  • Loading branch information
OctopBP committed Mar 18, 2024
1 parent 303bb77 commit ee61bbd
Show file tree
Hide file tree
Showing 23 changed files with 631 additions and 454 deletions.
12 changes: 0 additions & 12 deletions UnityAttributes.sln
Original file line number Diff line number Diff line change
@@ -1,9 +1,5 @@

Microsoft Visual Studio Solution File, Format Version 12.00
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "PublicAccessor.IncrementalGenerator", "PublicAccessor.IncrementalGenerator\PublicAccessor.IncrementalGenerator.csproj", "{B8CB288F-9675-46BC-A282-06D1AF7B57E4}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "UnityAttributes.Example", "UnityAttributes.Example\UnityAttributes.Example.csproj", "{A01713F5-F846-4C3C-A274-AC21807C67E5}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "UnityAttributes", "UnityAttributes\UnityAttributes.csproj", "{F8986E61-FBE1-4A42-BFE8-9EFF365E44B0}"
EndProject
Global
Expand All @@ -12,14 +8,6 @@ Global
Release|Any CPU = Release|Any CPU
EndGlobalSection
GlobalSection(ProjectConfigurationPlatforms) = postSolution
{B8CB288F-9675-46BC-A282-06D1AF7B57E4}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{B8CB288F-9675-46BC-A282-06D1AF7B57E4}.Debug|Any CPU.Build.0 = Debug|Any CPU
{B8CB288F-9675-46BC-A282-06D1AF7B57E4}.Release|Any CPU.ActiveCfg = Release|Any CPU
{B8CB288F-9675-46BC-A282-06D1AF7B57E4}.Release|Any CPU.Build.0 = Release|Any CPU
{A01713F5-F846-4C3C-A274-AC21807C67E5}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{A01713F5-F846-4C3C-A274-AC21807C67E5}.Debug|Any CPU.Build.0 = Debug|Any CPU
{A01713F5-F846-4C3C-A274-AC21807C67E5}.Release|Any CPU.ActiveCfg = Release|Any CPU
{A01713F5-F846-4C3C-A274-AC21807C67E5}.Release|Any CPU.Build.0 = Release|Any CPU
{F8986E61-FBE1-4A42-BFE8-9EFF365E44B0}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{F8986E61-FBE1-4A42-BFE8-9EFF365E44B0}.Debug|Any CPU.Build.0 = Debug|Any CPU
{F8986E61-FBE1-4A42-BFE8-9EFF365E44B0}.Release|Any CPU.ActiveCfg = Release|Any CPU
Expand Down
117 changes: 61 additions & 56 deletions UnityAttributes/Common/CodeBuilder.cs
Original file line number Diff line number Diff line change
@@ -1,72 +1,77 @@
using System;
using System.Text;

namespace UnityAttributes.Common;
namespace UnityAttributes.Common;

public class CodeBuilder
{
public enum IdentChange
{
None,
IncreaseBefore,
DecreaseBefore,
IncreaseAfter,
DecreaseAfter
}

const string IndentSymbol = " ";

readonly StringBuilder stringBuilder = new();
int indent;
private readonly StringBuilder _sb = new StringBuilder();
private int _ident = 0;

public void append(string text) => stringBuilder.Append(text);
public void appendEmptyLine() => stringBuilder.AppendLine();

public void appendLine(string text, IdentChange identChange = IdentChange.None)
{
switch (identChange) {
case IdentChange.None:
case IdentChange.IncreaseAfter:
case IdentChange.DecreaseAfter:
break;
case IdentChange.IncreaseBefore:
indent++; break;
case IdentChange.DecreaseBefore:
indent--; break;
default: throw new ArgumentOutOfRangeException(nameof(identChange), identChange, null);
public CodeBuilder AppendLine()
{
_sb.AppendLine();
return this;
}

public CodeBuilder AppendLine(string value)
{
_sb.AppendLine(value);
return this;
}

public CodeBuilder AppendLineWithIdent(string value)
{
return AppendIdent().AppendLine(value);
}

stringBuilder.AppendLine(identToSpaces() + text);
public CodeBuilder Append(string value)
{
_sb.Append(value);
return this;
}

switch (identChange)
public CodeBuilder Append(char value)
{
case IdentChange.None:
case IdentChange.IncreaseBefore:
case IdentChange.DecreaseBefore:
break;
case IdentChange.IncreaseAfter:
indent++; break;
case IdentChange.DecreaseAfter:
indent--; break;
default: throw new ArgumentOutOfRangeException(nameof(identChange), identChange, null);
_sb.Append(value);
return this;
}
string identToSpaces()

public CodeBuilder AppendIdent()
{
if (indent <= 0) return string.Empty;
var textAsSpan = IndentSymbol.AsSpan();
var span = new Span<char>(new char[textAsSpan.Length * indent]);
for (var idx = 0; idx < indent; idx++)
{
textAsSpan.CopyTo(span.Slice(idx * textAsSpan.Length, textAsSpan.Length));
}
for (var i = 0; i < _ident; i++)
{
_sb.Append(" ");
}
return this;
}

return span.ToString();
public CodeBuilder OpenBrackets()
{
return AppendLineWithIdent("{").IncreaseIdent();
}

public CodeBuilder CloseBrackets()
{
return DecreaseIdent().AppendLineWithIdent("}");
}

public CodeBuilder IncreaseIdent()
{
_ident++;
return this;
}

public CodeBuilder DecreaseIdent()
{
if (_ident > 0)
{
_ident--;
}
return this;
}
}

public string getResult()
{
return stringBuilder.ToString();
}
public override string ToString()
{
return _sb.ToString();
}
}
20 changes: 11 additions & 9 deletions UnityAttributes/Common/Const.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,15 @@ namespace UnityAttributes.Common;

public static class Const
{
public const string AutoGeneratedText =
@"//------------------------------------------------------------------------------
// <auto-generated>
// This code was generated by a tool.
//
// Changes to this file may cause incorrect behavior and will be lost if
// the code is regenerated.
// </auto-generated>
//------------------------------------------------------------------------------";
public const string AUTO_GENERATED_TEXT =
"""
//------------------------------------------------------------------------------
// <auto-generated>
// This code was generated by a tool.
//
// Changes to this file may cause incorrect behavior and will be lost if
// the code is regenerated.
// </auto-generated>
//------------------------------------------------------------------------------
""";
}
85 changes: 85 additions & 0 deletions UnityAttributes/Common/MemberDeclarationSyntaxExtensions.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
using System;
using System.Collections.Generic;
using Microsoft.CodeAnalysis.CSharp.Syntax;

namespace UnityAttributes.Common;

public static class MemberDeclarationSyntaxExtensions
{
public static bool HaveAttribute(this MemberDeclarationSyntax enumDeclarationSyntax, string attributeName)
{
foreach (var attributeListSyntax in enumDeclarationSyntax.AttributeLists)
{
foreach (var attributeSyntax in attributeListSyntax.Attributes)
{
if (attributeSyntax.Name.IsEqualByName(attributeName))
{
return true;
}
}
}

return false;
}

public static List<AttributeSyntax> AllAttributesWithName(this MemberDeclarationSyntax enumDeclarationSyntax, string attributeName)
{
List<AttributeSyntax> list = [];

foreach (var attributeListSyntax in enumDeclarationSyntax.AttributeLists)
{
foreach (var attributeSyntax in attributeListSyntax.Attributes)
{
if (attributeSyntax.Name.IsEqualByName(attributeName))
{
list.Add(attributeSyntax);
}
}
}

return list;
}

public static List<AttributeSyntax> AllAttributesWhere(this MemberDeclarationSyntax enumDeclarationSyntax, Predicate<AttributeSyntax> filter)
{
List<AttributeSyntax> list = [];

foreach (var attributeListSyntax in enumDeclarationSyntax.AttributeLists)
{
foreach (var attributeSyntax in attributeListSyntax.Attributes)
{
if (filter(attributeSyntax))
{
list.Add(attributeSyntax);
}
}
}

return list;
}

public static bool HaveAttributeWithArguments(this MemberDeclarationSyntax enumDeclarationSyntax, string attributeName, out AttributeArgumentListSyntax argumentList)
{
foreach (var attributeListSyntax in enumDeclarationSyntax.AttributeLists)
{
foreach (var attributeSyntax in attributeListSyntax.Attributes)
{
if (!attributeSyntax.Name.IsEqualByName(attributeName))
{
continue;
}

if (attributeSyntax.ArgumentList is not { Arguments.Count: > 0, })
{
continue;
}

argumentList = attributeSyntax.ArgumentList;
return true;
}
}

argumentList = default;
return false;
}
}
12 changes: 12 additions & 0 deletions UnityAttributes/Common/NameSyntaxExtensions.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
using Microsoft.CodeAnalysis.CSharp.Syntax;

namespace UnityAttributes.Common;

public static class NameSyntaxExtensions
{
public static bool IsEqualByName(this NameSyntax syntax, string attributeName)
{
var nameText = syntax.GetNameText();
return nameText == attributeName || nameText == attributeName + "Attribute";
}
}
20 changes: 13 additions & 7 deletions UnityAttributes/Common/StringExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,21 +4,27 @@ namespace UnityAttributes.Common;

public static class StringExtensions
{
public static string firstCharToUpper(this string input) =>
input switch
public static string FirstCharToUpper(this string input)
{
return input switch
{
null => throw new ArgumentNullException(nameof(input)),
"" => throw new ArgumentException($"{nameof(input)} cannot be empty", nameof(input)),
_ => input[0].ToString().ToUpper() + input.Substring(1)
};

public static string upperFirstCharOrAddUnderline(this string input) =>
input switch
}

public static string UpperFirstCharOrAddUnderline(this string input)
{
return input switch
{
null => throw new ArgumentNullException(nameof(input)),
"" => throw new ArgumentException($"{nameof(input)} cannot be empty", nameof(input)),
_ => input[0] == '_' && input.Length > 1 && !char.IsUpper(input[1])
? upperFirstCharOrAddUnderline(input.Substring(1))
: char.IsUpper(input[0]) ? "_" + input : firstCharToUpper(input)
? UpperFirstCharOrAddUnderline(input.Substring(1))
: char.IsUpper(input[0])
? "_" + input
: FirstCharToUpper(input)
};
}
}
50 changes: 50 additions & 0 deletions UnityAttributes/Common/SymbolExtensions.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
#nullable enable
using Microsoft.CodeAnalysis;

namespace UnityAttributes.Common;

public static class SymbolExtensions
{
public static string? GetNamespace(this ISymbol symbol)
{
string? result = null;
var ns = symbol.ContainingNamespace;
while (ns is not null && !ns.IsGlobalNamespace)
{
if (result is not null)
{
result = ns.Name + "." + result;
}
else
{
result = ns.Name;
}

ns = ns.ContainingNamespace;
}

return result;
}

public static bool IsVisibleOutsideOfAssembly(this ISymbol? symbol)
{
if (symbol is null)
{
return false;
}

if (symbol.DeclaredAccessibility != Accessibility.Public &&
symbol.DeclaredAccessibility != Accessibility.Protected &&
symbol.DeclaredAccessibility != Accessibility.ProtectedOrInternal)
{
return false;
}

if (symbol.ContainingType is null)
{
return true;
}

return IsVisibleOutsideOfAssembly(symbol.ContainingType);
}
}
17 changes: 17 additions & 0 deletions UnityAttributes/Common/SyntaxExtensions.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
#nullable enable
using Microsoft.CodeAnalysis.CSharp.Syntax;

namespace UnityAttributes.Common;

public static class SyntaxExtensions
{
public static string? GetNameText(this NameSyntax? name)
{
return name switch
{
SimpleNameSyntax ins => ins.Identifier.Text,
QualifiedNameSyntax qns => qns.Right.Identifier.Text,
_ => null,
};
}
}
Loading

0 comments on commit ee61bbd

Please sign in to comment.