Fix ToolWindow's NPE when reopening the project

This commit is contained in:
Carl-Robert Linnupuu 2023-03-14 20:01:21 +00:00
parent 63020ba49d
commit 4418c6dd46
7 changed files with 34 additions and 27 deletions

View file

@ -19,7 +19,7 @@ public class AskAction extends AnAction {
var project = event.getProject();
if (project != null) {
ConversationsState.getInstance().startConversation();
ContentManagerService.getInstance(project).displayChatTab();
project.getService(ContentManagerService.class).displayChatTab();
var chatToolWindow = project.getService(ToolWindowService.class).getChatToolWindow();
chatToolWindow.show();
chatToolWindow.displayLandingView();

View file

@ -5,13 +5,23 @@ import com.intellij.openapi.actionSystem.AnActionEvent;
import com.intellij.openapi.actionSystem.PlatformDataKeys;
import com.intellij.openapi.editor.Editor;
import com.intellij.openapi.project.Project;
import com.intellij.openapi.util.NlsActions;
import ee.carlrobert.codegpt.ide.conversations.ConversationsState;
import ee.carlrobert.codegpt.ide.toolwindow.ContentManagerService;
import ee.carlrobert.codegpt.ide.toolwindow.ToolWindowService;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
public abstract class BaseAction extends AnAction {
public BaseAction() {
super();
}
public BaseAction(@Nullable @NlsActions.ActionText String text) {
super(text);
}
protected abstract void actionPerformed(Project project, Editor editor, String selectedText);
public void actionPerformed(@NotNull AnActionEvent event) {
@ -34,7 +44,7 @@ public abstract class BaseAction extends AnAction {
protected void sendMessage(@NotNull Project project, String prompt) {
ConversationsState.getInstance().startConversation();
ContentManagerService.getInstance(project).displayChatTab();
project.getService(ContentManagerService.class).displayChatTab();
var toolWindowService = project.getService(ToolWindowService.class);
var chatToolWindow = toolWindowService.getChatToolWindow();

View file

@ -216,5 +216,6 @@ public class ChatGptToolWindow {
scrollablePanel.setLayout(new BoxLayout(scrollablePanel, BoxLayout.Y_AXIS));
scrollPane = new ScrollPane(scrollablePanel);
generateButton = new GenerateButton();
displayLandingView();
}
}

View file

@ -2,9 +2,7 @@ package ee.carlrobert.codegpt.ide.toolwindow;
import static java.util.Objects.requireNonNull;
import com.intellij.openapi.application.ApplicationManager;
import com.intellij.openapi.project.Project;
import com.intellij.openapi.wm.ToolWindowManager;
import com.intellij.openapi.wm.ToolWindow;
import com.intellij.ui.content.Content;
import com.intellij.ui.content.ContentFactory;
import com.intellij.ui.content.ContentManager;
@ -15,42 +13,39 @@ import org.jetbrains.annotations.NotNull;
public class ContentManagerService {
private static ContentManagerService instance;
private final ContentManager contentManager;
private ToolWindow toolWindow;
private ContentManagerService(Project project) {
this.contentManager = requireNonNull(ToolWindowManager.getInstance(project).getToolWindow("CodeGPT")).getContentManager();
public void setToolWindow(@NotNull ToolWindow toolWindow) {
this.toolWindow = toolWindow;
}
public static ContentManagerService getInstance(@NotNull Project project) {
if (instance == null) {
instance = new ContentManagerService(project);
}
return instance;
}
public void addContent(JPanel content, String displayName) {
contentManager.addContent(ApplicationManager.getApplication()
.getService(ContentFactory.class)
.createContent(content, displayName, false));
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(
contentManager::setSelectedContent,
() -> contentManager.setSelectedContent(requireNonNull(contentManager.getContent(0)))
() -> contentManager.setSelectedContent(requireNonNull(getContentManager().getContent(0)))
);
}
public Optional<Content> tryFindChatTabContent() {
return Arrays.stream(contentManager.getContents())
return Arrays.stream(getContentManager().getContents())
.filter(content -> "Chat".equals(content.getTabName()))
.findFirst();
}
public boolean isChatTabSelected() {
return tryFindChatTabContent()
.filter(contentManager::isSelected)
.filter(content -> getContentManager().isSelected(content))
.isPresent();
}
private ContentManager getContentManager() {
return toolWindow.getContentManager();
}
}

View file

@ -18,7 +18,8 @@ public class ProjectToolWindowFactory implements ToolWindowFactory, DumbAware {
var conversationsToolWindow = new ConversationsToolWindow(project);
toolWindowService.setChatToolWindow(chatToolWindow);
var contentManagerService = ContentManagerService.getInstance(project);
var contentManagerService = project.getService(ContentManagerService.class);
contentManagerService.setToolWindow(toolWindow);
contentManagerService.addContent(chatToolWindow.getContent(), "Chat");
contentManagerService.addContent(conversationsToolWindow.getContent(), "Conversation History");
toolWindow.addContentManagerListener(new ContentManagerListener() {

View file

@ -46,7 +46,7 @@ public class ConversationsToolWindow {
var mainPanel = new RootConversationPanel(() -> {
ConversationsState.getInstance().setCurrentConversation(conversation);
changeSettings(conversation);
ContentManagerService.getInstance(project).displayChatTab();
project.getService(ContentManagerService.class).displayChatTab();
project.getService(ToolWindowService.class)
.getChatToolWindow()
.displayConversation(conversation);

View file

@ -93,8 +93,7 @@
<change-notes>
<![CDATA[
<ul>
<li>Replace model on conversation change</li>
<li>Start new conversation on model change</li>
<li>Fix ToolWindow's NPE when reopening the project</li>
</ul>
]]>
</change-notes>
@ -113,6 +112,7 @@
<applicationService serviceImplementation="ee.carlrobert.codegpt.ide.settings.SettingsState"/>
<applicationService serviceImplementation="ee.carlrobert.codegpt.ide.conversations.ConversationsState"/>
<projectService serviceImplementation="ee.carlrobert.codegpt.ide.toolwindow.ToolWindowService"/>
<projectService serviceImplementation="ee.carlrobert.codegpt.ide.toolwindow.ContentManagerService"/>
<toolWindow id="CodeGPT" icon="Icons.ToolWindowIcon" anchor="right"
factoryClass="ee.carlrobert.codegpt.ide.toolwindow.ProjectToolWindowFactory"/>
</extensions>