3030import java .util .Collection ;
3131import java .util .logging .Level ;
3232import java .util .logging .Logger ;
33+ import java .util .regex .Matcher ;
34+ import java .util .regex .Pattern ;
3335
3436import static com .jetbrains .idear .GoogleHelper .getBestTextForUtterance ;
3537import static java .awt .event .KeyEvent .*;
@@ -152,7 +154,7 @@ private void applyAction(String c) {
152154 } else if (c .endsWith ("symbols" )) {
153155 AsyncResult ar = ideService .invokeAction ("AceJumpAction" );
154156
155- while (!ar .isProcessed ()) {
157+ while (!ar .isProcessed ()) {
156158 //Spin lock
157159 logger .info ("Not done..." );
158160 try {
@@ -195,11 +197,11 @@ private void applyAction(String c) {
195197 pressKeystroke (VK_TAB );
196198 } else if (c .contains (UNDO )) {
197199 ideService .invokeAction ("$Undo" );
198- } else if (c .contains ("shift" )) {
200+ } else if (c .contains ("shift" )) {
199201 ideService .pressShift ();
200202 }
201- } else if (c .startsWith ("release" )) {
202- if (c .contains ("shift" ))
203+ } else if (c .startsWith ("release" )) {
204+ if (c .contains ("shift" ))
203205 ideService .releaseShift ();
204206 } else if (c .startsWith ("following" )) {
205207 if (c .endsWith ("line" )) {
@@ -212,6 +214,8 @@ private void applyAction(String c) {
212214 ideService .invokeAction ("Diff.FocusOppositePane" );
213215 } else if (c .endsWith ("page" )) {
214216 ideService .invokeAction ("EditorPageDown" );
217+ } else if (c .endsWith ("word" )) {
218+ ideService .type (VK_ALT , VK_RIGHT );
215219 }
216220 } else if (c .startsWith ("previous" )) {
217221 if (c .endsWith ("line" )) {
@@ -235,9 +239,10 @@ private void applyAction(String c) {
235239 ideService .invokeAction ("CodeInspection.OnEditor" );
236240 } else if (c .startsWith ("speech pause" )) {
237241 pauseSpeech ();
238- } else if (c .equals (SHOW_USAGES )) {
242+ } else if (c .equals (SHOW_USAGES )) {
239243 ideService .invokeAction ("ShowUsages" );
240- } if (c .startsWith (OK_IDEA ) || c .startsWith (OKAY_IDEA )) {
244+ }
245+ if (c .startsWith (OK_IDEA ) || c .startsWith (OKAY_IDEA )) {
241246 beep ();
242247 fireVoiceCommand ();
243248 } else if (c .startsWith (OKAY_GOOGLE ) || c .startsWith (OK_GOOGLE )) {
@@ -249,7 +254,8 @@ private void applyAction(String c) {
249254 ideService .invokeAction ("ViewBreakpoints" );
250255 }
251256 } else if (c .startsWith ("debug" )) {
252- ideService .invokeAction ("Debug" );
257+ // ideService.invokeAction("Debug");
258+ ideService .type (VK_CONTROL , VK_SHIFT , VK_F9 );
253259 } else if (c .startsWith ("step" )) {
254260 if (c .endsWith ("over" )) {
255261 ideService .invokeAction ("StepOver" );
@@ -269,32 +275,67 @@ private void applyAction(String c) {
269275 .getDataContextFromFocus ()
270276 .doWhenDone ((Consumer <DataContext >) dataContext -> run (nullCheckRecognizer , c , dataContext ));
271277 }
272- } else if (c .contains ("tell me about yourself" )) {
278+ } else if (c .contains ("tell me about yourself" )) {
273279 ApplicationInfo ai = ApplicationInfo .getInstance ();
274280
275281 Calendar cal = ai .getBuildDate ();
276282 SimpleDateFormat df = new SimpleDateFormat ("EEEE, MMMM dd, yyyy" );
277283
278284 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 ());
279- } else if (c .contains ("add new class" )) {
285+ } else if (c .contains ("add new class" )) {
280286 ideService .invokeAction ("NewElement" );
281287 pressKeystroke (VK_ENTER );
282- String className = getWebSpeechResult ().first ;
283- if (className != null ) {
284- String camelCase = convertToCamelCase (className );
285- logger .log (Level .INFO , "Class name: " + camelCase );
288+ Pair <String , Double > className = getWebSpeechResult ();
289+ if (className != null ) {
290+ String camelCase = convertToCamelCase (className .first );
291+ logger .log (Level .INFO , "Class name: " + camelCase );
292+ camelCase = camelCase .substring (0 , 1 ).toUpperCase () + camelCase .substring (1 );
286293 ideService .type (camelCase );
287294 pressKeystroke (VK_ENTER );
288295 }
289- } else if (c .contains ("print line" )) {
296+ } else if (c .contains ("print line" )) {
290297 ideService .type ("sout" );
291298 pressKeystroke (VK_TAB );
299+ } else if (c .contains ("new string" )) {
300+ Pair <String , Double > result = getWebSpeechResult ();
301+ if (result != null ) {
302+ ideService .type (VK_SHIFT , VK_QUOTE );
303+ ideService .type (result .first );
304+ ideService .type (VK_SHIFT , VK_QUOTE );
305+ }
306+ } else if (c .contains ("enter " )) {
307+ Pair <String , Double > result = getWebSpeechResult ();
308+ if (result != null ) {
309+ if (c .endsWith ("text" )) {
310+ ideService .type (result .first );
311+ } else if (c .endsWith ("camel case" )) {
312+ ideService .type (convertToCamelCase (result .first ));
313+ }
314+ }
315+ } else if (c .contains ("public static void main" )) {
316+ ideService .type ("psvm" );
317+ pressKeystroke (VK_TAB );
318+ } else if (c .endsWith ("of line" )) {
319+ if (c .startsWith ("beginning" )) {
320+ ideService .type (VK_META , VK_LEFT );
321+ } else if (c .startsWith ("end" )) {
322+ ideService .type (VK_META , VK_RIGHT );
323+ }
292324 }
293325 }
294326
295327 private String convertToCamelCase (String s ) {
296- String noSpaces = s .replaceAll ("([\\ W_]+)([a-zA-Z0-9])" , "$2" .toUpperCase ());
297- return noSpaces .substring (0 , 1 ).toUpperCase () + noSpaces .substring (1 );
328+ Matcher m = Pattern .compile ("([\\ s]+)([A-Za-z0-9])" ).matcher (s );
329+ StringBuilder sb = new StringBuilder ();
330+ int last = 0 ;
331+ while (m .find ()) {
332+ sb .append (s .substring (last , m .start ()));
333+ sb .append (m .group (2 ).toUpperCase ());
334+ last = m .end ();
335+ }
336+ sb .append (s .substring (last ));
337+
338+ return sb .toString ();
298339 }
299340
300341 private void pressKeystroke (final int ... keys ) {
@@ -377,22 +418,22 @@ private void fireVoiceCommand() {
377418
378419 private void fireGoogleSearch () {
379420
380- Pair <String , Double > searchQueryTuple = getWebSpeechResult ();
381- if (searchQueryTuple == null ) return ;
421+ Pair <String , Double > searchQueryTuple = getWebSpeechResult ();
422+ if (searchQueryTuple == null ) return ;
382423
383- ServiceManager
384- .getService (TTSService .class )
385- .say ("I think you said " + searchQueryTuple .first + ", searching Google now" );
424+ ServiceManager
425+ .getService (TTSService .class )
426+ .say ("I think you said " + searchQueryTuple .first + ", searching Google now" );
386427
387- GoogleHelper .searchGoogle (searchQueryTuple .first );
428+ GoogleHelper .searchGoogle (searchQueryTuple .first );
388429 }
389430
390431 @ Nullable
391432 private Pair <String , Double > getWebSpeechResult () {
392433 Pair <String , Double > searchQueryTuple = null ;
393434 beep ();
394435 try {
395- searchQueryTuple = GoogleHelper .getBestTextForUtterance (CustomMicrophone .recordFromMic (GOOGLE_QUERY_DURATION ));
436+ searchQueryTuple = GoogleHelper .getBestTextForUtterance (CustomMicrophone .recordFromMic (GOOGLE_QUERY_DURATION ));
396437 } catch (IOException e ) {
397438 logger .log (Level .SEVERE , "Panic! Failed to dump WAV" , e );
398439 }
@@ -405,10 +446,12 @@ private Pair<String, Double> getWebSpeechResult() {
405446 }
406447
407448 private void pauseSpeech () {
449+ beep ();
408450 String result ;
409451 while (ListeningState .isActive ()) {
410452 result = getResultFromRecognizer ();
411453 if (result .equals ("speech resume" )) {
454+ beep ();
412455 break ;
413456 }
414457 }
0 commit comments