diff --git a/src/ComputeSharp.Shaders/Extensions/StringExtensions.cs b/src/ComputeSharp.Shaders/Extensions/StringExtensions.cs new file mode 100644 index 00000000..401321a3 --- /dev/null +++ b/src/ComputeSharp.Shaders/Extensions/StringExtensions.cs @@ -0,0 +1,28 @@ +using System; +using System.Diagnostics.Contracts; +using System.Linq; +using System.Text.RegularExpressions; + +namespace ComputeSharp.Shaders.Extensions +{ + /// + /// A that provides extension methods for the type + /// + internal static class StringExtensions + { + /// + /// Removes the left padding for an input multiline + /// + /// The input to process + /// A left-aligned with the same text as the input + [Pure] + public static string RemoveLeftPadding(this string text) + { + string[] lines = text.Split(Environment.NewLine); + int spaces = text.TakeWhile(c => c == ' ').Count(); + Regex regex = new Regex($"^{new string(Enumerable.Repeat(' ', spaces).ToArray())}"); + + return lines.Aggregate(string.Empty, (result, line) => $"{result}{Environment.NewLine}{regex.Replace(line, string.Empty)}"); + } + } +} diff --git a/src/ComputeSharp.Shaders/Translation/ShaderLoader.cs b/src/ComputeSharp.Shaders/Translation/ShaderLoader.cs index 13576969..d9010301 100644 --- a/src/ComputeSharp.Shaders/Translation/ShaderLoader.cs +++ b/src/ComputeSharp.Shaders/Translation/ShaderLoader.cs @@ -305,7 +305,7 @@ namespace ComputeSharp.Shaders.Translation // Extract the implicit local functions var locals = root.DescendantNodes().OfType().ToArray(); root = root.RemoveNodes(locals, SyntaxRemoveOptions.KeepNoTrivia); - _LocalFunctionsList.AddRange(locals.Select(local => new LocalFunctionInfo(local.ToString()))); + _LocalFunctionsList.AddRange(locals.Select(local => new LocalFunctionInfo(local.ToFullString().RemoveLeftPadding().Trim(' ', '\r', '\n')))); // Register the captured static members foreach (var member in syntaxRewriter.StaticMembers)