-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Migrate PublicAccessor to IncrementalGenerator. Update CodeBuilder. U…
…pdate code style.
- Loading branch information
Showing
23 changed files
with
631 additions
and
454 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
85 changes: 85 additions & 0 deletions
85
UnityAttributes/Common/MemberDeclarationSyntaxExtensions.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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"; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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, | ||
}; | ||
} | ||
} |
Oops, something went wrong.