Skip to content

Commit 22e9d8c

Browse files
committed
add new ai context menu items in components
1 parent 9c08c1d commit 22e9d8c

4 files changed

Lines changed: 77 additions & 12 deletions

File tree

src/main/java/gr/sqlbrowserfx/SqlBrowserFXApp.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -425,8 +425,7 @@ private MenuBar createMenu(DockPane dockPane) {
425425
chatGPTViewItem.setOnAction(event -> {
426426
var chatGptWebView = new ChatGptWebView();
427427
SqlBrowserFXAppManager.registerChatGpt(chatGptWebView);
428-
var dockNode = new DockNode(chatGptWebView, "ChatGPT", JavaFXUtils.createIcon("/icons/chatgpt.png"));
429-
dockNode.dock(dockPane, DockPos.RIGHT);
428+
var dockNode = new DockNode(dockPane, chatGptWebView, "ChatGPT", JavaFXUtils.createIcon("/icons/chatgpt.png"));
430429
dockNode.setOnClose(() -> SqlBrowserFXAppManager.unregisterChatGpt());
431430

432431
});

src/main/java/gr/sqlbrowserfx/SqlBrowserFXAppManager.java

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
import gr.sqlbrowserfx.dock.nodes.DDBTreeView;
1111
import gr.sqlbrowserfx.dock.nodes.DSqlConsolePane;
1212
import gr.sqlbrowserfx.dock.nodes.DSqlPane;
13+
import gr.sqlbrowserfx.factories.DialogFactory;
1314
import gr.sqlbrowserfx.nodes.ChatGptWebView;
1415
import gr.sqlbrowserfx.nodes.sqlpane.SqlPane;
1516

@@ -92,8 +93,17 @@ public static void unregisterChatGpt() {
9293

9394
public static void askChatGpt(String question) {
9495
if (chatGpt == null) {
95-
throw new RuntimeException("ChatGPT is not open");
96+
DialogFactory.createNotification("ChatGPT is not open", "Please open ChatGPT dock node first.");
97+
return;
9698
}
9799
chatGpt.askChatGpt(question);
98100
}
101+
102+
public static String getAiGeneratedCode() {
103+
if (chatGpt == null) {
104+
DialogFactory.createNotification("ChatGPT is not open", "Please open ChatGPT dock node first.");
105+
return "";
106+
}
107+
return chatGpt.getAiGeneratedCode();
108+
}
99109
}

src/main/java/gr/sqlbrowserfx/nodes/DBTreeView.java

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
import org.slf4j.LoggerFactory;
2020

2121
import gr.sqlbrowserfx.LoggerConf;
22+
import gr.sqlbrowserfx.SqlBrowserFXAppManager;
2223
import gr.sqlbrowserfx.conn.DbCash;
2324
import gr.sqlbrowserfx.conn.MysqlConnector;
2425
import gr.sqlbrowserfx.conn.SqlConnector;
@@ -580,7 +581,24 @@ public ContextMenu createContextMenu() {
580581
popOver.show(this.getSelectionModel().getSelectedItem().getGraphic());
581582
});
582583
showSchema.disableProperty().bind(this.hasSelectedSchemaProperty.not());
584+
585+
var feedSchemaToChatGpt = new MenuItem("Feed Schema (ChatGPT)", JavaFXUtils.createIcon("/icons/chatgpt.png"));
586+
feedSchemaToChatGpt.setOnAction(action -> {
587+
var schema = this.copyScemaAction();
588+
SqlBrowserFXAppManager.askChatGpt("Here is the sql schema: " + schema
589+
+ ". Use this information to assist with SQL queries. Do not answer anything else.");
590+
});
591+
feedSchemaToChatGpt.disableProperty().bind(this.hasSelectedSchemaProperty.not());
592+
593+
var explainChatGpt = new MenuItem("Explain (ChaGPT)", JavaFXUtils.createIcon("/icons/chatgpt.png"));
594+
explainChatGpt.setOnAction(action -> {
595+
var schema = this.copyScemaAction();
596+
SqlBrowserFXAppManager.askChatGpt("Explain the following sql schema with short answer: " + schema);
597+
});
598+
explainChatGpt.disableProperty().bind(this.hasSelectedSchemaProperty.not());
583599

600+
601+
584602
var setAsRoot = new MenuItem("Select As Root", JavaFXUtils.createIcon("/icons/database.png"));
585603
setAsRoot.setOnAction(action -> {
586604
if (this.getSelectionModel().getSelectedItem() == null)
@@ -596,7 +614,13 @@ public ContextMenu createContextMenu() {
596614
restoreRoot.setOnAction(event -> this.setRoot(this.rootItem));
597615
restoreRoot.disableProperty().bind(this.getSelectionModel().selectedItemProperty().isEqualTo(this.rootItem));
598616

599-
contextMenu.getItems().addAll(copy, copySchema, new SeparatorMenuItem(), collapseAll, showSchema, new SeparatorMenuItem(), setAsRoot, restoreRoot, new SeparatorMenuItem(), drop);
617+
contextMenu.getItems().addAll(
618+
copy, copySchema, new SeparatorMenuItem(),
619+
feedSchemaToChatGpt, explainChatGpt, new SeparatorMenuItem(),
620+
collapseAll, showSchema, new SeparatorMenuItem(),
621+
setAsRoot, restoreRoot, new SeparatorMenuItem(),
622+
drop
623+
);
600624

601625
return contextMenu;
602626
}

src/main/java/gr/sqlbrowserfx/nodes/codeareas/sql/SqlCodeArea.java

Lines changed: 40 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -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

Comments
 (0)