From 9956ad348351a6dc0539d6ecac139b828c99b83c Mon Sep 17 00:00:00 2001 From: Sergio Pedri Date: Thu, 5 Sep 2019 18:21:47 +0200 Subject: [PATCH] Improved formatting for implicitly captured local functions --- .../Extensions/StringExtensions.cs | 28 +++++++++++++++++++ .../Translation/ShaderLoader.cs | 2 +- 2 files changed, 29 insertions(+), 1 deletion(-) create mode 100644 src/ComputeSharp.Shaders/Extensions/StringExtensions.cs 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)