[Pydev-cvs] org.python.pydev/src/org/python/pydev/editor/actions PyToggleComment.java, NONE, 1.1 Py
Brought to you by:
fabioz
From: Fabio Z. <fa...@us...> - 2008-06-22 08:34:59
|
Update of /cvsroot/pydev/org.python.pydev/src/org/python/pydev/editor/actions In directory sc8-pr-cvs1.sourceforge.net:/tmp/cvs-serv9722/src/org/python/pydev/editor/actions Modified Files: PyComment.java PyUncomment.java Added Files: PyToggleComment.java Log Message: PyToggleComment: Created from patch: https://sourceforge.net/tracker/?func=detail&atid=577329&aid=1999389&group_id=85796 --- NEW FILE: PyToggleComment.java --- package org.python.pydev.editor.actions; import org.eclipse.jface.text.BadLocationException; import org.python.pydev.core.Tuple; import org.python.pydev.core.docutils.PySelection; import org.python.pydev.core.structure.FastStringBuffer; /** * Same toggle comment action as we are used to it in the java perspective * * @author e0525580 at student.tuwien.ac.at * Created from patch: https://sourceforge.net/tracker/?func=detail&atid=577329&aid=1999389&group_id=85796 */ public class PyToggleComment extends PyComment { @Override public Tuple<Integer, Integer> perform(final PySelection ps) throws BadLocationException { ps.selectCompleteLine(); final boolean shouldAddCommentSign = PyToggleComment.allLinesStartWithCommentSign(ps) == false; String endLineDelim = ps.getEndLineDelim(); int endLineIndex = ps.getEndLineIndex(); int startLineIndex = ps.getStartLineIndex(); final FastStringBuffer sb = new FastStringBuffer(ps.getSelLength()+(endLineIndex-startLineIndex)+10); for (int i = startLineIndex, n = endLineIndex; i <= n; i++) { final String line = ps.getLine(i); if(shouldAddCommentSign) { sb.append("#"); sb.append(line); } else { // remove comment sign sb.append(line.replaceFirst("#", "")); } //add a new line if we're not in the last line. sb.append((i < endLineIndex ? endLineDelim : "")); } final int start = ps.getStartLine().getOffset(); final String replacement = sb.toString(); ps.getDoc().replace(start, ps.getSelLength(), replacement); return new Tuple<Integer, Integer>(start, replacement.length()); } /** * Checks if all lines start with '#' */ private static boolean allLinesStartWithCommentSign(final PySelection ps) { int endLineIndex = ps.getEndLineIndex(); for (int i = ps.getStartLineIndex(), n = endLineIndex; i <= n; i++) { final String line = ps.getLine(i); if(line.trim().startsWith("#") == false) { return false; } } return true; } } Index: PyComment.java =================================================================== RCS file: /cvsroot/pydev/org.python.pydev/src/org/python/pydev/editor/actions/PyComment.java,v retrieving revision 1.10 retrieving revision 1.11 diff -C2 -d -r1.10 -r1.11 *** PyComment.java 14 Jun 2008 22:14:55 -0000 1.10 --- PyComment.java 21 Jun 2008 14:31:22 -0000 1.11 *************** *** 24,27 **** --- 24,28 ---- */ public class PyComment extends PyAction { + /** * Grabs the selection information and performs the action. *************** *** 59,64 **** FastStringBuffer strbuf = new FastStringBuffer(selectedText.length()+ret.size()+2); for(String line: ret){ ! strbuf.append('#'); ! strbuf.append(line); } --- 60,64 ---- FastStringBuffer strbuf = new FastStringBuffer(selectedText.length()+ret.size()+2); for(String line: ret){ ! strbuf.append('#').append(line); } Index: PyUncomment.java =================================================================== RCS file: /cvsroot/pydev/org.python.pydev/src/org/python/pydev/editor/actions/PyUncomment.java,v retrieving revision 1.9 retrieving revision 1.10 diff -C2 -d -r1.9 -r1.10 *** PyUncomment.java 14 Jun 2008 22:14:55 -0000 1.9 --- PyUncomment.java 21 Jun 2008 14:31:22 -0000 1.10 *************** *** 18,23 **** /* Selection element */ - - /** * Performs the action with a given PySelection --- 18,21 ---- *************** *** 28,46 **** */ public Tuple<Integer, Integer> perform(PySelection ps) throws BadLocationException { - // What we'll be replacing the selected text with - FastStringBuffer strbuf = new FastStringBuffer(); // If they selected a partial line, count it as a full one ps.selectCompleteLine(); ! int i; ! // For each line, comment them out ! for (i = ps.getStartLineIndex(); i <= ps.getEndLineIndex(); i++) { String l = ps.getLine(i); if (l.trim().startsWith("#")) { // we may want to remove comment that are not really in the beggining... ! strbuf.append(l.replaceFirst("#", "") + (i < ps.getEndLineIndex() ? ps.getEndLineDelim() : "")); } else { ! strbuf.append(l + (i < ps.getEndLineIndex() ? ps.getEndLineDelim() : "")); } } --- 26,49 ---- */ public Tuple<Integer, Integer> perform(PySelection ps) throws BadLocationException { // If they selected a partial line, count it as a full one ps.selectCompleteLine(); + + // What we'll be replacing the selected text with + FastStringBuffer strbuf = new FastStringBuffer(ps.getSelLength()+1); //no, it won't be more that the current sel ! // For each line, uncomment it ! int endLineIndex = ps.getEndLineIndex(); ! String endLineDelim = ps.getEndLineDelim(); ! ! for (int i = ps.getStartLineIndex(); i <= endLineIndex; i++) { String l = ps.getLine(i); if (l.trim().startsWith("#")) { // we may want to remove comment that are not really in the beggining... ! strbuf.append(l.replaceFirst("#", "")); } else { ! strbuf.append(l); } + //add a new line if we're not in the last line. + strbuf.append(i < endLineIndex ? endLineDelim : ""); } *************** *** 52,65 **** } - /** - * Same as comment, but remove the first char. - */ - protected String replaceStr(String str, String endLineDelim) { - str = str.replaceAll(endLineDelim + "#", endLineDelim); - if (str.startsWith("#")) { - str = str.substring(1); - } - return str; - } - } \ No newline at end of file --- 55,57 ---- |