Skip to content

Commit 64f68fa

Browse files
committed
Reducing interpolation code
1 parent f6f3b70 commit 64f68fa

File tree

2 files changed

+28
-42
lines changed

2 files changed

+28
-42
lines changed

src/AutoCtor.Shared/Helpers/CodeBuilder.InterpolatedStringHandler.cs

Lines changed: 22 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -11,23 +11,23 @@ public CodeBuilder Append(
1111

1212
public CodeBuilder Append(bool enabled,
1313
[InterpolatedStringHandlerArgument("", nameof(enabled))]
14-
ref ConditionalCodeBuilderInterpolatedStringHandler builder) => this;
14+
ref CodeBuilderInterpolatedStringHandler builder) => this;
1515

1616
public CodeBuilder AppendLineRaw(
1717
[InterpolatedStringHandlerArgument("")]
1818
ref CodeBuilderInterpolatedStringHandler builder) => AppendLine();
1919

2020
public CodeBuilder AppendLineRaw(bool enabled,
2121
[InterpolatedStringHandlerArgument("", nameof(enabled))]
22-
ref ConditionalCodeBuilderInterpolatedStringHandler builder) => enabled ? AppendLine() : this;
22+
ref CodeBuilderInterpolatedStringHandler builder) => enabled ? AppendLine() : this;
2323

2424
public CodeBuilder AppendLine(
2525
[InterpolatedStringHandlerArgument("")]
2626
IndentedCodeBuilderInterpolatedStringHandler builder) => AppendLine();
2727

2828
public CodeBuilder AppendLine(bool enabled,
2929
[InterpolatedStringHandlerArgument("", nameof(enabled))]
30-
ConditionalIndentedCodeBuilderInterpolatedStringHandler builder) => enabled ? AppendLine() : this;
30+
IndentedCodeBuilderInterpolatedStringHandler builder) => enabled ? AppendLine() : this;
3131

3232
private void AppendFormatted(IEnumerable<string> items, string? format)
3333
{
@@ -76,51 +76,37 @@ private void AppendCommaIndented(IEnumerable<string> items)
7676

7777
[InterpolatedStringHandler]
7878
internal readonly struct CodeBuilderInterpolatedStringHandler(
79-
int literalLength, int formattedCount, CodeBuilder codeBuilder)
80-
{
81-
public readonly void AppendLiteral(string s) => codeBuilder.Append(s);
82-
public readonly void AppendFormatted(string s) => codeBuilder.Append(s);
83-
public readonly void AppendFormatted(IEnumerable<string> items, string? format)
84-
=> codeBuilder.AppendFormatted(items, format);
85-
}
86-
87-
[InterpolatedStringHandler]
88-
internal readonly struct ConditionalCodeBuilderInterpolatedStringHandler(
89-
int literalLength, int formattedCount, CodeBuilder codeBuilder, bool enabled)
79+
int literalLength, int formattedCount, CodeBuilder codeBuilder, bool enabled = true)
9080
{
91-
public readonly bool AppendLiteral(string s) { if (enabled) codeBuilder.Append(s); return enabled; }
92-
public readonly bool AppendFormatted(string s) { if (enabled) codeBuilder.Append(s); return enabled; }
81+
public readonly bool AppendLiteral(string s)
82+
{ if (enabled) codeBuilder.Append(s); return enabled; }
83+
public readonly bool AppendFormatted(string s)
84+
{ if (enabled) codeBuilder.Append(s); return enabled; }
9385
public readonly bool AppendFormatted(IEnumerable<string> items, string? format)
9486
{ if (enabled) codeBuilder.AppendFormatted(items, format); return enabled; }
9587
}
9688

9789
[InterpolatedStringHandler]
9890
internal class IndentedCodeBuilderInterpolatedStringHandler(
99-
int literalLength, int formattedCount, CodeBuilder codeBuilder)
91+
int literalLength, int formattedCount, CodeBuilder codeBuilder, bool enabled = true)
10092
{
10193
private bool _hasIndented;
10294

103-
private void EnsureIndent()
95+
private CodeBuilder EnsureIndent()
10496
{
105-
if (_hasIndented) return;
106-
codeBuilder.AppendIndent();
107-
_hasIndented = true;
97+
if (!_hasIndented)
98+
{
99+
codeBuilder.AppendIndent();
100+
_hasIndented = true;
101+
}
102+
return codeBuilder;
108103
}
109104

110-
public void AppendLiteral(string s) { EnsureIndent(); codeBuilder.Append(s); }
111-
public void AppendFormatted(string s) { EnsureIndent(); codeBuilder.Append(s); }
112-
public void AppendFormatted(IEnumerable<string> items, string? format)
113-
{ EnsureIndent(); codeBuilder.AppendFormatted(items, format); }
114-
}
115-
116-
[InterpolatedStringHandler]
117-
internal class ConditionalIndentedCodeBuilderInterpolatedStringHandler(
118-
int literalLength, int formattedCount, CodeBuilder codeBuilder, bool enabled)
119-
: IndentedCodeBuilderInterpolatedStringHandler(literalLength, formattedCount, codeBuilder)
120-
{
121-
public new bool AppendLiteral(string s) { if (enabled) base.AppendLiteral(s); return enabled; }
122-
public new bool AppendFormatted(string s) { if (enabled) base.AppendFormatted(s); return enabled; }
123-
public new bool AppendFormatted(IEnumerable<string> items, string? format)
124-
{ if (enabled) base.AppendFormatted(items, format); return enabled; }
105+
public bool AppendLiteral(string s)
106+
{ if (enabled) EnsureIndent().Append(s); return enabled; }
107+
public bool AppendFormatted(string s)
108+
{ if (enabled) EnsureIndent().Append(s); return enabled; }
109+
public bool AppendFormatted(IEnumerable<string> items, string? format)
110+
{ if (enabled) EnsureIndent().AppendFormatted(items, format); return enabled; }
125111
}
126112
}

src/AutoCtor.Shared/Helpers/CodeBuilder.cs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -12,13 +12,13 @@ internal partial class CodeBuilder
1212
public CodeBuilder IncreaseIndent() { _indent++; return this; }
1313
public CodeBuilder DecreaseIndent() { if (_indent > 0) _indent--; return this; }
1414

15-
public CodeBuilder Append(string text) { _stringBuilder.Append(text); return this; }
16-
public CodeBuilder Append(bool enabled, string text) { if (enabled) _stringBuilder.Append(text); return this; }
15+
public CodeBuilder Append(string value) { _stringBuilder.Append(value); return this; }
16+
public CodeBuilder Append(bool enabled, string value) { if (enabled) _stringBuilder.Append(value); return this; }
1717
public CodeBuilder AppendLine() { _stringBuilder.AppendLine(); return this; }
18-
public CodeBuilder AppendLine(string line) { _stringBuilder.AppendLine(Indent + line); return this; }
19-
public CodeBuilder AppendLine(bool enabled, string line) { if (enabled) _stringBuilder.AppendLine(Indent + line); return this; }
20-
public CodeBuilder AppendLineRaw(string line) { _stringBuilder.AppendLine(line); return this; }
21-
public CodeBuilder AppendLineRaw(bool enabled, string line) { if (enabled) _stringBuilder.AppendLine(line); return this; }
18+
public CodeBuilder AppendLine(string value) { _stringBuilder.AppendLine(Indent + value); return this; }
19+
public CodeBuilder AppendLine(bool enabled, string value) { if (enabled) _stringBuilder.AppendLine(Indent + value); return this; }
20+
public CodeBuilder AppendLineRaw(string value) { _stringBuilder.AppendLine(value); return this; }
21+
public CodeBuilder AppendLineRaw(bool enabled, string value) { if (enabled) _stringBuilder.AppendLine(value); return this; }
2222
public CodeBuilder AppendIndent() { _stringBuilder.Append(Indent); return this; }
2323

2424
public static implicit operator SourceText(CodeBuilder codeBuilder)

0 commit comments

Comments
 (0)