[Pydev-cvs] org.python.pydev/src/org/python/pydev/editor/actions PyFormatStd.java, 1.18, 1.19
Brought to you by:
fabioz
From: Fabio Z. <fa...@us...> - 2008-07-19 11:57:50
|
Update of /cvsroot/pydev/org.python.pydev/src/org/python/pydev/editor/actions In directory sc8-pr-cvs1.sourceforge.net:/tmp/cvs-serv13369/src/org/python/pydev/editor/actions Modified Files: PyFormatStd.java Log Message: PyFormatStd changes: comments/ minor refactorings Index: PyFormatStd.java =================================================================== RCS file: /cvsroot/pydev/org.python.pydev/src/org/python/pydev/editor/actions/PyFormatStd.java,v retrieving revision 1.18 retrieving revision 1.19 diff -C2 -d -r1.18 -r1.19 *** PyFormatStd.java 6 Jul 2008 21:58:13 -0000 1.18 --- PyFormatStd.java 19 Jul 2008 11:57:58 -0000 1.19 *************** *** 25,31 **** --- 25,43 ---- public class PyFormatStd extends PyAction implements IFormatter { + /** + * Class that defines the format standard to be used + * + * @author Fabio + */ public static class FormatStd { + + /** + * Defines whether spaces should be added after a comma + */ public boolean spaceAfterComma; + /** + * Defines whether ( and ) should have spaces + */ public boolean parametersWithSpace; } *************** *** 61,97 **** } } 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(); ! // formatter.formatAll(doc, edit); ! performFormatAll(doc); ! } ! ! /** ! * @param doc ! * @param endLineDelim ! * @param startLineIndex ! * @param endLineIndex ! */ ! public void performFormatSelection(IDocument doc, int startLineIndex, int endLineIndex) { try { ! 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(); --- 73,99 ---- } } + + /** + * Formats the given selection + * @see IFormatter + */ public void formatSelection(IDocument doc, int startLine, int endLineIndex, IPyEdit edit, PySelection ps) { // Formatter formatter = new Formatter(); // formatter.formatSelection(doc, startLine, endLineIndex, edit, ps); ! try { ! IRegion start = doc.getLineInformation(startLine); 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(); *************** *** 99,114 **** } - private FormatStd getFormat() { - FormatStd formatStd = new FormatStd(); - formatStd.parametersWithSpace = PyCodeFormatterPage.useSpaceForParentesis(); - formatStd.spaceAfterComma = PyCodeFormatterPage.useSpaceAfterComma(); - return formatStd; - } - /** ! * @param doc ! * @param endLineDelim */ ! public void performFormatAll(IDocument doc) { String d = doc.get(); FormatStd formatStd = getFormat(); --- 101,112 ---- } /** ! * Formats the whole document ! * @see IFormatter */ ! public void formatAll(IDocument doc, IPyEdit edit) { ! // Formatter formatter = new Formatter(); ! // formatter.formatAll(doc, edit); ! String d = doc.get(); FormatStd formatStd = getFormat(); *************** *** 117,128 **** } /** * This method formats a string given some standard. * ! * @param str ! * @param std ! * @return */ ! public static String formatStr(String str, FormatStd std) { char[] cs = str.toCharArray(); FastStringBuffer buf = new FastStringBuffer(); --- 115,137 ---- } + + /** + * @return the format standard that should be used to do the formatting + */ + private FormatStd getFormat() { + FormatStd formatStd = new FormatStd(); + formatStd.parametersWithSpace = PyCodeFormatterPage.useSpaceForParentesis(); + formatStd.spaceAfterComma = PyCodeFormatterPage.useSpaceAfterComma(); + return formatStd; + } + /** * This method formats a string given some standard. * ! * @param str the string to be formatted ! * @param std the standard to be used ! * @return a new (formatted) string */ ! public String formatStr(String str, FormatStd std) { char[] cs = str.toCharArray(); FastStringBuffer buf = new FastStringBuffer(); *************** *** 142,151 **** } else if (c == '(') { - i = formatForPar(parsingUtils, cs, i, std, buf); } else { if (c == '\r' || c == '\n') { ! if (lastChar == ',' && std.spaceAfterComma) { buf.deleteLast(); } --- 151,159 ---- } else if (c == '(') { i = formatForPar(parsingUtils, cs, i, std, buf); } else { if (c == '\r' || c == '\n') { ! if (lastChar == ',' && std.spaceAfterComma && buf.lastChar() == ' ') { buf.deleteLast(); } *************** *** 159,166 **** /** * @param cs * @param i */ ! private static int formatForPar(ParsingUtils parsingUtils, char[] cs, int i, FormatStd std, FastStringBuffer buf) { char c = ' '; FastStringBuffer locBuf = new FastStringBuffer(); --- 167,175 ---- /** + * 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(); *************** *** 181,186 **** } else { - locBuf.append(c); } } --- 190,195 ---- } else { locBuf.append(c); + } } *************** *** 188,196 **** 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); } --- 197,210 ---- if (c == ')') { + //Now, when a closing parens is found, let's see the contents of the line where that parens was found + //and if it's only whitespaces, add all those whitespaces (to handle the following case: + //a(a, + // b + // ) <-- we don't want to change this one. char c1; FastStringBuffer buf1 = new FastStringBuffer(); ! if (locBuf.indexOf('\n') != -1 || locBuf.indexOf('\r') != -1) { ! for (int k = locBuf.length(); k > 0 && (c1 = locBuf.charAt(k - 1)) != '\n' && c1 != '\r'; k--) { buf1.insert(0, c1); } *************** *** 230,237 **** /** * We just want to trim whitespaces, not newlines! ! * @param locBuf ! * @return */ ! private static FastStringBuffer trim(FastStringBuffer locBuf) { while (locBuf.length() > 0 && locBuf.firstChar() == ' ') { locBuf.deleteCharAt(0); --- 244,251 ---- /** * We just want to trim whitespaces, not newlines! ! * @param locBuf the buffer to be trimmed ! * @return the same buffer passed as a parameter */ ! private FastStringBuffer trim(FastStringBuffer locBuf) { while (locBuf.length() > 0 && locBuf.firstChar() == ' ') { locBuf.deleteCharAt(0); *************** *** 244,254 **** /** ! * @param std ! * @param cs ! * @param buf ! * @param i ! * @return */ ! private static int formatForComma(FormatStd std, char[] cs, FastStringBuffer buf, int i) { while (i < cs.length - 1 && (cs[i + 1]) == ' ') { i++; --- 258,270 ---- /** ! * When a comma is found, it's formatted accordingly (spaces added after it). ! * ! * @param std the coding standard to be used ! * @param cs the contents of the document to be formatted ! * @param buf the buffer where the comma should be added ! * @param i the current index ! * @return the new index on the original doc. */ ! private int formatForComma(FormatStd std, char[] cs, FastStringBuffer buf, int i) { while (i < cs.length - 1 && (cs[i + 1]) == ' ') { i++; |