diff --git a/src/main/java/ee/carlrobert/codegpt/actions/editor/EditorActionsUtil.java b/src/main/java/ee/carlrobert/codegpt/actions/editor/EditorActionsUtil.java index 9196b2ea..300d644e 100644 --- a/src/main/java/ee/carlrobert/codegpt/actions/editor/EditorActionsUtil.java +++ b/src/main/java/ee/carlrobert/codegpt/actions/editor/EditorActionsUtil.java @@ -33,7 +33,7 @@ public class EditorActionsUtil { public static String[][] toArray(Map actionsMap) { return actionsMap.entrySet() .stream() - .map((entry) -> new String[] {entry.getKey(), entry.getValue()}) + .map((entry) -> new String[]{entry.getKey(), entry.getValue()}) .collect(toList()) .toArray(new String[0][0]); } @@ -66,10 +66,6 @@ public class EditorActionsUtil { var toolWindow = toolWindowContentManager.getToolWindow(); if (toolWindow != null) { toolWindow.show(); - - if (ConfigurationState.getInstance().isCreateNewChatOnEachAction()) { - toolWindowContentManager.createNewTabPanel(); - } } toolWindowContentManager.sendMessage(message); } diff --git a/src/main/java/ee/carlrobert/codegpt/toolwindow/chat/standard/StandardChatToolWindowContentManager.java b/src/main/java/ee/carlrobert/codegpt/toolwindow/chat/standard/StandardChatToolWindowContentManager.java index 495d7382..1b88cecb 100644 --- a/src/main/java/ee/carlrobert/codegpt/toolwindow/chat/standard/StandardChatToolWindowContentManager.java +++ b/src/main/java/ee/carlrobert/codegpt/toolwindow/chat/standard/StandardChatToolWindowContentManager.java @@ -12,7 +12,6 @@ import ee.carlrobert.codegpt.conversations.ConversationService; import ee.carlrobert.codegpt.conversations.ConversationsState; import ee.carlrobert.codegpt.conversations.message.Message; import ee.carlrobert.codegpt.settings.configuration.ConfigurationState; -import ee.carlrobert.codegpt.toolwindow.chat.ChatToolWindowTabPanel; import java.util.Arrays; import java.util.Optional; import org.jetbrains.annotations.NotNull; @@ -27,18 +26,6 @@ public final class StandardChatToolWindowContentManager { } public void sendMessage(Message message) { - var chatTabbedPane = tryFindChatTabbedPane(); - if (chatTabbedPane.isPresent()) { - var activeTabPanel = chatTabbedPane.get().tryFindActiveTabPanel(); - if (activeTabPanel.isPresent()) { - sendMessage(activeTabPanel.get(), message); - return; - } - } - sendMessage(createNewTabPanel(), message); - } - - public void sendMessage(ChatToolWindowTabPanel toolWindowTabPanel, Message message) { var toolWindow = getToolWindow(); if (toolWindow != null) { toolWindow.show(); @@ -46,10 +33,14 @@ public final class StandardChatToolWindowContentManager { if (ConfigurationState.getInstance().isCreateNewChatOnEachAction() || ConversationsState.getCurrentConversation() == null) { - ConversationService.getInstance().startConversation(); - } else { - toolWindowTabPanel.sendMessage(message); + createNewTabPanel().sendMessage(message); + return; } + + tryFindChatTabbedPane() + .map(tabbedPane -> tabbedPane.tryFindActiveTabPanel().orElseGet(this::createNewTabPanel)) + .orElseGet(this::createNewTabPanel) + .sendMessage(message); } public void displayConversation(@NotNull Conversation conversation) { @@ -64,15 +55,15 @@ public final class StandardChatToolWindowContentManager { public StandardChatToolWindowTabPanel createNewTabPanel() { displayChatTab(); - var tabbedPane = tryFindChatTabbedPane(); - if (tabbedPane.isPresent()) { - var panel = new StandardChatToolWindowTabPanel( - project, - ConversationService.getInstance().startConversation()); - tabbedPane.get().addNewTab(panel); - return panel; - } - return null; + return tryFindChatTabbedPane() + .map(item -> { + var panel = new StandardChatToolWindowTabPanel( + project, + ConversationService.getInstance().startConversation()); + item.addNewTab(panel); + return panel; + }) + .orElseThrow(); } public void displayChatTab() { @@ -80,14 +71,14 @@ public final class StandardChatToolWindowContentManager { toolWindow.show(); var contentManager = toolWindow.getContentManager(); - tryFindChatTabContent().ifPresentOrElse( + tryFindFirstChatTabContent().ifPresentOrElse( contentManager::setSelectedContent, () -> contentManager.setSelectedContent(requireNonNull(contentManager.getContent(0))) ); } public Optional tryFindChatTabbedPane() { - var chatTabContent = tryFindChatTabContent(); + var chatTabContent = tryFindFirstChatTabContent(); if (chatTabContent.isPresent()) { var tabbedPane = Arrays.stream(chatTabContent.get().getComponent().getComponents()) .filter(component -> component instanceof StandardChatToolWindowTabbedPane) @@ -112,7 +103,7 @@ public final class StandardChatToolWindowContentManager { return requireNonNull(ToolWindowManager.getInstance(project).getToolWindow("CodeGPT")); } - private Optional tryFindChatTabContent() { + private Optional tryFindFirstChatTabContent() { return Arrays.stream(getToolWindow().getContentManager().getContents()) .filter(content -> "Chat".equals(content.getTabName())) .findFirst();