diff --git a/build.gradle.kts b/build.gradle.kts index 866ad718..db8e3fd8 100644 --- a/build.gradle.kts +++ b/build.gradle.kts @@ -4,7 +4,7 @@ plugins { } group = "ee.carlrobert" -version = "1.5.1" +version = "1.5.2" repositories { mavenCentral() diff --git a/src/main/java/ee/carlrobert/codegpt/ide/action/AskAction.java b/src/main/java/ee/carlrobert/codegpt/ide/action/AskAction.java index c9db7e34..a983e9b8 100644 --- a/src/main/java/ee/carlrobert/codegpt/ide/action/AskAction.java +++ b/src/main/java/ee/carlrobert/codegpt/ide/action/AskAction.java @@ -24,9 +24,8 @@ public class AskAction extends AnAction { var project = event.getProject(); if (project != null) { ConversationsState.getInstance().startConversation(); - project.getService(ContentManagerService.class).displayChatTab(); + project.getService(ContentManagerService.class).displayChatTab(project); var chatToolWindow = project.getService(ToolWindowService.class).getChatToolWindow(); - chatToolWindow.show(); chatToolWindow.displayLandingView(); } } diff --git a/src/main/java/ee/carlrobert/codegpt/ide/action/BaseAction.java b/src/main/java/ee/carlrobert/codegpt/ide/action/BaseAction.java index abe0a7e4..ed16ffb9 100644 --- a/src/main/java/ee/carlrobert/codegpt/ide/action/BaseAction.java +++ b/src/main/java/ee/carlrobert/codegpt/ide/action/BaseAction.java @@ -48,11 +48,8 @@ public abstract class BaseAction extends AnAction { protected void sendMessage(@NotNull Project project, String prompt) { ConversationsState.getInstance().startConversation(); - project.getService(ContentManagerService.class).displayChatTab(); - - var toolWindowService = project.getService(ToolWindowService.class); - var chatToolWindow = toolWindowService.getChatToolWindow(); - chatToolWindow.show(); + project.getService(ContentManagerService.class).displayChatTab(project); + var chatToolWindow = project.getService(ToolWindowService.class).getChatToolWindow(); chatToolWindow.clearWindow(); chatToolWindow.displayUserMessage(prompt); chatToolWindow.sendMessage(prompt, project); diff --git a/src/main/java/ee/carlrobert/codegpt/ide/toolwindow/ChatGptToolWindow.java b/src/main/java/ee/carlrobert/codegpt/ide/toolwindow/ChatGptToolWindow.java index fafd10c9..3c508bb1 100644 --- a/src/main/java/ee/carlrobert/codegpt/ide/toolwindow/ChatGptToolWindow.java +++ b/src/main/java/ee/carlrobert/codegpt/ide/toolwindow/ChatGptToolWindow.java @@ -9,7 +9,6 @@ import com.intellij.icons.AllIcons; import com.intellij.openapi.options.ShowSettingsUtil; import com.intellij.openapi.project.Project; import com.intellij.openapi.roots.ui.componentsList.components.ScrollablePanel; -import com.intellij.openapi.wm.ToolWindow; import com.intellij.ui.JBColor; import com.intellij.ui.components.JBScrollPane; import ee.carlrobert.codegpt.ide.conversations.Conversation; @@ -45,7 +44,6 @@ public class ChatGptToolWindow { private static final List textAreas = new ArrayList<>(); private final Project project; - private final ToolWindow toolWindow; private JPanel chatGptToolWindowContent; private ScrollPane scrollPane; private ScrollablePanel scrollablePanel; @@ -54,19 +52,14 @@ public class ChatGptToolWindow { private GenerateButton generateButton; private boolean isLandingViewVisible; - public ChatGptToolWindow(@NotNull Project project, @NotNull ToolWindow toolWindow) { + public ChatGptToolWindow(@NotNull Project project) { this.project = project; - this.toolWindow = toolWindow; } public JPanel getContent() { return chatGptToolWindowContent; } - public void show() { - toolWindow.show(); - } - public void displayUserMessage(String userMessage) { addIconLabel(AllIcons.General.User, "User:"); scrollablePanel.add(createTextArea(userMessage)); diff --git a/src/main/java/ee/carlrobert/codegpt/ide/toolwindow/ContentManagerService.java b/src/main/java/ee/carlrobert/codegpt/ide/toolwindow/ContentManagerService.java index 2fd1740f..6e774cb7 100644 --- a/src/main/java/ee/carlrobert/codegpt/ide/toolwindow/ContentManagerService.java +++ b/src/main/java/ee/carlrobert/codegpt/ide/toolwindow/ContentManagerService.java @@ -2,50 +2,35 @@ package ee.carlrobert.codegpt.ide.toolwindow; import static java.util.Objects.requireNonNull; -import com.intellij.openapi.wm.ToolWindow; +import com.intellij.openapi.project.Project; +import com.intellij.openapi.wm.ToolWindowManager; import com.intellij.ui.content.Content; -import com.intellij.ui.content.ContentFactory; import com.intellij.ui.content.ContentManager; import java.util.Arrays; import java.util.Optional; -import javax.swing.JPanel; import org.jetbrains.annotations.NotNull; public class ContentManagerService { - private ToolWindow toolWindow; - - public void setToolWindow(@NotNull ToolWindow toolWindow) { - this.toolWindow = toolWindow; - } - - public void addContent(JPanel panel, String displayName) { - ContentFactory contentFactory = ContentFactory.SERVICE.getInstance(); - Content content = contentFactory.createContent(panel, displayName, false); - toolWindow.getContentManager().addContent(content); - } - - public void displayChatTab() { - var contentManager = getContentManager(); - tryFindChatTabContent().ifPresentOrElse( + public void displayChatTab(@NotNull Project project) { + var toolWindow = requireNonNull(ToolWindowManager.getInstance(project).getToolWindow("CodeGPT")); + toolWindow.show(); + var contentManager = toolWindow.getContentManager(); + tryFindChatTabContent(contentManager).ifPresentOrElse( contentManager::setSelectedContent, - () -> contentManager.setSelectedContent(requireNonNull(getContentManager().getContent(0))) + () -> contentManager.setSelectedContent(requireNonNull(contentManager.getContent(0))) ); } - public Optional tryFindChatTabContent() { - return Arrays.stream(getContentManager().getContents()) + public Optional tryFindChatTabContent(ContentManager contentManager) { + return Arrays.stream(contentManager.getContents()) .filter(content -> "Chat".equals(content.getTabName())) .findFirst(); } - public boolean isChatTabSelected() { - return tryFindChatTabContent() - .filter(content -> getContentManager().isSelected(content)) + public boolean isChatTabSelected(ContentManager contentManager) { + return tryFindChatTabContent(contentManager) + .filter(contentManager::isSelected) .isPresent(); } - - private ContentManager getContentManager() { - return toolWindow.getContentManager(); - } } diff --git a/src/main/java/ee/carlrobert/codegpt/ide/toolwindow/ProjectToolWindowFactory.java b/src/main/java/ee/carlrobert/codegpt/ide/toolwindow/ProjectToolWindowFactory.java index 88e034f6..ff6a552f 100644 --- a/src/main/java/ee/carlrobert/codegpt/ide/toolwindow/ProjectToolWindowFactory.java +++ b/src/main/java/ee/carlrobert/codegpt/ide/toolwindow/ProjectToolWindowFactory.java @@ -4,24 +4,26 @@ import com.intellij.openapi.project.DumbAware; import com.intellij.openapi.project.Project; import com.intellij.openapi.wm.ToolWindow; import com.intellij.openapi.wm.ToolWindowFactory; +import com.intellij.ui.content.Content; +import com.intellij.ui.content.ContentFactory; import com.intellij.ui.content.ContentManagerEvent; import com.intellij.ui.content.ContentManagerListener; import ee.carlrobert.codegpt.ide.conversations.ConversationsState; import ee.carlrobert.codegpt.ide.toolwindow.conversations.ConversationsToolWindow; +import javax.swing.JPanel; import org.jetbrains.annotations.NotNull; public class ProjectToolWindowFactory implements ToolWindowFactory, DumbAware { public void createToolWindowContent(@NotNull Project project, @NotNull ToolWindow toolWindow) { - var toolWindowService = project.getService(ToolWindowService.class); - var chatToolWindow = new ChatGptToolWindow(project, toolWindow); + var chatToolWindow = new ChatGptToolWindow(project); var conversationsToolWindow = new ConversationsToolWindow(project); + var toolWindowService = project.getService(ToolWindowService.class); toolWindowService.setChatToolWindow(chatToolWindow); var contentManagerService = project.getService(ContentManagerService.class); - contentManagerService.setToolWindow(toolWindow); - contentManagerService.addContent(chatToolWindow.getContent(), "Chat"); - contentManagerService.addContent(conversationsToolWindow.getContent(), "Conversation History"); + addContent(toolWindow, chatToolWindow.getContent(), "Chat"); + addContent(toolWindow, conversationsToolWindow.getContent(), "Conversation History"); toolWindow.addContentManagerListener(new ContentManagerListener() { public void selectionChanged(@NotNull ContentManagerEvent event) { var content = event.getContent(); @@ -31,7 +33,7 @@ public class ProjectToolWindowFactory implements ToolWindowFactory, DumbAware { } }); - if (contentManagerService.isChatTabSelected()) { + if (contentManagerService.isChatTabSelected(toolWindow.getContentManager())) { var conversation = ConversationsState.getCurrentConversation(); if (conversation == null) { chatToolWindow.displayLandingView(); @@ -40,4 +42,10 @@ public class ProjectToolWindowFactory implements ToolWindowFactory, DumbAware { } } } + + public void addContent(ToolWindow toolWindow, JPanel panel, String displayName) { + ContentFactory contentFactory = ContentFactory.SERVICE.getInstance(); + Content content = contentFactory.createContent(panel, displayName, false); + toolWindow.getContentManager().addContent(content); + } } diff --git a/src/main/java/ee/carlrobert/codegpt/ide/toolwindow/conversations/ConversationsToolWindow.java b/src/main/java/ee/carlrobert/codegpt/ide/toolwindow/conversations/ConversationsToolWindow.java index 8bbd1581..760a8415 100644 --- a/src/main/java/ee/carlrobert/codegpt/ide/toolwindow/conversations/ConversationsToolWindow.java +++ b/src/main/java/ee/carlrobert/codegpt/ide/toolwindow/conversations/ConversationsToolWindow.java @@ -46,7 +46,7 @@ public class ConversationsToolWindow { var mainPanel = new RootConversationPanel(() -> { ConversationsState.getInstance().setCurrentConversation(conversation); changeSettings(conversation); - project.getService(ContentManagerService.class).displayChatTab(); + project.getService(ContentManagerService.class).displayChatTab(project); project.getService(ToolWindowService.class) .getChatToolWindow() .displayConversation(conversation);