Drizin / CodegenCS

C# Toolkit for Code Generation (T4 alternative!)
MIT License
223 stars 30 forks source link

Unexpected behavior when rendering multiline items #24

Closed obirider closed 8 months ago

obirider commented 8 months ago
        List<FormattableString> list = new List<FormattableString>()
        {
            $$"""
            Multi line 1{{IF(true)}}
            Multi line 2{{ENDIF}}
            Multi line 3
            """,

            $"Single line",
        };

        var textWriter = new CodegenTextWriter();
        textWriter.Write($"{list.Select(x => (FormattableString)$"Value start: {x}")}");
        string result = textWriter.ToString();

result:

Value start: Multi line 1
Value start: Multi line 2
Value start: Multi line 3

Value start: Single line

expected result:

Value start: Multi line 1
Multi line 2
Multi line 3

Value start: Single line

Removing the explicit cast to FormattableString results in adding Value start: as expected but the symbols IF and ENDIF are not resolved.

What can I do to generate the expected result?

obirider commented 8 months ago

It seems to be a duplicate of that comment and can be "fixed" by setting codegenTextWriter.PreserveNonWhitespaceIndentBehavior = CodegenTextWriter.PreserveNonWhitespaceIndentBehaviorType.Trim or codegenTextWriter.PreserveNonWhitespaceIndentBehavior = CodegenTextWriter.PreserveNonWhitespaceIndentBehaviorType.PreservePosition