[Pydev-cvs] org.python.pydev/src/org/python/pydev/editor/actions PyFormatStd.java, 1.19, 1.20
Brought to you by:
fabioz
From: Fabio Z. <fa...@us...> - 2008-07-19 19:53:23
|
Update of /cvsroot/pydev/org.python.pydev/src/org/python/pydev/editor/actions In directory sc8-pr-cvs1.sourceforge.net:/tmp/cvs-serv28692/src/org/python/pydev/editor/actions Modified Files: PyFormatStd.java Log Message: - Improved code formatter to deal with operators (+, -, *, etc) - Improved code formatter to handle '=' differently inside function calls / keyword args Index: PyFormatStd.java =================================================================== RCS file: /cvsroot/pydev/org.python.pydev/src/org/python/pydev/editor/actions/PyFormatStd.java,v retrieving revision 1.19 retrieving revision 1.20 diff -C2 -d -r1.19 -r1.20 *** PyFormatStd.java 19 Jul 2008 11:57:58 -0000 1.19 --- PyFormatStd.java 19 Jul 2008 19:53:31 -0000 1.20 *************** *** 41,44 **** --- 41,56 ---- */ public boolean parametersWithSpace; + + /** + * Defines whether = should be spaces surrounded when inside of a parens (function call) + * (as well as others related: *= +=, -=, !=, ==, etc). + */ + public boolean assignWithSpaceInsideParens; + + /** + * Defines whether operators should be spaces surrounded: + * + - * / // ** | & ^ ~ = + */ + public boolean operatorsWithSpace; } *************** *** 121,124 **** --- 133,138 ---- private FormatStd getFormat() { FormatStd formatStd = new FormatStd(); + formatStd.assignWithSpaceInsideParens = PyCodeFormatterPage.useAssignWithSpacesInsideParenthesis(); + formatStd.operatorsWithSpace = PyCodeFormatterPage.useOperatorsWithSpace(); formatStd.parametersWithSpace = PyCodeFormatterPage.useSpaceForParentesis(); formatStd.spaceAfterComma = PyCodeFormatterPage.useSpaceAfterComma(); *************** *** 134,137 **** --- 148,163 ---- */ public String formatStr(String str, FormatStd std) { + return formatStr(str, std, 0); + } + + /** + * This method formats a string given some standard. + * + * @param str the string to be formatted + * @param std the standard to be used + * @param parensLevel the level of the parenthesis available. + * @return a new (formatted) string + */ + private String formatStr(String str, FormatStd std, int parensLevel) { char[] cs = str.toCharArray(); FastStringBuffer buf = new FastStringBuffer(); *************** *** 141,175 **** char c = cs[i]; ! if (c == '\'' || c == '"') { //ignore comments or multiline comments... ! i = parsingUtils.eatLiterals(buf, i); ! } else if (c == '#') { ! i = parsingUtils.eatComments(buf, i); ! } else if (c == ',') { ! i = formatForComma(std, cs, buf, i); ! } else if (c == '(') { ! i = formatForPar(parsingUtils, cs, i, std, buf); ! } else { ! if (c == '\r' || c == '\n') { ! if (lastChar == ',' && std.spaceAfterComma && buf.lastChar() == ' ') { buf.deleteLast(); } ! } ! buf.append(c); } lastChar = c; } return buf.toString(); } /** * Formats the contents for when a parenthesis is found (so, go until the closing parens and format it accordingly) * @param cs * @param i */ ! private int formatForPar(ParsingUtils parsingUtils, char[] cs, int i, FormatStd std, FastStringBuffer buf) { char c = ' '; FastStringBuffer locBuf = new FastStringBuffer(); --- 167,384 ---- char c = cs[i]; ! switch(c){ ! case '\'': ! case '"': ! //ignore comments or multiline comments... ! i = parsingUtils.eatLiterals(buf, i); ! break; ! ! case '#': ! i = parsingUtils.eatComments(buf, i); ! break; ! case ',': ! i = formatForComma(std, cs, buf, i); ! break; ! case '(': ! i = formatForPar(parsingUtils, cs, i, std, buf, parensLevel+1); ! break; ! ! ! //Things to treat: ! //+, -, *, /, % ! //** // << >> ! //<, >, !=, <>, <=, >=, //=, *=, /=, ! //& ^ ~ | ! case '*': ! //for *, we also need to treat when it's used in varargs, kwargs and list expansion ! boolean isOperator = false; ! for(int j=buf.length()-1;j>=0;j--){ ! char localC = buf.charAt(j); ! if(Character.isWhitespace(localC)){ ! continue; ! } ! if(localC == '(' || localC == ','){ ! //it's not an operator, but vararg. kwarg or list expansion ! break; ! } ! if(Character.isJavaIdentifierPart(localC)){ ! //ok, there's a chance that it can be an operator, but we still have to check ! //the chance that it's a wild import ! FastStringBuffer localBufToCheckWildImport = new FastStringBuffer(); ! while(Character.isJavaIdentifierPart(localC)){ ! localBufToCheckWildImport.append(localC); ! j--; ! if(j < 0){ ! break; //break while ! } ! localC = buf.charAt(j); ! } ! if(!localBufToCheckWildImport.reverse().toString().equals("import")){ ! isOperator = true; ! } ! break; ! } ! } ! if(!isOperator){ ! buf.append('*'); ! break;//break switch ! } ! //Otherwise, FALLTHROUGH ! ! case '+': ! case '-': ! case '/': ! case '%': ! case '<': ! case '>': ! case '!': ! case '&': ! case '^': ! case '~': ! case '|': ! ! i = handleOperator(std, cs, buf, parsingUtils, i, c); ! c = cs[i]; ! break; ! ! //check for = and == (other cases that have an = as the operator should already be treated) ! case '=': ! if(i < cs.length-1 && cs[i+1] == '='){ ! //if == handle as if a regular operator ! i = handleOperator(std, cs, buf, parsingUtils, i, c); ! c = cs[i]; ! break; ! } ! ! while(buf.length() > 0 && buf.lastChar() == ' '){ buf.deleteLast(); } ! ! boolean surroundWithSpaces = std.operatorsWithSpace; ! if(parensLevel > 0){ ! surroundWithSpaces = std.assignWithSpaceInsideParens; ! } ! ! //add space before ! if(surroundWithSpaces){ ! buf.append(' '); ! } ! ! //add the operator and the '=' ! buf.append('='); ! ! //add space after ! if(surroundWithSpaces){ ! buf.append(' '); ! } ! ! i = parsingUtils.eatWhitespaces(null, i+1); ! break; ! ! default: ! if (c == '\r' || c == '\n') { ! if (lastChar == ',' && std.spaceAfterComma && buf.lastChar() == ' ') { ! buf.deleteLast(); ! } ! } ! buf.append(c); ! } lastChar = c; + } return buf.toString(); } + + /** + * Handles having an operator + * + * @param std the coding standard to be used + * @param cs the contents of the string + * @param buf the buffer where the contents should be added + * @param parsingUtils helper to get the contents + * @param i current index + * @param c current char + * @return the new index after handling the operator + */ + private int handleOperator(FormatStd std, char[] cs, FastStringBuffer buf, ParsingUtils parsingUtils, int i, char c) { + while(buf.length() > 0 && buf.lastChar() == ' '){ + buf.deleteLast(); + } + + boolean surroundWithSpaces = std.operatorsWithSpace; + + //add space before + if(surroundWithSpaces){ + buf.append(' '); + } + + char localC = c; + boolean backOne = true; + while(isOperatorPart(localC)){ + buf.append(localC); + i++; + if(i == cs.length){ + break; + } + localC = cs[i]; + if(localC == '='){ + //when we get to an assign, we have found a full stmt (with assign) -- e.g.: a \\= a += a == + buf.append(localC); + backOne = false; + break; + } + } + if(backOne){ + i--; + } + + //add space after + if(surroundWithSpaces){ + buf.append(' '); + } + + i = parsingUtils.eatWhitespaces(null, i+1); + return i; + } + + + /** + * @param c the char to be checked + * @return true if the passed char is part of an operator + */ + private boolean isOperatorPart(char c) { + switch(c){ + case '+': + case '-': + case '*': + case '/': + case '%': + case '<': + case '>': + case '!': + case '&': + case '^': + case '~': + case '|': + case '=': + return true; + } + return false; + } + + /** * Formats the contents for when a parenthesis is found (so, go until the closing parens and format it accordingly) * @param cs * @param i + * @param parensLevel */ ! private int formatForPar(ParsingUtils parsingUtils, char[] cs, int i, FormatStd std, FastStringBuffer buf, int parensLevel) { char c = ' '; FastStringBuffer locBuf = new FastStringBuffer(); *************** *** 187,191 **** } else if (c == '(') { //open another par. ! j = formatForPar(parsingUtils, cs, j - 1, std, locBuf) + 1; } else { --- 396,400 ---- } else if (c == '(') { //open another par. ! j = formatForPar(parsingUtils, cs, j - 1, std, locBuf, parensLevel+1) + 1; } else { *************** *** 211,215 **** } ! String formatStr = formatStr(trim(locBuf).toString(), std); FastStringBuffer formatStrBuf = trim(new FastStringBuffer(formatStr, 10)); --- 420,424 ---- } ! String formatStr = formatStr(trim(locBuf).toString(), std, parensLevel); FastStringBuffer formatStrBuf = trim(new FastStringBuffer(formatStr, 10)); |