Skip to content

Commit f729381

Browse files
committed
Add some features for writing code
1 parent e733728 commit f729381

5 files changed

Lines changed: 72 additions & 18 deletions

File tree

src/main/java/com/jetbrains/idear/GoogleHelper.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,11 @@ public static Pair<String, Double> getBestTextForUtterance(File file) {
7474

7575
private static List<Pair<String, Double>> parseGSAPIResponse(String r) throws JSONException {
7676
List<Pair<String, Double>> res = new ArrayList<>();
77+
logger.log(Level.INFO, r);
78+
if (r.length() < 13) {
79+
logger.log(Level.WARNING, "No result!");
80+
return res;
81+
}
7782

7883
JSONObject o = new JSONObject(r.substring(13));
7984
JSONArray results = o.getJSONArray("result");

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

Lines changed: 49 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import com.intellij.ide.DataManager;
44
import com.intellij.openapi.actionSystem.*;
55
import com.intellij.openapi.actionSystem.impl.SimpleDataContext;
6+
import com.intellij.openapi.application.ApplicationInfo;
67
import com.intellij.openapi.application.ApplicationManager;
78
import com.intellij.openapi.components.ServiceManager;
89
import com.intellij.openapi.util.Pair;
@@ -16,22 +17,20 @@
1617
import com.jetbrains.idear.recognizer.CustomMicrophone;
1718
import com.jetbrains.idear.tts.TTSService;
1819
import edu.cmu.sphinx.api.SpeechResult;
19-
20-
import com.intellij.openapi.application.ApplicationInfo;
20+
import org.jetbrains.annotations.Nullable;
2121

2222
import javax.sound.sampled.AudioInputStream;
2323
import javax.sound.sampled.AudioSystem;
2424
import javax.sound.sampled.Clip;
2525
import java.awt.*;
2626
import java.io.IOException;
2727
import java.text.SimpleDateFormat;
28-
import java.time.format.DateTimeFormatter;
2928
import java.util.Calendar;
3029
import java.util.Collection;
31-
import java.util.Locale;
3230
import java.util.logging.Level;
3331
import java.util.logging.Logger;
3432

33+
import static com.jetbrains.idear.GoogleHelper.getBestTextForUtterance;
3534
import static java.awt.event.KeyEvent.*;
3635

3736
/**
@@ -152,8 +151,13 @@ private void applyAction(String c) {
152151
} else if (c.endsWith("symbols")) {
153152
ideService.invokeAction("AceJumpAction").doWhenDone((Consumer<DataContext>) dataContext -> {
154153
ideService.type(VK_SPACE);
155-
ideService.type(("" + recognizeNumber()).toCharArray());
156154
});
155+
try {
156+
Thread.sleep(500);
157+
} catch (InterruptedException e) {
158+
e.printStackTrace();
159+
}
160+
ideService.type(("" + recognizeNumber()).toCharArray());
157161
}
158162
} else if (c.startsWith(GOTO)) {
159163
if (c.startsWith("goto line")) {
@@ -223,7 +227,6 @@ private void applyAction(String c) {
223227
beep();
224228
fireVoiceCommand();
225229
} else if (c.startsWith(OKAY_GOOGLE) || c.startsWith(OK_GOOGLE)) {
226-
beep();
227230
fireGoogleSearch();
228231
} else if (c.contains("break point")) {
229232
if (c.startsWith("toggle")) {
@@ -258,11 +261,28 @@ private void applyAction(String c) {
258261
Calendar cal = ai.getBuildDate();
259262
SimpleDateFormat df = new SimpleDateFormat("EEEE, MMMM dd, yyyy");
260263

261-
262264
say("My name is " + ai.getVersionName() + ", I was built on " + df.format(cal.getTime()) + ", I am running version " + ai.getApiVersion() + " of the IntelliJ Platform, and I am registered to " + ai.getCompanyName());
265+
} else if(c.contains("add new class")) {
266+
ideService.invokeAction("NewElement");
267+
pressKeystroke(VK_ENTER);
268+
String className = getWebSpeechResult().first;
269+
if(className != null) {
270+
String camelCase = convertToCamelCase(className);
271+
logger.log(Level.INFO, "Class name: "+ camelCase);
272+
ideService.type(camelCase);
273+
pressKeystroke(VK_ENTER);
274+
}
275+
} else if(c.contains("print line")) {
276+
ideService.type("sout");
277+
pressKeystroke(VK_TAB);
263278
}
264279
}
265280

281+
private String convertToCamelCase(String s) {
282+
String noSpaces = s.replaceAll("([\\W_]+)([a-zA-Z0-9])", "$2".toUpperCase());
283+
return noSpaces.substring(0, 1).toUpperCase() + noSpaces.substring(1);
284+
}
285+
266286
private void pressKeystroke(final int... keys) {
267287
ServiceManager.getService(IDEService.class)
268288
.type(keys);
@@ -314,7 +334,7 @@ private void tellJoke() {
314334

315335
private void fireVoiceCommand() {
316336
try {
317-
Pair<String, Double> commandTuple = GoogleHelper.getBestTextForUtterance(CustomMicrophone.recordFromMic(COMMAND_DURATION));
337+
Pair<String, Double> commandTuple = getBestTextForUtterance(CustomMicrophone.recordFromMic(COMMAND_DURATION));
318338

319339
if (commandTuple == null || commandTuple.first.isEmpty() /* || searchQuery.second < CONFIDENCE_LEVEL_THRESHOLD */)
320340
return;
@@ -343,21 +363,31 @@ private void fireVoiceCommand() {
343363

344364
private void fireGoogleSearch() {
345365

346-
try {
347-
Pair<String, Double> searchQueryTuple = GoogleHelper.getBestTextForUtterance(CustomMicrophone.recordFromMic(GOOGLE_QUERY_DURATION));
348-
349-
if (searchQueryTuple == null || searchQueryTuple.first.isEmpty() /* || searchQuery.second < CONFIDENCE_LEVEL_THRESHOLD */)
350-
return;
366+
Pair<String, Double> searchQueryTuple = getWebSpeechResult();
367+
if (searchQueryTuple == null) return;
351368

352369
ServiceManager
353370
.getService(TTSService.class)
354371
.say("I think you said " + searchQueryTuple.first + ", searching Google now");
355372

356373
GoogleHelper.searchGoogle(searchQueryTuple.first);
374+
}
357375

376+
@Nullable
377+
private Pair<String, Double> getWebSpeechResult() {
378+
Pair<String, Double> searchQueryTuple = null;
379+
beep();
380+
try {
381+
searchQueryTuple = GoogleHelper.getBestTextForUtterance(CustomMicrophone.recordFromMic(GOOGLE_QUERY_DURATION));
358382
} catch (IOException e) {
359383
logger.log(Level.SEVERE, "Panic! Failed to dump WAV", e);
360384
}
385+
386+
if (searchQueryTuple == null || searchQueryTuple.first.isEmpty() /* || searchQuery.second < CONFIDENCE_LEVEL_THRESHOLD */)
387+
return null;
388+
389+
beep();
390+
return searchQueryTuple;
361391
}
362392

363393
private void pauseSpeech() {
@@ -372,10 +402,14 @@ private void pauseSpeech() {
372402

373403
private int recognizeNumber() {
374404
String result;
405+
logger.info("Recognizing number...");
375406
while (true) {
376407
result = getResultFromRecognizer();
377-
if (result.startsWith("jump "))
378-
return WordToNumberConverter.getNumber(result.substring(5));
408+
if (result.startsWith("jump ")) {
409+
int number = WordToNumberConverter.getNumber(result.substring(5));
410+
logger.info("Recognized number: " + number);
411+
return number;
412+
}
379413
}
380414
}
381415

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

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@
66
import com.intellij.util.Consumer;
77

88
import java.awt.*;
9-
import java.lang.reflect.InvocationTargetException;
109
import java.util.function.Function;
1110
import java.util.logging.Logger;
1211

@@ -76,4 +75,12 @@ public void type(final char... keys) {
7675
robot.keyRelease(key);
7776
}
7877
}
78+
79+
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+
}
85+
}
7986
}

src/main/java/com/johnlindquist/acejump/AceJumpAction.kt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ import javax.swing.event.ChangeListener
3232

3333
open class AceJumpAction() : DumbAwareAction() {
3434

35+
3536
override fun update(e: AnActionEvent?) {
3637
e?.presentation?.isEnabled = (e?.getData(CommonDataKeys.EDITOR)) != null
3738
}

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

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ grammar dialog;
3939

4040
<keyboard_command> = press [return | escape | tab | undo | delete] key;
4141

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

4444
<check_not_null> = check not null;
4545

@@ -49,6 +49,11 @@ grammar dialog;
4949

5050
<jump_command> = jump <number>;
5151

52+
<search_command> = okay google;
53+
54+
<code_command> = add new class | public static void main | print line | new string;
55+
56+
5257
<number> = <hundreds> | <tens> | <teens> | <ones>;
5358
<hundreds> = <ones> hundred [<tens> | <teens> | <ones>];
5459
<tens> = (twenty | thirty | forty | fifty | sixty | seventy | eighty | ninety) [<ones>];
@@ -65,4 +70,6 @@ public <command> = <init_command> |
6570
<joke_command> |
6671
<check_not_null> |
6772
<keyboard_command> |
68-
<about_command>;
73+
<about_command> |
74+
<search_command> |
75+
<code_command>;

0 commit comments

Comments
 (0)