1.5.2 - Fix ToolWindow NPE (fixes #41)

This commit is contained in:
Carl-Robert Linnupuu 2023-03-15 23:31:02 +00:00
parent 95673a838b
commit 3317fb8603
7 changed files with 33 additions and 51 deletions

View file

@ -4,7 +4,7 @@ plugins {
}
group = "ee.carlrobert"
version = "1.5.1"
version = "1.5.2"
repositories {
mavenCentral()

View file

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

View file

@ -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);

View file

@ -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<SyntaxTextArea> 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));

View file

@ -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<Content> tryFindChatTabContent() {
return Arrays.stream(getContentManager().getContents())
public Optional<Content> 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();
}
}

View file

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

View file

@ -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);