Fixed: 1760857 - Avoid refreshing the preview when phpeditor got focus.
[phpeclipse.git] / net.sourceforge.phpeclipse / src / net / sourceforge / phpeclipse / phpeditor / actions / RTrimAction.java
1 /*
2  * Created on Oct 15, 2004
3  *
4  * The MIT License
5  * Copyright (c) 2004 Stephen Milligan
6  *
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:
13  *
14  * The above copyright notice and this permission notice shall be included in 
15  * all copies or substantial portions of the Software.
16  *
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 
23  * SOFTWARE.
24  */
25 package net.sourceforge.phpeclipse.phpeditor.actions;
26
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;
35
36 /**
37  * @author Stephen Milligan
38  */
39 public class RTrimAction implements IEditorActionDelegate {
40
41         ITextEditor editor = null;
42
43         /**
44          * 
45          */
46         public RTrimAction() {
47                 super();
48         }
49
50         public void setActiveEditor(IAction action, IEditorPart targetEditor) {
51
52                 if (targetEditor instanceof ITextEditor) {
53                         editor = (ITextEditor) targetEditor;
54                 }
55         }
56
57         /**
58          * this gets called for every action
59          */
60         public void run(IAction action) {
61                 IDocument doc = editor.getDocumentProvider().getDocument(
62                                 editor.getEditorInput());
63                 ITextSelection sel = (ITextSelection) editor.getSelectionProvider()
64                                 .getSelection();
65
66                 int currentLine = 0;
67                 int originalCursorOffset = sel.getOffset();
68                 int cursorOffset = originalCursorOffset;
69                 int originalSelectionLength = sel.getLength();
70                 int selectionLength = originalSelectionLength;
71                 String oldText;
72                 int lineEnd;
73
74                 try {
75
76                         while (currentLine < doc.getNumberOfLines()) {
77                                 int offset = doc.getLineOffset(currentLine);
78                                 int length = doc.getLineLength(currentLine);
79                                 oldText = doc.get(offset, length);
80                                 String lineDelimiter = doc.getLineDelimiter(currentLine);
81                                 if (lineDelimiter == null) lineDelimiter = "";
82
83                                 // -- Starts at the end of the line, looking for the first
84                                 // non-first 'white space'
85                                 // -- it then breaks out. No point in carrying on, as we have
86                                 // found our true line end
87                                 for (lineEnd = oldText.length() - lineDelimiter.length(); lineEnd > 0; --lineEnd) {
88                                         if (oldText.charAt(lineEnd - 1) != '\t'
89                                                         && oldText.charAt(lineEnd - 1) != ' ') {
90                                                 break;
91                                         }
92                                 }
93
94                                 // -- Only replace the line if the lengths are different
95                                 if (lineEnd != oldText.length() - lineDelimiter.length()) {
96                                         String newText = oldText.substring(0, lineEnd) + lineDelimiter;
97                                         doc.replace(offset, length, newText);
98
99 //                                      if (offset + length <= cursorOffset) {
100 //                                              if (oldText.length() != newText.length()) {
101 //                                                      cursorOffset -= oldText.length() - newText.length();
102 //                                              }
103 //                                      } else if (offset <= cursorOffset + selectionLength
104 //                                                      && selectionLength > 0) {
105 //                                              selectionLength -= oldText.length() - newText.length();
106 //                                      } else if (offset + length == cursorOffset + 2) {
107 //                                              // Check if the cursor is at the end of the line.
108 //                                              cursorOffset -= 2;
109 //                                      }
110
111                                         int oldEndOffset = offset + length - lineDelimiter.length();
112                                         int newEndOffset = offset + lineEnd;
113
114                                         if (cursorOffset >= oldEndOffset) {
115                                                 cursorOffset -= oldText.length() - newText.length();
116                                         } else if (cursorOffset >= newEndOffset) {
117                                                 cursorOffset = newEndOffset;
118                                         }
119                                         if (selectionLength > 0) {
120                                                 int selectionEndOffset = cursorOffset + selectionLength;
121                                                 if (selectionEndOffset >= oldEndOffset) {
122                                                         if (cursorOffset <= newEndOffset) {
123                                                                 // full overlap
124                                                                 selectionLength -= oldText.length() - newText.length();
125                                                         } else {
126                                                                 // starts inside
127                                                                 selectionLength -= oldEndOffset - cursorOffset;
128                                                         }
129                                                 } else if (selectionEndOffset >= newEndOffset) {
130                                                         if (cursorOffset <= newEndOffset) {
131                                                                 // ends inside
132                                                                 selectionLength -= selectionEndOffset - newEndOffset;
133                                                         } else {
134                                                                 // full inside
135                                                                 selectionLength = 0;
136                                                         }
137                                                 }
138                                         }
139                                 }
140                                 currentLine++;
141                         }
142
143                         TextSelection selection = new TextSelection(doc, cursorOffset,
144                                         selectionLength);
145                         editor.getSelectionProvider().setSelection(selection);
146                 } catch (Exception blx) {
147                         blx.printStackTrace();
148                 }
149         }
150
151         public void selectionChanged(IAction action, ISelection selection) {
152         }
153 }