[Pydev-cvs] org.python.pydev/src/org/python/pydev/editor/actions PyFormatStd.java, 1.16, 1.17 PyAct
Brought to you by:
fabioz
From: Fabio Z. <fa...@us...> - 2008-06-15 16:45:20
|
Update of /cvsroot/pydev/org.python.pydev/src/org/python/pydev/editor/actions In directory sc8-pr-cvs1.sourceforge.net:/tmp/cvs-serv1992/src/org/python/pydev/editor/actions Modified Files: PyFormatStd.java PyAction.java Log Message: Minors: ParsingUtils accepts null instead of buffer (so, objects that don't need the actual values can just use it to skip comments, parenthesis, etc). Index: PyAction.java =================================================================== RCS file: /cvsroot/pydev/org.python.pydev/src/org/python/pydev/editor/actions/PyAction.java,v retrieving revision 1.36 retrieving revision 1.37 diff -C2 -d -r1.36 -r1.37 *** PyAction.java 14 Jun 2008 22:14:55 -0000 1.36 --- PyAction.java 15 Jun 2008 16:45:26 -0000 1.37 *************** *** 273,276 **** --- 273,294 ---- /** + * Counts the number of occurences of a certain character in a string. + * + * @param line the string to search in + * @param c the character to search for + * @return an integer (int) representing the number of occurences of this character + */ + public static int countChars(char c, FastStringBuffer line) { + int ret = 0; + int len = line.length(); + for (int i = 0; i < len; i++) { + if(line.charAt(i) == c){ + ret += 1; + } + } + return ret; + } + + /** * @param ps * @return string with the token or empty token if not found. Index: PyFormatStd.java =================================================================== RCS file: /cvsroot/pydev/org.python.pydev/src/org/python/pydev/editor/actions/PyFormatStd.java,v retrieving revision 1.16 retrieving revision 1.17 diff -C2 -d -r1.16 -r1.17 *** PyFormatStd.java 26 Feb 2008 16:33:01 -0000 1.16 --- PyFormatStd.java 15 Jun 2008 16:45:26 -0000 1.17 *************** *** 15,20 **** import org.python.pydev.core.docutils.ParsingUtils; import org.python.pydev.core.docutils.PySelection; import org.python.pydev.editor.PyEdit; - import org.python.pydev.parser.prettyprinter.Formatter; import org.python.pydev.parser.prettyprinter.IFormatter; import org.python.pydev.plugin.PyCodeFormatterPage; --- 15,20 ---- import org.python.pydev.core.docutils.ParsingUtils; import org.python.pydev.core.docutils.PySelection; + import org.python.pydev.core.structure.FastStringBuffer; import org.python.pydev.editor.PyEdit; import org.python.pydev.parser.prettyprinter.IFormatter; import org.python.pydev.plugin.PyCodeFormatterPage; *************** *** 23,70 **** * @author Fabio Zadrozny */ ! public class PyFormatStd extends PyAction implements IFormatter{ ! public static class FormatStd{ public boolean spaceAfterComma; public boolean parametersWithSpace; } ! /** * @see org.eclipse.ui.IActionDelegate#run(org.eclipse.jface.action.IAction) */ public void run(IAction action) { ! try { IFormatter participant = (IFormatter) ExtensionHelper.getParticipant(ExtensionHelper.PYDEV_FORMATTER); ! if(participant==null){ participant = this; } ! PySelection ps = new PySelection ( getTextEditor ()); IDocument doc = ps.getDoc(); ! int startLine = ps.getStartLineIndex(); PyEdit pyEdit = getPyEdit(); ! if(ps.getTextSelection().getLength() == 0){ participant.formatAll(doc, pyEdit); ! }else{ participant.formatSelection(doc, startLine, ps.getEndLineIndex(), pyEdit, ps); } ! ! if(startLine >= doc.getNumberOfLines()){ ! startLine = doc.getNumberOfLines()-1; } TextSelection sel = new TextSelection(doc, doc.getLineOffset(startLine), 0); getTextEditor().getSelectionProvider().setSelection(sel); ! } catch ( Exception e ) { ! beep ( e ); ! } } ! public void formatSelection(IDocument doc, int startLine, int endLineIndex, IPyEdit edit, PySelection ps){ // Formatter formatter = new Formatter(); // formatter.formatSelection(doc, startLine, endLineIndex, edit, ps); performFormatSelection(doc, startLine, endLineIndex); } ! public void formatAll(IDocument doc, IPyEdit edit) { // Formatter formatter = new Formatter(); --- 23,71 ---- * @author Fabio Zadrozny */ ! public class PyFormatStd extends PyAction implements IFormatter { ! public static class FormatStd { public boolean spaceAfterComma; + public boolean parametersWithSpace; } ! /** * @see org.eclipse.ui.IActionDelegate#run(org.eclipse.jface.action.IAction) */ public void run(IAction action) { ! try { IFormatter participant = (IFormatter) ExtensionHelper.getParticipant(ExtensionHelper.PYDEV_FORMATTER); ! if (participant == null) { participant = this; } ! PySelection ps = new PySelection(getTextEditor()); IDocument doc = ps.getDoc(); ! int startLine = ps.getStartLineIndex(); PyEdit pyEdit = getPyEdit(); ! if (ps.getTextSelection().getLength() == 0) { participant.formatAll(doc, pyEdit); ! } else { participant.formatSelection(doc, startLine, ps.getEndLineIndex(), pyEdit, ps); } ! ! if (startLine >= doc.getNumberOfLines()) { ! startLine = doc.getNumberOfLines() - 1; } TextSelection sel = new TextSelection(doc, doc.getLineOffset(startLine), 0); getTextEditor().getSelectionProvider().setSelection(sel); ! } catch (Exception e) { ! beep(e); ! } } ! public void formatSelection(IDocument doc, int startLine, int endLineIndex, IPyEdit edit, PySelection ps) { // Formatter formatter = new Formatter(); // formatter.formatSelection(doc, startLine, endLineIndex, edit, ps); performFormatSelection(doc, startLine, endLineIndex); } ! public void formatAll(IDocument doc, IPyEdit edit) { // Formatter formatter = new Formatter(); *************** *** 72,76 **** performFormatAll(doc); } ! /** * @param doc --- 73,77 ---- performFormatAll(doc); } ! /** * @param doc *************** *** 83,96 **** IRegion start = doc.getLineInformation(startLineIndex); IRegion end = doc.getLineInformation(endLineIndex); ! int iStart = start.getOffset(); ! int iEnd = end.getOffset()+end.getLength(); ! ! String d = doc.get(iStart, iEnd-iStart); ! FormatStd formatStd = getFormat(); ! String formatted = formatStr(d, formatStd); ! ! doc.replace(iStart, iEnd-iStart, formatted); ! } catch (BadLocationException e) { e.printStackTrace(); --- 84,97 ---- IRegion start = doc.getLineInformation(startLineIndex); IRegion end = doc.getLineInformation(endLineIndex); ! int iStart = start.getOffset(); ! int iEnd = end.getOffset() + end.getLength(); ! ! String d = doc.get(iStart, iEnd - iStart); ! FormatStd formatStd = getFormat(); ! String formatted = formatStr(d, formatStd); ! ! doc.replace(iStart, iEnd - iStart, formatted); ! } catch (BadLocationException e) { e.printStackTrace(); *************** *** 98,102 **** } ! private FormatStd getFormat(){ FormatStd formatStd = new FormatStd(); formatStd.parametersWithSpace = PyCodeFormatterPage.useSpaceForParentesis(); --- 99,103 ---- } ! private FormatStd getFormat() { FormatStd formatStd = new FormatStd(); formatStd.parametersWithSpace = PyCodeFormatterPage.useSpaceForParentesis(); *************** *** 104,108 **** return formatStd; } ! /** * @param doc --- 105,109 ---- return formatStd; } ! /** * @param doc *************** *** 116,120 **** } - /** * This method formats a string given some standard. --- 117,120 ---- *************** *** 124,154 **** * @return */ ! public static String formatStr(String str, FormatStd std ){ char[] cs = str.toCharArray(); ! StringBuffer buf = new StringBuffer(); char lastChar = '\0'; for (int i = 0; i < cs.length; i++) { char c = cs[i]; ! ! if(c == '\'' || c == '"'){ //ignore comments or multiline comments... i = ParsingUtils.eatLiterals(cs, buf, i); ! ! }else if(c == '#'){ i = ParsingUtils.eatComments(cs, buf, i); ! ! }else if(c == ','){ ! i = formatForComma(std, cs, buf, i); ! ! }else if(c == '('){ ! i = formatForPar(cs, i, std, buf); ! ! ! }else{ ! if(c == '\r' || c == '\n'){ ! if(lastChar == ',' && std.spaceAfterComma){ ! buf.deleteCharAt(buf.length()-1); ! } ! } buf.append(c); } --- 124,153 ---- * @return */ ! public static String formatStr(String str, FormatStd std) { char[] cs = str.toCharArray(); ! FastStringBuffer buf = new FastStringBuffer(); char lastChar = '\0'; for (int i = 0; i < cs.length; i++) { char c = cs[i]; ! ! if (c == '\'' || c == '"') { //ignore comments or multiline comments... i = ParsingUtils.eatLiterals(cs, buf, i); ! ! } else if (c == '#') { i = ParsingUtils.eatComments(cs, buf, i); ! ! } else if (c == ',') { ! i = formatForComma(std, cs, buf, i); ! ! } else if (c == '(') { ! i = formatForPar(cs, i, std, buf); ! ! } else { ! if (c == '\r' || c == '\n') { ! if (lastChar == ',' && std.spaceAfterComma) { ! buf.deleteLast(); ! } ! } buf.append(c); } *************** *** 162,232 **** * @param i */ ! private static int formatForPar(char[] cs, int i, FormatStd std, StringBuffer buf) { char c = ' '; ! StringBuffer locBuf = new StringBuffer(); ! ! int j = i+1; ! while(j < cs.length && (c = cs[j]) != ')'){ ! j++; ! ! if(c == '\'' || c == '"'){ //ignore comments or multiline comments... ! j = ParsingUtils.eatLiterals( cs, locBuf, j-1)+1; ! ! }else if(c == '#'){ ! j = ParsingUtils.eatComments(cs, locBuf, j-1)+1; ! ! }else if( c == '('){ //open another par. ! j = formatForPar(cs, j-1, std, locBuf)+1; ! ! }else{ locBuf.append(c); } } ! ! if(c == ')'){ ! char c1; ! StringBuffer buf1 = new StringBuffer(); ! ! if(locBuf.indexOf("\n") != -1){ ! for (int k = locBuf.length(); k > 0 && (c1 = locBuf.charAt(k-1))!= '\n'; k--) { ! buf1.insert(0, c1); ! } } ! ! String formatStr = formatStr(trim(locBuf), std); ! formatStr = trim(new StringBuffer(formatStr)); ! ! String closing = ")"; ! if(buf1.length() > 0 && PySelection.containsOnlyWhitespaces(buf1.toString())){ ! formatStr += buf1.toString(); ! }else if(std.parametersWithSpace){ ! closing = " )"; ! } ! ! if(std.parametersWithSpace){ ! if(formatStr.length() == 0){ ! buf.append( "()" ); ! ! }else{ ! buf.append( "( " ); ! buf.append( formatStr ); ! buf.append( closing ); ! } ! }else{ ! buf.append( "(" ); ! buf.append( formatStr ); ! buf.append( closing ); ! } ! return j; ! }else{ return i; } } - - /** * We just want to trim whitespaces, not newlines! --- 161,230 ---- * @param i */ ! private static int formatForPar(char[] cs, int i, FormatStd std, FastStringBuffer buf) { char c = ' '; ! FastStringBuffer locBuf = new FastStringBuffer(); ! ! int j = i + 1; ! while (j < cs.length && (c = cs[j]) != ')') { ! j++; ! ! if (c == '\'' || c == '"') { //ignore comments or multiline comments... ! j = ParsingUtils.eatLiterals(cs, locBuf, j - 1) + 1; ! ! } else if (c == '#') { ! j = ParsingUtils.eatComments(cs, locBuf, j - 1) + 1; ! ! } else if (c == '(') { //open another par. ! j = formatForPar(cs, j - 1, std, locBuf) + 1; ! ! } else { locBuf.append(c); } } ! ! if (c == ')') { ! char c1; ! FastStringBuffer buf1 = new FastStringBuffer(); ! ! if (locBuf.indexOf('\n') != -1) { ! for (int k = locBuf.length(); k > 0 && (c1 = locBuf.charAt(k - 1)) != '\n'; k--) { ! buf1.insert(0, c1); ! } } ! ! String formatStr = formatStr(trim(locBuf).toString(), std); ! FastStringBuffer formatStrBuf = trim(new FastStringBuffer(formatStr, 10)); ! ! String closing = ")"; ! if (buf1.length() > 0 && PySelection.containsOnlyWhitespaces(buf1.toString())) { ! formatStrBuf.append(buf1); ! ! } else if (std.parametersWithSpace) { ! closing = " )"; ! } ! ! if (std.parametersWithSpace) { ! if (formatStrBuf.length() == 0) { ! buf.append("()"); ! ! } else { ! buf.append("( "); ! buf.append(formatStrBuf); ! buf.append(closing); ! } ! } else { ! buf.append('('); ! buf.append(formatStrBuf); ! buf.append(closing); ! } ! return j; ! } else { return i; } } /** * We just want to trim whitespaces, not newlines! *************** *** 234,245 **** * @return */ ! private static String trim(StringBuffer locBuf) { ! while(locBuf.length() > 0 && locBuf.charAt(0) == ' '){ locBuf.deleteCharAt(0); } ! while(locBuf.length() > 0 && locBuf.charAt(locBuf.length()-1) == ' '){ ! locBuf.deleteCharAt(locBuf.length()-1); } ! return locBuf.toString(); } --- 232,243 ---- * @return */ ! private static FastStringBuffer trim(FastStringBuffer locBuf) { ! while (locBuf.length() > 0 && locBuf.firstChar() == ' ') { locBuf.deleteCharAt(0); } ! while (locBuf.length() > 0 && locBuf.lastChar() == ' ') { ! locBuf.deleteLast(); } ! return locBuf; } *************** *** 251,262 **** * @return */ ! private static int formatForComma(FormatStd std, char[] cs, StringBuffer buf, int i) { ! while(i < cs.length-1 && (cs[i+1]) == ' '){ i++; } ! ! if(std.spaceAfterComma){ buf.append(", "); ! }else{ buf.append(','); } --- 249,260 ---- * @return */ ! private static int formatForComma(FormatStd std, char[] cs, FastStringBuffer buf, int i) { ! while (i < cs.length - 1 && (cs[i + 1]) == ' ') { i++; } ! ! if (std.spaceAfterComma) { buf.append(", "); ! } else { buf.append(','); } |