Skip to content

Commit b06f563

Browse files
author
jdf
committed
Fix and regression test #405.
1 parent e1e204f commit b06f563

4 files changed

Lines changed: 24 additions & 7 deletions

File tree

app/src/processing/app/format/AutoFormat.java

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -301,7 +301,6 @@ private boolean lookup_com(final String keyword) {
301301
}
302302

303303
public String format(final String source) {
304-
// Adding an additional newline as a hack around other errors
305304
final String normalizedText = source.replaceAll("\r", "");
306305
final String cleanText = normalizedText
307306
+ (normalizedText.endsWith("\n") ? "" : "\n");
@@ -592,10 +591,20 @@ public String format(final String source) {
592591
case '(':
593592
castFlags.push(Pattern.matches("^.*?(?:int|color|float)\\s*$", buf));
594593

594+
final boolean isFor = lookup("for");
595+
final boolean isIf = lookup("if");
596+
597+
if (isFor || isIf || lookup("while")) {
598+
if (!Character.isWhitespace(buf.charAt(buf.length() - 1))) {
599+
buf.append(' ');
600+
}
601+
}
602+
595603
buf.append(c);
596604
paren++;
597605

598-
if ((lookup("for"))) {
606+
if (isFor) {
607+
// TODO(feinberg): handle new-style for loops
599608
c = get_string();
600609
while (c != ';' && c != ':') {
601610
c = get_string();
@@ -618,8 +627,7 @@ public String format(final String source) {
618627
} // endwhile for_done
619628
paren--;
620629
if (paren < 0) {
621-
paren = 0;//EOF = true;
622-
//System.out.println("eof d");
630+
paren = 0;
623631
}
624632
writeIndentedLine();
625633
if (getnl()) {
@@ -629,9 +637,7 @@ public String format(final String source) {
629637
ind[level] = 0;
630638
}
631639
break;
632-
}
633-
634-
if (lookup("if")) {
640+
} else if (isIf) {
635641
writeIndentedLine();
636642
s_tabs[c_level][if_lev] = tabs;
637643
sp_flg[c_level][if_lev] = p_flg[level];

app/test/resources/bug405.expected

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
for (int i : new int[] {1,2,3}) {
2+
println(i);
3+
}

app/test/resources/bug405.pde

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
for(int i : new int[] {1,2,3}) {
2+
println(i);
3+
}

app/test/src/test/processing/parsing/AutoFormatTests.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,11 @@ public void bug109() {
5151
expectGood("bug109");
5252
}
5353

54+
@Test
55+
public void bug405() {
56+
expectGood("bug405");
57+
}
58+
5459
@Test
5560
public void bug420() {
5661
expectGood("bug420");

0 commit comments

Comments
 (0)