Skip to content

Commit

Permalink
Switch to webview-based sign-in flow
Browse files Browse the repository at this point in the history
  • Loading branch information
pkukielka committed Oct 16, 2024
1 parent 7822694 commit 7a32f34
Show file tree
Hide file tree
Showing 102 changed files with 420 additions and 3,937 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,8 @@ import com.intellij.testFramework.fixtures.BasePlatformTestCase
import com.intellij.testFramework.runInEdtAndWait
import com.sourcegraph.cody.agent.CodyAgentService
import com.sourcegraph.cody.agent.protocol_generated.ProtocolCodeLens
import com.sourcegraph.cody.config.CodyPersistentAccountsHost
import com.sourcegraph.cody.config.SourcegraphServerPath
import com.sourcegraph.cody.auth.CodyPersistentAccountsHost
import com.sourcegraph.cody.auth.SourcegraphServerPath
import com.sourcegraph.cody.edit.lenses.LensListener
import com.sourcegraph.cody.edit.lenses.LensesService
import com.sourcegraph.cody.edit.lenses.providers.EditAcceptCodeVisionProvider
Expand Down
3 changes: 2 additions & 1 deletion src/main/java/com/sourcegraph/cody/CodyActionGroup.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import com.intellij.openapi.actionSystem.ActionUpdateThread;
import com.intellij.openapi.actionSystem.AnActionEvent;
import com.intellij.openapi.actionSystem.DefaultActionGroup;
import com.sourcegraph.cody.auth.CodyAccount;
import com.sourcegraph.config.ConfigUtil;
import org.jetbrains.annotations.NotNull;

Expand All @@ -21,6 +22,6 @@ public boolean isDumbAware() {
@Override
public void update(@NotNull AnActionEvent e) {
super.update(e);
e.getPresentation().setVisible(ConfigUtil.isCodyEnabled());
e.getPresentation().setVisible(ConfigUtil.isCodyEnabled() && CodyAccount.Companion.hasActiveAccount());
}
}
2 changes: 0 additions & 2 deletions src/main/java/com/sourcegraph/cody/CodyToolWindowFactory.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@
import com.intellij.ui.content.ContentFactory;
import com.sourcegraph.cody.config.actions.OpenCodySettingsEditorAction;
import com.sourcegraph.config.ConfigUtil;
import com.sourcegraph.config.OpenPluginSettingsAction;
import org.jetbrains.annotations.NotNull;

public class CodyToolWindowFactory implements ToolWindowFactory, DumbAware {
Expand All @@ -30,7 +29,6 @@ public void createToolWindowContent(@NotNull Project project, @NotNull ToolWindo
content.setPreferredFocusableComponent(null);
toolWindow.getContentManager().addContent(content);
DefaultActionGroup customCodySettings = new DefaultActionGroup();
customCodySettings.add(new OpenPluginSettingsAction("Cody Settings..."));
customCodySettings.add(new OpenCodySettingsEditorAction());
customCodySettings.addSeparator();

Expand Down
19 changes: 0 additions & 19 deletions src/main/java/com/sourcegraph/cody/api/Promises.java

This file was deleted.

6 changes: 0 additions & 6 deletions src/main/java/com/sourcegraph/cody/chat/ChatUIConstants.java

This file was deleted.

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -5,18 +5,14 @@
import com.intellij.notification.Notifications;
import com.intellij.openapi.actionSystem.AnAction;
import com.intellij.openapi.actionSystem.AnActionEvent;
import com.intellij.openapi.application.ApplicationManager;
import com.intellij.openapi.project.Project;
import com.intellij.openapi.wm.ToolWindow;
import com.intellij.openapi.wm.ToolWindowManager;
import com.sourcegraph.Icons;
import com.sourcegraph.cody.CodyToolWindowFactory;
import com.sourcegraph.cody.config.CodyAccount;
import com.sourcegraph.cody.config.CodyAccountManager;
import com.sourcegraph.cody.auth.CodyAccount;
import com.sourcegraph.cody.config.CodyApplicationSettings;
import com.sourcegraph.cody.config.CodyAuthenticationManager;
import com.sourcegraph.cody.initialization.Activity;
import com.sourcegraph.cody.statusbar.CodyManageAccountsAction;
import com.sourcegraph.common.NotificationGroups;
import com.sourcegraph.common.ui.DumbAwareEDTAction;
import org.jetbrains.annotations.NotNull;
Expand All @@ -25,19 +21,8 @@ public class CodyAuthNotificationActivity implements Activity {

@Override
public void runActivity(@NotNull Project project) {
CodyAccount activeAccount = CodyAuthenticationManager.getInstance().getAccount();
CodyAccountManager service =
ApplicationManager.getApplication().getService(CodyAccountManager.class);

if (activeAccount != null) {
String token = service.findCredentials(activeAccount);
if (token == null) {
showMissingTokenNotification();
}
}

if (!CodyApplicationSettings.getInstance().isGetStartedNotificationDismissed()
&& activeAccount == null) {
&& !CodyAccount.Companion.hasActiveAccount()) {
showOpenCodySidebarNotification(project);
}
}
Expand Down Expand Up @@ -79,15 +64,4 @@ public void actionPerformed(@NotNull AnActionEvent anActionEvent) {
notification.addAction(neverShowAgainAction);
Notifications.Bus.notify(notification);
}

private void showMissingTokenNotification() {
// Display notification
Notification notification =
new Notification(
NotificationGroups.CODY_AUTH, "Missing access token", "", NotificationType.WARNING);

notification.setIcon(Icons.CodyLogo);
notification.addAction(new CodyManageAccountsAction());
Notifications.Bus.notify(notification);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import com.intellij.util.ui.JBDimension;
import com.intellij.util.ui.JBUI;
import com.sourcegraph.Icons;
import com.sourcegraph.cody.config.actions.OpenCodySettingsEditorAction;
import javax.swing.*;
import org.jetbrains.annotations.NotNull;

Expand All @@ -16,7 +17,7 @@ public class GoToPluginSettingsButtonFactory {
public static ActionButton createGoToPluginSettingsButton() {
JBDimension actionButtonSize = JBUI.size(22, 22);

AnAction action = new OpenPluginSettingsAction();
AnAction action = new OpenCodySettingsEditorAction();
Presentation presentation = new Presentation("Open Plugin Settings");

ActionButton button =
Expand Down
25 changes: 0 additions & 25 deletions src/main/java/com/sourcegraph/config/OpenPluginSettingsAction.java

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
import com.intellij.ui.components.JBPanelWithEmptyText;
import com.intellij.util.ui.JBUI;
import com.intellij.util.ui.StatusText;
import com.sourcegraph.cody.config.ui.AccountConfigurable;
import com.sourcegraph.cody.config.ui.CodyConfigurable;
import java.awt.*;
import javax.swing.*;
import org.apache.commons.lang.WordUtils;
Expand Down Expand Up @@ -69,9 +69,7 @@ private void refreshUI() {
emptyText.appendLine(
"Click here to configure your Sourcegraph Cody + Code Search settings.",
new SimpleTextAttributes(STYLE_PLAIN, JBUI.CurrentTheme.Link.Foreground.ENABLED),
__ ->
ShowSettingsUtil.getInstance()
.showSettingsDialog(project, AccountConfigurable.class));
__ -> ShowSettingsUtil.getInstance().showSettingsDialog(project, CodyConfigurable.class));

} else if (errorMessage != null) {
String wrappedText = WordUtils.wrap("Error: " + errorMessage, 100);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

import com.intellij.openapi.util.Disposer;
import com.intellij.ui.jcef.JBCefBrowser;
import com.sourcegraph.cody.config.notification.AccountSettingChangeListener;
import com.sourcegraph.cody.config.notification.CodySettingChangeListener;
import com.sourcegraph.config.ThemeUtil;
import javax.swing.*;
Expand All @@ -24,11 +23,6 @@ public SourcegraphJBCefBrowser(@NotNull JSToJavaBridgeRequestHandler requestHand
Disposer.register(this, jsToJavaBridge);
javaToJSBridge = new JavaToJSBridge(this);

requestHandler
.getProject()
.getService(AccountSettingChangeListener.class)
.setJavaToJSBridge(javaToJSBridge);

requestHandler
.getProject()
.getService(CodySettingChangeListener.class)
Expand Down
65 changes: 8 additions & 57 deletions src/main/kotlin/com/sourcegraph/cody/CodyToolWindowContent.kt
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,12 @@ package com.sourcegraph.cody
import com.intellij.openapi.components.Service
import com.intellij.openapi.diagnostic.Logger
import com.intellij.openapi.project.Project
import com.intellij.openapi.wm.ToolWindowManager
import com.intellij.ui.components.JBLabel
import com.intellij.util.concurrency.annotations.RequiresEdt
import com.intellij.util.ui.UIUtil
import com.sourcegraph.cody.chat.SignInWithSourcegraphPanel
import com.sourcegraph.cody.chat.ui.CodyOnboardingGuidancePanel
import com.sourcegraph.cody.config.CodyAccount
import com.sourcegraph.cody.config.CodyApplicationSettings
import com.sourcegraph.cody.config.CodyAuthenticationManager
import com.sourcegraph.cody.ui.web.CodyToolWindowContentWebviewHost
import com.sourcegraph.cody.ui.web.WebUIService
import java.awt.CardLayout
import java.awt.GridBagConstraints
import java.awt.GridBagLayout
import java.awt.GridLayout
Expand All @@ -22,67 +17,27 @@ import javax.swing.JPanel

@Service(Service.Level.PROJECT)
class CodyToolWindowContent(project: Project) {
private val cardLayout = CardLayout()
private val cardPanel = JPanel(cardLayout)
val allContentPanel: JComponent = JPanel(GridLayout(1, 1))
private val mainPanel = JPanel(GridBagLayout())
private var webview: CodyToolWindowContentWebviewHost? = null

init {
cardPanel.add(SignInWithSourcegraphPanel(project), SIGN_IN_PANEL, SIGN_IN_PANEL_INDEX)
val codyOnboardingGuidancePanel = CodyOnboardingGuidancePanel(project)
codyOnboardingGuidancePanel.addMainButtonActionListener {
CodyApplicationSettings.instance.isOnboardingGuidanceDismissed = true
refreshPanelsVisibility()
}
cardPanel.add(codyOnboardingGuidancePanel, ONBOARDING_PANEL, ONBOARDING_PANEL_INDEX)

// Because the webview may be created lazily, populate a placeholder control.
val placeholder = JPanel(GridBagLayout())
val spinnerLabel =
JBLabel("Starting Cody...", Icons.StatusBar.CompletionInProgress, JBLabel.CENTER)
placeholder.add(spinnerLabel, GridBagConstraints())
cardPanel.add(placeholder, LOADING_PANEL, LOADING_PANEL_INDEX)

allContentPanel.add(spinnerLabel, GridBagConstraints())
WebUIService.getInstance(project).views.provideCodyToolWindowContent(this)

refreshPanelsVisibility()
}

@RequiresEdt
fun refreshPanelsVisibility() {
val codyAuthenticationManager = CodyAuthenticationManager.getInstance()
if (codyAuthenticationManager.hasNoActiveAccount() ||
codyAuthenticationManager.showInvalidAccessTokenError()) {
cardLayout.show(cardPanel, SIGN_IN_PANEL)
showView(cardPanel)
return
}
val activeAccount = codyAuthenticationManager.account
if (!CodyApplicationSettings.instance.isOnboardingGuidanceDismissed) {
val displayName = activeAccount?.let(CodyAccount::displayName)
cardPanel.getComponent(ONBOARDING_PANEL_INDEX)?.let {
(it as CodyOnboardingGuidancePanel).updateDisplayName(displayName)
}
cardLayout.show(cardPanel, ONBOARDING_PANEL)
showView(cardPanel)
return
}
cardLayout.show(cardPanel, LOADING_PANEL)
showView(webview?.proxy?.component ?: cardPanel)
}

// Flips the sidebar view to the specified top level component. We do it this way
// because JetBrains Remote does not display webviews inside a component using
// CardLayout.
private fun showView(component: JComponent) {
val component = webview?.proxy?.component ?: mainPanel
if (allContentPanel.components.isEmpty() || allContentPanel.getComponent(0) != component) {
allContentPanel.removeAll()
allContentPanel.add(component)
}
}

/** Sets the webview component to display, if any. */
@RequiresEdt
internal fun setWebviewComponent(host: CodyToolWindowContentWebviewHost?) {
webview = host
if (host != null && host.proxy?.component == null) {
Expand All @@ -96,16 +51,12 @@ class CodyToolWindowContent(project: Project) {
}

companion object {
const val ONBOARDING_PANEL = "onboardingPanel"
const val SIGN_IN_PANEL = "signInWithSourcegraphPanel"
const val LOADING_PANEL = "loadingPanel"

const val SIGN_IN_PANEL_INDEX = 0
const val ONBOARDING_PANEL_INDEX = 1
const val LOADING_PANEL_INDEX = 2

var logger = Logger.getInstance(CodyToolWindowContent::class.java)

fun show(project: Project) {
ToolWindowManager.getInstance(project).getToolWindow(CodyToolWindowFactory.TOOL_WINDOW_ID)?.show()
}

fun executeOnInstanceIfNotDisposed(
project: Project,
myAction: CodyToolWindowContent.() -> Unit
Expand Down
2 changes: 2 additions & 0 deletions src/main/kotlin/com/sourcegraph/cody/agent/CodyAgent.kt
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,7 @@ private constructor(
extensionConfiguration = ConfigUtil.getAgentConfiguration(project),
capabilities =
ClientCapabilities(
authentication = ClientCapabilities.AuthenticationEnum.Enabled,
edit = ClientCapabilities.EditEnum.Enabled,
editWorkspace = ClientCapabilities.EditWorkspaceEnum.Enabled,
codeLenses = ClientCapabilities.CodeLensesEnum.Enabled,
Expand All @@ -128,6 +129,7 @@ private constructor(
untitledDocuments = ClientCapabilities.UntitledDocumentsEnum.Enabled,
codeActions = ClientCapabilities.CodeActionsEnum.Enabled,
globalState = ClientCapabilities.GlobalStateEnum.`Server-managed`,
secrets = ClientCapabilities.SecretsEnum.`Client-managed`,
webview = ClientCapabilities.WebviewEnum.Native,
webviewNativeConfig =
WebviewNativeConfig(
Expand Down
Loading

0 comments on commit 7a32f34

Please sign in to comment.