22
22
public class PythonKeyListener extends processing .mode .java .PdeKeyListener {
23
23
PythonEditor peditor ;
24
24
JEditTextArea ptextarea ;
25
+
26
+
27
+ //ctrl-alt on windows & linux, cmd-alt on os x
28
+ private static int CTRL_ALT = ActionEvent .ALT_MASK
29
+ | Toolkit .getDefaultToolkit ().getMenuShortcutKeyMask ();
30
+
25
31
26
32
public PythonKeyListener (Editor editor , JEditTextArea textarea ) {
27
33
super (editor , textarea );
@@ -33,16 +39,87 @@ public PythonKeyListener(Editor editor, JEditTextArea textarea) {
33
39
/*
34
40
* Handles special stuff for Java brace indenting & outdenting, etc.
35
41
* Overriding it 'cause we do things different here in python-land
42
+ *
43
+ * TODO use actual parser; handle spaces
44
+ *
45
+ * @return true if we've handled things correctly
36
46
*/
37
- /* @Override
47
+ @ Override
38
48
public boolean keyPressed (KeyEvent event ){
39
49
char c = event .getKeyChar ();
40
50
int code = event .getKeyCode ();
41
51
42
52
Sketch sketch = peditor .getSketch ();
43
53
54
+ // things that change the content of the text area
55
+ if ((code == KeyEvent .VK_BACK_SPACE ) || (code == KeyEvent .VK_TAB )
56
+ || (code == KeyEvent .VK_ENTER ) || ((c >= 32 ) && (c < 128 ))) {
57
+ sketch .setModified (true );
58
+ }
59
+
60
+ // ctrl-alt-[arrow] switches sketch tab
61
+ if ((event .getModifiers () & CTRL_ALT ) == CTRL_ALT ) {
62
+ if (code == KeyEvent .VK_LEFT ) {
63
+ sketch .handlePrevCode ();
64
+ return true ;
65
+ } else if (code == KeyEvent .VK_RIGHT ) {
66
+ sketch .handleNextCode ();
67
+ return true ;
68
+ }
69
+ }
70
+
71
+ //TODO handle ctrl-[up|down]; should move cursor to next empty line in that direction
72
+
73
+ // handle specific keypresses
74
+ switch (c ) {
75
+
76
+ case 9 : //tab; may do something here later. NOT overriding with spaces- this is python!
77
+ ptextarea .setSelectedText ("\t " );
78
+ break ;
79
+
80
+ case 10 : //return
81
+ case 13 : //also return
82
+ char [] text = ptextarea .getText ().toCharArray (); //text
83
+ int cursorLocation = ptextarea .getCaretPosition (); //location of element to be placed; may be out of bounds
84
+
85
+ int tabs = getTabCount (cursorLocation , text );
86
+
87
+ String insert = "\n " ;
88
+ for (int i =0 ; i <tabs ; i ++){
89
+ insert += "\t " ;
90
+ }
91
+
92
+ ptextarea .setSelectedText (insert );
93
+ break ;
94
+ }
95
+
44
96
45
97
46
98
return false ;
47
- }*/
99
+ }
100
+
101
+ //given an index and a block of text, find the number of tabs in the line containing the index
102
+ //TODO dirty hack
103
+ int getTabCount (int index , char [] text ){
104
+ int prev = index -1 ; //the start of the line
105
+
106
+
107
+ //walk till we find the beginning of the line (or text)
108
+ while (prev >= 0 && text [prev ] != '\n' ){
109
+ prev --;
110
+ }
111
+ //prev is now at the previous newline
112
+ prev ++;
113
+
114
+ //prev is now at the beginning of the line
115
+ //walk forward, counting tabs
116
+ int tabCount = 0 ;
117
+ while (prev < text .length && text [prev ] == '\t' ){
118
+ tabCount ++;
119
+ prev ++;
120
+ }
121
+
122
+ return tabCount ;
123
+ }
124
+
48
125
}
0 commit comments