|
5 | 5 | import org.fife.ui.rtextarea.RecordableTextAction;
|
6 | 6 |
|
7 | 7 | 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.*; |
11 | 9 | import java.awt.event.ActionEvent;
|
12 | 10 |
|
13 | 11 | public class SketchTextAreaEditorKit extends RSyntaxTextAreaEditorKit {
|
14 | 12 |
|
15 | 13 | public static final String rtaDeleteNextWordAction = "RTA.DeleteNextWordAction";
|
| 14 | + public static final String rtaDeleteLineToCursorAction = "RTA.DeleteLineToCursorAction"; |
16 | 15 |
|
17 | 16 | private static final Action[] defaultActions = {
|
18 |
| - new DeleteNextWordAction() |
| 17 | + new DeleteNextWordAction(), |
| 18 | + new DeleteLineToCursorAction() |
19 | 19 | };
|
20 | 20 |
|
21 | 21 | @Override
|
@@ -62,4 +62,42 @@ protected int getNextWordStart(RTextArea textArea, int end)
|
62 | 62 |
|
63 | 63 | }
|
64 | 64 |
|
| 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 | + |
65 | 103 | }
|
0 commit comments