From d26fccaf8b561b493d5d379ec2cc3c45169c5bbf Mon Sep 17 00:00:00 2001 From: thenameless314159 Date: Sun, 18 Aug 2024 22:15:02 +0200 Subject: [PATCH] fixed spacing logic (2nd) --- .../Infrastructure/DescriptorExtensions.cs | 49 +++++++++++++++---- .../Infrastructure/SourceWriter.cs | 5 +- 2 files changed, 41 insertions(+), 13 deletions(-) diff --git a/src/ProtocolDumper/Infrastructure/DescriptorExtensions.cs b/src/ProtocolDumper/Infrastructure/DescriptorExtensions.cs index 7093291..350c16b 100644 --- a/src/ProtocolDumper/Infrastructure/DescriptorExtensions.cs +++ b/src/ProtocolDumper/Infrastructure/DescriptorExtensions.cs @@ -26,15 +26,46 @@ public static SourceWriter ToSourceWriter(this FileDescriptorProto proto, Source writer.AppendLine($"package {proto.Package};") .AppendLine(); - foreach (var service in proto.Service.array.Where(static s => s is not null)) + for (var i = 0; i < proto.Service.array.Count; i++) + { + var service = proto.Service.array[i]; + if (service is null) continue; + service.WriteTo(writer); - - foreach (var enumType in proto.EnumType.array.Where(static e => e is not null)) + + if (i < proto.Service.array.Count - 1) + writer.AppendLine(); // add a newline after each service except the last one + } + + if (proto.EnumType.array.Length > 0 && proto.Service.array.Length > 0) + writer.AppendLine(); // add a newline after services if there are any enums + + for (var i = 0; i < proto.EnumType.array.Count; i++) + { + var enumType = proto.EnumType.array[i]; + if (enumType is null) continue; + enumType.WriteTo(writer); - foreach (var message in proto.MessageType.array.Where(static m => m is not null)) + if (i < proto.EnumType.array.Count - 1) + writer.AppendLine(); // add a newline after each enum except the last one + } + + if (proto.MessageType.array.Length > 0 + && (proto.Service.array.Length > 0 || proto.EnumType.array.Length > 0)) + writer.AppendLine(); // add a newline after enums if there are any messages + + for (var i = 0; i < proto.MessageType.array.Count; i++) + { + var message = proto.MessageType.array[i]; + if (message is null) continue; + message.WriteTo(writer); + if (i < proto.MessageType.array.Count - 1) + writer.AppendLine(); // add a newline after each message except the last one + } + return writer; } @@ -43,7 +74,7 @@ public static SourceWriter WriteTo(this DescriptorProto message, SourceWriter wr writer.AppendLine($$"""message {{message.Name}} {"""); writer.Indentation++; - var indentBefore = writer.Indentation; + var indentationBeforeStart = writer.Indentation; for (var i = 0; i < message.Field.array.Count; i++) { var field = message.Field.array[i]; @@ -51,7 +82,7 @@ public static SourceWriter WriteTo(this DescriptorProto message, SourceWriter wr if (field.HasOneofIndex) { - var isFirst = writer.Indentation == indentBefore; + var isFirst = writer.Indentation == indentationBeforeStart; var hasNext = i < message.Field.array.Count - 1 && message.Field.array[i + 1] is not null && message.Field.array[i + 1].HasOneofIndex @@ -120,7 +151,7 @@ public static SourceWriter WriteTo(this DescriptorProto message, SourceWriter wr writer.AppendLine(); } - return writer.AppendLine().CloseBlock(); + return writer.CloseBlock(); static string GetLabelFor(FieldDescriptorProto proto) { @@ -186,7 +217,7 @@ public static SourceWriter WriteTo(this EnumDescriptorProto enumType, SourceWrit writer.AppendLine($"{value.Name} = {value.Number};"); } - return writer.AppendLine().CloseBlock(); + return writer.CloseBlock(); } public static SourceWriter WriteTo(this ServiceDescriptorProto service, SourceWriter writer) @@ -194,6 +225,6 @@ public static SourceWriter WriteTo(this ServiceDescriptorProto service, SourceWr writer.AppendLine($$"""service {{service.Name}} {"""); writer.Indentation++; - return writer.AppendLine().CloseBlock(); + return writer.CloseBlock(); } } diff --git a/src/ProtocolDumper/Infrastructure/SourceWriter.cs b/src/ProtocolDumper/Infrastructure/SourceWriter.cs index 31096bc..f02dbec 100644 --- a/src/ProtocolDumper/Infrastructure/SourceWriter.cs +++ b/src/ProtocolDumper/Infrastructure/SourceWriter.cs @@ -92,10 +92,7 @@ public SourceWriter CloseBlock() { Indentation--; - if (Indentation == 0) - _sb.Append(CloseBrace); - else - AppendLine(CloseBrace); + AppendLine(CloseBrace); return this; }