33import com .intellij .ide .DataManager ;
44import com .intellij .openapi .actionSystem .*;
55import com .intellij .openapi .actionSystem .impl .SimpleDataContext ;
6+ import com .intellij .openapi .application .ApplicationInfo ;
67import com .intellij .openapi .application .ApplicationManager ;
78import com .intellij .openapi .components .ServiceManager ;
89import com .intellij .openapi .util .Pair ;
1617import com .jetbrains .idear .recognizer .CustomMicrophone ;
1718import com .jetbrains .idear .tts .TTSService ;
1819import edu .cmu .sphinx .api .SpeechResult ;
19-
20- import com .intellij .openapi .application .ApplicationInfo ;
20+ import org .jetbrains .annotations .Nullable ;
2121
2222import javax .sound .sampled .AudioInputStream ;
2323import javax .sound .sampled .AudioSystem ;
2424import javax .sound .sampled .Clip ;
2525import java .awt .*;
2626import java .io .IOException ;
2727import java .text .SimpleDateFormat ;
28- import java .time .format .DateTimeFormatter ;
2928import java .util .Calendar ;
3029import java .util .Collection ;
31- import java .util .Locale ;
3230import java .util .logging .Level ;
3331import java .util .logging .Logger ;
3432
33+ import static com .jetbrains .idear .GoogleHelper .getBestTextForUtterance ;
3534import 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
0 commit comments