ITextEditor editor = null;
/**
- *
+ *
*/
public RTrimAction() {
super();
* 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();
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) != ' ' ){
+
+ // -- 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) != ' ') {
break;
}
}
-
- //-- Only replace the line if the lengths are different
- if ( lineEnd != oldText.length() ) {
+
+ // -- Only replace the line if the lengths are different
+ if (lineEnd != oldText.length()) {
String newText = oldText.substring(0, lineEnd);
doc.replace(offset, length, newText);
if (oldText.length() != newText.length()) {
cursorOffset -= oldText.length() - newText.length();
}
- } else if (offset <= cursorOffset + selectionLength && 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.
+ } 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();