Skip to content

Commit aef7e1b

Browse files
committed
CodegenCS.Core 3.3.0:
CodegenTextWriter.PreserveNonWhitespaceIndentBehaviorType: - PreserveAnything (previously PreserveNonWhitespaceIndent=true): will preserve any implicit indent (like "//" or "#" etc) across subsequent lines under same scope - PreservePosition: doesn't preserve characters (which are not whitespace) but will convert them into equivalent number of spaces, so the starting position is preserved - Trim: will not implicily preserve any indent unless it's only whitespace Added GetImplicitIndent Added virtual modifier to many methods Refs #16 : TrimEnd() was incorrectly recalculating _currentLine, so implicit indent was writing wrong blocks.
1 parent 37df0d1 commit aef7e1b

File tree

5 files changed

+262
-104
lines changed

5 files changed

+262
-104
lines changed

src/Core/CodegenCS.Tests/CoreTests/01-BasicTests.cs

+39-2
Original file line numberDiff line numberDiff line change
@@ -315,9 +315,9 @@ void MyMethod()
315315
}
316316

317317
[Test]
318-
public void AutoIndentSQLComments()
318+
public void AutoIndentSQLComments_PreserveNonWhitespaceIndentBehaviorType_PreserveAnything()
319319
{
320-
_w.PreserveNonWhitespaceIndent = true;
320+
//_w.PreserveNonWhitespaceIndentBehavior = CodegenTextWriter.PreserveNonWhitespaceIndentBehaviorType.PreserveAnything; // default.
321321
string myMultilineBlock = "My\r\nMultiline\r\nstring";
322322
_w.Write($$"""
323323
-- {{myMultilineBlock}}
@@ -331,6 +331,43 @@ INSERT INTO MyTable (...)
331331
""";
332332
Assert.AreEqual(expected, _w.GetContents());
333333
}
334+
335+
[Test]
336+
public void AutoIndentSQLComments_PreserveNonWhitespaceIndentBehaviorType_Trim()
337+
{
338+
_w.PreserveNonWhitespaceIndentBehavior = CodegenTextWriter.PreserveNonWhitespaceIndentBehaviorType.Trim;
339+
string myMultilineBlock = "My\r\nMultiline\r\nstring";
340+
_w.Write($$"""
341+
-- {{myMultilineBlock}}
342+
INSERT INTO MyTable (...)
343+
""");
344+
string expected = $$"""
345+
-- My
346+
Multiline
347+
string
348+
INSERT INTO MyTable (...)
349+
""";
350+
Assert.AreEqual(expected, _w.GetContents());
351+
}
352+
353+
[Test]
354+
public void AutoIndentSQLComments_PreserveNonWhitespaceIndentBehaviorType_()
355+
{
356+
_w.PreserveNonWhitespaceIndentBehavior = CodegenTextWriter.PreserveNonWhitespaceIndentBehaviorType.PreservePosition;
357+
string myMultilineBlock = "My\r\nMultiline\r\nstring";
358+
_w.Write($$"""
359+
-- {{myMultilineBlock}}
360+
INSERT INTO MyTable (...)
361+
""");
362+
string expected = $$"""
363+
-- My
364+
Multiline
365+
string
366+
INSERT INTO MyTable (...)
367+
""";
368+
Assert.AreEqual(expected, _w.GetContents());
369+
}
370+
334371
#endregion
335372

336373
#region Text-Manipulation

src/Core/CodegenCS.Tests/CoreTests/03-WhitespaceControlTests.cs

+70
Original file line numberDiff line numberDiff line change
@@ -136,6 +136,76 @@ My paragraphThe end.
136136
""", _w.GetContents());
137137
}
138138

139+
[Test]
140+
public void TestTLW2()
141+
{
142+
FormattableString code = $"""
143+
var a = 1;
144+
var b = 2;
145+
""";
146+
147+
_w.Write($$"""
148+
public void Test()
149+
{
150+
{{TLW}}{{code}}
151+
}
152+
""");
153+
154+
Assert.AreEqual("""
155+
public void Test()
156+
{var a = 1;
157+
{var b = 2;
158+
}
159+
""", _w.GetContents());
160+
}
161+
162+
[Test]
163+
public void TestTLW3()
164+
{
165+
FormattableString code = $"""
166+
var a = 1;
167+
var b = 2;
168+
""";
169+
170+
_w.PreserveNonWhitespaceIndentBehavior = CodegenTextWriter.PreserveNonWhitespaceIndentBehaviorType.Trim;
171+
_w.Write($$"""
172+
public void Test()
173+
{
174+
{{TLW}}{{code}}
175+
}
176+
""");
177+
178+
Assert.AreEqual("""
179+
public void Test()
180+
{var a = 1;
181+
var b = 2;
182+
}
183+
""", _w.GetContents());
184+
}
185+
186+
[Test]
187+
public void TestTLW4()
188+
{
189+
FormattableString code = $"""
190+
var a = 1;
191+
var b = 2;
192+
""";
193+
194+
_w.PreserveNonWhitespaceIndentBehavior = CodegenTextWriter.PreserveNonWhitespaceIndentBehaviorType.PreservePosition;
195+
_w.Write($$"""
196+
public void Test()
197+
{{{code}}
198+
}
199+
""");
200+
201+
Assert.AreEqual("""
202+
public void Test()
203+
{var a = 1;
204+
var b = 2;
205+
}
206+
""", _w.GetContents());
207+
}
208+
139209
[Test]
140210
public void TestTTW()
141211
{

src/Core/CodegenCS/CodegenCS.Core.csproj

+1-1
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
<Description>Class Library and Toolkit for Code Generation using plain C#</Description>
99
<Copyright>Rick Drizin</Copyright>
1010
<Company>Rick Drizin</Company>
11-
<Version>3.2.2</Version>
11+
<Version>3.3.0</Version>
1212
<DocumentationFile>CodegenCS.xml</DocumentationFile>
1313
<PackageTags>code generation;code generator;templating;codegencs</PackageTags>
1414
<PublishRepositoryUrl>true</PublishRepositoryUrl>

0 commit comments

Comments
 (0)