Skip to content

Commit 8113b4d

Browse files
committed
Fixes AceJump bug and adds support for Shift key
1 parent f729381 commit 8113b4d

4 files changed

Lines changed: 214 additions & 31 deletions

File tree

src/main/java/com/jetbrains/idear/asr/ASRControlLoop.java

Lines changed: 24 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
import com.intellij.openapi.application.ApplicationInfo;
77
import com.intellij.openapi.application.ApplicationManager;
88
import com.intellij.openapi.components.ServiceManager;
9+
import com.intellij.openapi.util.AsyncResult;
910
import com.intellij.openapi.util.Pair;
1011
import com.intellij.util.Consumer;
1112
import com.jetbrains.idear.GoogleHelper;
@@ -149,15 +150,23 @@ private void applyAction(String c) {
149150
} else if (c.endsWith(PROJECT)) {
150151
ideService.invokeAction("ActivateProjectToolWindow");
151152
} else if (c.endsWith("symbols")) {
152-
ideService.invokeAction("AceJumpAction").doWhenDone((Consumer<DataContext>) dataContext -> {
153-
ideService.type(VK_SPACE);
154-
});
155-
try {
156-
Thread.sleep(500);
157-
} catch (InterruptedException e) {
158-
e.printStackTrace();
153+
AsyncResult ar = ideService.invokeAction("AceJumpAction");
154+
155+
while(!ar.isProcessed()) {
156+
//Spin lock
157+
logger.info("Not done...");
158+
try {
159+
Thread.sleep(250);
160+
} catch (InterruptedException e) {
161+
logger.warning(e.toString());
162+
}
159163
}
160-
ideService.type(("" + recognizeNumber()).toCharArray());
164+
logger.info("Done!");
165+
166+
ideService.type(" ");
167+
int jumpMarker = recognizeJumpMarker();
168+
ideService.type("" + jumpMarker);
169+
logger.info("Typed: " + jumpMarker);
161170
}
162171
} else if (c.startsWith(GOTO)) {
163172
if (c.startsWith("goto line")) {
@@ -186,7 +195,12 @@ private void applyAction(String c) {
186195
pressKeystroke(VK_TAB);
187196
} else if (c.contains(UNDO)) {
188197
ideService.invokeAction("$Undo");
198+
} else if(c.contains("shift")) {
199+
ideService.pressShift();
189200
}
201+
} else if(c.startsWith("release")) {
202+
if(c.contains("shift"))
203+
ideService.releaseShift();
190204
} else if (c.startsWith("following")) {
191205
if (c.endsWith("line")) {
192206
ideService.invokeAction("EditorDown");
@@ -400,7 +414,7 @@ private void pauseSpeech() {
400414
}
401415
}
402416

403-
private int recognizeNumber() {
417+
private int recognizeJumpMarker() {
404418
String result;
405419
logger.info("Recognizing number...");
406420
while (true) {
@@ -441,7 +455,7 @@ public static synchronized void beep() {
441455
}
442456
}
443457

444-
static String splitCamelCase(String s) {
458+
private static String splitCamelCase(String s) {
445459
return s.replaceAll(
446460
String.format("%s|%s|%s",
447461
"(?<=[A-Z])(?=[A-Z][a-z])",

src/main/java/com/jetbrains/idear/ide/IDEService.java

Lines changed: 12 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -15,11 +15,11 @@
1515
public class IDEService {
1616
private static final Logger logger = Logger.getLogger(IDEService.class.getSimpleName());
1717

18-
private Robot robot;
18+
private Keyboard keyboard;
1919

2020
public IDEService() {
2121
try {
22-
robot = new Robot();
22+
keyboard = new Keyboard();
2323
} catch (AWTException e) {
2424
e.printStackTrace();
2525
}
@@ -57,30 +57,22 @@ public AsyncResult<DataContext> invokeAction(String action, Function<DataContext
5757
}
5858

5959
public void type(final int... keys) {
60-
for (int key : keys) {
61-
robot.keyPress(key);
62-
}
60+
keyboard.type(keys);
61+
}
6362

64-
for (int key : keys) {
65-
robot.keyRelease(key);
66-
}
63+
public void pressShift() {
64+
keyboard.pressShift();
6765
}
6866

69-
public void type(final char... keys) {
70-
for (int key : keys) {
71-
robot.keyPress(key);
72-
}
67+
public void releaseShift() {
68+
keyboard.releaseShift();
69+
}
7370

74-
for (int key : keys) {
75-
robot.keyRelease(key);
76-
}
71+
public void type(final char... keys) {
72+
keyboard.type(keys);
7773
}
7874

7975
public void type(final String string) {
80-
char[] charArray = string.toCharArray();
81-
for (char key: charArray) {
82-
robot.keyPress(key);
83-
robot.keyRelease(key);
84-
}
76+
keyboard.type(string);
8577
}
8678
}
Lines changed: 177 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,177 @@
1+
package com.jetbrains.idear.ide;
2+
3+
import java.awt.*;
4+
5+
import static java.awt.event.KeyEvent.*;
6+
7+
public class Keyboard {
8+
9+
private Robot robot;
10+
11+
public static void main(String... args) throws Exception {
12+
Keyboard keyboard = new Keyboard();
13+
keyboard.type("Hello there, how are you?");
14+
}
15+
16+
public Keyboard() throws AWTException {
17+
this.robot = new Robot();
18+
}
19+
20+
public Keyboard(Robot robot) {
21+
this.robot = robot;
22+
}
23+
24+
public void type(CharSequence characters) {
25+
int length = characters.length();
26+
for (int i = 0; i < length; i++) {
27+
char character = characters.charAt(i);
28+
type(character);
29+
}
30+
}
31+
32+
public void type(char character) {
33+
switch (character) {
34+
case 'a': doType(VK_A); break;
35+
case 'b': doType(VK_B); break;
36+
case 'c': doType(VK_C); break;
37+
case 'd': doType(VK_D); break;
38+
case 'e': doType(VK_E); break;
39+
case 'f': doType(VK_F); break;
40+
case 'g': doType(VK_G); break;
41+
case 'h': doType(VK_H); break;
42+
case 'i': doType(VK_I); break;
43+
case 'j': doType(VK_J); break;
44+
case 'k': doType(VK_K); break;
45+
case 'l': doType(VK_L); break;
46+
case 'm': doType(VK_M); break;
47+
case 'n': doType(VK_N); break;
48+
case 'o': doType(VK_O); break;
49+
case 'p': doType(VK_P); break;
50+
case 'q': doType(VK_Q); break;
51+
case 'r': doType(VK_R); break;
52+
case 's': doType(VK_S); break;
53+
case 't': doType(VK_T); break;
54+
case 'u': doType(VK_U); break;
55+
case 'v': doType(VK_V); break;
56+
case 'w': doType(VK_W); break;
57+
case 'x': doType(VK_X); break;
58+
case 'y': doType(VK_Y); break;
59+
case 'z': doType(VK_Z); break;
60+
case 'A': doType(VK_SHIFT, VK_A); break;
61+
case 'B': doType(VK_SHIFT, VK_B); break;
62+
case 'C': doType(VK_SHIFT, VK_C); break;
63+
case 'D': doType(VK_SHIFT, VK_D); break;
64+
case 'E': doType(VK_SHIFT, VK_E); break;
65+
case 'F': doType(VK_SHIFT, VK_F); break;
66+
case 'G': doType(VK_SHIFT, VK_G); break;
67+
case 'H': doType(VK_SHIFT, VK_H); break;
68+
case 'I': doType(VK_SHIFT, VK_I); break;
69+
case 'J': doType(VK_SHIFT, VK_J); break;
70+
case 'K': doType(VK_SHIFT, VK_K); break;
71+
case 'L': doType(VK_SHIFT, VK_L); break;
72+
case 'M': doType(VK_SHIFT, VK_M); break;
73+
case 'N': doType(VK_SHIFT, VK_N); break;
74+
case 'O': doType(VK_SHIFT, VK_O); break;
75+
case 'P': doType(VK_SHIFT, VK_P); break;
76+
case 'Q': doType(VK_SHIFT, VK_Q); break;
77+
case 'R': doType(VK_SHIFT, VK_R); break;
78+
case 'S': doType(VK_SHIFT, VK_S); break;
79+
case 'T': doType(VK_SHIFT, VK_T); break;
80+
case 'U': doType(VK_SHIFT, VK_U); break;
81+
case 'V': doType(VK_SHIFT, VK_V); break;
82+
case 'W': doType(VK_SHIFT, VK_W); break;
83+
case 'X': doType(VK_SHIFT, VK_X); break;
84+
case 'Y': doType(VK_SHIFT, VK_Y); break;
85+
case 'Z': doType(VK_SHIFT, VK_Z); break;
86+
case '`': doType(VK_BACK_QUOTE); break;
87+
case '0': doType(VK_0); break;
88+
case '1': doType(VK_1); break;
89+
case '2': doType(VK_2); break;
90+
case '3': doType(VK_3); break;
91+
case '4': doType(VK_4); break;
92+
case '5': doType(VK_5); break;
93+
case '6': doType(VK_6); break;
94+
case '7': doType(VK_7); break;
95+
case '8': doType(VK_8); break;
96+
case '9': doType(VK_9); break;
97+
case '-': doType(VK_MINUS); break;
98+
case '=': doType(VK_EQUALS); break;
99+
case '~': doType(VK_SHIFT, VK_BACK_QUOTE); break;
100+
case '!': doType(VK_EXCLAMATION_MARK); break;
101+
case '@': doType(VK_AT); break;
102+
case '#': doType(VK_NUMBER_SIGN); break;
103+
case '$': doType(VK_DOLLAR); break;
104+
case '%': doType(VK_SHIFT, VK_5); break;
105+
case '^': doType(VK_CIRCUMFLEX); break;
106+
case '&': doType(VK_AMPERSAND); break;
107+
case '*': doType(VK_ASTERISK); break;
108+
case '(': doType(VK_LEFT_PARENTHESIS); break;
109+
case ')': doType(VK_RIGHT_PARENTHESIS); break;
110+
case '_': doType(VK_UNDERSCORE); break;
111+
case '+': doType(VK_PLUS); break;
112+
case '\t': doType(VK_TAB); break;
113+
case '\n': doType(VK_ENTER); break;
114+
case '[': doType(VK_OPEN_BRACKET); break;
115+
case ']': doType(VK_CLOSE_BRACKET); break;
116+
case '\\': doType(VK_BACK_SLASH); break;
117+
case '{': doType(VK_SHIFT, VK_OPEN_BRACKET); break;
118+
case '}': doType(VK_SHIFT, VK_CLOSE_BRACKET); break;
119+
case '|': doType(VK_SHIFT, VK_BACK_SLASH); break;
120+
case ';': doType(VK_SEMICOLON); break;
121+
case ':': doType(VK_COLON); break;
122+
case '\'': doType(VK_QUOTE); break;
123+
case '"': doType(VK_QUOTEDBL); break;
124+
case ',': doType(VK_COMMA); break;
125+
case '<': doType(VK_SHIFT, VK_COMMA); break;
126+
case '.': doType(VK_PERIOD); break;
127+
case '>': doType(VK_SHIFT, VK_PERIOD); break;
128+
case '/': doType(VK_SLASH); break;
129+
case '?': doType(VK_SHIFT, VK_SLASH); break;
130+
case ' ': doType(VK_SPACE); break;
131+
default:
132+
throw new IllegalArgumentException("Cannot type character " + character);
133+
}
134+
}
135+
136+
public void type(final int... keys) {
137+
for (int key : keys) {
138+
robot.keyPress(key);
139+
}
140+
141+
for (int key : keys) {
142+
robot.keyRelease(key);
143+
}
144+
}
145+
146+
public void type(final char... keys) {
147+
for (int key : keys) {
148+
robot.keyPress(key);
149+
}
150+
151+
for (int key : keys) {
152+
robot.keyRelease(key);
153+
}
154+
}
155+
156+
private void doType(int... keyCodes) {
157+
doType(keyCodes, 0, keyCodes.length);
158+
}
159+
160+
private void doType(int[] keyCodes, int offset, int length) {
161+
if (length == 0) {
162+
return;
163+
}
164+
165+
robot.keyPress(keyCodes[offset]);
166+
doType(keyCodes, offset + 1, length - 1);
167+
robot.keyRelease(keyCodes[offset]);
168+
}
169+
170+
public void pressShift() {
171+
robot.keyPress(VK_SHIFT);
172+
}
173+
174+
public void releaseShift() {
175+
robot.keyRelease(VK_SHIFT);
176+
}
177+
}

src/main/resources/com.jetbrains.idear/grammars/command.gram

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ grammar dialog;
3737

3838
<debug_command> = debug | toggle break point | view break points | step over | step into | step return | resume;
3939

40-
<keyboard_command> = press [return | escape | tab | undo | delete] key;
40+
<keyboard_command> = (press [return | escape | tab | undo | delete | shift] key ) | (release shift);
4141

4242
<next_command> = [following | previous] [line | method | error | tab | page | word];
4343

0 commit comments

Comments
 (0)