From 9d2da916ec17f38ee59ddb1cbab883bd34b455bf Mon Sep 17 00:00:00 2001 From: marcos henrich Date: Sat, 23 Mar 2013 15:34:11 +0000 Subject: [PATCH] TextGenerator now indents multiple lines of the given string --- src/Generator/Utils/TextGenerator.cs | 18 +++++++++++------- src/Generator/Utils/Utils.cs | 11 +++++++++++ 2 files changed, 22 insertions(+), 7 deletions(-) diff --git a/src/Generator/Utils/TextGenerator.cs b/src/Generator/Utils/TextGenerator.cs index 030468b8..e14a3905 100644 --- a/src/Generator/Utils/TextGenerator.cs +++ b/src/Generator/Utils/TextGenerator.cs @@ -1,4 +1,5 @@ -using System.Collections.Generic; +using System; +using System.Collections.Generic; using System.Linq; using System.Text; @@ -24,16 +25,19 @@ namespace Cxxi public void Write(string msg, params object[] args) { - if (isStartOfLine) - sb.Append(new string(' ', (int)CurrentIndent.Sum(u => u))); - if (args.Length > 0) msg = string.Format(msg, args); - if (msg.Length > 0) - isStartOfLine = msg.EndsWith(System.Environment.NewLine); + foreach(var line in msg.SplitAndKeep(Environment.NewLine)) + { + if (isStartOfLine && !string.IsNullOrWhiteSpace(line)) + sb.Append(new string(' ', (int) CurrentIndent.Sum(u => u))); + + if (line.Length > 0) + isStartOfLine = line.EndsWith(Environment.NewLine); - sb.Append(msg); + sb.Append(line); + } } public void WriteLine(string msg, params object[] args) diff --git a/src/Generator/Utils/Utils.cs b/src/Generator/Utils/Utils.cs index 0d6c8c6e..3485a3d2 100644 --- a/src/Generator/Utils/Utils.cs +++ b/src/Generator/Utils/Utils.cs @@ -164,6 +164,17 @@ namespace Cxxi var str = Regex.Replace(input, "([A-Z])", " $1", RegexOptions.Compiled); return str.Trim().Split(); } + + public static IEnumerable SplitAndKeep(this string s, string seperator) + { + string[] obj = s.Split(new string[] { seperator }, StringSplitOptions.None); + + for (int i = 0; i < obj.Length; i++) + { + string result = i == obj.Length - 1 ? obj[i] : obj[i] + seperator; + yield return result; + } + } } public static class AssemblyHelpers