Skip to content

Commit

Permalink
Clean up MethodParentDeclaration creation
Browse files Browse the repository at this point in the history
  • Loading branch information
virzak committed Jul 8, 2024
1 parent 702ed51 commit b4d4eb9
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 59 deletions.
1 change: 1 addition & 0 deletions .vscode/spellright.dict
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
awaitable
codecov
equatable
extensi
impl
func
Expand Down
15 changes: 13 additions & 2 deletions src/Zomp.SyncMethodGenerator/MethodParentDeclaration.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,19 @@
/// Represents a class a <see cref="MethodToGenerate"/> belongs to.
/// </summary>
/// <param name="MethodParent">Type of container.</param>
/// <param name="ClassOrStructKeyword">Indicates whether struct or class are explicitly specified for a record.</param>
/// <param name="ParentName">Class name.</param>
/// <param name="Modifiers">A list of modifiers.</param>
/// <param name="TypeParameterListSyntax">A list of type parameters.</param>
internal sealed record MethodParentDeclaration(MethodParent MethodParent, SyntaxToken ClassOrStructKeyword, string ParentName, EquatableArray<ushort> Modifiers, EquatableArray<string> TypeParameterListSyntax);
/// <param name="ClassOrStructKeyword">Indicates whether struct or class are explicitly specified for a record.</param>
internal sealed record MethodParentDeclaration(MethodParent MethodParent, string ParentName, EquatableArray<ushort> Modifiers, EquatableArray<string> TypeParameterListSyntax, SyntaxToken ClassOrStructKeyword)
{
public MethodParentDeclaration(MethodParent methodParent, SyntaxToken parentName, SyntaxTokenList modifiers, TypeParameterListSyntax? typeParameterList, SyntaxToken classOrStructKeyword = default)
: this(
methodParent,
parentName.ValueText,
modifiers.Select(z => (ushort)z.RawKind).Where(z => z != (ushort)SyntaxKind.PartialKeyword).ToImmutableArray(),
(typeParameterList is null ? [] : typeParameterList.Parameters.Select(z => z.Identifier.ValueText)).ToImmutableArray(),
classOrStructKeyword)
{
}
}
67 changes: 10 additions & 57 deletions src/Zomp.SyncMethodGenerator/SyncMethodSourceGenerator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -143,68 +143,21 @@ static string BuildClassName(MethodParentDeclaration c)
while (node.Parent is not null)
{
node = node.Parent;
SyntaxTokenList originalModifiers;
SyntaxToken identifier;
SyntaxToken classOrStructKeyword = default;
TypeParameterListSyntax? typeParameterList;
MethodParent methodParent;
if (node is ClassDeclarationSyntax classSyntax)
MethodParentDeclaration? mpd = node switch
{
originalModifiers = classSyntax.Modifiers;
typeParameterList = classSyntax.TypeParameterList;
identifier = classSyntax.Identifier;
methodParent = MethodParent.Class;
}
else if (node is StructDeclarationSyntax structSyntax)
{
originalModifiers = structSyntax.Modifiers;
typeParameterList = structSyntax.TypeParameterList;
identifier = structSyntax.Identifier;
methodParent = MethodParent.Struct;
}
else if (node is RecordDeclarationSyntax recordSyntax)
{
originalModifiers = recordSyntax.Modifiers;
typeParameterList = recordSyntax.TypeParameterList;
identifier = recordSyntax.Identifier;
methodParent = MethodParent.Record;
classOrStructKeyword = recordSyntax.ClassOrStructKeyword;
}
else if (node is InterfaceDeclarationSyntax idx)
{
originalModifiers = idx.Modifiers;
typeParameterList = idx.TypeParameterList;
identifier = idx.Identifier;
methodParent = MethodParent.Interface;
}
else
ClassDeclarationSyntax o => new(MethodParent.Class, o.Identifier, o.Modifiers, o.TypeParameterList),
StructDeclarationSyntax o => new(MethodParent.Struct, o.Identifier, o.Modifiers, o.TypeParameterList),
RecordDeclarationSyntax o => new(MethodParent.Record, o.Identifier, o.Modifiers, o.TypeParameterList, o.ClassOrStructKeyword),
InterfaceDeclarationSyntax o => new(MethodParent.Interface, o.Identifier, o.Modifiers, o.TypeParameterList),
_ => null,
};

if (mpd is null)
{
break;
}

////var modifiers = classSyntax?.Modifiers ?? structSyntax.Modifiers;

var modifiers = ImmutableArray.CreateBuilder<ushort>();

foreach (var mod in originalModifiers)
{
var kind = mod.RawKind;
if (kind == (int)SyntaxKind.PartialKeyword)
{
continue;
}

modifiers.Add((ushort)kind);
}

var typeParameters = ImmutableArray.CreateBuilder<string>();

foreach (var typeParameter in typeParameterList?.Parameters ?? default)
{
typeParameters.Add(typeParameter.Identifier.ValueText);
}

classes.Insert(0, new(methodParent, classOrStructKeyword, identifier.ValueText, modifiers.ToImmutable(), typeParameters.ToImmutable()));
classes.Insert(0, mpd);
}

if (classes.Count == 0)
Expand Down

0 comments on commit b4d4eb9

Please sign in to comment.