Skip to content

Commit 7fc25b9

Browse files
committed
Other: Improved 'for' loop formatting.
The bulk of the 'for' expression will now always start on the next line.
1 parent 1377925 commit 7fc25b9

File tree

4 files changed

+24
-5
lines changed

4 files changed

+24
-5
lines changed

ShaderShrinker/Shrinker.Parser/OutputFormatter.cs

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -363,13 +363,26 @@ o.Parent is not VariableDeclarationSyntaxNode && // ...not defined in within a d
363363
break;
364364

365365
case ForSyntaxNode o:
366+
var indent = sb.GetColumn();
366367
sb.Append("for ");
367368
AppendCode(sb, o.LoopSetup);
368-
sb.Append(' ');
369369

370370
var loopCode = new StringBuilder();
371371
AppendCode(loopCode, o.LoopCode);
372-
sb.AppendLine(loopCode.ToString().AllowBraceRemoval());
372+
var braceSection = loopCode.ToString().AllowBraceRemoval();
373+
374+
if (braceSection.StartsWith("{"))
375+
{
376+
sb.Append(' ');
377+
}
378+
else
379+
{
380+
sb.AppendLine();
381+
sb.Append(new string(' ', indent));
382+
sb.Append('\t');
383+
}
384+
385+
sb.AppendLine(braceSection);
373386

374387
if (o.Next != null)
375388
sb.AppendLine();

ShaderShrinker/UnitTests/OutputFormatterTests.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ public void CheckFormattingSingleLineForStatement()
2929
Assert.That(() => parser.Parse(), Throws.Nothing);
3030

3131
var newCode = parser.RootNode.ToCode().Trim();
32-
Assert.That(newCode, Is.EqualTo("for (i = 0; i < 2; i++) continue;"));
32+
Assert.That(newCode, Is.EqualTo("for (i = 0; i < 2; i++)\n\tcontinue;"));
3333
}
3434

3535
[Test]

ShaderShrinker/UnitTests/TestFiles/SimplifiedReference/PowerStone.glsl

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -204,7 +204,10 @@ void mainImage(out vec4 fragColor, vec2 fc) {
204204
col = scene(ro, rayDir(ro, vec3(0, 2, 0), uv));
205205
#ifdef AA
206206
if (fwidth(col.r) > .03) {
207-
for (float dx = Z0; dx <= 1.; dx++) { for (float dy = Z0; dy <= 1.; dy++) col += scene(ro, rayDir(ro, vec3(0, 2, 0), uv + (vec2(dx, dy) - .5) / iResolution.xy)); }
207+
for (float dx = Z0; dx <= 1.; dx++) {
208+
for (float dy = Z0; dy <= 1.; dy++)
209+
col += scene(ro, rayDir(ro, vec3(0, 2, 0), uv + (vec2(dx, dy) - .5) / iResolution.xy));
210+
}
208211

209212
col /= 5.;
210213
}

ShaderShrinker/UnitTests/TestFiles/SimplifiedReference/Start.glsl

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -141,7 +141,10 @@ void mainImage(out vec4 fragColor, vec2 fc) {
141141
col = scene(ro, rayDir(ro, vec3(0, 2, 0), uv));
142142
#ifdef AA
143143
if (fwidth(col.r) > .01) {
144-
for (float dx = Z0; dx <= 1.; dx++) { for (float dy = Z0; dy <= 1.; dy++) col += scene(ro, rayDir(ro, vec3(0, 2, 0), uv + (vec2(dx, dy) - .5) / iResolution.xy)); }
144+
for (float dx = Z0; dx <= 1.; dx++) {
145+
for (float dy = Z0; dy <= 1.; dy++)
146+
col += scene(ro, rayDir(ro, vec3(0, 2, 0), uv + (vec2(dx, dy) - .5) / iResolution.xy));
147+
}
145148

146149
col /= 5.;
147150
}

0 commit comments

Comments
 (0)