[Pydev-cvs] org.python.pydev/src/org/python/pydev/editor/actions PyAddBlockComment.java,NONE,1.1 Fir
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-serv32053/src/org/python/pydev/editor/actions Added Files: PyAddBlockComment.java FirstCharAction.java PyUncomment.java PyAction.java PyComment.java package.html Log Message: Implemented editor menus --- NEW FILE: PyAddBlockComment.java --- /* * @author: fabioz * Created: January 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; /** * Creates a comment block * * @author Fabio Zadrozny */ public class PyAddBlockComment extends PyAction { /** * Create a 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(); IRegion startLine = null; int startLineIndex = selection.getStartLine(); String endLineDelim = getDelimiter(doc, startLineIndex); startLine = doc.getLineInformation(startLineIndex); IRegion line = doc.getLineInformation(startLineIndex); int pos = line.getOffset(); StringBuffer strBuf = new StringBuffer(); strBuf.append(endLineDelim); strBuf.append( "#==============================================================================="); strBuf.append(endLineDelim); strBuf.append("# "); strBuf.append(endLineDelim); strBuf.append( "#==============================================================================="); strBuf.append(endLineDelim); strBuf.append(endLineDelim); doc.replace(pos, 0, strBuf.toString()); } catch (Exception e) { beep(e); } } } --- NEW FILE: FirstCharAction.java --- package org.python.pydev.editor.actions; import org.eclipse.jface.action.IAction; import org.eclipse.jface.text.IDocument; import org.eclipse.jface.text.ITextSelection; import org.eclipse.ui.texteditor.ITextEditor; /** * @author Fabio Zadrozny */ public class FirstCharAction extends PyAction { /** * Run to the first char (other than whitespaces) or to the real first char. */ public void run(IAction action) { try{ ITextEditor textEditor = getTextEditor(); IDocument doc = textEditor.getDocumentProvider().getDocument(textEditor.getEditorInput()); ITextSelection selection = (ITextSelection)textEditor.getSelectionProvider().getSelection(); boolean isAtFirstChar = isAtFirstVisibleChar(doc, selection.getOffset()); if (! isAtFirstChar){ gotoFirstVisibleChar(doc, selection.getOffset()); }else{ gotoFirstChar(doc, selection.getOffset()); } }catch(Exception e){ beep(e); } } } --- NEW FILE: PyUncomment.java --- /* * @author: fabioz * Created: January 2004 * License: Common Public License v1.0 */ package org.python.pydev.editor.actions; /** * @author fabioz */ public class PyUncomment extends PyComment { /** * 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; } } --- NEW FILE: PyAction.java --- /* * @author: fabioz * Created: January 2004 * License: Common Public License v1.0 */ package org.python.pydev.editor.actions; import org.eclipse.jface.action.IAction; import org.eclipse.jface.text.BadLocationException; import org.eclipse.jface.text.IDocument; import org.eclipse.jface.text.IRegion; import org.eclipse.jface.text.TextSelection; import org.eclipse.jface.viewers.ISelection; import org.eclipse.ui.IEditorActionDelegate; import org.eclipse.ui.IEditorPart; import org.eclipse.ui.PlatformUI; import org.eclipse.ui.texteditor.ITextEditor; /** * @author Fabio Zadrozny * * Superclass of all our actions. Contains utility functions. * * Subclasses should implement run(IAction action) method. */ public abstract class PyAction implements IEditorActionDelegate { // Always points to the current editor protected IEditorPart targetEditor; public void setEditor(IEditorPart targetEditor) { this.targetEditor = targetEditor; } /** * This is an IEditorActionDelegate override */ public void setActiveEditor(IAction action, IEditorPart targetEditor) { setEditor(targetEditor); } /** * Activate action (if we are getting text) */ public void selectionChanged(IAction action, ISelection selection) { if (selection instanceof TextSelection) { action.setEnabled(true); return; } action.setEnabled( targetEditor instanceof ITextEditor); } /** * This method returns the delimiter for the document * @param doc * @param startLineIndex * @return * @throws BadLocationException */ protected String getDelimiter(IDocument doc, int startLineIndex) throws BadLocationException { String endLineDelim = doc.getLineDelimiter(startLineIndex); if (endLineDelim == null) { endLineDelim = doc.getLegalLineDelimiters()[0]; } return endLineDelim; } /** * This function returns the text editor. * @return */ protected ITextEditor getTextEditor() { if (targetEditor instanceof ITextEditor) { return (ITextEditor) targetEditor; } else { throw new RuntimeException("Expecting text editor. Found:"+targetEditor.getClass().getName()); } } /** * Helper for setting caret * @param pos * @throws BadLocationException */ protected void setCaretPosition(int pos) throws BadLocationException { getTextEditor().selectAndReveal(pos, 0); } /** * Are we in the first char of the line with the offset passed? * @param doc * @param cursorOffset */ protected void isInFirstVisibleChar(IDocument doc, int cursorOffset) { try { IRegion region = doc.getLineInformationOfOffset(cursorOffset); int offset = region.getOffset(); String src = doc.get(offset, region.getLength()); if ("".equals(src)) return; int i = 0; while (i < src.length()) { if (!Character.isWhitespace(src.charAt(i++))) { break; } } setCaretPosition(offset + i - 1); } catch (BadLocationException e) { beep(e); return; } } /** * Returns the position of the first char. * @param doc * @param cursorOffset * @return * @throws BadLocationException */ protected int getFirstCharPosition(IDocument doc, int cursorOffset) throws BadLocationException { IRegion region; region = doc.getLineInformationOfOffset(cursorOffset); int offset = region.getOffset(); String src = doc.get(offset, region.getLength()); if ("".equals(src)) return 0; int i = 0; while (i < src.length()) { if (!Character.isWhitespace(src.charAt(i++))) { break; } } return (offset + i - 1); } /** * Goes to first char of the line. * @param doc * @param cursorOffset */ protected void gotoFirstChar(IDocument doc, int cursorOffset) { try { IRegion region = doc.getLineInformationOfOffset(cursorOffset); int offset = region.getOffset(); setCaretPosition(offset); } catch (BadLocationException e) { beep(e); } } /** * Goes to the first visible char. * @param doc * @param cursorOffset */ protected void gotoFirstVisibleChar(IDocument doc, int cursorOffset) { try { setCaretPosition(getFirstCharPosition(doc, cursorOffset)); } catch (BadLocationException e) { beep(e); } } /** * Goes to the first visible char. * @param doc * @param cursorOffset */ protected boolean isAtFirstVisibleChar(IDocument doc, int cursorOffset) { try { return getFirstCharPosition(doc, cursorOffset) == cursorOffset; } catch (BadLocationException e) { return false; } } //================================================================ // HELPER FOR DEBBUGING... //================================================================ /** * Beep...humm... yeah....beep....ehehheheh */ protected void beep(Exception e) { PlatformUI.getWorkbench().getActiveWorkbenchWindow().getShell().getDisplay().beep(); e.printStackTrace(); } protected void print(Object o) { System.out.println(o); } protected void print(boolean b) { System.out.println(b); } private void print(int i) { System.out.println(i); } } --- NEW FILE: PyComment.java --- /* * @author: fabioz * Created: January 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; /** * @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); startLine = doc.getLineInformation(startLineIndex); endLine = doc.getLineInformation(endLineIndex); String str = new String(doc.get(initialPos, length)); str = replaceStr(str, endLineDelim); doc.replace(initialPos, length, str); } 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 + "#"); } } --- NEW FILE: package.html --- <body> Editor actions. Menu items and toolbar commands are all implemented here.<p> <h3>How to add a new action</h3> Implement your class as subclass of PyAction. See {@link org.python.pydev.editor.actions.PyAction PyAction} for more info.<p> Edit plugin.xml. Inside: <li>If you are adding your action to top-level menus, see org.eclipse.ui.editorActions <li>If you adding your action to context popup, see org.eclipse.ui.popupMenus </body> |