Skip to content

Commit

Permalink
Actually use the PyInputHandler.
Browse files Browse the repository at this point in the history
  • Loading branch information
Jonathan Feinberg committed Apr 29, 2016
1 parent f7e8364 commit 69fbad4
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 30 deletions.
7 changes: 7 additions & 0 deletions runtime/src/jycessing/mode/PyEditor.java
Expand Up @@ -36,6 +36,8 @@
import processing.app.Platform;
import processing.app.SketchCode;
import processing.app.SketchException;
import processing.app.syntax.JEditTextArea;
import processing.app.syntax.PdeTextAreaDefaults;
import processing.app.ui.Editor;
import processing.app.ui.EditorException;
import processing.app.ui.EditorState;
Expand Down Expand Up @@ -103,6 +105,11 @@ public void windowClosing(final WindowEvent e) {
});
}

@Override
protected JEditTextArea createTextArea() {
return new JEditTextArea(new PdeTextAreaDefaults(mode), new PyInputHandler(this));
}

public String getId() {
return id;
}
Expand Down
49 changes: 19 additions & 30 deletions runtime/src/jycessing/mode/PyInputHandler.java
Expand Up @@ -2,7 +2,6 @@

import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.util.Stack;
import java.util.regex.Matcher;
Expand All @@ -14,13 +13,11 @@
import processing.app.ui.Editor;

/**
* This class provides Pythonic handling of TAB, BACKSPACE, and ENTER keys.
* This class provides Pythonic handling of keystrokes.
*/
public class PyInputHandler extends PdeInputHandler {
final PyEditor pyEditor;

JEditTextArea textArea; // assigned on first key press

// ctrl-alt on windows & linux, cmd-alt on os x
private static int CTRL_ALT = ActionEvent.ALT_MASK
| Toolkit.getDefaultToolkit().getMenuShortcutKeyMask();
Expand All @@ -31,29 +28,6 @@ public class PyInputHandler extends PdeInputHandler {

public PyInputHandler(final Editor editor) {
pyEditor = (PyEditor)editor;
textArea = pyEditor.getTextArea();

// This is a somewhat gross "shim" keyListener.
// The HandlePressed() Method was not properly overriding the
// handlePressed() within pdeInputHandler.java so I circumvent the need for
// the pde call by adding an additional keyListener here. It's in no way ideal,
// but is a quick fix to the "No Returns or Tabs" bug in the new
// python mode PDEX. You can find the pdeInputHandler.java at the url below.
// https://github.com/processing/processing/blob/master/
// app/src/processing/app/syntax/PdeInputHandler.java#L243

textArea.addKeyListener(new KeyAdapter() {
@Override
public void keyPressed(final KeyEvent e) {
if (e.getKeyCode() == 0) {
return;
}
if (handlePressed(e)) {
e.consume();
}
}
});

}

private static boolean isPrintableChar(final char c) {
Expand All @@ -67,16 +41,19 @@ private static boolean isPrintableChar(final char c) {
return block != null && block != Character.UnicodeBlock.SPECIALS;
}

JEditTextArea getTextArea() {
return pyEditor.getTextArea();
}


@Override
public boolean handlePressed(final KeyEvent event) {
final char c = event.getKeyChar();
final int code = event.getKeyCode();
final int mods = event.getModifiers();

final JEditTextArea textArea = getTextArea();
final Sketch sketch = pyEditor.getSketch();
if (textArea == null) {
textArea = pyEditor.getTextArea();
}

// things that change the content of the text area
if (!event.isMetaDown()
Expand Down Expand Up @@ -166,6 +143,7 @@ private class LineInfo {
LineInfo(final int lineNumber) {
this.lineNumber = lineNumber;

final JEditTextArea textArea = getTextArea();
final Matcher m = LINE.matcher(textArea.getLineText(lineNumber));
if (!m.matches()) {
throw new AssertionError("How can a line have less than nothing in it?");
Expand Down Expand Up @@ -206,6 +184,8 @@ public String toString() {
* @param sign The direction in which to modify the indent of the current line.
*/
public void indent(final int sign) {
final JEditTextArea textArea = getTextArea();

final int startLine = textArea.getSelectionStartLine();
final int stopLine = textArea.getSelectionStopLine();
final int selectionStart = textArea.getSelectionStart();
Expand Down Expand Up @@ -253,11 +233,15 @@ public void indent(final int sign) {

private int getAbsoluteCaretPositionRelativeToLineEnd(final int line,
final int lineEndRelativePosition) {
final JEditTextArea textArea = getTextArea();

return Math.max(textArea.getLineStopOffset(line) - lineEndRelativePosition, textArea
.getLineStartOffset(line));
}

private void indentLineBy(final int line, final int deltaIndent) {
final JEditTextArea textArea = getTextArea();

final LineInfo currentLine = new LineInfo(line);
final int newIndent = Math.max(0, currentLine.indent + deltaIndent);

Expand Down Expand Up @@ -298,6 +282,8 @@ private boolean isClose(final char c) {
* @return The index of the unterminated paren, or -1.
*/
private int indexOfUnclosedParen() {
final JEditTextArea textArea = getTextArea();

final int cursor = textArea.getCaretPosition();
final String text = textArea.getText();
final Stack<Integer> stack = new Stack<Integer>();
Expand Down Expand Up @@ -332,6 +318,7 @@ private String indentOf(final String line) {
}

private String getInitialWhitespace() {
final JEditTextArea textArea = getTextArea();
final String text = textArea.getText();
final int cursor = textArea.getCaretPosition();
final int lineNumber = textArea.getLineOfOffset(cursor);
Expand Down Expand Up @@ -377,6 +364,8 @@ private String getInitialWhitespace() {
}

private String newline() {
final JEditTextArea textArea = getTextArea();

final int cursor = textArea.getCaretPosition();
if (cursor <= 1) {
return "\n";
Expand Down

0 comments on commit 69fbad4

Please sign in to comment.