Fixed: malfunctioned "Remove trailing spaces on editor save"
authortoshihiro <toshihiro>
Thu, 14 Jun 2007 03:31:37 +0000 (03:31 +0000)
committertoshihiro <toshihiro>
Thu, 14 Jun 2007 03:31:37 +0000 (03:31 +0000)
 related 1539022 - Trim trailing LINE whitespace on save

net.sourceforge.phpeclipse/src/net/sourceforge/phpeclipse/phpeditor/actions/RTrimAction.java

index d4a1718..d0da3ca 100644 (file)
@@ -77,12 +77,14 @@ public class RTrimAction implements IEditorActionDelegate {
                                int offset = doc.getLineOffset(currentLine);
                                int length = doc.getLineLength(currentLine);
                                oldText = doc.get(offset, length);
+                               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(); lineEnd > 0; --lineEnd) {
+                               for (lineEnd = oldText.length() - lineDelimiter.length(); lineEnd > 0; --lineEnd) {
                                        if (oldText.charAt(lineEnd - 1) != '\t'
                                                        && oldText.charAt(lineEnd - 1) != ' ') {
                                                break;
@@ -90,8 +92,8 @@ public class RTrimAction implements IEditorActionDelegate {
                                }
 
                                // -- Only replace the line if the lengths are different
-                               if (lineEnd != oldText.length()) {
-                                       String newText = oldText.substring(0, lineEnd);
+                               if (lineEnd != oldText.length() - lineDelimiter.length()) {
+                                       String newText = oldText.substring(0, lineEnd) + lineDelimiter;
                                        doc.replace(offset, length, newText);
 
                                        if (offset + length <= cursorOffset) {
@@ -101,16 +103,8 @@ public class RTrimAction implements IEditorActionDelegate {
                                        } 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.
+                                       } else if (offset + length == cursorOffset + 2) {
+                                               // Check if the cursor is at the end of the line.
                                                cursorOffset -= 2;
                                        }
                                }