Skip to content

Commit ea6c3fd

Browse files
committed
got basic- very basic- indenting working
1 parent 14723eb commit ea6c3fd

File tree

2 files changed

+79
-2
lines changed

2 files changed

+79
-2
lines changed

release/PythonMode.zip

1.08 KB
Binary file not shown.

src/info/sansgills/mode/python/PythonKeyListener.java

Lines changed: 79 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,12 @@
2222
public class PythonKeyListener extends processing.mode.java.PdeKeyListener {
2323
PythonEditor peditor;
2424
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+
2531

2632
public PythonKeyListener(Editor editor, JEditTextArea textarea) {
2733
super(editor, textarea);
@@ -33,16 +39,87 @@ public PythonKeyListener(Editor editor, JEditTextArea textarea) {
3339
/*
3440
* Handles special stuff for Java brace indenting & outdenting, etc.
3541
* 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
3646
*/
37-
/*@Override
47+
@Override
3848
public boolean keyPressed(KeyEvent event){
3949
char c = event.getKeyChar();
4050
int code = event.getKeyCode();
4151

4252
Sketch sketch = peditor.getSketch();
4353

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+
4496

4597

4698
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+
48125
}

0 commit comments

Comments
 (0)