[Pydev-cvs] org.python.pydev/src/org/python/pydev/editor/actions PyUncommentTest.java,NONE,1.1 PyStr
Brought to you by:
fabioz
Update of /cvsroot/pydev/org.python.pydev/src/org/python/pydev/editor/actions In directory sc8-pr-cvs1.sourceforge.net:/tmp/cvs-serv21826/src/org/python/pydev/editor/actions Modified Files: PyUncomment.java PyRemoveBlockComment.java PyAddBlockComment.java PyAction.java PyComment.java PyStripTrailingWhitespace.java Added Files: PyUncommentTest.java PyStripTrailingWhitespaceTest.java PyConvertSpaceToTab.java PyConvertTabToSpace.java PyConvertSpaceToTabTest.java PyAddBlockCommentTest.java PySelection.java PyConvertTabToSpaceTest.java PyCommentTest.java AllTests.java PyRemoveBlockCommentTest.java Log Message: - Added JUnit test for editor actions that I've made or are related to ones I've made. This is something I need for my project that this is for, and in order to test things out like adding block comments, I had to revamp how they functioned to allow them to accept a mock Document as opposed to grabbing it from the Active Editor. This created the PySelection entity, which was basically being used anyway by many of the actions, which serves to accept a document input and make the proper selection given passed criteria. Since I didn't get a 'no' when I asked if the JUnit testing was ok, I assumed it wasn't a problem to put them in. I can revert them all back to their original form, if desired. - 2 new actions - ConvertTabToSpace and ConvertSpaceToTab, basically the user can select lines or none at all (to affect whole document) and convert tabs and tab-width-spaces back and forth - Plugin.xml adjusted to account for the new action - Skeleton help files added for new actions, TOC modified --- NEW FILE: PyStripTrailingWhitespaceTest.java --- /* * Created on Jun 16, 2004 * * To change the template for this generated file go to * Window>Preferences>Java>Code Generation>Code and Comments */ package org.python.pydev.editor.actions; import org.eclipse.jface.text.Document; import org.eclipse.jface.text.IDocument; import junit.framework.TestCase; /** * @author Dreamer * * Tests the 'Strip Trailing Whitespace' editor feature. It performs 4 checks. * * The first checks to see if the simple String-input 'trim' feature works properly. * * The other 3 are code-based checks. * * The first fakes a selection of a couple lines in a fake document, and checks * to see that the proper whitespace is removed. * * The second fakes a selection but stops in the * middle of a line, to make sure that the Whitespace handler doesn't just strip whitespace from * the partial selection unless that line has whitespace to be stripped. * * The third selects * nothing, and makes sure that the whole document is affected by whitespace stripping. */ public class PyStripTrailingWhitespaceTest extends TestCase { /* The document that will fake an editor environment */ IDocument document; /* Lines of 'code' in the fake document */ String [] documentLines; /* For my own debugging edification, to output the name later */ static final String TestFileName = "PyStripTrailingWhitespaceTest"; /** * Constructor for PyStripTrailingWhitespaceTest. * @param arg0 */ public PyStripTrailingWhitespaceTest(String arg0) { super(arg0); } /* * Sets up the document 'code' and adds it to the document. * * @see TestCase#setUp() */ protected void setUp() throws Exception { super.setUp(); int i = -1; int length = 7; documentLines = new String[length]; documentLines[++i] = "def bar ( self ):"; documentLines[++i] = "\tprint \"foo1\"\t "; documentLines[++i] = "\tprint \"bar1\""; documentLines[++i] = "\t "; documentLines[++i] = "def foo ( self ): "; documentLines[++i] = "\tprint \"foo2\"\t "; documentLines[++i] = "\tprint \"bar2\" "; StringBuffer doc = new StringBuffer ( ); for ( i = 0; i < documentLines.length; i++ ) { doc.append ( documentLines[i] + ( i < documentLines.length - 1 ? "\n" : "" ) ); } document = new Document ( doc.toString ( ) ); } /* * @see TestCase#tearDown() */ protected void tearDown() throws Exception { super.tearDown(); } /* * Just to shorten the lines in the later tests, this calls the action's trim function * * @param in Line of 'code' to be stripped * @return String Stripped 'code' line */ public String callTrim ( String in ) { return PyStripTrailingWhitespace.trimTrailingWhitespace ( in ); } /* * Checks single-line trimming. */ public void testTrimTrailingWhitespace ( ) { // Preserves everything but last whitespace assertEquals ( "\tprint \"foo\"", callTrim ( "\tprint \"foo\"\t" ) ); // Ditto, without tab in front assertEquals ( "print \"foo\"", callTrim ( "print \"foo\" \t" ) ); // Full line blank assertEquals ( "", callTrim ( "\t \t" ) ); // Null input assertEquals ( "", callTrim ( null ) ); } /* * Checks multiple-line selection. */ public void testPerform1 ( ) { StringBuffer result = new StringBuffer ( ); int i = -1; int startLineIndex = 0; int endLineIndex = 0; int selBegin = 0; int selLength = 0; // 'Select' the entire last def result.append ( documentLines[++i] + "\n" ); // "def bar ( self ):\n" result.append ( documentLines[++i] + "\n" ); // "\tprint \"foo1\"\t \n" result.append ( documentLines[++i] + "\n" ); // "\tprint \"bar1\"\n" result.append ( documentLines[++i] + "\n" ); // "\n \n" startLineIndex = i + 1; selBegin = result.toString ( ).length ( ); result.append ( callTrim ( documentLines[++i] ) + "\n" ); // "def foo ( self ): \n" result.append ( callTrim ( documentLines[++i] ) + "\n" ); // "\tprint \"foo2\"\t \n" result.append ( callTrim ( documentLines[++i] ) ); // "\tprint \"bar2\" \n" endLineIndex = i; selLength = document.get ( ).length ( ) - selBegin; // Our expected result IDocument resultDoc = new Document ( result.toString ( ) ); // For timing data long begin = System.currentTimeMillis ( ); PySelection ps = new PySelection ( document, startLineIndex, endLineIndex, selLength, true ); PyStripTrailingWhitespace.perform ( ps ); long end = System.currentTimeMillis ( ); // Timing result System.err.print ( TestFileName + " :: " ); System.err.println ( "testPerform1: " + ( end - begin ) + "ms" ); // Document affected properly? assertEquals ( document.get ( ), resultDoc.get ( ) ); } /* * Checks multiple-line selection with the last line partially selected. */ public void testPerform2 ( ) { StringBuffer result = new StringBuffer ( ); int i = -1; int startLineIndex = 0; int endLineIndex = 0; int selBegin = 0; int selLength = 0; // 'Select' part of the last def result.append ( documentLines[++i] + "\n" ); // "def bar ( self ):\n" result.append ( documentLines[++i] + "\n" ); // "\tprint \"foo1\"\t \n" result.append ( documentLines[++i] + "\n" ); // "\tprint \"bar1\"\n" result.append ( documentLines[++i] + "\n" ); // "\n \n" startLineIndex = i + 1; selBegin = result.toString ( ).length ( ); result.append ( callTrim ( documentLines[++i] ) + "\n" ); // "def foo ( self ): \n" result.append ( callTrim ( documentLines[++i] ) + "\n" ); // "\tprint \"foo2\"\t \n" result.append ( documentLines[++i] ); // "\tprint \"bar2\" \n" endLineIndex = i; selLength = document.get ( ).length ( ) - selBegin - 6; // Our expected result IDocument resultDoc = new Document ( result.toString ( ) ); // For timing data long begin = System.currentTimeMillis ( ); PySelection ps = new PySelection ( document, startLineIndex, endLineIndex, selLength, true ); PyStripTrailingWhitespace.perform ( ps ); long end = System.currentTimeMillis ( ); // Timing result System.err.print ( TestFileName + " :: " ); System.err.println ( "testPerform2: " + ( end - begin ) + "ms" ); // Document affected properly? assertEquals ( document.get ( ), resultDoc.get ( ) ); } /* * Checks empty selection to affect cursor line. */ public void testPerform3 ( ) { StringBuffer result = new StringBuffer ( ); int i = -1; int startLineIndex = 0; int endLineIndex = 0; int selBegin = 0; int selLength = 0; // 'Select' nothing, which should call action on the whole thing result.append ( callTrim ( documentLines[++i] ) + "\n" ); // "def bar ( self ):\n" result.append ( callTrim ( documentLines[++i] ) + "\n" ); // "\tprint \"foo1\"\t \n" result.append ( callTrim ( documentLines[++i] ) + "\n" ); // "\tprint \"bar1\"\n" result.append ( callTrim ( documentLines[++i] ) + "\n" ); // "\n \n" result.append ( callTrim ( documentLines[++i] ) + "\n" ); // "def foo ( self ): \n" result.append ( callTrim ( documentLines[++i] ) + "\n" ); // "\tprint \"foo2\"\t \n" result.append ( callTrim ( documentLines[++i] ) ); // "\tprint \"bar2\" \n" endLineIndex = i; // Our expected result IDocument resultDoc = new Document ( result.toString ( ) ); // For timing data long begin = System.currentTimeMillis ( ); PySelection ps = new PySelection ( document, startLineIndex, endLineIndex, selLength, true ); PyStripTrailingWhitespace.perform ( ps ); long end = System.currentTimeMillis ( ); // Timing result System.err.print ( TestFileName + " :: " ); System.err.println ( "testPerform3: " + ( end - begin ) + "ms" ); // Document affected properly? assertEquals ( document.get ( ), resultDoc.get ( ) ); } } --- NEW FILE: PyConvertTabToSpace.java --- /* * @author: ptoofani * Created: June 2004 * License: Common Public License v1.0 */ package org.python.pydev.editor.actions; import org.eclipse.jface.action.IAction; /** * Converts tab-width spacing to tab characters in selection or entire document, if nothing * selected. * * @author Parhaum Toofanian */ public class PyConvertTabToSpace extends PyConvertSpaceToTab { /* Selection element */ private static PySelection ps; /** * Grabs the selection information and performs the action. */ public void run ( IAction action ) { try { // Select from text editor ps = new PySelection ( getTextEditor ( ), true ); // Perform the action perform ( ); // Put cursor at the first area of the selection getTextEditor ( ).selectAndReveal ( ps.getCursorOffset ( ), 0 ); } catch ( Exception e ) { beep ( e ); } } /** * Performs the action with the class' PySelection. * * @return boolean The success or failure of the action */ public static boolean perform ( ) { return perform ( ps ); } /** * Performs the action with a given PySelection * * @param ps Given PySelection * @return boolean The success or failure of the action */ public static boolean perform ( PySelection ps ) { // What we'll be replacing the selected text with StringBuffer strbuf = new StringBuffer ( ); // If they selected a partial line, count it as a full one ps.selectCompleteLines ( ); int i; try { // For each line, strip their whitespace for ( i = ps.startLineIndex; i <= ps.endLineIndex; i++ ) { String line = ps.doc.get ( ps.doc.getLineInformation ( i ).getOffset ( ), ps.doc.getLineInformation ( i ).getLength ( ) ); strbuf.append ( line.replaceAll ( "\t", getTabSpace ( ) ) + ( i < ps.endLineIndex ? ps.endLineDelim : "" ) ); } // If all goes well, replace the text with the modified information if ( strbuf.toString ( ) != null ) { ps.doc.replace ( ps.startLine.getOffset ( ), ps.selLength, strbuf.toString ( ) ); return true; } } catch ( Exception e ) { beep( e ); } // In event of problems, return false return false; } } --- NEW FILE: PyRemoveBlockCommentTest.java --- /* * Created on Jun 17, 2004 * * To change the template for this generated file go to * Window>Preferences>Java>Code Generation>Code and Comments */ package org.python.pydev.editor.actions; import org.eclipse.jface.text.Document; import org.eclipse.jface.text.IDocument; import junit.framework.TestCase; /** * @author Dreamer * * Tests the 'Remove Block Comment' editor feature. It performs 3 checks. * * The first fakes a selection of a couple lines in a fake document, and checks to see that the * code is properly un-commented. * * The second fakes a selection but stops in the middle of a line, to make sure that the proper * lines are un-commented, including the beginning of partial lines. * * The third selects nothing, and makes sure that the no lines are otherwise affected. * TODO Maybe in the future, selecting anything within a comment block and calling the action will remove the entire surrounding block comment */ public class PyRemoveBlockCommentTest extends TestCase { /* The document that will fake an editor environment */ IDocument document; /* Lines of 'code' in the fake document */ String [] documentLines; /* For my own debugging edification, to output the name later */ static final String TestFileName = "PyRemoveBlockCommentTest"; /** * Constructor for PyAddBlockCommentTest. * @param arg0 */ public PyRemoveBlockCommentTest(String arg0) { super(arg0); } /* * Sets up the document 'code' and adds it to the document. * * @see TestCase#setUp() */ protected void setUp() throws Exception { super.setUp(); int i = -1; int length = 7; documentLines = new String[length]; documentLines[++i] = "def bar ( self ):"; documentLines[++i] = "\tprint \"foo1\"\t "; documentLines[++i] = "\tprint \"bar1\""; documentLines[++i] = "\t "; documentLines[++i] = "def foo ( self ): "; documentLines[++i] = "\tprint \"foo2\"\t "; documentLines[++i] = "\tprint \"bar2\" "; StringBuffer doc = new StringBuffer ( ); for ( i = 0; i < documentLines.length; i++ ) { doc.append ( documentLines[i] + ( i < documentLines.length - 1 ? "\n" : "" ) ); } document = new Document ( doc.toString ( ) ); } /* * @see TestCase#tearDown() */ protected void tearDown() throws Exception { super.tearDown(); } /* * Just to shorten the lines in the later tests, this calls the action's get comment line * function * * @return String Comment line */ public String getFullCommentLine ( ) { return PyAddBlockComment.getFullCommentLine ( ); } /* * Just to shorten the lines in the later tests, this calls the action's comment function * * @param in Line of 'code' to be commented * @return String Commented 'code' line */ public boolean callComment ( PySelection ps ) { return PyRemoveBlockComment.perform ( ps ); } /* * Checks multiple-line selection. */ public void testPerform1 ( ) { StringBuffer result = new StringBuffer ( ); int i = -1; int startLineIndex = 0; int endLineIndex = 0; int selBegin = 0; int selLength = 0; // 'Select' the entire last def result.append ( documentLines[++i] + "\n" ); // "def bar ( self ):\n" result.append ( documentLines[++i] + "\n" ); // "\tprint \"foo1\"\t \n" result.append ( documentLines[++i] + "\n" ); // "\tprint \"bar1\"\n" result.append ( documentLines[++i] + "\n" ); // "\n \n" startLineIndex = i + 1; selBegin = result.toString ( ).length ( ); result.append ( "#" + getFullCommentLine ( ) + "\n" ); result.append ( "#" + documentLines[++i] + "\n" ); // "def foo ( self ): \n" result.append ( "#" + documentLines[++i] + "\n" ); // "\tprint \"foo2\"\t \n" result.append ( "#" + documentLines[++i] + "\n" ); // "\tprint \"bar2\" \n" result.append ( "#" + getFullCommentLine ( ) ); endLineIndex = i + 2; selLength = result.toString ( ).length ( ) - selBegin; // Our expected result IDocument resultDoc = new Document ( result.toString ( ) ); // For timing data long begin = System.currentTimeMillis ( ); PySelection ps = new PySelection ( resultDoc, startLineIndex, endLineIndex, selLength, false ); PyRemoveBlockComment.perform ( ps ); long end = System.currentTimeMillis ( ); // Timing result System.err.print ( TestFileName + " :: " ); System.err.println ( "testPerform1: " + ( end - begin ) + "ms" ); // Document affected properly? assertEquals ( document.get ( ), resultDoc.get ( ) ); } /* * Checks multiple-line selection with the last line partially selected. */ public void testPerform2 ( ) { StringBuffer result = new StringBuffer ( ); int i = -1; int startLineIndex = 0; int endLineIndex = 0; int selBegin = 0; int selLength = 0; // 'Select' part of the last def result.append ( documentLines[++i] + "\n" ); // "def bar ( self ):\n" result.append ( documentLines[++i] + "\n" ); // "\tprint \"foo1\"\t \n" result.append ( documentLines[++i] + "\n" ); // "\tprint \"bar1\"\n" result.append ( documentLines[++i] + "\n" ); // "\n \n" startLineIndex = i + 1; selBegin = result.toString ( ).length ( ); result.append ( "#" + getFullCommentLine ( ) + "\n" ); result.append ( "#" + documentLines[++i] + "\n" ); // "def foo ( self ): \n" result.append ( "#" + documentLines[++i] + "\n" ); // "\tprint \"foo2\"\t \n" result.append ( "#" + documentLines[++i] + "\n" ); // "\tprint \"bar2\" \n" result.append ( "#" + getFullCommentLine ( ) ); endLineIndex = i + 2; selLength = result.toString ( ).length ( ) - selBegin - 6; // Our expected result IDocument resultDoc = new Document ( result.toString ( ) ); // For timing data long begin = System.currentTimeMillis ( ); PySelection ps = new PySelection ( resultDoc, startLineIndex, endLineIndex, selLength, false ); PyRemoveBlockComment.perform ( ps ); long end = System.currentTimeMillis ( ); // Timing result System.err.print ( TestFileName + " :: " ); System.err.println ( "testPerform2: " + ( end - begin ) + "ms" ); // Document affected properly? assertEquals ( document.get ( ), resultDoc.get ( ) ); } /* * Checks multiple-line selection with the last line partially selected. */ public void testPerform3 ( ) { StringBuffer result = new StringBuffer ( ); int i = -1; int startLineIndex = 0; int endLineIndex = 0; int selBegin = 0; int selLength = 0; // 'Select' in middle of one line, show that it blocks that whole line result.append ( documentLines[++i] + "\n" ); // "def bar ( self ):\n" result.append ( documentLines[++i] + "\n" ); // "\tprint \"foo1\"\t \n" result.append ( documentLines[++i] + "\n" ); // "\tprint \"bar1\"\n" result.append ( documentLines[++i] + "\n" ); // "\n \n" result.append ( documentLines[++i] + "\n" ); // "def foo ( self ): \n" selBegin = result.toString ( ).length ( ) + 5; result.append ( "#" + getFullCommentLine ( ) + "\n" ); result.append ( "#" + documentLines[++i] + "\n" ); // "\tprint \"foo2\"\t \n" startLineIndex = i + 1; endLineIndex = startLineIndex; result.append ( "#" + getFullCommentLine ( ) + "\n" ); result.append ( documentLines[++i] ); // "\tprint \"bar2\" \n" // Our expected result IDocument resultDoc = new Document ( result.toString ( ) ); // For timing data long begin = System.currentTimeMillis ( ); PySelection ps = new PySelection ( resultDoc, startLineIndex, endLineIndex, selLength, false ); PyRemoveBlockComment.perform ( ps ); long end = System.currentTimeMillis ( ); // Timing result System.err.print ( TestFileName + " :: " ); System.err.println ( "testPerform3: " + ( end - begin ) + "ms" ); // Document affected properly? assertEquals ( result.toString ( ), resultDoc.get ( ) ); } } Index: PyAction.java =================================================================== RCS file: /cvsroot/pydev/org.python.pydev/src/org/python/pydev/editor/actions/PyAction.java,v retrieving revision 1.4 retrieving revision 1.5 diff -C2 -d -r1.4 -r1.5 *** PyAction.java 10 Apr 2004 01:48:14 -0000 1.4 --- PyAction.java 21 Jun 2004 21:36:29 -0000 1.5 *************** *** 60,64 **** * @throws BadLocationException */ ! protected String getDelimiter(IDocument doc, int startLineIndex) throws BadLocationException { String endLineDelim = doc.getLineDelimiter(startLineIndex); --- 60,64 ---- * @throws BadLocationException */ ! protected static String getDelimiter(IDocument doc, int startLineIndex) throws BadLocationException { String endLineDelim = doc.getLineDelimiter(startLineIndex); *************** *** 198,202 **** * Beep...humm... yeah....beep....ehehheheh */ ! protected void beep(Exception e) { PlatformUI.getWorkbench().getActiveWorkbenchWindow().getShell().getDisplay().beep(); e.printStackTrace(); --- 198,202 ---- * Beep...humm... yeah....beep....ehehheheh */ ! protected static void beep(Exception e) { PlatformUI.getWorkbench().getActiveWorkbenchWindow().getShell().getDisplay().beep(); e.printStackTrace(); Index: PyUncomment.java =================================================================== RCS file: /cvsroot/pydev/org.python.pydev/src/org/python/pydev/editor/actions/PyUncomment.java,v retrieving revision 1.1 retrieving revision 1.2 diff -C2 -d -r1.1 -r1.2 *** PyUncomment.java 28 Feb 2004 05:13:16 -0000 1.1 --- PyUncomment.java 21 Jun 2004 21:36:29 -0000 1.2 *************** *** 7,16 **** package org.python.pydev.editor.actions; /** * @author fabioz */ ! public class PyUncomment extends PyComment { /** * Same as comment, but remove the first char. --- 7,90 ---- package org.python.pydev.editor.actions; + import org.eclipse.jface.action.IAction; + /** * @author fabioz */ ! public class PyUncomment extends PyComment ! { ! /* Selection element */ ! private static PySelection ps; ! ! /** ! * Grabs the selection information and performs the action. ! */ ! public void run ( IAction action ) ! { ! try ! { ! // Select from text editor ! ps = new PySelection ( getTextEditor ( ), false ); ! // Perform the action ! perform ( ); ! ! // Put cursor at the first area of the selection ! getTextEditor ( ).selectAndReveal ( ps.endLine.getOffset ( ), 0 ); ! } ! catch ( Exception e ) ! { ! beep ( e ); ! } ! } ! ! ! /** ! * Performs the action with the class' PySelection. ! * ! * @return boolean The success or failure of the action ! */ ! public static boolean perform ( ) ! { ! return perform ( ps ); ! } ! ! ! /** ! * Performs the action with a given PySelection ! * ! * @param ps Given PySelection ! * @return boolean The success or failure of the action ! */ ! public static boolean perform ( PySelection ps ) ! { ! // What we'll be replacing the selected text with ! StringBuffer strbuf = new StringBuffer ( ); ! ! // If they selected a partial line, count it as a full one ! ps.selectCompleteLines ( ); ! ! int i; ! try ! { ! // For each line, comment them out ! for ( i = ps.startLineIndex; i <= ps.endLineIndex; i++ ) ! { ! if ( ps.getLine ( i ).startsWith ( "#" ) ) ! strbuf.append ( ps.getLine ( i ).substring ( 1 ) + ( i < ps.endLineIndex ? ps.endLineDelim : "" ) ); ! } + // Replace the text with the modified information + ps.doc.replace ( ps.startLine.getOffset ( ), ps.selLength, strbuf.toString ( ) ); + return true; + } + catch ( Exception e ) + { + beep ( e ); + } + + // In event of problems, return false + return false; + } /** * Same as comment, but remove the first char. --- NEW FILE: PyUncommentTest.java --- /* * Created on Jun 17, 2004 * * To change the template for this generated file go to * Window>Preferences>Java>Code Generation>Code and Comments */ package org.python.pydev.editor.actions; import org.eclipse.jface.text.Document; import org.eclipse.jface.text.IDocument; import junit.framework.TestCase; /** * @author Dreamer * * Tests the 'Uncomment' editor feature. It performs 3 checks. * * The first fakes a selection of a couple lines in a fake document, and checks to see that the * code is properly un-commented. * * The second fakes a selection but stops in the middle of a line, to make sure that the proper * lines are un-commented, including the beginning of partial lines. * * The third selects nothing, and makes sure that the only line affected is the one the cursor * is on. */ public class PyUncommentTest extends TestCase { /* The document that will fake an editor environment */ IDocument document; /* Lines of 'code' in the fake document */ String [] documentLines; /* For my own debugging edification, to output the name later */ static final String TestFileName = "PyUncommentTest"; /** * Constructor for PyUncommentTest. * @param arg0 */ public PyUncommentTest(String arg0) { super(arg0); } /* * Sets up the document 'code' and adds it to the document. * * @see TestCase#setUp() */ protected void setUp() throws Exception { super.setUp(); int i = -1; int length = 7; documentLines = new String[length]; documentLines[++i] = "def bar ( self ):"; documentLines[++i] = "\tprint \"foo1\"\t "; documentLines[++i] = "\tprint \"bar1\""; documentLines[++i] = "\t "; documentLines[++i] = "def foo ( self ): "; documentLines[++i] = "\tprint \"foo2\"\t "; documentLines[++i] = "\tprint \"bar2\" "; StringBuffer doc = new StringBuffer ( ); for ( i = 0; i < documentLines.length; i++ ) { doc.append ( documentLines[i] + ( i < documentLines.length - 1 ? "\n" : "" ) ); } document = new Document ( doc.toString ( ) ); } /* * @see TestCase#tearDown() */ protected void tearDown() throws Exception { super.tearDown(); } /* * Just to shorten the lines in the later tests, this calls the action's uncomment function * * @param in Line of 'code' to be commented * @return String Commented 'code' line */ public boolean callComment ( PySelection ps ) { return PyUncomment.perform ( ps ); } /* * Checks multiple-line selection. */ public void testPerform1 ( ) { StringBuffer result = new StringBuffer ( ); int i = -1; int startLineIndex = 0; int endLineIndex = 0; int selBegin = 0; int selLength = 0; // 'Select' the entire last def result.append ( documentLines[++i] + "\n" ); // "def bar ( self ):\n" result.append ( documentLines[++i] + "\n" ); // "\tprint \"foo1\"\t \n" result.append ( documentLines[++i] + "\n" ); // "\tprint \"bar1\"\n" result.append ( documentLines[++i] + "\n" ); // "\n \n" startLineIndex = i + 1; selBegin = result.toString ( ).length ( ); result.append ( "#" + documentLines[++i] + "\n" ); // "def foo ( self ): \n" result.append ( "#" + documentLines[++i] + "\n" ); // "\tprint \"foo2\"\t \n" result.append ( "#" + documentLines[++i] ); // "\tprint \"bar2\" \n" endLineIndex = i; selLength = document.get ( ).length ( ) - selBegin; // Our expected result IDocument resultDoc = new Document ( result.toString ( ) ); // For timing data long begin = System.currentTimeMillis ( ); PySelection ps = new PySelection ( resultDoc, startLineIndex, endLineIndex, selLength, false ); PyUncomment.perform ( ps ); long end = System.currentTimeMillis ( ); // Timing result System.err.print ( TestFileName + " :: " ); System.err.println ( "testPerform1: " + ( end - begin ) + "ms" ); // Document affected properly? assertEquals ( document.get ( ), resultDoc.get ( ) ); } /* * Checks multiple-line selection with the last line partially selected. */ public void testPerform2 ( ) { StringBuffer result = new StringBuffer ( ); int i = -1; int startLineIndex = 0; int endLineIndex = 0; int selBegin = 0; int selLength = 0; // 'Select' part of the last def result.append ( documentLines[++i] + "\n" ); // "def bar ( self ):\n" result.append ( documentLines[++i] + "\n" ); // "\tprint \"foo1\"\t \n" result.append ( documentLines[++i] + "\n" ); // "\tprint \"bar1\"\n" result.append ( documentLines[++i] + "\n" ); // "\n \n" startLineIndex = i + 1; selBegin = result.toString ( ).length ( ); result.append ( "#" + documentLines[++i] + "\n" ); // "def foo ( self ): \n" result.append ( "#" + documentLines[++i] + "\n" ); // "\tprint \"foo2\"\t \n" result.append ( "#" + documentLines[++i] ); // "\tprint \"bar2\" \n" endLineIndex = i; selLength = document.get ( ).length ( ) - selBegin - 6; // Our expected result IDocument resultDoc = new Document ( result.toString ( ) ); // For timing data long begin = System.currentTimeMillis ( ); PySelection ps = new PySelection ( resultDoc, startLineIndex, endLineIndex, selLength, false ); PyUncomment.perform ( ps ); long end = System.currentTimeMillis ( ); // Timing result System.err.print ( TestFileName + " :: " ); System.err.println ( "testPerform2: " + ( end - begin ) + "ms" ); // Document affected properly? assertEquals ( document.get ( ), resultDoc.get ( ) ); } /* * Checks empty selection to affect cursor line. */ public void testPerform3 ( ) { StringBuffer result = new StringBuffer ( ); int i = -1; int startLineIndex = 0; int endLineIndex = 0; int selBegin = 0; int selLength = 0; // 'Select' in middle of one line, show that it blocks that whole line only result.append ( documentLines[++i] + "\n" ); // "def bar ( self ):\n" result.append ( documentLines[++i] + "\n" ); // "\tprint \"foo1\"\t \n" result.append ( documentLines[++i] + "\n" ); // "\tprint \"bar1\"\n" result.append ( documentLines[++i] + "\n" ); // "\n \n" result.append ( documentLines[++i] + "\n" ); // "def foo ( self ): \n" selBegin = result.toString ( ).length ( ) + 5; result.append ( "#" + documentLines[++i] + "\n" ); // "\tprint \"foo2\"\t \n" startLineIndex = i; endLineIndex = startLineIndex; result.append ( documentLines[++i] ); // "\tprint \"bar2\" \n" // Our expected result IDocument resultDoc = new Document ( result.toString ( ) ); // For timing data long begin = System.currentTimeMillis ( ); PySelection ps = new PySelection ( resultDoc, startLineIndex, endLineIndex, selLength, false ); PyUncomment.perform ( ps ); long end = System.currentTimeMillis ( ); // Timing result System.err.print ( TestFileName + " :: " ); System.err.println ( "testPerform3: " + ( end - begin ) + "ms" ); // Document affected properly? assertEquals ( document.get ( ), resultDoc.get ( ) ); } } Index: PyAddBlockComment.java =================================================================== RCS file: /cvsroot/pydev/org.python.pydev/src/org/python/pydev/editor/actions/PyAddBlockComment.java,v retrieving revision 1.6 retrieving revision 1.7 diff -C2 -d -r1.6 -r1.7 *** PyAddBlockComment.java 11 Jun 2004 20:03:56 -0000 1.6 --- PyAddBlockComment.java 21 Jun 2004 21:36:29 -0000 1.7 *************** *** 9,12 **** --- 9,13 ---- import org.python.pydev.plugin.PydevPrefs; import org.eclipse.core.runtime.Preferences; + import org.eclipse.jface.action.IAction; import org.python.pydev.plugin.PydevPlugin; *************** *** 21,51 **** * @author Parhaum Toofanian */ ! public class PyAddBlockComment extends PyComment { /** ! * This method is called to return the text that should be replaced ! * by the text passed as a parameter. * ! * The text passed as a parameter represents the text from the whole ! * lines of the selection. * ! * @param str the string to be replaced. ! * @param endLineDelim delimiter used. ! * @return the new string. */ ! protected String replaceStr(String str, String endLineDelim) { ! return "#" + getFullCommentLine ( ) + endLineDelim + ! "#" + str.replaceAll(endLineDelim, endLineDelim + "#") + ! endLineDelim + "#" + getFullCommentLine ( ); } /** * Currently returns a string with the comment block. ! * @return Comment line string */ ! protected String getFullCommentLine(){ ! Preferences prefs = PydevPlugin.getDefault().getPluginPreferences(); ! return prefs.getString(PydevPrefs.BLOCK_COMMENT); } - } --- 22,123 ---- * @author Parhaum Toofanian */ ! public class PyAddBlockComment extends PyAction ! { ! /* Selection element */ ! private static PySelection ps; ! /** ! * Grabs the selection information and performs the action. ! */ ! public void run ( IAction action ) ! { ! try ! { ! // Select from text editor ! ps = new PySelection ( getTextEditor ( ), false ); ! // Perform the action ! perform ( ); ! ! // Put cursor at the first area of the selection ! getTextEditor ( ).selectAndReveal ( ps.endLine.getOffset ( ), 0 ); ! } ! catch ( Exception e ) ! { ! beep ( e ); ! } ! } ! ! ! /** ! * Performs the action with the class' PySelection. * ! * @return boolean The success or failure of the action ! */ ! public static boolean perform ( ) ! { ! return perform ( ps ); ! } ! ! ! /** ! * Performs the action with a given PySelection * ! * @param ps Given PySelection ! * @return boolean The success or failure of the action */ ! public static boolean perform ( PySelection ps ) ! { ! // What we'll be replacing the selected text with ! StringBuffer strbuf = new StringBuffer ( ); ! ! // If they selected a partial line, count it as a full one ! ps.selectCompleteLines ( ); ! ! int i; ! try ! { ! // Start of block ! strbuf.append ( "#" + getFullCommentLine ( ) + ps.endLineDelim ); ! ! // For each line, comment them out ! for ( i = ps.startLineIndex; i <= ps.endLineIndex; i++ ) ! { ! strbuf.append ( "#" + ps.getLine ( i ) + ps.endLineDelim ); ! } ! ! // End of block ! strbuf.append ( "#" + getFullCommentLine ( ) ); ! ! // Replace the text with the modified information ! ps.doc.replace ( ps.startLine.getOffset ( ), ps.selLength, strbuf.toString ( ) ); ! return true; ! } ! catch ( Exception e ) ! { ! beep ( e ); ! } ! ! // In event of problems, return false ! return false; } + /** * Currently returns a string with the comment block. ! * ! * @return Comment line string, or a default one if Preferences are null */ ! protected static String getFullCommentLine ( ) ! { ! try ! { ! Preferences prefs = PydevPlugin.getDefault().getPluginPreferences(); ! return prefs.getString(PydevPrefs.BLOCK_COMMENT); ! } ! catch ( Exception e ) ! { ! return "========================="; ! } } } Index: PyRemoveBlockComment.java =================================================================== RCS file: /cvsroot/pydev/org.python.pydev/src/org/python/pydev/editor/actions/PyRemoveBlockComment.java,v retrieving revision 1.1 retrieving revision 1.2 diff -C2 -d -r1.1 -r1.2 *** PyRemoveBlockComment.java 11 Jun 2004 20:03:56 -0000 1.1 --- PyRemoveBlockComment.java 21 Jun 2004 21:36:29 -0000 1.2 *************** *** 7,10 **** --- 7,12 ---- package org.python.pydev.editor.actions; + import org.eclipse.jface.action.IAction; + /** *************** *** 19,44 **** * @author Parhaum Toofanian */ ! public class PyRemoveBlockComment extends PyAddBlockComment { /** ! * This method is called to return the text that should be replaced ! * by the text passed as a parameter. * ! * The text passed as a parameter represents the text from the whole ! * lines of the selection. * ! * @param str the string to be replaced. ! * @param endLineDelim delimiter used. ! * @return the new string. */ ! protected String replaceStr(String str, String endLineDelim) { ! str = str.replaceAll(endLineDelim+"#", endLineDelim ); ! if (str.startsWith("#")){ ! str = str.substring(1); } - str = str.replaceAll(getFullCommentLine()+endLineDelim, ""); - str = str.replaceAll(endLineDelim+getFullCommentLine(), ""); ! return str; } --- 21,105 ---- * @author Parhaum Toofanian */ ! public class PyRemoveBlockComment extends PyAddBlockComment ! { ! /* Selection element */ ! private static PySelection ps; ! /** ! * Grabs the selection information and performs the action. ! */ ! public void run ( IAction action ) ! { ! try ! { ! // Select from text editor ! ps = new PySelection ( getTextEditor ( ), false ); ! // Perform the action ! perform ( ); ! ! // Put cursor at the first area of the selection ! getTextEditor ( ).selectAndReveal ( ps.endLine.getOffset ( ), 0 ); ! } ! catch ( Exception e ) ! { ! beep ( e ); ! } ! } ! ! ! /** ! * Performs the action with the class' PySelection. * ! * @return boolean The success or failure of the action ! */ ! public static boolean perform ( ) ! { ! return perform ( ps ); ! } ! ! ! /** ! * Performs the action with a given PySelection * ! * @param ps Given PySelection ! * @return boolean The success or failure of the action */ ! public static boolean perform ( PySelection ps ) ! { ! // What we'll be replacing the selected text with ! StringBuffer strbuf = new StringBuffer ( ); ! ! // If they selected a partial line, count it as a full one ! ps.selectCompleteLines ( ); ! ! int i; ! try ! { ! // Start of block, if not block, don't bother ! if ( ! ps.getLine ( ps.startLineIndex ).equals ( "#" + getFullCommentLine ( ) ) ) ! return false; ! // End of block, if not block, don't bother ! if ( ! ps.getLine ( ps.endLineIndex ).equals ( "#" + getFullCommentLine ( ) ) ) ! return false; ! ! // For each line, comment them out ! for ( i = ps.startLineIndex + 1; i < ps.endLineIndex; i++ ) ! { ! if ( ps.getLine ( i ).startsWith ( "#" ) && ! ps.getLine ( i ).substring ( 1 ).equals ( getFullCommentLine ( ) ) ) ! strbuf.append ( ps.getLine ( i ).substring ( 1 ) + ( i < ps.endLineIndex - 1 ? ps.endLineDelim : "" ) ); ! } ! ! // Replace the text with the modified information ! ps.doc.replace ( ps.startLine.getOffset ( ), ps.selLength, strbuf.toString ( ) ); ! return true; ! } ! catch ( Exception e ) ! { ! beep ( e ); } ! // In event of problems, return false ! return false; } Index: PyComment.java =================================================================== RCS file: /cvsroot/pydev/org.python.pydev/src/org/python/pydev/editor/actions/PyComment.java,v retrieving revision 1.3 retrieving revision 1.4 diff -C2 -d -r1.3 -r1.4 *** PyComment.java 11 Jun 2004 20:03:56 -0000 1.3 --- PyComment.java 21 Jun 2004 21:36:29 -0000 1.4 *************** *** 8,82 **** import org.eclipse.jface.action.IAction; - import org.eclipse.jface.text.IDocument; - import org.eclipse.jface.text.IRegion; - import org.eclipse.jface.text.ITextSelection; - import org.eclipse.ui.texteditor.ITextEditor; /** * @author Fabio Zadrozny */ ! public class PyComment extends PyAction { /** ! * Comment the first lines. */ ! public void run(IAction action) { ! try { ! ! ITextEditor textEditor = getTextEditor(); ! ! IDocument doc = ! textEditor.getDocumentProvider().getDocument( ! textEditor.getEditorInput()); ! ITextSelection selection = ! (ITextSelection) textEditor ! .getSelectionProvider() ! .getSelection(); ! ! int startLineIndex = selection.getStartLine(); ! int endLineIndex = selection.getEndLine(); ! ! //special cases...first char of the editor, last char... ! if (endLineIndex < startLineIndex) { ! endLineIndex = startLineIndex; ! } ! ! IRegion startLine = doc.getLineInformation(startLineIndex); ! IRegion endLine = doc.getLineInformation(endLineIndex); ! ! int initialPos = startLine.getOffset(); ! int length = ! (endLine.getOffset() - startLine.getOffset()) ! + endLine.getLength(); ! ! String endLineDelim = getDelimiter(doc, startLineIndex); ! ! String str = doc.get(initialPos, length); ! ! str = replaceStr(str, endLineDelim); ! doc.replace(initialPos, length, str); ! ! // Select the new comment block automatically ! textEditor.selectAndReveal(initialPos,str.length()); ! } catch (Exception e) { ! beep(e); } } /** ! * This method is called to return the text that should be replaced ! * by the text passed as a parameter. ! * ! * The text passed as a parameter represents the text from the whole ! * lines of the selection. * ! * @param str the string to be replaced. ! * @param endLineDelim delimiter used. ! * @return the new string. */ ! protected String replaceStr(String str, String endLineDelim) { ! return "#" + str.replaceAll(endLineDelim, endLineDelim + "#"); } } --- 8,93 ---- import org.eclipse.jface.action.IAction; /** + * Creates a bulk comment. Comments all selected lines + * * @author Fabio Zadrozny + * @author Parhaum Toofanian */ ! public class PyComment extends PyAction ! { ! /* Selection element */ ! private static PySelection ps; ! /** ! * Grabs the selection information and performs the action. */ ! public void run ( IAction action ) ! { ! try ! { ! // Select from text editor ! ps = new PySelection ( getTextEditor ( ), false ); ! // Perform the action ! perform ( ); ! // Put cursor at the first area of the selection ! getTextEditor ( ).selectAndReveal ( ps.endLine.getOffset ( ), 0 ); ! } ! catch ( Exception e ) ! { ! beep ( e ); } } + /** ! * Performs the action with the class' PySelection. * ! * @return boolean The success or failure of the action */ ! public static boolean perform ( ) ! { ! return perform ( ps ); } + + /** + * Performs the action with a given PySelection + * + * @param ps Given PySelection + * @return boolean The success or failure of the action + */ + public static boolean perform ( PySelection ps ) + { + // What we'll be replacing the selected text with + StringBuffer strbuf = new StringBuffer ( ); + + // If they selected a partial line, count it as a full one + ps.selectCompleteLines ( ); + + int i; + try + { + // For each line, comment them out + for ( i = ps.startLineIndex; i < ps.endLineIndex; i++ ) + { + strbuf.append ( "#" + ps.getLine ( i ) + ps.endLineDelim ); + } + // Last line shouldn't add the delimiter + strbuf.append ( "#" + ps.getLine ( i ) ); + + // Replace the text with the modified information + ps.doc.replace ( ps.startLine.getOffset ( ), ps.selLength, strbuf.toString ( ) ); + return true; + } + catch ( Exception e ) + { + beep ( e ); + } + + // In event of problems, return false + return false; + } } --- NEW FILE: PyConvertTabToSpaceTest.java --- /* * Created on Jun 21, 2004 * * To change the template for this generated file go to * Window>Preferences>Java>Code Generation>Code and Comments */ package org.python.pydev.editor.actions; import org.eclipse.jface.text.Document; import org.eclipse.jface.text.IDocument; import junit.framework.TestCase; /** * @author Dreamer * * Tests the 'Convert Tab to Space' editor feature. It performs 3 checks. * * The first fakes a selection of a couple lines in a fake document, and checks * to see that the proper tabs are converted to spaces. * * The second fakes a selection but stops in the middle of a line, to make sure that the * whole line is considered. * * The third selects nothing, and makes sure that the whole document is affected by * the conversion. */ public class PyConvertTabToSpaceTest extends TestCase { /* The document that will fake an editor environment */ IDocument document; /* Lines of 'code' in the fake document */ String [] documentLines; /* For my own debugging edification, to output the name later */ static final String TestFileName = "PyConvertTabToSpaceTest"; /** * Constructor for PyConvertTabToSpaceTest. * @param arg0 */ public PyConvertTabToSpaceTest(String arg0) { super(arg0); } /* * Sets up the document 'code' and adds it to the document. * * @see TestCase#setUp() */ protected void setUp() throws Exception { super.setUp(); int i = -1; int length = 7; documentLines = new String[length]; // The X will be used to interchange tabs/spaces documentLines[++i] = "def bar ( self ):"; documentLines[++i] = "X" + "print \"foo1\"X "; documentLines[++i] = "X" + "print \"bar1\""; documentLines[++i] = "X "; documentLines[++i] = "def foo ( self ):X"; documentLines[++i] = "X" + "print \"foo2\"X "; documentLines[++i] = "X" + "print \"bar2\" "; StringBuffer doc = new StringBuffer ( ); for ( i = 0; i < documentLines.length; i++ ) { doc.append ( documentLines[i] + ( i < documentLines.length - 1 ? "\n" : "" ) ); } document = new Document ( doc.toString ( ) ); } /* * @see TestCase#tearDown() */ protected void tearDown() throws Exception { super.tearDown(); } /* * Just for this and the vice versa case, to make things easier * * @return String Space string */ public String from ( ) { return "\t"; } /* * Just for this and the vice versa case, to make things easier * * @return String Tab string */ public String to ( ) { return PyConvertTabToSpace.getTabSpace ( ); } /* * Just to shorten the lines in the later tests, this calls the action's get tab width * function * * @return int Tab width */ public int getTabWidth ( ) { return PyConvertTabToSpace.getTabWidth ( ); } /* * Just to shorten the lines in the later tests, this calls the action's trim function * * @param in Line of 'code' to be stripped * @return String Stripped 'code' line */ public String callTrim ( String in ) { return PyStripTrailingWhitespace.trimTrailingWhitespace ( in ); } /* * Checks multiple-line selection. */ public void testPerform1 ( ) { StringBuffer result = new StringBuffer ( ); int i = -1; int startLineIndex = 0; int endLineIndex = 0; int selBegin = 0; int selLength = 0; // 'Select' the entire last def String [] strArr = document.get ( ).replaceAll ( "X", from ( ) ).split ( "\n" ); int len = 0; len += strArr[++i].length ( ) + 1; // "def bar ( self ):\n" len += strArr[++i].length ( ) + 1; // "\tprint \"foo1\"\t \n" len += strArr[++i].length ( ) + 1; // "\tprint \"bar1\"\n" len += strArr[++i].length ( ) + 1; // "\n \n" startLineIndex = i + 1; selBegin = len; len += strArr[++i].length ( ) + 1; // "def foo ( self ): len += strArr[++i].length ( ) + 1; // "\tprint \"foo2\"\t \n" len += strArr[++i].length ( ); // "\tprint \"bar2\" \n" endLineIndex = i; selLength = len - selBegin; // Create result document i = -1; result.append ( documentLines[++i].replaceAll ( "X", from ( ) ) + "\n" ); result.append ( documentLines[++i].replaceAll ( "X", from ( ) ) + "\n" ); result.append ( documentLines[++i].replaceAll ( "X", from ( ) ) + "\n" ); result.append ( documentLines[++i].replaceAll ( "X", from ( ) ) + "\n" ); result.append ( documentLines[++i].replaceAll ( "X", to ( ) ) + "\n" ); result.append ( documentLines[++i].replaceAll ( "X", to ( ) ) + "\n" ); result.append ( documentLines[++i].replaceAll ( "X", to ( ) ) ); // Our expected result IDocument resultDoc = new Document ( result.toString ( ) ); // For timing data document.set ( document.get ( ).replaceAll ( "X", from ( ) ) ); long begin = System.currentTimeMillis ( ); PySelection ps = new PySelection ( document, startLineIndex, endLineIndex, selLength, true ); PyConvertTabToSpace.perform ( ps ); long end = System.currentTimeMillis ( ); // Timing result System.err.print ( TestFileName + " :: " ); System.err.println ( "testPerform1: " + ( end - begin ) + "ms" ); // Document affected properly? assertEquals ( document.get ( ), resultDoc.get ( ) ); } /* * Checks multiple-line selection with the last line partially selected. */ public void testPerform2 ( ) { StringBuffer result = new StringBuffer ( ); int i = -1; int startLineIndex = 0; int endLineIndex = 0; int selBegin = 0; int selLength = 0; // 'Select' the entire last def String [] strArr = document.get ( ).replaceAll ( "X", from ( ) ).split ( "\n" ); int len = 0; len += strArr[++i].length ( ) + 1; // "def bar ( self ):\n" len += strArr[++i].length ( ) + 1; // "\tprint \"foo1\"\t \n" len += strArr[++i].length ( ) + 1; // "\tprint \"bar1\"\n" len += strArr[++i].length ( ) + 1; // "\n \n" startLineIndex = i + 1; selBegin = len; len += strArr[++i].length ( ) + 1; // "def foo ( self ): len += strArr[++i].length ( ) + 1; // "\tprint \"foo2\"\t \n" len += strArr[++i].length ( ); // "\tprint \"bar2\" \n" endLineIndex = i; selLength = len - selBegin - 6; // Create result document i = -1; result.append ( documentLines[++i].replaceAll ( "X", from ( ) ) + "\n" ); result.append ( documentLines[++i].replaceAll ( "X", from ( ) ) + "\n" ); result.append ( documentLines[++i].replaceAll ( "X", from ( ) ) + "\n" ); result.append ( documentLines[++i].replaceAll ( "X", from ( ) ) + "\n" ); result.append ( documentLines[++i].replaceAll ( "X", to ( ) ) + "\n" ); result.append ( documentLines[++i].replaceAll ( "X", to ( ) ) + "\n" ); result.append ( documentLines[++i].replaceAll ( "X", to ( ) ) ); // Our expected result IDocument resultDoc = new Document ( result.toString ( ) ); // For timing data document.set ( document.get ( ).replaceAll ( "X", from ( ) ) ); long begin = System.currentTimeMillis ( ); PySelection ps = new PySelection ( document, startLineIndex, endLineIndex, selLength, true ); PyConvertTabToSpace.perform ( ps ); long end = System.currentTimeMillis ( ); // Timing result System.err.print ( TestFileName + " :: " ); System.err.println ( "testPerform2: " + ( end - begin ) + "ms" ); // Document affected properly? assertEquals ( document.get ( ), resultDoc.get ( ) ); } /* * Checks empty selection to affect cursor line. */ public void testPerform3 ( ) { StringBuffer result = new StringBuffer ( ); int i = -1; int startLineIndex = 0; int endLineIndex = 0; int selBegin = 0; int selLength = 0; // 'Select' the entire last def startLineIndex = endLineIndex = 1; selBegin = 1; selLength = 0; // Our expected result IDocument resultDoc = new Document ( document.get ( ).toString ( ).replaceAll ( "X", to ( ) ) ); // For timing data document.set ( document.get ( ).replaceAll ( "X", from ( ) ) ); long begin = System.currentTimeMillis ( ); PySelection ps = new PySelection ( document, startLineIndex, endLineIndex, selLength, true ); PyConvertTabToSpace.perform ( ps ); long end = System.currentTimeMillis ( ); // Timing result System.err.print ( TestFileName + " :: " ); System.err.println ( "testPerform3: " + ( end - begin ) + "ms" ); // Document affected properly? assertEquals ( document.get ( ), resultDoc.get ( ) ); } } Index: PyStripTrailingWhitespace.java =================================================================== RCS file: /cvsroot/pydev/org.python.pydev/src/org/python/pydev/editor/actions/PyStripTrailingWhitespace.java,v retrieving revision 1.1 retrieving revision 1.2 diff -C2 -d -r1.1 -r1.2 *** PyStripTrailingWhitespace.java 11 Jun 2004 20:03:56 -0000 1.1 --- PyStripTrailingWhitespace.java 21 Jun 2004 21:36:29 -0000 1.2 *************** *** 8,15 **** import org.eclipse.jface.action.IAction; ! import org.eclipse.jface.text.IDocument; ! import org.eclipse.jface.text.IRegion; ! import org.eclipse.jface.text.ITextSelection; ! import org.eclipse.ui.texteditor.ITextEditor; /** --- 8,12 ---- import org.eclipse.jface.action.IAction; ! /** *************** *** 19,120 **** * @author Parhaum Toofanian */ ! public class PyStripTrailingWhitespace extends PyAction { /** ! * Strip whitespace. */ ! public void run(IAction action) { ! try { ! // Grab the editor ! ITextEditor textEditor = getTextEditor(); ! // Grab the document ! IDocument doc = ! textEditor.getDocumentProvider().getDocument( ! textEditor.getEditorInput()); ! ! // Grab the selection ! ITextSelection selection = ! (ITextSelection) textEditor ! .getSelectionProvider() ! .getSelection(); ! ! // What we'll be modifying ! String str = new String ( ); ! // End line delimiter character ! String endLineDelim = new String ( ); ! ! // Get some line information ! int initialPos = 0; ! int length = 0; ! int startLineIndex = selection.getStartLine(); ! int endLineIndex = selection.getEndLine(); ! //special cases...first char of the editor, last char... ! if (endLineIndex < startLineIndex) { ! endLineIndex = startLineIndex; ! } ! IRegion startLine = doc.getLineInformation(startLineIndex); ! IRegion endLine = doc.getLineInformation(endLineIndex); ! ! // If anything is actually selected, we'll be modifying the selection only ! if ( selection.getLength ( ) > 0 ) ! { ! ! // Get offsets and lengths ! initialPos = startLine.getOffset(); ! length = ! (endLine.getOffset() - startLine.getOffset()) ! + endLine.getLength(); ! ! endLineDelim = getDelimiter(doc, startLineIndex); ! ! // Grab the selected text into our string ! str = doc.get(initialPos, length); ! } ! // Otherwise we'll modify the whole document ! else ! { ! // Grab the whole document ! str = doc.get ( ); ! endLineDelim = getDelimiter ( doc, 0 ); ! length = doc.getLength(); ! } ! // We can't use trim() since we only want to get rid of trailing whitespace, ! // so we need to go line by line. ! String [] lines = str.split ( endLineDelim ); ! // What we'll be replacing the selected text with ! StringBuffer strbuf = new StringBuffer ( ); ! // For all but the last line, trim whitespace normally ! for ( int i = 0; i < lines.length - 1; i++ ) { ! strbuf.append ( trimTrailingWhitespace ( lines[i] ) + endLineDelim ); } ! // Handle the last line differently, because it might not be a full line with // and ending delimiter, so we don't just want to trim whitespace (it could be // ending in the middle of a line, and this would screw up the code, which we // imagine the developer doesn't want to do ! if ( lines.length > 0 ) { // Check if full last line is selected or not ! if ( lines[lines.length - 1].length ( ) == endLine.getLength ( ) ) ! strbuf.append ( trimTrailingWhitespace ( lines[lines.length - 1] ) ); else ! strbuf.append ( lines[lines.length - 1] ); } - - // Replace selection with our new stripped text - doc.replace(initialPos, length, strbuf.toString ( ) ); - - // Put cursor at the first area of the selection - textEditor.selectAndReveal(endLine.getOffset(),0); ! } catch (Exception e) { ! beep(e); ! } } /** * This method is called to check for trailing whitespace and get rid of it --- 16,115 ---- * @author Parhaum Toofanian */ ! public class PyStripTrailingWhitespace extends PyAction ! { ! /* Selection element */ ! private static PySelection ps; ! /** ! * Grabs the selection information and performs the action. */ ! public void run ( IAction action ) ! { ! try ! { ! // Select from text editor ! ps = new PySelection ( getTextEditor ( ), false ); ! // Perform the action ! perform ( ); ! // Put cursor at the first area of the selection ! getTextEditor ( ).selectAndReveal ( ps.endLine.getOffset ( ), 0 ); ! } ! catch ( Exception e ) ! { ! beep ( e ); ! } ! } ! ! /** ! * Performs the action with the class' PySelection. ! * ! * @return boolean The success or failure of the action ! */ ! public static boolean perform ( ) ! { ! return perform ( ps ); ! } ! ! ! /** ! * Performs the action with a given PySelection ! * ! * @param ps Given PySelection ! * @return boolean The success or failure of the action ! */ ! public static boolean perform ( PySelection ps ) ! { ! // What we'll be replacing the selected text with ! StringBuffer strbuf = new StringBuffer ( ); ! int i; ! ! try ! { ! // For each line, strip their whitespace ! for ( i = ps.startLineIndex; i < ps.endLineIndex; i++ ) { ! strbuf.append ( trimTrailingWhitespace ( ps.doc.get ( ps.doc.getLineInformation ( i ).getOffset ( ), ps.doc.getLineInformation ( i ).getLength ( ) ) ) + ps.endLineDelim ); } ! // Handle the last line differently, because it might not be a full line with // and ending delimiter, so we don't just want to trim whitespace (it could be // ending in the middle of a line, and this would screw up the code, which we // imagine the developer doesn't want to do ! if ( ps.endLineIndex - ps.startLineIndex > 0 ) { + String lastline = ps.doc.get ( ps.endLine.getOffset ( ), ps.startLine.getOffset ( ) + ps.selLength - ps.endLine.getOffset ( ) ); + // Check if full last line is selected or not ! if ( lastline.length ( ) == ps.endLine.getLength ( ) ) ! { ! strbuf.append ( trimTrailingWhitespace ( lastline ) ); ! } else ! { ! strbuf.append ( lastline ); ! } } ! // If all goes well, replace the text with the modified information ! if ( strbuf.toString ( ) != null ) ! { ! ps.doc.replace ( ps.startLine.getOffset ( ), ps.selLength, strbuf.toString ( ) ); ! return true; ! } ! } ! catch ( Exception e ) ! { ! beep( e ); ! } ! ! // In event of problems, return false ! return false; } + /** * This method is called to check for trailing whitespace and get rid of it *************** *** 122,128 **** * @param str the string to be checked. * @return String the string, stripped of whitespace, or an empty string. ! */ private String trimTrailingWhitespace ( String str ) { // If nothing at all, just return if ( str.length ( ) == 0 ) return ""; --- 117,125 ---- * @param str the string to be checked. * @return String the string, stripped of whitespace, or an empty string. ! */ public static String trimTrailingWhitespace ( String str ) { // If nothing at all, just return + if ( str == null ) + return ""; if ( str.length ( ) == 0 ) return ""; *************** *** 153,156 **** } } - } --- 150,152 ---- --- NEW FILE: AllTests.java --- /* * Created on Jun 17, 2004 * * To change the template for this generated file go to * Window>Preferences>Java>Code Generation>Code and Comments */ package org.python.pydev.editor.actions; import junit.framework.Test; import junit.framework.TestSuite; /** * @author Dreamer * * To change the template for this generated type comment go to * Window>Preferences>Java>Code Generation>Code and Comments */ public class AllTests { public static String TestFileName = "Editor.Actions"; public static Test suite() { TestSuite suite = new TestSuite("Test for org.python.pydev.editor.actions"); //$JUnit-BEGIN$ suite.addTest(new TestSuite(PyCommentTest.class)); suite.addTest(new TestSuite(PyUncommentTest.class)); suite.addTest(new TestSuite(PyAddBlockCommentTest.class)); suite.addTest(new TestSuite(PyRemoveBlockCommentTest.class)); suite.addTest(new TestSuite(PyStripTrailingWhitespaceTest.class)); suite.addTest(new TestSuite(PyConvertSpaceToTabTest.class)); suite.addTest(new TestSuite(PyConvertTabToSpaceTest.class)); //$JUnit-END$ System.out.println ( "Running Test Suite '" + TestFileName + "'..." ); return suite; } } --- NEW FILE: PySelection.java --- /* * @author: ptoofani * Created: June 2004 * License: Common Public License v1.0 */ package org.python.pydev.editor.actions; import org.eclipse.jface.text.IDocument; import org.eclipse.jface.text.IRegion; import org.eclipse.jface.text.ITextSelection; import org.eclipse.ui.PlatformUI; import org.eclipse.ui.texteditor.ITextEditor; /** * More for my benefit in making the unit tests, this assembles information from a document, * whether in Eclipse or faked from outside, and performs a common selection routine for many * of the actions in the editor. Serves to refactor repetitive code in some Actions and tests. * * @author Parhaum Toofanian */ public cla... [truncated message content] |