Thread: [Pydev-cvs] org.python.pydev/src/org/python/pydev/editor/actions PyRemoveBlockComment.java,NONE,1.1
Brought to you by:
fabioz
From: Parhaum T. <dre...@us...> - 2004-06-11 20:04:05
|
Update of /cvsroot/pydev/org.python.pydev/src/org/python/pydev/editor/actions In directory sc8-pr-cvs1.sourceforge.net:/tmp/cvs-serv6185/src/org/python/pydev/editor/actions Modified Files: PyAddBlockComment.java PyComment.java Added Files: PyRemoveBlockComment.java PyStripTrailingWhitespace.java Log Message: Modified in org.python.pydev: PyAddCommentBlock - Extends PyComment, works on multiple lines PyComment - Selects newly commented lines after performing action PyUnComment - Selects newly uncommented lines after performing action Plugin.xml - Added PyRemoveCommentBlock and PyStripTrailingWhitespace functionality to menu, context, and hotkey (Ctrl+5) PluginPrefs - Made block comment separator a user-defined string Modified in org.python.pydev.help: toc_main.xml - user guide structure New: PyRemoveCommentBlock - Created to undo AddCommentBlock action PyStripTrailingWhitespace - Created to strip trailing whitespace from a selection or the document Index: PyAddBlockComment.java =================================================================== RCS file: /cvsroot/pydev/org.python.pydev/src/org/python/pydev/editor/actions/PyAddBlockComment.java,v retrieving revision 1.5 retrieving revision 1.6 diff -C2 -d -r1.5 -r1.6 *** PyAddBlockComment.java 27 Apr 2004 01:06:59 -0000 1.5 --- PyAddBlockComment.java 11 Jun 2004 20:03:56 -0000 1.6 *************** *** 7,86 **** package org.python.pydev.editor.actions; ! 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; /** ! * Creates a comment block * * @author Fabio Zadrozny */ ! public class PyAddBlockComment extends PyAction { /** ! * . ! * Insert a comment block. ! * #===... ! * # ! * #===... ! * (TODO:This could be customized somewhere...) */ ! public void run(IAction action) { ! try { ! ITextEditor textEditor = getTextEditor(); ! ! IDocument doc = ! textEditor.getDocumentProvider().getDocument( ! textEditor.getEditorInput()); ! ITextSelection selection = ! (ITextSelection) textEditor ! .getSelectionProvider() ! .getSelection(); ! ! //strange things happen when we try to get the line information on the last line of the ! //document (it doesn't return the end line delimiter correctly), so we always get on position ! //0 and check to see if we are not in the last line. ! if (doc.getNumberOfLines() <= 1) ! return; ! ! IRegion startLine = null; ! ! int startLineIndex = selection.getStartLine(); ! ! String endLineDelim = getDelimiter(doc, 0); ! startLine = doc.getLineInformation(startLineIndex); ! ! IRegion line = doc.getLineInformation(startLineIndex); ! int lineLenght = line.getLength(); ! int pos = line.getOffset(); ! ! String content = doc.get(pos, lineLenght); ! String comment = getFullCommentLine(); ! ! StringBuffer strBuf = new StringBuffer(); ! ! strBuf.append(endLineDelim); ! strBuf.append(comment); //#=========... ! strBuf.append(endLineDelim); ! ! strBuf.append("# "); ! strBuf.append(content); ! ! strBuf.append(endLineDelim); ! strBuf.append(comment); //#=========... ! strBuf.append(endLineDelim); ! ! doc.replace(pos, lineLenght, strBuf.toString()); ! textEditor.selectAndReveal(pos+strBuf.length()-comment.length()-4,0); ! ! } catch (Exception e) { ! beep(e); ! } } ! private String getFullCommentLine(){ ! return "#==============================================================================="; } } --- 7,51 ---- package org.python.pydev.editor.actions; ! import org.python.pydev.plugin.PydevPrefs; ! import org.eclipse.core.runtime.Preferences; ! import org.python.pydev.plugin.PydevPlugin; ! /** ! * Creates a comment block. Comment blocks are slightly different than regular comments ! * created in that they provide a distinguishing element at the beginning and end as a ! * separator. In this case, it is a string of <code>=======</code> symbols to strongly ! * differentiate this comment block. * * @author Fabio Zadrozny + * @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); } + } --- NEW FILE: PyRemoveBlockComment.java --- /* * @author: ptoofani * Created: June 2004 * License: Common Public License v1.0 */ package org.python.pydev.editor.actions; /** * Removes a comment block. Comment blocks are slightly different than regular comments * created in that they provide a distinguishing element at the beginning and end as a * separator. In this case, it is a string of <code>=======</code> symbols to strongly * differentiate this comment block. * * This will handle regular comment blocks as well by removing the # token at the head of * each line, but will also remove the block separators if they are present * * @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; } } Index: PyComment.java =================================================================== RCS file: /cvsroot/pydev/org.python.pydev/src/org/python/pydev/editor/actions/PyComment.java,v retrieving revision 1.2 retrieving revision 1.3 diff -C2 -d -r1.2 -r1.3 *** PyComment.java 15 Apr 2004 23:19:21 -0000 1.2 --- PyComment.java 11 Jun 2004 20:03:56 -0000 1.3 *************** *** 51,56 **** String endLineDelim = getDelimiter(doc, startLineIndex); - startLine = doc.getLineInformation(startLineIndex); - endLine = doc.getLineInformation(endLineIndex); String str = doc.get(initialPos, length); --- 51,54 ---- *************** *** 59,62 **** --- 57,63 ---- doc.replace(initialPos, length, str); + // Select the new comment block automatically + textEditor.selectAndReveal(initialPos,str.length()); + } catch (Exception e) { beep(e); *************** *** 78,80 **** --- 79,82 ---- return "#" + str.replaceAll(endLineDelim, endLineDelim + "#"); } + } --- NEW FILE: PyStripTrailingWhitespace.java --- /* * @author: ptoofani * Created: June 2004 * License: Common Public License v1.0 */ package org.python.pydev.editor.actions; 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; /** * Strips trailing whitespace on the selected lines. If no lines are selected, it performs * the action on the entire document. * * @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 * * @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 ""; // Find the last non-whitespace character int j; for ( j = str.length ( ) - 1; j >= 0; j-- ) { if ( ! Character.isWhitespace ( str.charAt ( j ) ) ) { break; } } // If the whole line is whitespace, return empty string if ( j < 0 ) return ""; // If the last character isn't whitespace, nothing to trim, so return the string if ( j == str.length ( ) - 1 ) { return ( str ); } // Otherwise, return a substring else { return ( str.substring ( 0, j + 1 ) ); } } } |