Skip to content

Commit

Permalink
272 - Fix editor actions when createNewChatOnEachAction cfg is turned…
Browse files Browse the repository at this point in the history
… on (#283)
  • Loading branch information
carlrobertoh authored Nov 21, 2023
1 parent ecf6ac0 commit 2317b54
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 33 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ public class EditorActionsUtil {
public static String[][] toArray(Map<String, String> 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]);
}
Expand Down Expand Up @@ -66,10 +66,6 @@ protected void actionPerformed(Project project, Editor editor, String selectedTe
var toolWindow = toolWindowContentManager.getToolWindow();
if (toolWindow != null) {
toolWindow.show();

if (ConfigurationState.getInstance().isCreateNewChatOnEachAction()) {
toolWindowContentManager.createNewTabPanel();
}
}
toolWindowContentManager.sendMessage(message);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@
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;
Expand All @@ -27,29 +26,21 @@ public StandardChatToolWindowContentManager(Project project) {
}

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();
}

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) {
Expand All @@ -64,30 +55,30 @@ public void displayConversation(@NotNull Conversation conversation) {

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() {
var toolWindow = getToolWindow();
toolWindow.show();

var contentManager = toolWindow.getContentManager();
tryFindChatTabContent().ifPresentOrElse(
tryFindFirstChatTabContent().ifPresentOrElse(
contentManager::setSelectedContent,
() -> contentManager.setSelectedContent(requireNonNull(contentManager.getContent(0)))
);
}

public Optional<StandardChatToolWindowTabbedPane> tryFindChatTabbedPane() {
var chatTabContent = tryFindChatTabContent();
var chatTabContent = tryFindFirstChatTabContent();
if (chatTabContent.isPresent()) {
var tabbedPane = Arrays.stream(chatTabContent.get().getComponent().getComponents())
.filter(component -> component instanceof StandardChatToolWindowTabbedPane)
Expand All @@ -112,7 +103,7 @@ public ToolWindow getToolWindow() {
return requireNonNull(ToolWindowManager.getInstance(project).getToolWindow("CodeGPT"));
}

private Optional<Content> tryFindChatTabContent() {
private Optional<Content> tryFindFirstChatTabContent() {
return Arrays.stream(getToolWindow().getContentManager().getContents())
.filter(content -> "Chat".equals(content.getTabName()))
.findFirst();
Expand Down

0 comments on commit 2317b54

Please sign in to comment.