Skip to content

Commit cdf91ff

Browse files
committed
Feat: AppendBody()
1 parent b46e0c5 commit cdf91ff

File tree

2 files changed

+35
-1
lines changed

2 files changed

+35
-1
lines changed

src/CodeOfChaos.GeneratorTools/StringBuilders/GeneratorStringBuilder.cs

+12-1
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,18 @@ public GeneratorStringBuilder AppendMultipleUsings(params Func<IEnumerable<strin
5050

5151
#region AppendLine methods(stringbuilder + indent)
5252
public GeneratorStringBuilder AppendLine() => BuilderAction(() => _builder.AppendLine());
53-
public GeneratorStringBuilder AppendLine(string text) => BuilderAction(() => _builder.Append(IndentString(IndentAmount)).AppendLine(text));
53+
public GeneratorStringBuilder AppendLine(string text) => BuilderAction(() => _builder
54+
.Append(IndentString(IndentAmount))
55+
.AppendLine(text)
56+
);
57+
public GeneratorStringBuilder AppendBody(string text) => BuilderAction(() => {
58+
string[] lines = text.Split(["\r\n", "\n"], StringSplitOptions.None);
59+
60+
foreach (string line in lines) _builder
61+
.Append(IndentString(IndentAmount))
62+
.AppendLine(line);
63+
});
64+
5465
#endregion
5566
#region Auto Indented methods
5667
private string IndentString(int amount) => new(' ', amount * _paddingChars);

tests/Tests.CodeOfChaos.GeneratorTools/StringBuilders/GeneratorStringBuilderTests.cs

+23
Original file line numberDiff line numberDiff line change
@@ -179,4 +179,27 @@ public async Task IndentAction_ShouldIndentAndExecuteAction_MultipleTimes() {
179179
// Assert
180180
await Assert.That(generator.ToString()).IsEqualTo($" Indented Text{Environment.NewLine} Something{Environment.NewLine} Else{Environment.NewLine}");
181181
}
182+
183+
[Test]
184+
public async Task AppendBody_ShouldIndentAndAppendText() {
185+
// Arrange
186+
var generator = new GeneratorStringBuilder();
187+
generator.AppendLine("Something special");
188+
189+
190+
// Act
191+
generator.Indent(g => g.AppendBody("""
192+
SomeData
193+
Something
194+
"""));
195+
196+
// Assert
197+
await Assert.That(generator.ToString()).IsEqualTo($"Something special{Environment.NewLine} SomeData{Environment.NewLine} Something{Environment.NewLine}");
198+
// equal to:
199+
// """
200+
// Something special
201+
// SomeData
202+
// Something
203+
// """"
204+
}
182205
}

0 commit comments

Comments
 (0)