From 6146888909f5234662e349ca2422d73ff538de12 Mon Sep 17 00:00:00 2001 From: AmnonOwed Date: Wed, 23 Apr 2014 18:51:47 +0200 Subject: [PATCH 1/3] Fix if-else autoformatting --- app/src/processing/mode/java/AutoFormat.java | 25 ++++++++++++++++---- 1 file changed, 21 insertions(+), 4 deletions(-) diff --git a/app/src/processing/mode/java/AutoFormat.java b/app/src/processing/mode/java/AutoFormat.java index f7a86a0bf..3f576ab13 100644 --- a/app/src/processing/mode/java/AutoFormat.java +++ b/app/src/processing/mode/java/AutoFormat.java @@ -133,7 +133,7 @@ public class AutoFormat implements Formatter { private void writeIndentedLine() { if (buf.length() == 0) { if (s_flag) { - s_flag = a_flg = false; + s_flag = a_flg = e_flg = false; } return; } @@ -150,9 +150,25 @@ public class AutoFormat implements Formatter { } a_flg = false; } + if (e_flg) { + if (lastNonSpaceChar() == '}') { + trimRight(result); + result.append(" "); + } + } result.append(buf); buf.setLength(0); } + + + private char lastNonSpaceChar() { + for (int i=result.length()-1; i>=0; i--) { + char c_i = result.charAt(i); + if (c_i == ' ' || c_i == '\n') continue; + else return c_i; + } + return 0; + } private void writeIndentedComment() { @@ -371,7 +387,8 @@ public class AutoFormat implements Formatter { case ' ': case '\t': - if (lookup("else")) { + e_flg = lookup("else"); + if (e_flg) { gotelse(); if ((!s_flag) || buf.length() > 0) { buf.append(c); @@ -380,7 +397,6 @@ public class AutoFormat implements Formatter { // s_flag = false; // trimRight(result); // result.append(" "); - writeIndentedLine(); s_flag = false; break; @@ -418,7 +434,8 @@ public class AutoFormat implements Formatter { break; case '{': - if (lookup("else")) { + e_flg = lookup("else"); + if (e_flg) { gotelse(); } if (s_if_lev.length == c_level) { From dc5571a75f23fe20bccb30c3dc382dc0e0faaa2b Mon Sep 17 00:00:00 2001 From: AmnonOwed Date: Wed, 23 Apr 2014 19:34:58 +0200 Subject: [PATCH 2/3] Cleanup --- app/src/processing/mode/java/AutoFormat.java | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-) diff --git a/app/src/processing/mode/java/AutoFormat.java b/app/src/processing/mode/java/AutoFormat.java index 3f576ab13..e60671bf5 100644 --- a/app/src/processing/mode/java/AutoFormat.java +++ b/app/src/processing/mode/java/AutoFormat.java @@ -154,6 +154,7 @@ public class AutoFormat implements Formatter { if (lastNonSpaceChar() == '}') { trimRight(result); result.append(" "); + e_flg = false; } } result.append(buf); @@ -393,10 +394,6 @@ public class AutoFormat implements Formatter { if ((!s_flag) || buf.length() > 0) { buf.append(c); } -// // issue https://github.com/processing/processing/issues/364 -// s_flag = false; -// trimRight(result); -// result.append(" "); writeIndentedLine(); s_flag = false; break; From b6da3317fb5c482e942a65de7e93ab43b74e9262 Mon Sep 17 00:00:00 2001 From: AmnonOwed Date: Wed, 23 Apr 2014 19:39:48 +0200 Subject: [PATCH 3/3] Moving flag reset --- app/src/processing/mode/java/AutoFormat.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/app/src/processing/mode/java/AutoFormat.java b/app/src/processing/mode/java/AutoFormat.java index e60671bf5..935e2a903 100644 --- a/app/src/processing/mode/java/AutoFormat.java +++ b/app/src/processing/mode/java/AutoFormat.java @@ -154,8 +154,8 @@ public class AutoFormat implements Formatter { if (lastNonSpaceChar() == '}') { trimRight(result); result.append(" "); - e_flg = false; } + e_flg = false; } result.append(buf); buf.setLength(0);