Skip to content

Commit

Permalink
Fix Inconsistent line endings in Service.g.cs - #3694
Browse files Browse the repository at this point in the history
  • Loading branch information
pepone committed Oct 18, 2023
1 parent 715f18b commit c877f02
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 2 deletions.
2 changes: 1 addition & 1 deletion src/IceRpc.Protobuf.Generators/Internal/Emitter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ internal string Emit(IReadOnlyList<ServiceClass> serviceClasses, CancellationTok

string generated = string.Join("\n\n", generatedClasses).Trim();
generated += "\n";
return generated;
return generated.ReplaceLineEndings();
}

private static string GenerateContainer(string header, string body)
Expand Down
18 changes: 18 additions & 0 deletions src/IceRpc.Protobuf.Generators/Internal/StringExtensions.cs
Original file line number Diff line number Diff line change
@@ -1,12 +1,30 @@
// Copyright (c) ZeroC, Inc.

using System.Runtime.InteropServices;

namespace IceRpc.Protobuf.Generators.Internal;

internal static class StringExtensions
{
private static readonly string _newLine = RuntimeInformation.IsOSPlatform(OSPlatform.Windows) ? "\r\n" : "\n";

internal static string WithIndent(this string contents, string indent) =>
string.Join("\n", contents.Split('\n').Select(value => $"{indent}{value}")).Trim();

internal static string TrimTrailingWhiteSpaces(this string contents) =>
string.Join("\n", contents.Split('\n').Select(value => value.TrimEnd()));

// TODO Once generators can target .NET 8 we can use String.ReplaceLineEndings and remove this implementation.
internal static string ReplaceLineEndings(this string contents)
{
if (contents.IndexOf("\r\n") != -1)
{
contents = contents.Replace("\r\n", "\n");
}
if (_newLine != "\n")
{
contents = contents.Replace("\n", _newLine);
}
return contents;
}
}
2 changes: 1 addition & 1 deletion src/IceRpc.Slice.Generators/Internal/Emitter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ internal string Emit(IReadOnlyList<ServiceClass> serviceClasses, CancellationTok

string generated = string.Join("\n\n", generatedClasses).Trim();
generated += "\n";
return generated;
return generated.ReplaceLineEndings();
}

private static string GenerateContainer(string header, string body)
Expand Down
18 changes: 18 additions & 0 deletions src/IceRpc.Slice.Generators/Internal/StringExtensions.cs
Original file line number Diff line number Diff line change
@@ -1,12 +1,30 @@
// Copyright (c) ZeroC, Inc.

using System.Runtime.InteropServices;

namespace IceRpc.Slice.Generators.Internal;

internal static class StringExtensions
{
private static readonly string _newLine = RuntimeInformation.IsOSPlatform(OSPlatform.Windows) ? "\r\n" : "\n";

internal static string WithIndent(this string contents, string indent) =>
string.Join("\n", contents.Split('\n').Select(value => $"{indent}{value}")).Trim();

internal static string TrimTrailingWhiteSpaces(this string contents) =>
string.Join("\n", contents.Split('\n').Select(value => value.TrimEnd()));

// TODO Once generators can target .NET 8 we can use String.ReplaceLineEndings and remove this implementation.
internal static string ReplaceLineEndings(this string contents)
{
if (contents.IndexOf("\r\n") != -1)
{
contents = contents.Replace("\r\n", "\n");
}
if (_newLine != "\n")
{
contents = contents.Replace("\n", _newLine);
}
return contents;
}
}

0 comments on commit c877f02

Please sign in to comment.