1) Moved net.sourceforge.phpeclipse.ui\src\net\sourceforge\phpdt back to net.sourcefo...
[phpeclipse.git] / net.sourceforge.phpeclipse / src / net / sourceforge / phpeclipse / phpeditor / actions / RTrimAction.java
index 154b992..a014e9e 100644 (file)
@@ -41,7 +41,7 @@ public class RTrimAction implements IEditorActionDelegate {
        ITextEditor editor = null;
 
        /**
-        *  
+        * 
         */
        public RTrimAction() {
                super();
@@ -58,8 +58,10 @@ public class RTrimAction implements IEditorActionDelegate {
         * this gets called for every action
         */
        public void run(IAction action) {
-               IDocument doc = editor.getDocumentProvider().getDocument(editor.getEditorInput());
-               ITextSelection sel = (ITextSelection) editor.getSelectionProvider().getSelection();
+               IDocument doc = editor.getDocumentProvider().getDocument(
+                               editor.getEditorInput());
+               ITextSelection sel = (ITextSelection) editor.getSelectionProvider()
+                               .getSelection();
 
                int currentLine = 0;
                int originalCursorOffset = sel.getOffset();
@@ -67,42 +69,79 @@ public class RTrimAction implements IEditorActionDelegate {
                int originalSelectionLength = sel.getLength();
                int selectionLength = originalSelectionLength;
                String oldText;
-               int     lineEnd;
-               
+               int lineEnd;
+
                try {
 
                        while (currentLine < doc.getNumberOfLines()) {
                                int offset = doc.getLineOffset(currentLine);
                                int length = doc.getLineLength(currentLine);
                                oldText = doc.get(offset, length);
-                               
-                               //-- Starts at the end of the line, looking for the first non-first 'white space'
-                               //-- it then breaks out. No point in carrying on, as we have found our true line end
-                               for (lineEnd=oldText.length(); lineEnd > 0; --lineEnd ){
-                                       if ( oldText.charAt(lineEnd-1) != '\t' && oldText.charAt(lineEnd-1) != ' ' ){
+                               String lineDelimiter = doc.getLineDelimiter(currentLine);
+                               if (lineDelimiter == null) lineDelimiter = "";
+
+                               // -- Starts at the end of the line, looking for the first
+                               // non-first 'white space'
+                               // -- it then breaks out. No point in carrying on, as we have
+                               // found our true line end
+                               for (lineEnd = oldText.length() - lineDelimiter.length(); lineEnd > 0; --lineEnd) {
+                                       if (oldText.charAt(lineEnd - 1) != '\t'
+                                                       && oldText.charAt(lineEnd - 1) != ' ') {
                                                break;
                                        }
                                }
-                               
-                               //-- Only replace the line if the lengths are different
-                               if ( lineEnd != oldText.length() ) {
-                                       String newText = oldText.substring(0, lineEnd);
+
+                               // -- Only replace the line if the lengths are different
+                               if (lineEnd != oldText.length() - lineDelimiter.length()) {
+                                       String newText = oldText.substring(0, lineEnd) + lineDelimiter;
                                        doc.replace(offset, length, newText);
 
-                                       if (offset + length <= cursorOffset) {
-                                               if (oldText.length() != newText.length()) {
-                                                       cursorOffset -= oldText.length() - newText.length();
+//                                     if (offset + length <= cursorOffset) {
+//                                             if (oldText.length() != newText.length()) {
+//                                                     cursorOffset -= oldText.length() - newText.length();
+//                                             }
+//                                     } else if (offset <= cursorOffset + selectionLength
+//                                                     && selectionLength > 0) {
+//                                             selectionLength -= oldText.length() - newText.length();
+//                                     } else if (offset + length == cursorOffset + 2) {
+//                                             // Check if the cursor is at the end of the line.
+//                                             cursorOffset -= 2;
+//                                     }
+
+                                       int oldEndOffset = offset + length - lineDelimiter.length();
+                                       int newEndOffset = offset + lineEnd;
+
+                                       if (cursorOffset >= oldEndOffset) {
+                                               cursorOffset -= oldText.length() - newText.length();
+                                       } else if (cursorOffset >= newEndOffset) {
+                                               cursorOffset = newEndOffset;
+                                       }
+                                       if (selectionLength > 0) {
+                                               int selectionEndOffset = cursorOffset + selectionLength;
+                                               if (selectionEndOffset >= oldEndOffset) {
+                                                       if (cursorOffset <= newEndOffset) {
+                                                               // full overlap
+                                                               selectionLength -= oldText.length() - newText.length();
+                                                       } else {
+                                                               // starts inside
+                                                               selectionLength -= oldEndOffset - cursorOffset;
+                                                       }
+                                               } else if (selectionEndOffset >= newEndOffset) {
+                                                       if (cursorOffset <= newEndOffset) {
+                                                               // ends inside
+                                                               selectionLength -= selectionEndOffset - newEndOffset;
+                                                       } else {
+                                                               // full inside
+                                                               selectionLength = 0;
+                                                       }
                                                }
-                                       } else if (offset <= cursorOffset + selectionLength     && selectionLength > 0) {
-                                               selectionLength -= oldText.length() - newText.length();
-                                       }       else if (offset + length == cursorOffset + 2) { // Check if the cursor is at the end of the line.
-                                               cursorOffset -= 2;
                                        }
                                }
                                currentLine++;
                        }
 
-                       TextSelection selection = new TextSelection(doc, cursorOffset, selectionLength);
+                       TextSelection selection = new TextSelection(doc, cursorOffset,
+                                       selectionLength);
                        editor.getSelectionProvider().setSelection(selection);
                } catch (Exception blx) {
                        blx.printStackTrace();