mirror of
https://github.com/arduino/Arduino.git
synced 2025-02-18 12:54:25 +01:00
New editor on MacOSX: CMD+BACKSPACE deletes current line until cursor position, ALT+BACKSPACE deletes previous word. See #3098
This commit is contained in:
parent
28e02572bc
commit
176d366549
@ -12,10 +12,12 @@ import java.awt.event.KeyEvent;
|
|||||||
public class SketchTextAreaDefaultInputMap extends RSyntaxTextAreaDefaultInputMap {
|
public class SketchTextAreaDefaultInputMap extends RSyntaxTextAreaDefaultInputMap {
|
||||||
|
|
||||||
public SketchTextAreaDefaultInputMap() {
|
public SketchTextAreaDefaultInputMap() {
|
||||||
int defaultMod = getDefaultModifier();
|
int defaultModifier = getDefaultModifier();
|
||||||
int alt = InputEvent.ALT_MASK;
|
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")) {
|
if (PreferencesData.getBoolean("editor.advanced")) {
|
||||||
put(KeyStroke.getKeyStroke(KeyEvent.VK_DOWN, alt), RTextAreaEditorKit.rtaLineDownAction);
|
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));
|
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) {
|
if (isOSX) {
|
||||||
put(KeyStroke.getKeyStroke(KeyEvent.VK_BACK_SPACE, alt), SketchTextAreaEditorKit.rtaDeleteNextWordAction);
|
put(KeyStroke.getKeyStroke(KeyEvent.VK_BACK_SPACE, defaultModifier), SketchTextAreaEditorKit.rtaDeleteLineToCursorAction);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -5,17 +5,17 @@ import org.fife.ui.rtextarea.RTextArea;
|
|||||||
import org.fife.ui.rtextarea.RecordableTextAction;
|
import org.fife.ui.rtextarea.RecordableTextAction;
|
||||||
|
|
||||||
import javax.swing.*;
|
import javax.swing.*;
|
||||||
import javax.swing.text.BadLocationException;
|
import javax.swing.text.*;
|
||||||
import javax.swing.text.TextAction;
|
|
||||||
import javax.swing.text.Utilities;
|
|
||||||
import java.awt.event.ActionEvent;
|
import java.awt.event.ActionEvent;
|
||||||
|
|
||||||
public class SketchTextAreaEditorKit extends RSyntaxTextAreaEditorKit {
|
public class SketchTextAreaEditorKit extends RSyntaxTextAreaEditorKit {
|
||||||
|
|
||||||
public static final String rtaDeleteNextWordAction = "RTA.DeleteNextWordAction";
|
public static final String rtaDeleteNextWordAction = "RTA.DeleteNextWordAction";
|
||||||
|
public static final String rtaDeleteLineToCursorAction = "RTA.DeleteLineToCursorAction";
|
||||||
|
|
||||||
private static final Action[] defaultActions = {
|
private static final Action[] defaultActions = {
|
||||||
new DeleteNextWordAction()
|
new DeleteNextWordAction(),
|
||||||
|
new DeleteLineToCursorAction()
|
||||||
};
|
};
|
||||||
|
|
||||||
@Override
|
@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;
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user