@@ -345,13 +345,17 @@ public void setInputMap() {
345345 }
346346 }
347347 );
348-
348+ var generateCode = InputMap .consume (
349+ EventPattern .keyPressed (KeyCode .G , KeyCombination .CONTROL_DOWN ),
350+ action -> this .generateCode ()
351+ );
349352
350353
351354 Nodes .addInputMap (this , run );
352355 Nodes .addInputMap (this , autocomplete );
353356 Nodes .addInputMap (this , history );
354357 Nodes .addInputMap (this , comment );
358+ Nodes .addInputMap (this , generateCode );
355359 }
356360
357361 private void showHistoryPopOver () {
@@ -406,6 +410,22 @@ private void getQueriesHistory(CodeArea codeArea, String dateStr) {
406410 );
407411 }
408412
413+ private void generateCode () {
414+ // Assume codeArea is your CodeArea instance
415+ var caretPos = this .getCaretPosition ();
416+ var textBeforeCaret = this .getText ().substring (0 , caretPos );
417+ var lastCommentIdx = textBeforeCaret .lastIndexOf ("--" );
418+
419+ var result = lastCommentIdx != -1
420+ ? textBeforeCaret .substring (lastCommentIdx , caretPos )
421+ : "" ;
422+
423+ var question = this .getSelectedText ().isEmpty () ? result : this .getSelectedText ();
424+ syntaxProvider .getAiHelp ("Generate only code and only one code block for: " + question );
425+ // FIXME: enable this to paste code in code area
426+ // JavaFXUtils.setTimeout(() -> this.appendText('\n' + syntaxProvider.getAiGeneratedCode()), 6);
427+ }
428+
409429 @ Override
410430 public ContextMenu createContextMenu () {
411431 var menu = super .createContextMenu ();
@@ -418,19 +438,31 @@ public ContextMenu createContextMenu() {
418438
419439 var menuItemShowSchema = new MenuItem ("Show Schema" , JavaFXUtils .createIcon ("/icons/script.png" ));
420440 menuItemShowSchema .setOnAction (action -> SqlCodeArea .this .showSchemaPopOver ());
421- menuItemShowSchema .disableProperty ().bind (this .isTextSelectedProperty ().not ());
422441
423442 var menuItemCheckErrorsChatGpt = new MenuItem ("Check For Erros (ChatGPT)" , JavaFXUtils .createIcon ("/icons/chatgpt.png" ));
424- menuItemCheckErrorsChatGpt .setOnAction (event -> syntaxProvider .getAiHelp ("Check fllowing sql code for errors, keep your answer short with mainly code examples: " + (this .getSelectedText () != null ? this .getSelectedText () : this .getText ())));
425- menuItemCheckErrorsChatGpt .disableProperty ().bind (this .isTextSelectedProperty ().not ());
443+ menuItemCheckErrorsChatGpt .setOnAction (event -> {
444+ if (this .getText ().isEmpty () && this .getSelectedText () == null ) {
445+ return ;
446+ }
447+ syntaxProvider .getAiHelp ("Check following sql code for errors, keep your answer short with mainly code examples: " + (this .getSelectedText () != null ? this .getSelectedText () : this .getText ()));
448+ });
426449
427- var menuItemAskChatGpt = new MenuItem ("Ask ChaGPT" , JavaFXUtils .createIcon ("/icons/chatgpt.png" ));
428- menuItemAskChatGpt .setOnAction (event -> syntaxProvider .getAiHelp (this .getSelectedText ()));
429- menuItemAskChatGpt .disableProperty ().bind (this .isTextSelectedProperty ().not ());
450+ var menuItemExplainChatGpt = new MenuItem ("Explain (ChaGPT)" , JavaFXUtils .createIcon ("/icons/chatgpt.png" ));
451+ menuItemExplainChatGpt .setOnAction (event -> {
452+ if (this .getText ().isEmpty () && this .getSelectedText () == null ) {
453+ return ;
454+ }
455+ syntaxProvider .getAiHelp ("Explain the following with short answer: " + this .getSelectedText ());
456+ });
457+ menuItemExplainChatGpt .disableProperty ().bind (this .isTextSelectedProperty ().not ());
458+
459+ var menuItemGenerateCodeChatGpt = new MenuItem ("Generate Code (ChaGPT)" , JavaFXUtils .createIcon ("/icons/chatgpt.png" ));
460+ menuItemGenerateCodeChatGpt .setOnAction (event -> this .generateCode ());
461+ menuItemGenerateCodeChatGpt .disableProperty ().bind (this .isTextSelectedProperty ().not ());
430462
431463
432464 menu .getItems ().addAll (
433- new SeparatorMenuItem (), menuItemCheckErrorsChatGpt , menuItemAskChatGpt ,
465+ new SeparatorMenuItem (), menuItemCheckErrorsChatGpt , menuItemExplainChatGpt , menuItemGenerateCodeChatGpt ,
434466 new SeparatorMenuItem (), menuItemHistory , menuItemShowSchema );
435467
436468 return menu ;
0 commit comments