Skip to content
This repository was archived by the owner on Aug 10, 2022. It is now read-only.

Commit 6eaee9c

Browse files
committed
bobbylight#214: Alt+up/down arrow keys will now move all selected lines, not just the line the caret is on
1 parent 7421b4d commit 6eaee9c

File tree

2 files changed

+110
-39
lines changed

2 files changed

+110
-39
lines changed

.editorconfig

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
[*.java]
2+
indent_style = tab
3+
indent_size = 4
4+
trim_trailing_whitespace = true

src/main/java/org/fife/ui/rtextarea/RTextAreaEditorKit.java

Lines changed: 106 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -1655,20 +1655,36 @@ public LineMoveAction(String name, int moveAmt) {
16551655

16561656
@Override
16571657
public void actionPerformedImpl(ActionEvent e, RTextArea textArea) {
1658+
16581659
if (!textArea.isEditable() || !textArea.isEnabled()) {
16591660
UIManager.getLookAndFeel().provideErrorFeedback(textArea);
16601661
return;
16611662
}
1663+
16621664
try {
1663-
int caret = textArea.getCaretPosition();
1665+
1666+
int dot = textArea.getCaretPosition();
1667+
int mark = textArea.getCaret().getMark();
16641668
Document doc = textArea.getDocument();
16651669
Element root = doc.getDefaultRootElement();
1666-
int line = root.getElementIndex(caret);
1667-
if (moveAmt==-1 && line>0) {
1668-
moveLineUp(textArea, line);
1670+
int startLine = root.getElementIndex(Math.min(dot, mark));
1671+
int endLine = root.getElementIndex(Math.max(dot, mark));
1672+
1673+
// If we're moving more than one line, only move the last line
1674+
// if they've selected more than one char in it.
1675+
int moveCount = endLine - startLine + 1;
1676+
if (moveCount > 1) {
1677+
Element elem = root.getElement(endLine);
1678+
if (dot == elem.getStartOffset() || mark == elem.getStartOffset()) {
1679+
moveCount--;
1680+
}
16691681
}
1670-
else if (moveAmt==1 && line<root.getElementCount()-1) {
1671-
moveLineDown(textArea, line);
1682+
1683+
if (moveAmt==-1 && startLine>0) {
1684+
moveLineUp(textArea, startLine, moveCount);
1685+
}
1686+
else if (moveAmt==1 && endLine < root.getElementCount()-1) {
1687+
moveLineDown(textArea, startLine, moveCount);
16721688
}
16731689
else {
16741690
UIManager.getLookAndFeel().provideErrorFeedback(textArea);
@@ -1687,50 +1703,101 @@ public final String getMacroID() {
16871703
return getName();
16881704
}
16891705

1690-
private void moveLineDown(RTextArea textArea, int line)
1706+
private void moveLineDown(RTextArea textArea, int line, int lineCount)
16911707
throws BadLocationException {
1708+
System.out.println("--- " + line + ", " + lineCount + ", " + textArea.getLineCount());
1709+
// If we'd be moving lines past the end of the document, stop.
1710+
// We could perhaps just decide to move the lines to the end of the
1711+
// file, but this just keeps things simple.
1712+
// if (textArea.getLineCount() - line < lineCount) {
1713+
// UIManager.getLookAndFeel().provideErrorFeedback(textArea);
1714+
// return;
1715+
// }
1716+
16921717
Document doc = textArea.getDocument();
16931718
Element root = doc.getDefaultRootElement();
16941719
Element elem = root.getElement(line);
16951720
int start = elem.getStartOffset();
1721+
1722+
int endLine = line + lineCount - 1;
1723+
elem = root.getElement(endLine);
16961724
int end = elem.getEndOffset();
1697-
int caret = textArea.getCaretPosition();
1698-
int caretOffset = caret - start;
1699-
String text = doc.getText(start, end-start);
1700-
doc.remove(start, end-start);
1701-
Element elem2 = root.getElement(line); // not "line+1" - removed.
1702-
//int start2 = elem2.getStartOffset();
1703-
int end2 = elem2.getEndOffset();
1704-
doc.insertString(end2, text, null);
1705-
elem = root.getElement(line+1);
1706-
textArea.setCaretPosition(elem.getStartOffset()+caretOffset);
1707-
}
1708-
1709-
private void moveLineUp(RTextArea textArea, int line)
1725+
1726+
textArea.beginAtomicEdit();
1727+
try {
1728+
1729+
String text = doc.getText(start, end - start);
1730+
doc.remove(start, end - start);
1731+
1732+
int insertLine = Math.min(line + 1, textArea.getLineCount());
1733+
System.out.println("insertLine == " + insertLine);
1734+
boolean newlineInserted = false;
1735+
if (insertLine == textArea.getLineCount()) {
1736+
textArea.append("\n");
1737+
newlineInserted = true;
1738+
}
1739+
System.out.println("... " + newlineInserted);
1740+
1741+
int insertOffs = textArea.getLineStartOffset(insertLine);
1742+
doc.insertString(insertOffs, text, null);
1743+
textArea.setSelectionStart(insertOffs);
1744+
textArea.setSelectionEnd(insertOffs + text.length() - 1);
1745+
1746+
if (newlineInserted) {
1747+
doc.remove(doc.getLength() - 1, 1);
1748+
}
1749+
1750+
} finally {
1751+
textArea.endAtomicEdit();
1752+
}
1753+
1754+
}
1755+
1756+
private void moveLineUp(RTextArea textArea, int line, int moveCount)
17101757
throws BadLocationException {
1758+
17111759
Document doc = textArea.getDocument();
17121760
Element root = doc.getDefaultRootElement();
1713-
int lineCount = root.getElementCount();
17141761
Element elem = root.getElement(line);
17151762
int start = elem.getStartOffset();
1716-
int end = line==lineCount-1 ? elem.getEndOffset()-1 :
1717-
elem.getEndOffset();
1718-
int caret = textArea.getCaretPosition();
1719-
int caretOffset = caret - start;
1720-
String text = doc.getText(start, end-start);
1721-
if (line==lineCount-1) {
1722-
start--; // Remove previous line's ending \n
1723-
}
1724-
doc.remove(start, end-start);
1725-
Element elem2 = root.getElement(line-1);
1726-
int start2 = elem2.getStartOffset();
1727-
//int end2 = elem2.getEndOffset();
1728-
if (line==lineCount-1) {
1729-
text += '\n';
1730-
}
1731-
doc.insertString(start2, text, null);
1732-
//caretOffset = Math.min(start2+caretOffset, end2-1);
1733-
textArea.setCaretPosition(start2+caretOffset);
1763+
1764+
int endLine = line + moveCount - 1;
1765+
elem = root.getElement(endLine);
1766+
int end = elem.getEndOffset();
1767+
int lineCount = textArea.getLineCount();
1768+
boolean movingLastLine = false;
1769+
if (endLine == lineCount - 1) {
1770+
movingLastLine = true;
1771+
end--;
1772+
}
1773+
1774+
int insertLine = Math.max(line - 1, 0);
1775+
1776+
textArea.beginAtomicEdit();
1777+
try {
1778+
1779+
System.out.println("*** " + start + ", " + (end - start));
1780+
String text = doc.getText(start, end - start);
1781+
if (movingLastLine) {
1782+
text += '\n';
1783+
}
1784+
System.out.println("*** *** '" + text + "'");
1785+
doc.remove(start, end - start);
1786+
System.out.println("*** *** *** good");
1787+
1788+
int insertOffs = textArea.getLineStartOffset(insertLine);
1789+
doc.insertString(insertOffs, text, null);
1790+
textArea.setSelectionStart(insertOffs);
1791+
int selEnd = insertOffs + text.length() - 1;
1792+
textArea.setSelectionEnd(selEnd);
1793+
if (movingLastLine) { // Remove the artifically-added newline
1794+
doc.remove(doc.getLength() - 1, 1);
1795+
}
1796+
1797+
} finally {
1798+
textArea.endAtomicEdit();
1799+
}
1800+
17341801
}
17351802

17361803
}

0 commit comments

Comments
 (0)