Skip to content

Commit 176d366

Browse files
author
Federico Fissore
committed
New editor on MacOSX: CMD+BACKSPACE deletes current line until cursor position, ALT+BACKSPACE deletes previous word. See #3098
1 parent 28e0257 commit 176d366

File tree

2 files changed

+50
-8
lines changed

2 files changed

+50
-8
lines changed

app/src/processing/app/syntax/SketchTextAreaDefaultInputMap.java

+8-4
Original file line numberDiff line numberDiff line change
@@ -12,10 +12,12 @@
1212
public class SketchTextAreaDefaultInputMap extends RSyntaxTextAreaDefaultInputMap {
1313

1414
public SketchTextAreaDefaultInputMap() {
15-
int defaultMod = getDefaultModifier();
15+
int defaultModifier = getDefaultModifier();
1616
int alt = InputEvent.ALT_MASK;
17+
boolean isOSX = RTextArea.isOSX();
18+
int moveByWordMod = isOSX ? alt : defaultModifier;
1719

18-
remove(KeyStroke.getKeyStroke(KeyEvent.VK_K, defaultMod));
20+
remove(KeyStroke.getKeyStroke(KeyEvent.VK_K, defaultModifier));
1921

2022
if (PreferencesData.getBoolean("editor.advanced")) {
2123
put(KeyStroke.getKeyStroke(KeyEvent.VK_DOWN, alt), RTextAreaEditorKit.rtaLineDownAction);
@@ -25,9 +27,11 @@ public SketchTextAreaDefaultInputMap() {
2527
remove(KeyStroke.getKeyStroke(KeyEvent.VK_UP, alt));
2628
}
2729

28-
boolean isOSX = RTextArea.isOSX();
30+
remove(KeyStroke.getKeyStroke(KeyEvent.VK_BACK_SPACE, defaultModifier));
31+
put(KeyStroke.getKeyStroke(KeyEvent.VK_BACK_SPACE, moveByWordMod), RTextAreaEditorKit.rtaDeletePrevWordAction);
32+
2933
if (isOSX) {
30-
put(KeyStroke.getKeyStroke(KeyEvent.VK_BACK_SPACE, alt), SketchTextAreaEditorKit.rtaDeleteNextWordAction);
34+
put(KeyStroke.getKeyStroke(KeyEvent.VK_BACK_SPACE, defaultModifier), SketchTextAreaEditorKit.rtaDeleteLineToCursorAction);
3135
}
3236
}
3337
}

app/src/processing/app/syntax/SketchTextAreaEditorKit.java

+42-4
Original file line numberDiff line numberDiff line change
@@ -5,17 +5,17 @@
55
import org.fife.ui.rtextarea.RecordableTextAction;
66

77
import javax.swing.*;
8-
import javax.swing.text.BadLocationException;
9-
import javax.swing.text.TextAction;
10-
import javax.swing.text.Utilities;
8+
import javax.swing.text.*;
119
import java.awt.event.ActionEvent;
1210

1311
public class SketchTextAreaEditorKit extends RSyntaxTextAreaEditorKit {
1412

1513
public static final String rtaDeleteNextWordAction = "RTA.DeleteNextWordAction";
14+
public static final String rtaDeleteLineToCursorAction = "RTA.DeleteLineToCursorAction";
1615

1716
private static final Action[] defaultActions = {
18-
new DeleteNextWordAction()
17+
new DeleteNextWordAction(),
18+
new DeleteLineToCursorAction()
1919
};
2020

2121
@Override
@@ -62,4 +62,42 @@ protected int getNextWordStart(RTextArea textArea, int end)
6262

6363
}
6464

65+
public static class DeleteLineToCursorAction extends RecordableTextAction {
66+
67+
public DeleteLineToCursorAction() {
68+
super(rtaDeleteLineToCursorAction);
69+
}
70+
71+
@Override
72+
public void actionPerformedImpl(ActionEvent e, RTextArea textArea) {
73+
if (!textArea.isEditable() || !textArea.isEnabled()) {
74+
UIManager.getLookAndFeel().provideErrorFeedback(textArea);
75+
return;
76+
}
77+
try {
78+
79+
// We use the elements instead of calling getLineOfOffset(),
80+
// etc. to speed things up just a tad (i.e. micro-optimize).
81+
Document document = textArea.getDocument();
82+
int caretPosition = textArea.getCaretPosition();
83+
Element map = document.getDefaultRootElement();
84+
int currentLineNum = map.getElementIndex(caretPosition);
85+
Element currentLineElement = map.getElement(currentLineNum);
86+
int currentLineStart = currentLineElement.getStartOffset();
87+
if (caretPosition > currentLineStart) {
88+
document.remove(currentLineStart, caretPosition - currentLineStart);
89+
}
90+
91+
} catch (BadLocationException ble) {
92+
ble.printStackTrace();
93+
}
94+
}
95+
96+
@Override
97+
public String getMacroID() {
98+
return rtaDeleteLineToCursorAction;
99+
}
100+
101+
}
102+
65103
}

0 commit comments

Comments
 (0)