2 * Created on Oct 15, 2004
5 * Copyright (c) 2004 Stephen Milligan
7 * Permission is hereby granted, free of charge, to any person obtaining a
8 * copy of this software and associated documentation files (the "Software"),
9 * to deal in the Software without restriction, including without limitation
10 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
11 * and/or sell copies of the Software, and to permit persons to whom the Software
12 * is furnished to do so, subject to the following conditions:
14 * The above copyright notice and this permission notice shall be included in
15 * all copies or substantial portions of the Software.
17 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
20 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
22 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
25 package net.sourceforge.phpeclipse.phpeditor.actions;
27 import org.eclipse.jface.action.IAction;
28 import org.eclipse.jface.text.IDocument;
29 import org.eclipse.jface.text.ITextSelection;
30 import org.eclipse.jface.text.TextSelection;
31 import org.eclipse.jface.viewers.ISelection;
32 import org.eclipse.ui.IEditorActionDelegate;
33 import org.eclipse.ui.IEditorPart;
34 import org.eclipse.ui.texteditor.ITextEditor;
37 * @author Stephen Milligan
39 public class RTrimAction implements IEditorActionDelegate {
41 ITextEditor editor = null;
46 public RTrimAction() {
50 public void setActiveEditor(IAction action, IEditorPart targetEditor) {
52 if (targetEditor instanceof ITextEditor) {
53 editor = (ITextEditor) targetEditor;
58 * this gets called for every action
60 public void run(IAction action) {
61 IDocument doc = editor.getDocumentProvider().getDocument(
62 editor.getEditorInput());
63 ITextSelection sel = (ITextSelection) editor.getSelectionProvider()
67 int originalCursorOffset = sel.getOffset();
68 int cursorOffset = originalCursorOffset;
69 int originalSelectionLength = sel.getLength();
70 int selectionLength = originalSelectionLength;
76 while (currentLine < doc.getNumberOfLines()) {
77 int offset = doc.getLineOffset(currentLine);
78 int length = doc.getLineLength(currentLine);
79 oldText = doc.get(offset, length);
81 // -- Starts at the end of the line, looking for the first
82 // non-first 'white space'
83 // -- it then breaks out. No point in carrying on, as we have
84 // found our true line end
85 for (lineEnd = oldText.length(); lineEnd > 0; --lineEnd) {
86 if (oldText.charAt(lineEnd - 1) != '\t'
87 && oldText.charAt(lineEnd - 1) != ' ') {
92 // -- Only replace the line if the lengths are different
93 if (lineEnd != oldText.length()) {
94 String newText = oldText.substring(0, lineEnd);
95 doc.replace(offset, length, newText);
97 if (offset + length <= cursorOffset) {
98 if (oldText.length() != newText.length()) {
99 cursorOffset -= oldText.length() - newText.length();
101 } else if (offset <= cursorOffset + selectionLength
102 && selectionLength > 0) {
103 selectionLength -= oldText.length() - newText.length();
104 } else if (offset + length == cursorOffset + 2) { // Check
120 TextSelection selection = new TextSelection(doc, cursorOffset,
122 editor.getSelectionProvider().setSelection(selection);
123 } catch (Exception blx) {
124 blx.printStackTrace();
128 public void selectionChanged(IAction action, ISelection selection) {