New editor on MacOSX: CMD+BACKSPACE deletes current line until cursor position, ALT+BACKSPACE deletes previous word. See #3098

This commit is contained in:
Federico Fissore 2015-05-14 12:25:38 +02:00
parent 28e02572bc
commit 176d366549
2 changed files with 50 additions and 8 deletions

View File

@ -12,10 +12,12 @@ import java.awt.event.KeyEvent;
public class SketchTextAreaDefaultInputMap extends RSyntaxTextAreaDefaultInputMap {
public SketchTextAreaDefaultInputMap() {
int defaultMod = getDefaultModifier();
int defaultModifier = getDefaultModifier();
int alt = InputEvent.ALT_MASK;
boolean isOSX = RTextArea.isOSX();
int moveByWordMod = isOSX ? alt : defaultModifier;
remove(KeyStroke.getKeyStroke(KeyEvent.VK_K, defaultMod));
remove(KeyStroke.getKeyStroke(KeyEvent.VK_K, defaultModifier));
if (PreferencesData.getBoolean("editor.advanced")) {
put(KeyStroke.getKeyStroke(KeyEvent.VK_DOWN, alt), RTextAreaEditorKit.rtaLineDownAction);
@ -25,9 +27,11 @@ public class SketchTextAreaDefaultInputMap extends RSyntaxTextAreaDefaultInputMa
remove(KeyStroke.getKeyStroke(KeyEvent.VK_UP, alt));
}
boolean isOSX = RTextArea.isOSX();
remove(KeyStroke.getKeyStroke(KeyEvent.VK_BACK_SPACE, defaultModifier));
put(KeyStroke.getKeyStroke(KeyEvent.VK_BACK_SPACE, moveByWordMod), RTextAreaEditorKit.rtaDeletePrevWordAction);
if (isOSX) {
put(KeyStroke.getKeyStroke(KeyEvent.VK_BACK_SPACE, alt), SketchTextAreaEditorKit.rtaDeleteNextWordAction);
put(KeyStroke.getKeyStroke(KeyEvent.VK_BACK_SPACE, defaultModifier), SketchTextAreaEditorKit.rtaDeleteLineToCursorAction);
}
}
}

View File

@ -5,17 +5,17 @@ import org.fife.ui.rtextarea.RTextArea;
import org.fife.ui.rtextarea.RecordableTextAction;
import javax.swing.*;
import javax.swing.text.BadLocationException;
import javax.swing.text.TextAction;
import javax.swing.text.Utilities;
import javax.swing.text.*;
import java.awt.event.ActionEvent;
public class SketchTextAreaEditorKit extends RSyntaxTextAreaEditorKit {
public static final String rtaDeleteNextWordAction = "RTA.DeleteNextWordAction";
public static final String rtaDeleteLineToCursorAction = "RTA.DeleteLineToCursorAction";
private static final Action[] defaultActions = {
new DeleteNextWordAction()
new DeleteNextWordAction(),
new DeleteLineToCursorAction()
};
@Override
@ -62,4 +62,42 @@ public class SketchTextAreaEditorKit extends RSyntaxTextAreaEditorKit {
}
public static class DeleteLineToCursorAction extends RecordableTextAction {
public DeleteLineToCursorAction() {
super(rtaDeleteLineToCursorAction);
}
@Override
public void actionPerformedImpl(ActionEvent e, RTextArea textArea) {
if (!textArea.isEditable() || !textArea.isEnabled()) {
UIManager.getLookAndFeel().provideErrorFeedback(textArea);
return;
}
try {
// We use the elements instead of calling getLineOfOffset(),
// etc. to speed things up just a tad (i.e. micro-optimize).
Document document = textArea.getDocument();
int caretPosition = textArea.getCaretPosition();
Element map = document.getDefaultRootElement();
int currentLineNum = map.getElementIndex(caretPosition);
Element currentLineElement = map.getElement(currentLineNum);
int currentLineStart = currentLineElement.getStartOffset();
if (caretPosition > currentLineStart) {
document.remove(currentLineStart, caretPosition - currentLineStart);
}
} catch (BadLocationException ble) {
ble.printStackTrace();
}
}
@Override
public String getMacroID() {
return rtaDeleteLineToCursorAction;
}
}
}