mirror of
https://github.com/arduino/Arduino.git
synced 2025-02-19 13:54:23 +01:00
New editor: ALT+ BACKSPACE deletes next word (OSX only). See #3098
This commit is contained in:
parent
91da999cb0
commit
765519a562
@ -35,8 +35,8 @@ import org.fife.ui.rsyntaxtextarea.Theme;
|
||||
import org.fife.ui.rsyntaxtextarea.Token;
|
||||
import org.fife.ui.rsyntaxtextarea.focusabletip.FocusableTip;
|
||||
import org.fife.ui.rtextarea.RTextArea;
|
||||
import org.fife.ui.rtextarea.RTextAreaUI;
|
||||
import org.fife.ui.rtextarea.RUndoManager;
|
||||
|
||||
import processing.app.*;
|
||||
|
||||
import javax.swing.*;
|
||||
@ -47,7 +47,6 @@ import javax.swing.text.BadLocationException;
|
||||
import javax.swing.text.Document;
|
||||
import javax.swing.text.Segment;
|
||||
import javax.swing.undo.UndoManager;
|
||||
|
||||
import java.awt.*;
|
||||
import java.awt.event.KeyEvent;
|
||||
import java.awt.event.MouseEvent;
|
||||
@ -242,7 +241,7 @@ public class SketchTextArea extends RSyntaxTextArea {
|
||||
protected void configurePopupMenu(JPopupMenu popupMenu) {
|
||||
super.configurePopupMenu(popupMenu);
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
protected RTAMouseListener createMouseListener() {
|
||||
return new SketchTextAreaMouseListener(this);
|
||||
@ -315,8 +314,8 @@ public class SketchTextArea extends RSyntaxTextArea {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
/**
|
||||
* Handles http hyperlinks.
|
||||
* NOTE (@Ricardo JL Rufino): Workaround to enable hyperlinks by default: https://github.com/bobbylight/RSyntaxTextArea/issues/119
|
||||
@ -326,12 +325,12 @@ public class SketchTextArea extends RSyntaxTextArea {
|
||||
private Insets insets;
|
||||
private boolean isScanningForLinks;
|
||||
private int hoveredOverLinkOffset = -1;
|
||||
|
||||
|
||||
protected SketchTextAreaMouseListener(RTextArea textArea) {
|
||||
super(textArea);
|
||||
insets = new Insets(0, 0, 0, 0);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Notifies all listeners that have registered interest for notification
|
||||
* on this event type. The listener list is processed last to first.
|
||||
@ -344,22 +343,22 @@ public class SketchTextArea extends RSyntaxTextArea {
|
||||
Object[] listeners = listenerList.getListenerList();
|
||||
// Process the listeners last to first, notifying
|
||||
// those that are interested in this event
|
||||
for (int i = listeners.length-2; i>=0; i-=2) {
|
||||
if (listeners[i]==HyperlinkListener.class) {
|
||||
((HyperlinkListener)listeners[i+1]).hyperlinkUpdate(e);
|
||||
}
|
||||
for (int i = listeners.length - 2; i >= 0; i -= 2) {
|
||||
if (listeners[i] == HyperlinkListener.class) {
|
||||
((HyperlinkListener) listeners[i + 1]).hyperlinkUpdate(e);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
private HyperlinkEvent createHyperlinkEvent(MouseEvent e) {
|
||||
HyperlinkEvent he = null;
|
||||
|
||||
|
||||
Token t = viewToToken(e.getPoint());
|
||||
if (t!=null) {
|
||||
if (t != null) {
|
||||
// Copy token, viewToModel() unfortunately modifies Token
|
||||
t = new TokenImpl(t);
|
||||
}
|
||||
|
||||
|
||||
if (t != null && t.isHyperlink()) {
|
||||
URL url = null;
|
||||
String desc = null;
|
||||
@ -375,21 +374,21 @@ public class SketchTextArea extends RSyntaxTextArea {
|
||||
}
|
||||
he = new HyperlinkEvent(SketchTextArea.this, HyperlinkEvent.EventType.ACTIVATED, url, desc);
|
||||
}
|
||||
|
||||
|
||||
return he;
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public void mouseClicked(MouseEvent e) {
|
||||
if (getHyperlinksEnabled()) {
|
||||
HyperlinkEvent he = createHyperlinkEvent(e);
|
||||
if (he!=null) {
|
||||
if (he != null) {
|
||||
fireHyperlinkUpdate(he);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
|
||||
@Override
|
||||
public void mouseMoved(MouseEvent e) {
|
||||
|
||||
super.mouseMoved(e);
|
||||
@ -397,16 +396,16 @@ public class SketchTextArea extends RSyntaxTextArea {
|
||||
if (!getHyperlinksEnabled()) {
|
||||
return;
|
||||
}
|
||||
|
||||
|
||||
// LinkGenerator linkGenerator = getLinkGenerator();
|
||||
|
||||
|
||||
// GitHub issue RSyntaxTextArea/#25 - links identified at "edges" of editor
|
||||
// should not be activated if mouse is in margin insets.
|
||||
insets = getInsets(insets);
|
||||
if (insets!=null) {
|
||||
if (insets != null) {
|
||||
int x = e.getX();
|
||||
int y = e.getY();
|
||||
if (x<=insets.left || y<insets.top) {
|
||||
if (x <= insets.left || y < insets.top) {
|
||||
if (isScanningForLinks) {
|
||||
stopScanningForLinks();
|
||||
}
|
||||
@ -416,14 +415,14 @@ public class SketchTextArea extends RSyntaxTextArea {
|
||||
|
||||
isScanningForLinks = true;
|
||||
Token t = viewToToken(e.getPoint());
|
||||
if (t!=null) {
|
||||
if (t != null) {
|
||||
// Copy token, viewToModel() unfortunately modifies Token
|
||||
t = new TokenImpl(t);
|
||||
}
|
||||
Cursor c2 = null;
|
||||
if (t!=null && t.isHyperlink()) {
|
||||
if (hoveredOverLinkOffset==-1 ||
|
||||
hoveredOverLinkOffset!=t.getOffset()) {
|
||||
if (t != null && t.isHyperlink()) {
|
||||
if (hoveredOverLinkOffset == -1 ||
|
||||
hoveredOverLinkOffset != t.getOffset()) {
|
||||
hoveredOverLinkOffset = t.getOffset();
|
||||
repaint();
|
||||
}
|
||||
@ -456,15 +455,15 @@ public class SketchTextArea extends RSyntaxTextArea {
|
||||
else {
|
||||
c2 = Cursor.getPredefinedCursor(Cursor.TEXT_CURSOR);
|
||||
hoveredOverLinkOffset = -1;
|
||||
// linkGeneratorResult = null;
|
||||
// linkGeneratorResult = null;
|
||||
}
|
||||
if (getCursor()!=c2) {
|
||||
if (getCursor() != c2) {
|
||||
setCursor(c2);
|
||||
// TODO: Repaint just the affected line(s).
|
||||
repaint(); // Link either left or went into.
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
private void stopScanningForLinks() {
|
||||
if (isScanningForLinks) {
|
||||
Cursor c = getCursor();
|
||||
@ -478,4 +477,8 @@ public class SketchTextArea extends RSyntaxTextArea {
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
protected RTextAreaUI createRTextAreaUI() {
|
||||
return new SketchTextAreaUI(this);
|
||||
}
|
||||
}
|
||||
|
@ -1,6 +1,7 @@
|
||||
package processing.app.syntax;
|
||||
|
||||
import org.fife.ui.rsyntaxtextarea.RSyntaxTextAreaDefaultInputMap;
|
||||
import org.fife.ui.rtextarea.RTextArea;
|
||||
import org.fife.ui.rtextarea.RTextAreaEditorKit;
|
||||
import processing.app.PreferencesData;
|
||||
|
||||
@ -23,5 +24,10 @@ public class SketchTextAreaDefaultInputMap extends RSyntaxTextAreaDefaultInputMa
|
||||
remove(KeyStroke.getKeyStroke(KeyEvent.VK_DOWN, alt));
|
||||
remove(KeyStroke.getKeyStroke(KeyEvent.VK_UP, alt));
|
||||
}
|
||||
|
||||
boolean isOSX = RTextArea.isOSX();
|
||||
if (isOSX) {
|
||||
put(KeyStroke.getKeyStroke(KeyEvent.VK_BACK_SPACE, alt), SketchTextAreaEditorKit.rtaDeleteNextWordAction);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
65
app/src/processing/app/syntax/SketchTextAreaEditorKit.java
Normal file
65
app/src/processing/app/syntax/SketchTextAreaEditorKit.java
Normal file
@ -0,0 +1,65 @@
|
||||
package processing.app.syntax;
|
||||
|
||||
import org.fife.ui.rsyntaxtextarea.RSyntaxTextAreaEditorKit;
|
||||
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 java.awt.event.ActionEvent;
|
||||
|
||||
public class SketchTextAreaEditorKit extends RSyntaxTextAreaEditorKit {
|
||||
|
||||
public static final String rtaDeleteNextWordAction = "RTA.DeleteNextWordAction";
|
||||
|
||||
private static final Action[] defaultActions = {
|
||||
new DeleteNextWordAction()
|
||||
};
|
||||
|
||||
@Override
|
||||
public Action[] getActions() {
|
||||
return TextAction.augmentList(super.getActions(), SketchTextAreaEditorKit.defaultActions);
|
||||
}
|
||||
|
||||
public static class DeleteNextWordAction extends RecordableTextAction {
|
||||
|
||||
public DeleteNextWordAction() {
|
||||
super(rtaDeleteNextWordAction);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void actionPerformedImpl(ActionEvent e, RTextArea textArea) {
|
||||
if (!textArea.isEditable() || !textArea.isEnabled()) {
|
||||
UIManager.getLookAndFeel().provideErrorFeedback(textArea);
|
||||
return;
|
||||
}
|
||||
try {
|
||||
int start = textArea.getSelectionStart();
|
||||
int end = getNextWordStart(textArea, start);
|
||||
if (end > start) {
|
||||
textArea.getDocument().remove(start, end - start);
|
||||
}
|
||||
} catch (BadLocationException ex) {
|
||||
UIManager.getLookAndFeel().provideErrorFeedback(textArea);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getMacroID() {
|
||||
return rtaDeleteNextWordAction;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the starting offset to delete. Exists so subclasses can
|
||||
* override.
|
||||
*/
|
||||
protected int getNextWordStart(RTextArea textArea, int end)
|
||||
throws BadLocationException {
|
||||
return Utilities.getNextWord(textArea, end);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
21
app/src/processing/app/syntax/SketchTextAreaUI.java
Normal file
21
app/src/processing/app/syntax/SketchTextAreaUI.java
Normal file
@ -0,0 +1,21 @@
|
||||
package processing.app.syntax;
|
||||
|
||||
import org.fife.ui.rsyntaxtextarea.RSyntaxTextAreaUI;
|
||||
|
||||
import javax.swing.*;
|
||||
import javax.swing.text.EditorKit;
|
||||
import javax.swing.text.JTextComponent;
|
||||
|
||||
public class SketchTextAreaUI extends RSyntaxTextAreaUI {
|
||||
|
||||
private static final EditorKit defaultKit = new SketchTextAreaEditorKit();
|
||||
|
||||
public SketchTextAreaUI(JComponent rSyntaxTextArea) {
|
||||
super(rSyntaxTextArea);
|
||||
}
|
||||
|
||||
@Override
|
||||
public EditorKit getEditorKit(JTextComponent tc) {
|
||||
return defaultKit;
|
||||
}
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user