Skip to content

Commit 07b5c8c

Browse files
committed
Improved line moving functionality in Editor
- Updated line moving logic to select until the end of the line only, avoiding selection of the new line character. - Added functionality to move lines without selecting anything if no text is selected.
1 parent 77b354d commit 07b5c8c

File tree

1 file changed

+23
-3
lines changed

1 file changed

+23
-3
lines changed

app/src/processing/app/ui/Editor.java

Lines changed: 23 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1944,6 +1944,15 @@ public void handleIndentOutdent(boolean indent) {
19441944
*/
19451945
public void handleMoveLines(boolean moveUp) {
19461946
startCompoundEdit();
1947+
boolean isSelected = false;
1948+
1949+
if(textarea.isSelectionActive())
1950+
isSelected = true;
1951+
1952+
int caretPos = textarea.getCaretPosition();
1953+
int currentLine = textarea.getCaretLine();
1954+
int lineStart = textarea.getLineStartOffset(currentLine);
1955+
int column = caretPos - lineStart;
19471956

19481957
int startLine = textarea.getSelectionStartLine();
19491958
int stopLine = textarea.getSelectionStopLine();
@@ -2007,11 +2016,22 @@ public void handleMoveLines(boolean moveUp) {
20072016
? Math.min(textarea.getLineStopOffset(stopLine + 1), source.length())
20082017
: textarea.getLineStopOffset(stopLine); // Prevent out-of-bounds
20092018
}
2010-
2011-
SwingUtilities.invokeLater(() -> textarea.select(newSelectionStart, newSelectionEnd));
20122019
stopCompoundEdit();
2013-
}
20142020

2021+
if(isSelected)
2022+
SwingUtilities.invokeLater(() -> {
2023+
textarea.select(newSelectionStart, newSelectionEnd-1);
2024+
});
2025+
else if (replacedLine >= 0 && replacedLine < textarea.getLineCount()) {
2026+
int replacedLineStart = textarea.getLineStartOffset(replacedLine);
2027+
int replacedLineEnd = textarea.getLineStopOffset(replacedLine);
2028+
2029+
// Ensure caret stays within bounds of the new line
2030+
int newCaretPos = Math.min(replacedLineStart + column, replacedLineEnd - 1);
2031+
2032+
SwingUtilities.invokeLater(() -> textarea.setCaretPosition(newCaretPos));
2033+
}
2034+
}
20152035

20162036
static public boolean checkParen(char[] array, int index, int stop) {
20172037
while (index < stop) {

0 commit comments

Comments
 (0)