Skip to content

Commit 267a366

Browse files
committed
Merge pull request #2477 from AmnonOwed/Fix#364-AutoFormatIfElse
Fix #364 if-else autoformatting
2 parents 48090f6 + fa32397 commit 267a366

1 file changed

Lines changed: 22 additions & 8 deletions

File tree

app/src/processing/mode/java/AutoFormat.java

Lines changed: 22 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -133,7 +133,7 @@ private char get_string() {
133133
private void writeIndentedLine() {
134134
if (buf.length() == 0) {
135135
if (s_flag) {
136-
s_flag = a_flg = false;
136+
s_flag = a_flg = e_flg = false;
137137
}
138138
return;
139139
}
@@ -150,9 +150,26 @@ private void writeIndentedLine() {
150150
}
151151
a_flg = false;
152152
}
153+
if (e_flg) {
154+
if (lastNonSpaceChar() == '}') {
155+
trimRight(result);
156+
result.append(" ");
157+
}
158+
e_flg = false;
159+
}
153160
result.append(buf);
154161
buf.setLength(0);
155162
}
163+
164+
165+
private char lastNonSpaceChar() {
166+
for (int i=result.length()-1; i>=0; i--) {
167+
char c_i = result.charAt(i);
168+
if (c_i == ' ' || c_i == '\n') continue;
169+
else return c_i;
170+
}
171+
return 0;
172+
}
156173

157174

158175
private void writeIndentedComment() {
@@ -371,16 +388,12 @@ public String format(final String source) {
371388

372389
case ' ':
373390
case '\t':
374-
if (lookup("else")) {
391+
e_flg = lookup("else");
392+
if (e_flg) {
375393
gotelse();
376394
if ((!s_flag) || buf.length() > 0) {
377395
buf.append(c);
378396
}
379-
// // issue https://github.com/processing/processing/issues/364
380-
// s_flag = false;
381-
// trimRight(result);
382-
// result.append(" ");
383-
384397
writeIndentedLine();
385398
s_flag = false;
386399
break;
@@ -418,7 +431,8 @@ public String format(final String source) {
418431
break;
419432

420433
case '{':
421-
if (lookup("else")) {
434+
e_flg = lookup("else");
435+
if (e_flg) {
422436
gotelse();
423437
}
424438
if (s_if_lev.length == c_level) {

0 commit comments

Comments
 (0)