Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
  • Loading branch information
View committed Feb 23, 2024
1 parent a223cf6 commit ea1fef0
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 45 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -48,43 +48,43 @@ protected override (SyntaxKind modifier, ParameterListSyntax constructorParamete
switch (memberType)
{
case MemberType.Field:
{
var fields = typeDeclaration.Members
.OfType<FieldDeclarationSyntax>()
.Where(IsFieldRequired)
.Where(static p => !p.Modifiers.Any(SyntaxKind.StaticKeyword))
.Where(accessType)
.ToList();
if (fields.Count == 0)
{
return (modifier, ParameterList(), Block());
}
var fields = typeDeclaration.Members
.OfType<FieldDeclarationSyntax>()
.Where(IsFieldRequired)
.Where(static p => !p.Modifiers.Any(SyntaxKind.StaticKeyword))
.Where(accessType)
.ToList();
if (fields.Count == 0)
{
return (modifier, ParameterList(), Block());
}

List<(TypeSyntax Type, string Name)> typesAndNames = fields
.SelectMany(static p => p.Declaration.Variables.Select(v => (p.Declaration.Type, v.Identifier.Text)))
.ToList();
List<(TypeSyntax Type, string Name)> typesAndNames = fields
.SelectMany(static p => p.Declaration.Variables.Select(v => (p.Declaration.Type, v.Identifier.Text)))
.ToList();

return GetConstructorParts(modifier, typesAndNames, static s => s.ToCamelCaseIdentifier());
}
return GetConstructorParts(modifier, typesAndNames, static s => s.ToCamelCaseIdentifier());
}
case MemberType.Property:
{
var properties = typeDeclaration.Members
.OfType<PropertyDeclarationSyntax>()
.Where(IsPropertyRequired)
.Where(static p => !p.Modifiers.Any(SyntaxKind.StaticKeyword))
.Where(accessType)
.ToList();
if (properties.Count == 0)
{
return (modifier, ParameterList(), Block());
}
var properties = typeDeclaration.Members
.OfType<PropertyDeclarationSyntax>()
.Where(IsPropertyRequired)
.Where(static p => !p.Modifiers.Any(SyntaxKind.StaticKeyword))
.Where(accessType)
.ToList();
if (properties.Count == 0)
{
return (modifier, ParameterList(), Block());
}

List<(TypeSyntax Type, string Name)> typesAndNames = properties
.Select(static p => (p.Type, p.Identifier.Text))
.ToList();
List<(TypeSyntax Type, string Name)> typesAndNames = properties
.Select(static p => (p.Type, p.Identifier.Text))
.ToList();

return GetConstructorParts(modifier, typesAndNames, static s => s.ToCamelCaseIdentifier());
}
return GetConstructorParts(modifier, typesAndNames, static s => s.ToCamelCaseIdentifier());
}
default: throw new ArgumentOutOfRangeException(nameof(memberType));
}
}
Expand Down Expand Up @@ -117,8 +117,26 @@ protected virtual bool IsFieldRequired(FieldDeclarationSyntax f)
private static (SyntaxKind modifier, ParameterListSyntax Parameters, BlockSyntax Body) GetConstructorParts(SyntaxKind modifier, IReadOnlyCollection<(TypeSyntax Type, string Name)> members,
Func<string, string> parameterTransformer)
{
var constructorParameters = members.Select(tn => CreateParameter(tn.Type, parameterTransformer(tn.Name)));
var constructorBody = members.Select(tn => CreateExpression(tn.Name, parameterTransformer(tn.Name)));
var constructorParameters = members.Select(tn => CreateParameter(tn.Type, parameterTransformer(tn.Name))).ToList();
foreach (var sameParameters in constructorParameters.GroupBy(x => x.Identifier.ToString()).Where(x => x.Count() > 1))
{
int index = 1;
foreach (var parameter in sameParameters)
{
var parameterIndex = constructorParameters.IndexOf(parameter);
constructorParameters.Insert(parameterIndex, CreateParameter(parameter.Type!, parameterTransformer($"{parameter.Identifier.ValueText}{index}")));
constructorParameters.Remove(parameter);
index++;
}
}

var constructorBody = new List<ExpressionStatementSyntax>();
int i = 0;
foreach (var member in members)
{
constructorBody.Add(CreateExpression(member.Name, constructorParameters[i].Identifier.ValueText));
i++;
}

return (modifier, ParameterList(SeparatedList(constructorParameters)), Block(constructorBody));
}
Expand All @@ -128,7 +146,11 @@ private static ExpressionStatementSyntax CreateExpression(string variable, strin
return ExpressionStatement(
AssignmentExpression(
SyntaxKind.SimpleAssignmentExpression,
IdentifierName(variable),
variable == argument ? MemberAccessExpression(
SyntaxKind.SimpleMemberAccessExpression,
ThisExpression(),
Token(SyntaxKind.DotToken),
IdentifierName(variable)) : IdentifierName(variable),
IdentifierName(argument.EscapeReservedKeyword())
)
);
Expand Down
13 changes: 1 addition & 12 deletions Lombok.NET/Extensions/StringExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -161,17 +161,6 @@ public static string ToCamelCaseIdentifier(this string identifier)
return identifier.Substring(1).Decapitalize()!;
}

if (identifier.StartsWith("@"))
{
return "new" + identifier.Substring(1).Capitalize();
}

var decaptialized = identifier.Decapitalize()!;
if (decaptialized == identifier)
{
return "new" + identifier.Capitalize();
}

return decaptialized;
return identifier.Decapitalize()!;
}
}

0 comments on commit ea1fef0

Please sign in to comment.