private void moveSelectionNow(boolean up) { int start = _gsEditor.getEditor().getSelectionStart(); int end = _gsEditor.getEditor().getSelectionEnd(); String script = _gsEditor.getEditor().getText(); // If the caret is positioned at the first point in a new line, treat it as if it is on the // previous line only if (start != end && end > 0 && script.charAt(end - 1) == '\n') { end = end - 1; } // Handle raw whitespace moves int rawStart = TextComponentUtil.getLineStart(script, start); int rawEnd = TextComponentUtil.getLineEnd(script, end); String line = script.substring(rawStart, rawEnd); if (GosuStringUtil.isWhitespace(line) || line.trim().startsWith("//")) { if (up) { if (rawStart == 0) { return; } int lineStartBefore = TextComponentUtil.getLineStart(script, rawStart - 1); GosuRefactorUtil.MoveInstruction moveInstruction = new GosuRefactorUtil.MoveInstruction(false, false, lineStartBefore); handleMoveInstruction(script, moveInstruction, rawStart, rawEnd, up); return; } else { if (rawEnd >= script.length()) { return; } int lineStartBefore = rawEnd + 1; GosuRefactorUtil.MoveInstruction moveInstruction = new GosuRefactorUtil.MoveInstruction(false, false, lineStartBefore); handleMoveInstruction(script, moveInstruction, rawStart, rawEnd, up); return; } } int peStart = TextComponentUtil.findNonWhitespacePositionAfter(script, rawStart); int peEnd = TextComponentUtil.findNonWhitespacePositionBefore(script, rawEnd); peStart = Math.min(peStart, script.length() - 1); peEnd = Math.min(peEnd, script.length() - 1); // Find the first statement at the selection start IParseTree firstStatement = GosuRefactorUtil.findFirstStatementAtLine( TextComponentUtil.getLineAtPosition(_gsEditor.getEditor(), peStart), peStart, _gsEditor.getParser().getLocations()); // Find the last statement at the selection end IParseTree lastStatement = GosuRefactorUtil.findLastStatementAtLine( TextComponentUtil.getLineAtPosition(_gsEditor.getEditor(), peEnd), peEnd, _gsEditor.getParser().getLocations()); // Find the spanning range of those two statements IParseTree[] boundingPair = GosuRefactorUtil.findSpanningLogicalRange(firstStatement, lastStatement); // If a bounding pair exists, do the move if (boundingPair != null) { int clipStart = Math.min(rawStart, TextComponentUtil.getLineStart(script, boundingPair[0].getOffset())); int clipEnd = Math.max(rawEnd, TextComponentUtil.getLineEnd(script, boundingPair[1].getExtent())); // if this is not a class element (that is, we are in an impl), handle moving into a white // space line if (!GosuRefactorUtil.isClassElement(boundingPair[0])) { int lineStart = up ? TextComponentUtil.getWhiteSpaceOrCommentLineStartBefore(script, clipStart) : TextComponentUtil.getWhiteSpaceOrCommentLineStartAfter(script, clipEnd); if (lineStart != -1) { GosuRefactorUtil.MoveInstruction moveInstruction = new GosuRefactorUtil.MoveInstruction(false, false, lineStart); handleMoveInstruction(script, moveInstruction, clipStart, clipEnd, up); return; } } // Not handling an into whitespace move, so do a syntax aware move GosuRefactorUtil.MoveInstruction moveInstruction = up ? GosuRefactorUtil.getMoveUpInstruction(boundingPair[0]) : GosuRefactorUtil.getMoveDownInstruction(boundingPair[1]); if (moveInstruction != null) { if (GosuRefactorUtil.isClassElement(boundingPair[1])) { IParseTree nextSibling = boundingPair[1].getNextSibling(); if (nextSibling != null) { clipEnd = TextComponentUtil.getLineStart(script, nextSibling.getOffset()) - 1; if (!up) { moveInstruction.position = TextComponentUtil.getDeepestWhiteSpaceLineStartAfter( script, moveInstruction.position); } } } handleMoveInstruction(script, moveInstruction, clipStart, clipEnd, up); } } }