Multiline string indent control does not work
See original GitHub issueHi there,
First: Nice project, it reduced the code for my CodeGenerator from about 1200 very confusing lines to about 700 clearly readable lines. 😃
But I need to write some multiline strings, but they cause problems. A few examples:
using var stringWriter = new StringWriter();
var codeWriter = new CodegenTextWriter(stringWriter);
var s = @"a
empty:
c
empty:
e";
codeWriter.WriteLine($$"""
namespace N
{
public class A
{
{{TestMethod(s, "Test1", StringToCode1)}}
{{TestMethod(s, "Test2", StringToCode2)}}
{{TestMethod(s, "Test3", StringToCode3)}}
{{TestMethod(s, "Test4", StringToCode4)}}
{{TestMethod(s, "Test5", StringToCode5)}}
{{TestMethod(s, "Test_Workaround", StringToCode_Workaround)}}
// All tests end
}
}
""");
codeWriter.Flush();
Console.WriteLine(stringWriter.ToString());
static FormattableString TestMethod(string s, string name, Func<string, object> getStringCode)
{
return $$"""
public static string {{name}}()
{
return {{getStringCode(s)}};
}
// {{name}} end
""";
}
static object StringToCode1(string s)
=> $"\"{s}\"";
static FormattableString StringToCode2(string s)
=> $$"""
@"{{s}}"
""";
static FormattableString StringToCode3(string s)
=> $$"""
@"{{s.Split(new[] { "\r\n", "\r", "\n" }, StringSplitOptions.None)}}"
""";
static FormattableString StringToCode4(string s)
{
var lines = s
.Split(new[] { "\r\n", "\r", "\n" }, StringSplitOptions.None)
.Select<string, FormattableString>(l => $$"""{{l}}""");
return $$"""
@"{{lines}}"
""";
}
static FormattableString StringToCode5(string s)
{
var lines = s
.Split(new[] { "\r\n", "\r", "\n" }, StringSplitOptions.None)
.Select<string, FormattableString>(l => $$"""{{TTW}}{{l}}""");
return $$"""
@"{{lines}}"
""";
}
static Action<ICodegenTextWriter> StringToCode_Workaround(string s)
=> (ICodegenTextWriter writer) =>
{
var lines = s
.Split(new[] { "\r\n", "\r", "\n" }, StringSplitOptions.None);
var indentLevel = writer.IndentLevel;
for (int i = 0; i < indentLevel; i++)
writer.DecreaseIndent();
writer.Write($"@\"{lines[0]}");
foreach (var line in lines.Skip(1))
{
writer.WriteLine();
writer.Write(line);
}
writer.Write("\"");
for (int i = 0; i < indentLevel; i++)
writer.IncreaseIndent();
};
Das Ergebnis:
namespace N
{
public class A
{
public static string Test1()
{
return "a
return empty:
return c
return empty:
return e";
}
// Test1 end
public static string Test2()
{
return @"a
return @"empty:
return @"c
return @"empty:
return @"e";
}
// Test2 end
public static string Test3()
{
return @"a
return @"empty:
return @"c
return @"empty:
return @"e
return ";
}
// Test3 end
public static string Test4()
{
return @"a
return @"empty:
return @"c
return @"empty:
return @"e
return ";
}
// Test4 end
public static string Test5()
{
return @"a
return @"empty:
return @"c
return @"empty:
return @"e
return ";
}
// Test5 end
public static string Test_Workaround()
{
return @"a
empty:
c
empty:
e";
}
// Test_Workaround end
// All tests end
}
}
The last method is my workaround, for my purposes it works, but it messes up the indentation of the following lines, as you can see from the comments. I also had another problem where the string was written without a duplicated return, but the indentation of the new lines was messed up. However, I could not reproduce the problem in the small sample code.
For me this TODO would solve the problem, then I can split the string and put all following lines at the beginning of the line, but this would then no longer be automatic indent control?
Best greetings
Issue Analytics
- State:
- Created 5 months ago
- Comments:13 (6 by maintainers)
Top GitHub Comments
There it is 😃
However, I think it’s still a bug that it duplicates the text of the same line for multi-line strings.
Everything works for me - thank you 😃