diff --git a/app/src/processing/app/tools/AutoFormat.java b/app/src/processing/app/tools/AutoFormat.java index 9fe956194..7ce39dbcb 100644 --- a/app/src/processing/app/tools/AutoFormat.java +++ b/app/src/processing/app/tools/AutoFormat.java @@ -24,12 +24,12 @@ package processing.app.tools; -import processing.app.*; +import java.io.CharArrayReader; +import java.io.IOException; +import processing.app.Editor; +import processing.app.Preferences; import processing.core.PApplet; -import java.io.*; - - /** * Handler for dealing with auto format. * Contributed by Martin Gomez, additional bug fixes by Ben Fry. @@ -81,12 +81,10 @@ public class AutoFormat implements Tool { String line_feed; - - public void init(Editor editor) { + public void init(final Editor editor) { this.editor = editor; } - public String getMenuTitle() { return "Auto Format"; } @@ -100,7 +98,7 @@ public class AutoFormat implements Tool { while (done == 0) { c = buf[j++] = getchr(); while ((c != '/') && (j < buf.length)) { - if(c == '\n' || c == '\r') { + if (c == '\n' || c == '\r') { lineNumber++; putcoms(); s_flg = 1; @@ -108,7 +106,7 @@ public class AutoFormat implements Tool { c = buf[j++] = getchr(); } //String tmpstr = new String(string); - if (j>1 && buf[j-2] == '*') { + if (j > 1 && buf[j - 2] == '*') { done = 1; jdoc = 0; } @@ -120,7 +118,6 @@ public class AutoFormat implements Tool { return; } - private char get_string() throws IOException { char ch; ch = '*'; @@ -135,7 +132,9 @@ public class AutoFormat implements Tool { if (ch == '\'' || ch == '"') { cc = buf[j++] = getchr(); while (!EOF && cc != ch) { - if (cc == '\\') buf[j++] = getchr(); + if (cc == '\\') { + buf[j++] = getchr(); + } cc = buf[j++] = getchr(); } break; @@ -145,18 +144,17 @@ public class AutoFormat implements Tool { a_flg = 1; break; } else { - return(ch); + return (ch); } } } } - private void indent_puts() { buf[j] = '\0'; if (j > 0) { if (s_flg != 0) { - if((tabs > 0) && (buf[0] != '{') && (a_flg == 1)) { + if ((tabs > 0) && (buf[0] != '{') && (a_flg == 1)) { tabs++; } p_tabs(); @@ -166,7 +164,7 @@ public class AutoFormat implements Tool { } a_flg = 0; } - strOut.append(buf,0,j); + strOut.append(buf, 0, j); j = 0; } else { @@ -177,65 +175,56 @@ public class AutoFormat implements Tool { } } - //public void fprintf(int outfil, String out_string) { - private void fprintf(String out_string) { + private void fprintf(final String out_string) { //int out_len = out_string.length(); //String j_string = new String(string); strOut.append(out_string); } - /* special edition of put string for comment processing */ - private void putcoms() - { + private void putcoms() { int i = 0; - int sav_s_flg = s_flg; - if(j > 0) - { - if(s_flg != 0) - { + final int sav_s_flg = s_flg; + if (j > 0) { + if (s_flg != 0) { p_tabs(); s_flg = 0; } buf[j] = '\0'; i = 0; - while (buf[i] == ' ') i++; - if (lookup_com(w_jdoc) == 1) jdoc = 1; - String strBuffer = new String(buf,0,j); - if (buf[i] == '/' && buf[i+1]=='*') - { - if ((last_char != ';') && (sav_s_flg==1) ) - { + while (buf[i] == ' ') { + i++; + } + if (lookup_com(w_jdoc) == 1) { + jdoc = 1; + } + final String strBuffer = new String(buf, 0, j); + if (buf[i] == '/' && buf[i + 1] == '*') { + if ((last_char != ';') && (sav_s_flg == 1)) { //fprintf(outfil, strBuffer.substring(i,j)); - fprintf(strBuffer.substring(i,j)); - } - else - { + fprintf(strBuffer.substring(i, j)); + } else { //fprintf(outfil, strBuffer); fprintf(strBuffer); } - } - else - { - if (buf[i]=='*' || jdoc == 0) + } else { + if (buf[i] == '*' || jdoc == 0) { //fprintf (outfil, " "+strBuffer.substring(i,j)); - fprintf (" "+strBuffer.substring(i,j)); - else + fprintf(" " + strBuffer.substring(i, j)); + } else { //fprintf (outfil, " * "+strBuffer.substring(i,j)); - fprintf (" * "+strBuffer.substring(i,j)); + fprintf(" * " + strBuffer.substring(i, j)); + } } j = 0; buf[0] = '\0'; } } - - private void cpp_comment() throws IOException - { + private void cpp_comment() throws IOException { c = getchr(); - while(c != '\n' && c != '\r' && j 0) /* char was read previously */ + if (peek > 0) /* char was read previously */ { last_char = peekc; peek = -1; - } - else /* read next char in string */ + } else /* read next char in string */ { indexBlock++; - if (indexBlock >= lineLength) - { + if (indexBlock >= lineLength) { lineLength = readCount = 0; reader.mark(1); - if (reader.read() != -1) - { - reader.reset(); // back to the mark + if (reader.read() != -1) { + reader.reset(); // back to the mark readCount = reader.read(chars); lineLength = readCount; strBlock = new String(chars); @@ -290,21 +276,16 @@ public class AutoFormat implements Tool { last_char = strBlock.charAt(indexBlock); peek = -1; peekc = '`'; - } - else - { + } else { EOF = true; - peekc = '\0'; + peekc = '\0'; } - } - else - { + } else { last_char = strBlock.charAt(indexBlock); } } peek = -1; - if (last_char == '\r') - { + if (last_char == '\r') { last_char = getchr(); } @@ -312,8 +293,7 @@ public class AutoFormat implements Tool { } /* else processing */ - private void gotelse() - { + private void gotelse() { tabs = s_tabs[c_level][if_lev]; p_flg[level] = sp_flg[c_level][if_lev]; ind[level] = s_ind[c_level][if_lev]; @@ -321,8 +301,7 @@ public class AutoFormat implements Tool { } /* read to new_line */ - private int getnl() throws IOException - { + private int getnl() throws IOException { int save_s_flg; save_s_flg = tabs; peekc = getchr(); @@ -337,95 +316,97 @@ public class AutoFormat implements Tool { } peek = 1; - if (peekc == '/') - { + if (peekc == '/') { peek = -1; peekc = '`'; peekc = getchr(); - if (peekc == '*') - { + if (peekc == '*') { buf[j++] = '/'; buf[j++] = '*'; peek = -1; peekc = '`'; comment(); - } - else if (peekc == '/') - { + } else if (peekc == '/') { buf[j++] = '/'; buf[j++] = '/'; peek = -1; peekc = '`'; cpp_comment(); return (1); - } - else - { + } else { buf[j++] = '/'; peek = 1; } } peekc = getchr(); - if(peekc == '\n') - { + if (peekc == '\n') { lineNumber++; peek = -1; peekc = '`'; tabs = save_s_flg; - return(1); - } - else - { + return (1); + } else { peek = 1; } return 0; } - private boolean lookup (String keyword) - { + private boolean lookup(final String keyword) { char r; - int l,kk; //,k,i; - if (j<1) return false; - kk=0; - while(buf[kk] == ' ')kk++; - l=0; - l = new String(buf).indexOf(keyword); - if (l<0 || l!=kk) - { + int l, kk; //,k,i; + if (j < 1) { + return false; + } + kk = 0; + while (buf[kk] == ' ') { + kk++; + } + l = 0; + l = new String(buf).indexOf(keyword); + if (l < 0 || l != kk) { + return false; + } + r = buf[kk + keyword.length()]; + if (r >= 'a' && r <= 'z') { + return false; + } + if (r >= 'A' && r <= 'Z') { + return false; + } + if (r >= '0' && r <= '9') { + return false; + } + if (r == '_' || r == '&') { return false; } - r = buf[kk+keyword.length()]; - if(r >= 'a' && r <= 'z') return false; - if(r >= 'A' && r <= 'Z') return false; - if(r >= '0' && r <= '9') return false; - if(r == '_' || r == '&') return false; return true; } - private int lookup_com (String keyword) - { + private int lookup_com(final String keyword) { //char r; - int l,kk; //,k,i; - String j_string = new String(buf); + int l, kk; //,k,i; + final String j_string = new String(buf); - if (j<1) return (0); - kk=0; - while(buf[kk] == ' ')kk++; - l=0; + if (j < 1) { + return (0); + } + kk = 0; + while (buf[kk] == ' ') { + kk++; + } + l = 0; l = j_string.indexOf(keyword); - if (l<0 || l!=kk) - { + if (l < 0 || l != kk) { return 0; } return (1); } - public void run() { StringBuffer onechar; // Adding an additional newline as a hack around other errors - String originalText = editor.getText() + "\n"; + final String originalText = editor.getText() + "\n"; strOut = new StringBuffer(); indentValue = Preferences.getInteger("editor.tabs.size"); indentChar = new String(" "); @@ -439,30 +420,30 @@ public class AutoFormat implements Tool { s_flg = 1; jdoc = 0; - s_level = new int[10]; - sp_flg = new int[20][10]; - s_ind = new int[20][10]; + s_level = new int[10]; + sp_flg = new int[20][10]; + s_ind = new int[20][10]; s_if_lev = new int[10]; s_if_flg = new int[10]; - ind = new int[10]; - p_flg = new int[10]; - s_tabs = new int[20][10]; + ind = new int[10]; + p_flg = new int[10]; + s_tabs = new int[20][10]; w_else = "else"; - w_if_ = new String ("if"); - w_for = new String ("for"); - w_ds = new String ("default"); - w_case = new String ("case"); - w_cpp_comment = new String ("//"); - w_jdoc = new String ("/**"); - line_feed = new String ("\n"); + w_if_ = new String("if"); + w_for = new String("for"); + w_ds = new String("default"); + w_case = new String("case"); + w_cpp_comment = new String("//"); + w_jdoc = new String("/**"); + line_feed = new String("\n"); // read as long as there is something to read - EOF = false; // = 1 set in getchr when EOF + EOF = false; // = 1 set in getchr when EOF chars = new char[BLOCK_MAXLEN]; buf = new char[BLOCK_MAXLEN]; - try { // the whole process + try { // the whole process // open for input reader = new CharArrayReader(originalText.toCharArray()); lineLength = readCount = 0; @@ -471,50 +452,49 @@ public class AutoFormat implements Tool { strBlock = new String(chars); lineLength = readCount; - lineNumber = 1; + lineNumber = 1; indexBlock = -1; j = 0; - while (!EOF) - { + while (!EOF) { c = getchr(); - switch(c) - { + switch (c) { default: buf[j++] = c; - if(c != ',') - { + if (c != ',') { l_char = c; } break; case ' ': case '\t': - if(lookup(w_else)) - { + if (lookup(w_else)) { gotelse(); - if(s_flg == 0 || j > 0)buf[j++] = c; + if (s_flg == 0 || j > 0) { + buf[j++] = c; + } indent_puts(); s_flg = 0; break; } - if(s_flg == 0 || j > 0)buf[j++] = c; + if (s_flg == 0 || j > 0) { + buf[j++] = c; + } break; - case '\r': // for MS Windows 95 + case '\r': // for MS Windows 95 case '\n': lineNumber++; - if (EOF) - { + if (EOF) { break; } //String j_string = new String(string); e_flg = lookup(w_else); - if(e_flg) gotelse(); - if (lookup_com(w_cpp_comment) == 1) - { - if (buf[j] == '\n') - { + if (e_flg) { + gotelse(); + } + if (lookup_com(w_cpp_comment) == 1) { + if (buf[j] == '\n') { buf[j] = '\0'; j--; } @@ -524,20 +504,18 @@ public class AutoFormat implements Tool { //fprintf(outfil, line_feed); fprintf(line_feed); s_flg = 1; - if(e_flg) - { + if (e_flg) { p_flg[level]++; tabs++; + } else if (p_char == l_char) { + a_flg = 1; } - else - if(p_char == l_char) - { - a_flg = 1; - } break; case '{': - if(lookup(w_else))gotelse(); + if (lookup(w_else)) { + gotelse(); + } if (s_if_lev.length == c_level) { s_if_lev = PApplet.expand(s_if_lev); s_if_flg = PApplet.expand(s_if_flg); @@ -546,21 +524,19 @@ public class AutoFormat implements Tool { s_if_flg[c_level] = if_flg; if_lev = if_flg = 0; c_level++; - if(s_flg == 1 && p_flg[level] != 0) - { + if (s_flg == 1 && p_flg[level] != 0) { p_flg[level]--; tabs--; } buf[j++] = c; indent_puts(); - getnl() ; + getnl(); indent_puts(); //fprintf(outfil,"\n"); fprintf("\n"); tabs++; s_flg = 1; - if(p_flg[level] > 0) - { + if (p_flg[level] > 0) { ind[level] = 1; level++; s_level[level] = c_level; @@ -569,8 +545,7 @@ public class AutoFormat implements Tool { case '}': c_level--; - if (c_level < 0) - { + if (c_level < 0) { c_level = 0; //EOF = true; //System.out.println("eof b"); @@ -578,25 +553,23 @@ public class AutoFormat implements Tool { indent_puts(); break; } - if ((if_lev = s_if_lev[c_level]-1) < 0) + if ((if_lev = s_if_lev[c_level] - 1) < 0) { if_lev = 0; + } if_flg = s_if_flg[c_level]; indent_puts(); tabs--; p_tabs(); peekc = getchr(); - if( peekc == ';') - { + if (peekc == ';') { onechar = new StringBuffer(); - onechar.append(c); // the } + onechar.append(c); // the } onechar.append(';'); //fprintf(outfil, onechar.toString()); fprintf(onechar.toString()); peek = -1; peekc = '`'; - } - else - { + } else { onechar = new StringBuffer(); onechar.append(c); //fprintf(outfil, onechar.toString()); @@ -608,10 +581,12 @@ public class AutoFormat implements Tool { //fprintf(outfil,"\n"); fprintf("\n"); s_flg = 1; - if(c_level < s_level[level]) - if(level > 0) level--; - if(ind[level] != 0) - { + if (c_level < s_level[level]) { + if (level > 0) { + level--; + } + } + if (ind[level] != 0) { tabs -= p_flg[level]; p_flg[level] = 0; ind[level] = 0; @@ -622,17 +597,14 @@ public class AutoFormat implements Tool { case '\'': buf[j++] = c; cc = getchr(); - while(!EOF && cc != c) - { + while (!EOF && cc != c) { // max. length of line should be 256 buf[j++] = cc; - if(cc == '\\') - { + if (cc == '\\') { cc = buf[j++] = getchr(); } - if(cc == '\n') - { + if (cc == '\n') { lineNumber++; indent_puts(); s_flg = 1; @@ -641,8 +613,7 @@ public class AutoFormat implements Tool { } buf[j++] = cc; - if(getnl() == 1) - { + if (getnl() == 1) { l_char = cc; peek = 1; peekc = '\n'; @@ -652,8 +623,7 @@ public class AutoFormat implements Tool { case ';': buf[j++] = c; indent_puts(); - if(p_flg[level] > 0 && ind[level] == 0) - { + if (p_flg[level] > 0 && ind[level] == 0) { tabs -= p_flg[level]; p_flg[level] = 0; } @@ -662,13 +632,14 @@ public class AutoFormat implements Tool { //fprintf(outfil,"\n"); fprintf("\n"); s_flg = 1; - if(if_lev > 0) - if(if_flg == 1) - { + if (if_lev > 0) { + if (if_flg == 1) { if_lev--; if_flg = 0; + } else { + if_lev = 0; } - else if_lev = 0; + } break; case '\\': @@ -684,46 +655,36 @@ public class AutoFormat implements Tool { case ':': buf[j++] = c; peekc = getchr(); - if(peekc == ':') - { + if (peekc == ':') { indent_puts(); //fprintf (outfil,":"); fprintf(":"); peek = -1; peekc = '`'; break; - } - else - { + } else { //int double_colon = 0; peek = 1; } - if(q_flg == 1) - { + if (q_flg == 1) { q_flg = 0; break; } - if(!lookup(w_ds) && !lookup(w_case)) - { + if (!lookup(w_ds) && !lookup(w_case)) { s_flg = 0; indent_puts(); - } - else - { + } else { tabs--; indent_puts(); tabs++; } peekc = getchr(); - if(peekc == ';') - { + if (peekc == ';') { fprintf(";"); peek = -1; peekc = '`'; - } - else - { + } else { peek = 1; } getnl(); @@ -736,8 +697,7 @@ public class AutoFormat implements Tool { buf[j++] = c; peekc = getchr(); - if(peekc == '/') - { + if (peekc == '/') { buf[j++] = peekc; peekc = '`'; peek = -1; @@ -745,19 +705,19 @@ public class AutoFormat implements Tool { //fprintf(outfil,"\n"); fprintf("\n"); break; - } - else - { + } else { peek = 1; } - if(peekc != '*') { + if (peekc != '*') { break; - } - else - { - if (j > 0) buf[j--] = '\0'; - if (j > 0) indent_puts(); + } else { + if (j > 0) { + buf[j--] = '\0'; + } + if (j > 0) { + indent_puts(); + } buf[j++] = '/'; buf[j++] = '*'; peek = -1; @@ -768,23 +728,18 @@ public class AutoFormat implements Tool { case ')': paren--; - if (paren < 0) - { + if (paren < 0) { paren = 0;//EOF = true; //System.out.println("eof c"); } buf[j++] = c; indent_puts(); - if(getnl() == 1) - { + if (getnl() == 1) { peekc = '\n'; peek = 1; - if(paren != 0) - { + if (paren != 0) { a_flg = 1; - } - else if(tabs > 0) - { + } else if (tabs > 0) { p_flg[level]++; tabs++; ind[level] = 0; @@ -795,35 +750,34 @@ public class AutoFormat implements Tool { case '(': buf[j++] = c; paren++; - if ((lookup(w_for))) - { + if ((lookup(w_for))) { c = get_string(); - while(c != ';') c = get_string(); - ct=0; - int for_done = 0; - while (for_done==0) - { + while (c != ';') { c = get_string(); - while(c != ')') - { - if(c == '(') ct++; + } + ct = 0; + int for_done = 0; + while (for_done == 0) { + c = get_string(); + while (c != ')') { + if (c == '(') { + ct++; + } c = get_string(); } - if(ct != 0) - { + if (ct != 0) { ct--; + } else { + for_done = 1; } - else for_done = 1; - } // endwhile for_done + } // endwhile for_done paren--; - if (paren < 0) - { - paren=0;//EOF = true; + if (paren < 0) { + paren = 0;//EOF = true; //System.out.println("eof d"); } indent_puts(); - if(getnl() == 1) - { + if (getnl() == 1) { peekc = '\n'; peek = 1; p_flg[level]++; @@ -833,8 +787,7 @@ public class AutoFormat implements Tool { break; } - if(lookup(w_if_)) - { + if (lookup(w_if_)) { indent_puts(); s_tabs[c_level][if_lev] = tabs; sp_flg[c_level][if_lev] = p_flg[level]; @@ -879,20 +832,18 @@ public class AutoFormat implements Tool { reader.close(); // close buff - String formattedText = strOut.toString(); + final String formattedText = strOut.toString(); if (formattedText.equals(originalText)) { editor.statusNotice("No changes necessary for Auto Format."); } else if (paren != 0) { // warn user if there are too many parens in either direction - editor.statusError("Auto Format Canceled: Too many " + - ((paren < 0) ? "right" : "left") + - " parentheses."); + editor.statusError("Auto Format Canceled: Too many " + + ((paren < 0) ? "right" : "left") + " parentheses."); - } else if (c_level != 0) { // check braces only if parens are ok - editor.statusError("Auto Format Canceled: Too many " + - ((c_level < 0) ? "right" : "left") + - " curly braces."); + } else if (c_level != 0) { // check braces only if parens are ok + editor.statusError("Auto Format Canceled: Too many " + + ((c_level < 0) ? "right" : "left") + " curly braces."); } else { // replace with new bootiful text @@ -904,7 +855,7 @@ public class AutoFormat implements Tool { editor.statusNotice("Auto Format finished."); } - } catch (Exception e) { + } catch (final Exception e) { editor.statusError(e); } }